alwaysai
Version:
The alwaysAI command-line interface (CLI)
62 lines • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const util_1 = require("util");
const path_1 = require("path");
const tarJs = require("tar");
const mkdirpJs = require("mkdirp");
const rimrafJs = require("rimraf");
const spawner_base_1 = require("./spawner-base");
const gnu_spawner_1 = require("./gnu-spawner");
function JsSpawner(context = {}) {
const gnuSpawner = gnu_spawner_1.GnuSpawner(Object.assign({ resolvePath }, spawner_base_1.SpawnerBase(translate)));
return Object.assign({}, gnuSpawner, { resolvePath,
readdir,
mkdirp,
rimraf,
tar,
exists });
function resolvePath(...paths) {
return path_1.resolve(context.path || '', ...paths.map(path => path || ''));
}
function translate(cmd) {
const translated = Object.assign({}, cmd);
if (cmd.cwd) {
translated.cwd = resolvePath(cmd.cwd);
}
return translated;
}
async function mkdirp(path = '') {
await util_1.promisify(mkdirpJs)(resolvePath(path));
}
async function rimraf(path = '') {
await util_1.promisify(rimrafJs)(resolvePath(path));
}
function readdir(path = '') {
return util_1.promisify(fs_1.readdir)(resolvePath(path));
}
async function tar(...paths) {
for (const path in paths) {
if (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 util_1.promisify(fs_1.access)(resolvePath(path));
return true;
}
catch (ex) {
return false;
}
}
}
exports.JsSpawner = JsSpawner;
//# sourceMappingURL=js-spawner.js.map