alwaysai
Version:
The alwaysAI command-line interface (CLI)
106 lines • 3.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GnuSpawner = void 0;
const coded_error_1 = require("@carnesen/coded-error");
const node_stream_1 = require("node:stream");
function GnuSpawner(context) {
const { resolvePath, run, runForegroundSync, runForeground, runStreaming } = context;
return {
run,
runForegroundSync,
runForeground,
runStreaming,
resolvePath,
readdir,
walk,
async readFile(path) {
const output = await run({
exe: 'cat',
args: [resolvePath(path)]
});
return output;
},
async writeFile(path, data) {
await run({
exe: 'dd',
args: [`of=${resolvePath(path)}`],
input: node_stream_1.Readable.from([data])
});
},
async rename(oldPath, newPath) {
await run({
exe: 'mv',
args: [resolvePath(oldPath), resolvePath(newPath)]
});
},
mkdirp,
rimraf,
sanatizedrimraf,
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 sanatizedrimraf(path) {
const sanatizedPath = resolvePath(path).replace('*', '');
await run({ exe: 'rm', args: ['-rf', sanatizedPath] });
}
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;
}
}
async function walk(dir) {
throw new Error(`NotImplementedError: GnuSpawner 'walk' function not available. Please use JsSpawner. Args: ${dir !== null && dir !== void 0 ? dir : 'None'}`);
}
}
exports.GnuSpawner = GnuSpawner;
//# sourceMappingURL=gnu-spawner.js.map