UNPKG

veendor

Version:

a tool for stroing your npm dependencies in arbitraty storage

57 lines (56 loc) 2.31 kB
"use strict"; 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 fs_1 = __importDefault(require("fs")); const fs_extra_1 = __importDefault(require("fs-extra")); const tarWrapper = __importStar(require("../commandWrappers/tarWrapper")); const errors = __importStar(require("../errors")); function validateOptions(options) { if (options.compression && !(options.compression in tarWrapper.compression)) { throw new errors.InvalidOptionsError(`Invalid compression: ${options.compression}`); } if (!options.compression) { options.compression = 'gzip'; } if (typeof options.directory !== 'string') { throw new errors.InvalidOptionsError(`Invalid directory '${options.directory}'`); } try { fs_1.default.readdirSync(options.directory); } catch (e) { throw new errors.InvalidOptionsError(`Invalid directory '${options.directory}': ${e.message}`); } } exports.validateOptions = validateOptions; function pull(hash, options) { const archivePath = path_1.default.resolve(options.directory, `${hash}.tar${tarWrapper.compression[options.compression]}`); return fs_extra_1.default.stat(archivePath) .then(() => { return tarWrapper.extractArchive(archivePath); }, () => { throw new errors.BundleNotFoundError(); }); } exports.pull = pull; function push(hash, options) { const archivePath = path_1.default.resolve(options.directory, `${hash}.tar${tarWrapper.compression[options.compression]}`); return fs_extra_1.default.stat(archivePath) .then(() => { throw new errors.BundleAlreadyExistsError(); }, () => { return tarWrapper .createArchive(archivePath, [path_1.default.resolve(process.cwd(), 'node_modules')], options.compression); }); } exports.push = push;