UNPKG

@cocalc/project

Version:
36 lines 1.53 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const tmp_1 = require("tmp"); const async_utils_1 = require("@cocalc/util/async-utils"); const awaiting_1 = require("awaiting"); const child_process_1 = require("child_process"); async function genericFormat({ command, args, input, timeout_s, }) { // create input temp file const inputPath = await (0, awaiting_1.callback)(tmp_1.file); try { await (0, awaiting_1.callback)(fs_1.writeFile, inputPath, input); // spawn the formatter const child = (0, child_process_1.spawn)(command, args(inputPath)); // output stream capture: let stdout = ""; let stderr = ""; child.stdout.on("data", (data) => (stdout += data.toString("utf-8"))); child.stderr.on("data", (data) => (stderr += data.toString("utf-8"))); // wait for subprocess to close. const code = await (0, async_utils_1.once)(child, "close", (timeout_s ?? 30) * 1000); if (code[0]) throw Error(stderr); // all fine, we read from the temp file: return (await (0, awaiting_1.callback)(fs_1.readFile, inputPath)).toString("utf-8"); } finally { await (0, awaiting_1.callback)(fs_1.unlink, inputPath); } } exports.default = genericFormat; //# sourceMappingURL=generic-format.js.map