veendor
Version:
a tool for stroing your npm dependencies in arbitraty storage
76 lines (75 loc) • 2.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const errors = __importStar(require("../errors"));
const helpers = __importStar(require("./helpers"));
const helpers_1 = require("./helpers");
exports.compression = {
gzip: '.gz',
bzip2: '.bz2',
xz: '.xz',
};
function createArchive(outPath, inputPaths, compressionType) {
const baseDir = path_1.default.dirname(inputPaths[0]);
const pathsToAdd = inputPaths.map(p => path_1.default.relative(baseDir, p));
const args = [
'--create',
`--${compressionType}`,
'--file',
outPath,
...pathsToAdd
];
return helpers.getOutput('tar', args, { cwd: baseDir, stdout: helpers_1.StdioPolicy.copy, stderr: helpers_1.StdioPolicy.inherit });
}
exports.createArchive = createArchive;
function extractArchive(archive) {
const args = ['--extract', '--file', archive];
return helpers.getOutput('tar', args, { stdout: helpers_1.StdioPolicy.copy, stderr: helpers_1.StdioPolicy.inherit });
}
exports.extractArchive = extractArchive;
class ControlTokenError extends errors.VeendorError {
}
function createStreamArchive(inputPaths, compressionType, { controlToken = {} }) {
const baseDir = path_1.default.dirname(inputPaths[0]);
const pathsToAdd = inputPaths.map(p => path_1.default.relative(baseDir, p));
const args = [
'--create',
`--${compressionType}`,
'--file',
'-',
...pathsToAdd,
];
const procPromise = helpers.getOutput('tar', args, { stdout: helpers_1.StdioPolicy.pipe, stderr: helpers_1.StdioPolicy.pipe, controlToken });
if (!controlToken.stdio) {
throw new ControlTokenError('child_process stdio is not available');
}
return {
stream: controlToken.stdio[1],
promise: procPromise,
};
}
exports.createStreamArchive = createStreamArchive;
function extractArchiveFromStream(archiveStream, compressionType, { controlToken = {} }) {
const args = ['--extract', `--${compressionType}`, '--file', '-'];
const procPromise = helpers.getOutput('tar', args, {
stdout: helpers_1.StdioPolicy.pipe, stderr: helpers_1.StdioPolicy.pipe, controlToken
});
if (controlToken.stdio) {
archiveStream.pipe(controlToken.stdio[0]);
return procPromise;
}
else {
throw new ControlTokenError('child_process stdio is not available');
}
}
exports.extractArchiveFromStream = extractArchiveFromStream;