UNPKG

veendor

Version:

a tool for stroing your npm dependencies in arbitraty storage

76 lines (75 loc) 3.63 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_1 = __importDefault(require("crypto")); const fs_extra_1 = __importDefault(require("fs-extra")); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const logger_1 = require("../util/logger"); const originalCwd = process.cwd(); function createCleanCacheDir(backendConfig) { return __awaiter(this, void 0, void 0, function* () { const logger = logger_1.getLogger(); logger.trace(`Running 'createCleanCacheDir' for ${backendConfig.alias}`); const cacheDirPath = path_1.default.join(getTmpDir(), backendConfig.alias); if (backendConfig.backend.keepCache) { logger.trace(`Running 'ensureDir' for ${cacheDirPath}`); yield fs_extra_1.default.ensureDir(cacheDirPath); return cacheDirPath; } logger.trace(`Running 'emptyDir' for ${cacheDirPath}`); return fs_extra_1.default.emptyDir(cacheDirPath) .then(() => { logger.trace(`Cache directory for backend '${backendConfig.alias}' is set`); return cacheDirPath; }); }); } exports.createCleanCacheDir = createCleanCacheDir; function createCleanCwd(lockfilePath) { return __awaiter(this, void 0, void 0, function* () { const logger = logger_1.getLogger(); logger.trace('Running \'createCleanCwd\''); const newCwdDirPath = path_1.default.join(getTmpDir(), '__result'); yield fs_extra_1.default.ensureDir(newCwdDirPath); logger.trace(`New CWD:'${newCwdDirPath}'`); process.chdir(newCwdDirPath); yield fs_extra_1.default.emptyDir(process.cwd()); yield fs_extra_1.default.copy(path_1.default.join(originalCwd, 'package.json'), path_1.default.join(process.cwd(), 'package.json')); if (lockfilePath !== null) { yield fs_extra_1.default.copy(path_1.default.join(originalCwd, lockfilePath), path_1.default.join(process.cwd(), lockfilePath)); } }); } exports.createCleanCwd = createCleanCwd; function getTmpDir() { const tmpDir = os_1.default.tmpdir(); const cwdHash = crypto_1.default.createHash('sha1'); cwdHash.update(process.cwd()); return path_1.default.join(tmpDir, `veendor-${cwdHash.digest('hex')}`); } exports.getTmpDir = getTmpDir; function restoreCWD() { if (process.cwd() !== originalCwd) { const logger = logger_1.getLogger(); logger.trace(`Restoring CWD from '${process.cwd()}' to ${originalCwd}`); process.chdir(originalCwd); } } exports.restoreCWD = restoreCWD; exports.paths = { nodeModules: path_1.default.resolve(process.cwd(), 'node_modules'), pkgJsonPath: path_1.default.resolve(process.cwd(), 'package.json'), originalCwd, };