@cocalc/project
Version:
CoCalc: project daemon
72 lines • 2.52 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.bib_format = void 0;
const { writeFile, readFile, unlink } = require("fs");
const tmp = require("tmp");
const { callback } = require("awaiting");
const { execute_code } = require("@cocalc/backend/execute-code");
const { callback_opts } = require("@cocalc/util/async-utils");
// ref: man biber
async function biber(input_path, output_path) {
const args = [
"--tool",
"--output-align",
"--output-indent=2",
"--output-fieldcase=lower",
"--output-file",
output_path,
input_path,
];
return await callback_opts(execute_code)({
command: "biber",
args: args,
err_on_exit: false,
bash: false,
timeout: 20,
});
}
async function bib_format(input, options, logger) {
// create input temp file
const input_path = await callback(tmp.file);
const output_path = await callback(tmp.file);
try {
await callback(writeFile, input_path, input);
// spawn the bibtex formatter
let bib_formatter;
try {
switch (options.parser) {
case "bib-biber":
bib_formatter = await biber(input_path, output_path);
break;
default:
throw Error(`Unknown XML formatter utility '${options.parser}'`);
}
}
catch (e) {
logger.debug(`Calling Bibtex formatter raised ${e}`);
throw new Error(`Bibtex formatter broken or not available. Is '${options.parser}' installed?`);
}
const { exit_code, stdout, stderr } = bib_formatter;
const code = exit_code;
const problem = code >= 1;
if (problem) {
const msg = `Bibtex formatter "${options.parser}" exited with code ${code}\nOutput:\n${stdout}\n${stderr}`;
throw Error(msg);
}
// all fine, we read from the temp file
const output = await callback(readFile, output_path);
const s = output.toString("utf-8");
return s;
}
finally {
// logger.debug(`bibtex formatter done, unlinking ${input_path}`);
unlink(input_path, () => { });
unlink(output_path, () => { });
}
}
exports.bib_format = bib_format;
//# sourceMappingURL=bib-format.js.map