Sha256: 971df29f1b1953b1d42bd3eb07226e53acdcc61c6533810c31f5704edead5d38
Contents?: true
Size: 1.87 KB
Versions: 1
Compression:
Stored size: 1.87 KB
Contents
const { spawn, spawnSync, execSync } = require("child_process"); const path = require("path"); const { existsSync, mkdtempSync } = require("fs"); const process = require("process"); const os = require("os"); const getNetcat = require("./getNetcat"); const getLang = require("./getLang"); let sockfile = process.env.PRETTIER_RUBY_HOST; // Spawn the parser.rb subprocess. We do this since booting Ruby is slow, and we // can re-use the parser process multiple times since it is statelesss. function spawnParseServer() { const server = spawn( "ruby", [path.join(__dirname, "./server.rb"), sockfile], { env: Object.assign({}, process.env, { LANG: getLang() }), detached: true, stdio: "inherit" } ); process.on("exit", () => { try { process.kill(-server.pid); } catch (e) { // ignore } }); server.unref(); const now = new Date(); // Wait for server to go live. while (!existsSync(sockfile) && new Date() - now < 3000) { execSync("sleep 0.1"); } } // Ensures that a parser server is currently running by checking against the // sockfile variable. If it is not, create a temporary directory to house the // sockfile and spawn the ruby process. function ensureParseServer() { if (sockfile) { return; } const tmpDir = mkdtempSync(path.join(os.tmpdir(), "prettier-ruby")); sockfile = path.join(tmpDir, `${process.pid}.sock`); spawnParseServer(); } // Sends a request to the parse server to parse the given content. function requestParse(parser, source, opts) { ensureParseServer(); const { command, args } = getNetcat(opts); const { stdout, stderr, status } = spawnSync(command, args.concat(sockfile), { input: `${parser}|${source}`, maxBuffer: 15 * 1024 * 1024 }); return { command, stdout: stdout.toString(), stderr: stderr.toString(), status }; } module.exports = requestParse;
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
prettier-1.5.4 | src/parser/requestParse.js |