@cocalc/project
Version:
CoCalc: project daemon
71 lines • 2.6 kB
JavaScript
;
/*
* 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 });
exports.gofmt = void 0;
const { writeFile, readFile, unlink } = require("fs");
const tmp = require("tmp");
const { callback } = require("awaiting");
const { spawn } = require("child_process");
function close(proc, cb) {
proc.on("close", (code) => cb(undefined, code));
}
// ref: https://golang.org/cmd/gofmt/ ... but there isn't anything to configure
function run_gofmt(input_path) {
const args = ["-w", input_path];
return spawn("gofmt", args);
}
function cleanup_error(err, tmpfn) {
const ret = [];
for (let line of err.split("\n")) {
if (line.startsWith(tmpfn)) {
line = line.slice(tmpfn.length + 1);
}
ret.push(line);
}
return ret.join("\n");
}
async function gofmt(input, options, logger) {
// create input temp file
const input_path = await callback(tmp.file);
try {
// logger.debug(`gofmt tmp file: ${input_path}`);
await callback(writeFile, input_path, input);
// spawn the html formatter
let formatter;
switch (options.parser) {
case "gofmt":
formatter = run_gofmt(input_path /*, logger*/);
break;
default:
throw Error(`Unknown Go code formatting utility '${options.parser}'`);
}
// stdout/err capture
let stdout = "";
let stderr = "";
// read data as it is produced.
formatter.stdout.on("data", (data) => (stdout += data.toString()));
formatter.stderr.on("data", (data) => (stderr += data.toString()));
// wait for subprocess to close.
const code = await callback(close, formatter);
if (code >= 1) {
stdout = cleanup_error(stdout, input_path);
stderr = cleanup_error(stderr, input_path);
const err_msg = `Gofmt code formatting utility "${options.parser}" exited with code ${code}\nOutput:\n${stdout}\n${stderr}`;
logger.debug(`gofmt error: ${err_msg}`);
throw Error(err_msg);
}
// all fine, we read from the temp file
const output = await callback(readFile, input_path);
const s = output.toString("utf-8");
// logger.debug(`gofmt_format output s ${s}`);
return s;
}
finally {
unlink(input_path, () => { });
}
}
exports.gofmt = gofmt;
//# sourceMappingURL=gofmt.js.map