aws-lambda-nodejs-esbuild
Version:
λ💨 AWS CDK Construct to bundle JavaScript and TypeScript AWS lambdas using extremely fast esbuild
110 lines (109 loc) • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NPM = void 0;
const ramda_1 = require("ramda");
const utils_1 = require("../utils");
/**
* NPM packager.
*/
class NPM {
get lockfileName() {
return 'package-lock.json';
}
get copyPackageSectionNames() {
return [];
}
get mustCopyModules() {
return true;
}
isManagerInstalled(cwd) {
const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const args = ['--version'];
try {
utils_1.spawnProcess(command, args, { cwd });
return true;
}
catch (_e) {
return false;
}
}
getProdDependencies(cwd, depth) {
var _a, _b;
// Get first level dependency graph
const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const args = [
'ls',
'-prod',
'-json',
`-depth=${depth || 1}`
];
const ignoredNpmErrors = [
{ npmError: 'extraneous', log: false },
{ npmError: 'missing', log: false },
{ npmError: 'peer dep missing', log: true }
];
let processOutput;
try {
processOutput = utils_1.spawnProcess(command, args, { cwd });
}
catch (err) {
if (!(err instanceof utils_1.SpawnError)) {
throw err;
}
// Only exit with an error if we have critical npm errors for 2nd level inside
const errors = (_b = (_a = err.stderr) === null || _a === void 0 ? void 0 : _a.split('\n')) !== null && _b !== void 0 ? _b : [];
const failed = errors.reduce((f, error) => {
if (f) {
return true;
}
return (!ramda_1.isEmpty(error) &&
!ramda_1.any(ignoredError => error.startsWith(`npm ERR! ${ignoredError.npmError}`), ignoredNpmErrors));
}, false);
if (failed || ramda_1.isEmpty(err.stdout)) {
throw err;
}
processOutput = { stdout: err.stdout };
}
return JSON.parse(processOutput.stdout);
}
/**
* We should not be modifying 'package-lock.json'
* because this file should be treated as internal to npm.
*
* Rebase package-lock is a temporary workaround and must be
* removed as soon as https://github.com/npm/npm/issues/19183 gets fixed.
*/
rebaseLockfile(pathToPackageRoot, lockfile) {
if (lockfile.version) {
lockfile.version = this.rebaseFileReferences(pathToPackageRoot, lockfile.version);
}
if (lockfile.dependencies) {
for (const lockedDependency in lockfile.dependencies) {
this.rebaseLockfile(pathToPackageRoot, lockfile.dependencies[lockedDependency]);
}
}
return lockfile;
}
install(cwd) {
const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const args = ['install'];
utils_1.spawnProcess(command, args, { cwd });
}
prune(cwd) {
const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const args = ['prune'];
utils_1.spawnProcess(command, args, { cwd });
}
runScripts(cwd, scriptNames) {
const command = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
scriptNames.forEach(scriptName => utils_1.spawnProcess(command, ['run', scriptName], { cwd }));
}
rebaseFileReferences(pathToPackageRoot, moduleVersion) {
if (/^file:[^/]{2}/.test(moduleVersion)) {
const filePath = moduleVersion.replace(/^file:/, '');
return `file:${pathToPackageRoot}/${filePath}`.replace(/\\/g, '/');
}
return moduleVersion;
}
}
exports.NPM = NPM;