alwaysai
Version:
The alwaysAI command-line interface (CLI)
75 lines • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const coded_error_1 = require("@carnesen/coded-error");
function GnuSpawner(context) {
const { resolvePath, run, runForegroundSync, runForeground, runStreaming } = context;
return {
run,
runForegroundSync,
runForeground,
runStreaming,
resolvePath,
readdir,
mkdirp,
rimraf,
tar,
untar,
exists,
};
async function mkdirp(path) {
await run({ exe: 'mkdir', args: ['-p', resolvePath(path)] });
}
async function readdir(path) {
let output;
const resolvedPath = resolvePath(path);
try {
output = await run({ exe: 'ls', args: ['-A1', resolvedPath] });
// ^^ The output looks like '/foo/bar.txt' if path is an existing file
// Else it looks like 'a b c' if the path is a directory with files/subdirs a, b, c.
}
catch (ex) {
if (ex &&
typeof ex.message === 'string' &&
ex.message.includes('No such file or directory')) {
ex.code = 'ENOENT';
}
throw ex;
}
if (output.startsWith('/')) {
throw new coded_error_1.CodedError(`ENOTDIR: not a directory "${resolvedPath}"`, 'ENOTDIR');
}
return output.length > 0 ? output.split('\n') : [];
}
async function rimraf(path) {
await run({ exe: 'rm', args: ['-rf', resolvePath(path)] });
}
async function tar(...paths) {
return await runStreaming({
exe: 'tar',
args: ['-cz', ...paths],
cwd: resolvePath(),
});
}
async function untar(input, cwd = '.') {
await run({
exe: 'tar',
args: ['-xz'],
cwd,
input,
});
}
async function exists(path) {
if (!path) {
throw new Error('"path" is required');
}
try {
await run({ exe: 'stat', args: [resolvePath(path)] });
return true;
}
catch (ex) {
return false;
}
}
}
exports.GnuSpawner = GnuSpawner;
//# sourceMappingURL=gnu-spawner.js.map