alwaysai
Version:
The alwaysAI command-line interface (CLI)
120 lines • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsSpawner = void 0;
const fs_1 = require("fs");
const promises_1 = require("fs/promises");
const util_1 = require("util");
const path_1 = require("path");
const tarJs = require("tar");
const mkdirpJs = require("mkdirp");
const rimraf_1 = require("rimraf");
const spawner_base_1 = require("./spawner-base");
const gnu_spawner_1 = require("./gnu-spawner");
const pump = require("pump");
function JsSpawner(context = {}) {
const gnuSpawner = (0, gnu_spawner_1.GnuSpawner)(Object.assign({ resolvePath }, (0, spawner_base_1.SpawnerBase)(translate)));
return Object.assign(Object.assign({}, gnuSpawner), { resolvePath,
readdir,
async readFile(path) {
return await (0, util_1.promisify)(fs_1.readFile)(resolvePath(path), {
encoding: 'utf8'
});
},
async writeFile(path, data) {
await (0, util_1.promisify)(fs_1.writeFile)(resolvePath(path), data, {
encoding: 'utf8'
});
},
mkdirp,
rimraf,
tar,
walk,
async rename(oldPath, newPath) {
await (0, util_1.promisify)(fs_1.rename)(resolvePath(oldPath), resolvePath(newPath));
},
async untar(input, cwd) {
await new Promise((resolve, reject) => {
const writable = tarJs.extract({ cwd: resolvePath(cwd) });
pump(input, writable, (err) => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
},
exists });
function resolvePath(...paths) {
var _a;
return (0, path_1.resolve)((_a = context.path) !== null && _a !== void 0 ? _a : '', ...paths.map((path) => path !== null && path !== void 0 ? path : ''));
}
function translate(cmd) {
if (cmd.superuser) {
throw new Error(`${JsSpawner.name} does not support cmd option "superuser"`);
}
const translated = Object.assign({}, cmd);
if (cmd.cwd) {
translated.cwd = resolvePath(cmd.cwd);
}
return translated;
}
async function mkdirp(path = '') {
await mkdirpJs(resolvePath(path));
}
async function rimraf(path = '') {
await (0, rimraf_1.rimraf)(resolvePath(path));
}
function readdir(path = '') {
return (0, util_1.promisify)(fs_1.readdir)(resolvePath(path));
}
async function tar(...paths) {
for (const path in paths) {
if ((0, path_1.isAbsolute)(path)) {
throw new Error('Paths passed to spawner.tar must not be absolute');
}
}
const packageStream = tarJs.create({ sync: true, gzip: true, cwd: resolvePath() }, paths);
// ^^ The @types for tar.create are not correct
return packageStream;
}
async function exists(path) {
if (!path) {
throw new Error('path is mandatory');
}
try {
await (0, util_1.promisify)(fs_1.access)(resolvePath(path));
return true;
}
catch (ex) {
return false;
}
}
async function walk(dir) {
const relPath = dir !== null && dir !== void 0 ? dir : '';
const path = resolvePath(relPath);
const dirPromises = [];
const fileResults = await new Promise((_resolve, _reject) => {
(0, promises_1.readdir)(path, { withFileTypes: true })
.then((files) => {
const nonDirFiles = [];
files.forEach((file) => {
const filePath = (0, path_1.join)(relPath, file.name);
if (file.isDirectory()) {
dirPromises.push(walk(filePath));
}
else {
nonDirFiles.push(filePath);
}
});
_resolve(nonDirFiles);
})
.catch((e) => _reject(e));
});
const dirResults = await Promise.all(dirPromises);
return dirResults.flat().concat(fileResults);
}
}
exports.JsSpawner = JsSpawner;
//# sourceMappingURL=js-spawner.js.map