UNPKG

veendor

Version:

a tool for stroing your npm dependencies in arbitraty storage

98 lines (97 loc) 4.93 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 }; }; 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 fs_extra_1 = __importDefault(require("fs-extra")); const logger_1 = require("../util/logger"); const gitWrapper = __importStar(require("../commandWrappers/gitWrapper")); const pkgJsonUtils = __importStar(require("../pkgjson")); const index_1 = require("./index"); const helpers = __importStar(require("./helpers")); const { pkgJsonPath, originalCwd } = helpers.paths; function getHistoryHash(config, lockfilePath = null, oldHash = null, historyIndexStart = 0) { return __awaiter(this, void 0, void 0, function* () { const logger = logger_1.getLogger(); logger.trace(`Running getHistoryHash. pkgJsonPath: ${pkgJsonPath};` + ` lockfilePath: ${lockfilePath}, historyIndexStart: ${historyIndexStart}`); let currentHistoryIndex = historyIndexStart; while (true) { currentHistoryIndex++; const [pkgJsonString, lockfileString] = yield gitWrapper.olderRevision(originalCwd, [pkgJsonPath, lockfilePath], currentHistoryIndex); const pkgJson = yield pkgJsonUtils.parsePkgJson(pkgJsonString); const lockfileContents = typeof lockfileString === 'string' ? JSON.parse(lockfileString) : null; const hash = pkgJsonUtils.calcHash(pkgJson, lockfileContents, config.packageHash); if (hash === oldHash) { const message = `Hash at index '${historyIndexStart}' is still '${hash}'. Incrementing history depth`; logger.trace(message); config.useGitHistory.depth++; if (currentHistoryIndex > config.useGitHistory.depth) { throw new index_1.BundlesNotFoundError(`Backends don't have bundles up to ${config.useGitHistory.depth} entries in git history of ${pkgJsonPath}`); } } else { return { hash, historyIndexEnd: currentHistoryIndex, pkgJson }; } } }); } exports.getHistoryHash = getHistoryHash; function getFSHash(config, pkgJsonPath, lockfilePath) { return __awaiter(this, void 0, void 0, function* () { const logger = logger_1.getLogger(); const result = []; logger.trace(`Running getFSHash. pkgJsonPath: ${pkgJsonPath}; lockfilePath: ${lockfilePath}`); logger.trace('Reading package.json'); result.push(fs_extra_1.default .readFile(pkgJsonPath) .then(pkgJsonBuf => { const pkgJsonString = pkgJsonBuf.toString(); logger.trace('Parsing package.json'); return pkgJsonUtils.parsePkgJson(pkgJsonString); }, (err) => { if (isNodeJSException(err) && err.code === 'ENOENT') { throw new index_1.PkgJsonNotFoundError('Couldn\'t find package.json file'); } throw err; })); if (lockfilePath !== null) { logger.trace(`Reading ${lockfilePath}`); result.push(fs_extra_1.default .readFile(lockfilePath) .then(lockfileBuf => { logger.trace(`Parsing ${lockfilePath}`); return JSON.parse(lockfileBuf.toString()); })); } else { result.push(null); } const [pkgJson, lockfileContents] = yield Promise.all(result); logger.debug(`Got dependencies:\t${JSON.stringify(pkgJson.dependencies)}`); logger.debug(`Got devDependencies:\t${JSON.stringify(pkgJson.devDependencies)}`); logger.trace('Calculating hash'); const hash = pkgJsonUtils.calcHash(pkgJson, lockfileContents, config.packageHash); return { hash, pkgJson: pkgJson }; }); } exports.getFSHash = getFSHash; function isNodeJSException(err) { return err.code !== undefined; }