🧑‍💻 cli: introducing dev commands debug for ad-hoc work, labels to get status of all translation labels, and translate to help with adding new ones

This commit is contained in:
Eric Doughty-Papassideris
2024-04-23 15:01:31 +02:00
committed by ericlinagora
parent 5766d2684b
commit 79fc8f2ad8
9 changed files with 1043 additions and 0 deletions
+64
View File
@@ -0,0 +1,64 @@
import util from "util";
import { exec } from "child_process";
export const execPromise = util.promisify(exec);
import OS from "os";
const openCommandForPlatform = () => {
switch (OS.platform()) {
case "darwin":
return "open";
case "win32":
return "start";
case "linux":
return "xdg-open";
default:
throw new Error(`Platform ${OS.platform()} is not supported.`);
}
};
const veryPoorManShellEscape = str => "'" + str.replace(/'/g, "'\"'\"'") + "'";
export const openWithSystemViewer = (file: string) =>
execPromise([openCommandForPlatform(), veryPoorManShellEscape(file)].join(" "));
import { spawn as spawnWithCB } from "child_process";
export const spawn = (cmd: string, args: string[] = []) => {
const reader = stream =>
new Promise((resolve, reject) => {
const bufs = [];
let len = 0;
stream.on("error", reject);
stream.on("finish", () => {
resolve(Buffer.concat(bufs, len).toString("utf-8"));
});
stream.on("data", data => {
bufs.push(data);
len += data.length;
});
});
const process = spawnWithCB(cmd, args);
const stdout = reader(process.stdout),
stderr = reader(process.stderr);
const processPromise = new Promise((resolve, reject) => {
process.on("error", reject);
process.on("close", resolve);
});
process.stdin.end();
return Promise.all([processPromise, stdout, stderr]).then(([code, stdout, stderr]) => ({
code,
stdout,
stderr,
}));
};
export const spawnCheckingExitCode = (cmd: string, args: string[] = []) =>
spawn(cmd, args).then(execResult => {
if (execResult.code != 0)
throw new Error(
`Error running ${cmd} ${JSON.stringify(args)}: exit with ${execResult.code}. Output:\n${
execResult.stdout
}\n${execResult.stderr}`,
);
return execResult;
});