shrinkpack
Version:
Fast, resilient, reproducible builds with npm install.
56 lines (55 loc) • 1.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.getLockfile = void 0;
var fs_1 = __importDefault(require("fs"));
var path_1 = require("path");
var log_1 = require("./log");
/**
* Look for a package-lock.json or otherwise an npm-shrinkwrap.json within the
* provided absolute path to a project.
*/
function getLockfile(projectPath) {
var lockfilePath = (0, path_1.join)(projectPath, 'package-lock.json');
var lockfile = readLockfile(lockfilePath);
if (!lockfile) {
lockfilePath = (0, path_1.join)(projectPath, 'npm-shrinkwrap.json');
lockfile = readLockfile(lockfilePath);
}
if (!lockfile) {
log_1.log.error('no package-lock.json or npm-shrinkwrap.json found');
process.exit(1);
}
return {
lockfilePath: lockfilePath,
lockfile: lockfile
};
function readLockfile(filePath) {
var sw = readJson(filePath);
if (!sw) {
return null;
}
if (Number(sw.lockfileVersion) < 2) {
log_1.log.error("expected lockfileVersion to be 2 or greater in ".concat(filePath, "\n npm v7 or greater will create this lockfile version"));
process.exit(1);
}
return sw;
}
function readJson(filePath) {
try {
var json = fs_1["default"].readFileSync(filePath, { encoding: 'utf8' });
if (json.includes('git+')) {
log_1.log.error("lockfile contains packages installed directly from git, which is not supported ".concat(filePath));
process.exit(1);
}
return JSON.parse(json);
}
catch (err) {
log_1.log.verbose("no valid lockfile at ".concat(filePath));
return null;
}
}
}
exports.getLockfile = getLockfile;