aws-lambda-nodejs-esbuild
Version:
λ💨 AWS CDK Construct to bundle JavaScript and TypeScript AWS lambdas using extremely fast esbuild
119 lines (118 loc) • 4.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Yarn = void 0;
const ramda_1 = require("ramda");
const utils_1 = require("../utils");
/**
* Yarn packager.
*
* Yarn specific packagerOptions (default):
* flat (false) - Use --flat with install
* ignoreScripts (false) - Do not execute scripts during install
*/
class Yarn {
get lockfileName() {
return 'yarn.lock';
}
get copyPackageSectionNames() {
return ['resolutions'];
}
get mustCopyModules() {
return false;
}
isManagerInstalled(cwd) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['--version'];
try {
utils_1.spawnProcess(command, args, { cwd });
return true;
}
catch (_e) {
return false;
}
}
getProdDependencies(cwd, depth) {
var _a, _b;
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['list', `--depth=${depth || 1}`, '--json', '--production'];
// If we need to ignore some errors add them here
const ignoredYarnErrors = [];
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}`), ignoredYarnErrors));
}, false);
if (failed || ramda_1.isEmpty(err.stdout)) {
throw err;
}
processOutput = { stdout: err.stdout };
}
const lines = utils_1.splitLines(processOutput.stdout);
const parsedLines = lines.map(utils_1.safeJsonParse);
const parsedTree = parsedLines.find(line => line && line.type === 'tree');
const convertTrees = ts => ts.reduce((__, tree) => {
var _a;
const splitModule = ramda_1.split('@', tree.name);
// If we have a scoped module we have to re-add the @
if (tree.name.startsWith('@')) {
splitModule.splice(0, 1);
splitModule[0] = '@' + splitModule[0];
}
__[(_a = ramda_1.head(splitModule)) !== null && _a !== void 0 ? _a : ''] = {
version: ramda_1.join('@', ramda_1.tail(splitModule)),
dependencies: convertTrees(tree.children)
};
return __;
}, {});
const trees = ramda_1.pathOr([], ['data', 'trees'], parsedTree);
const result = {
problems: [],
dependencies: convertTrees(trees)
};
return result;
}
rebaseLockfile(pathToPackageRoot, lockfile) {
const fileVersionMatcher = /[^"/]@(?:file:)?((?:\.\/|\.\.\/).*?)[":,]/gm;
const replacements = [];
let match;
// Detect all references and create replacement line strings
while ((match = fileVersionMatcher.exec(lockfile)) !== null) {
replacements.push({
oldRef: match[1],
newRef: `${pathToPackageRoot}/${match[1]}`.replace(/\\/g, '/')
});
}
// Replace all lines in lockfile
return replacements.reduce((__, replacement) => __.replace(replacement.oldRef, replacement.newRef), lockfile);
}
install(cwd, packagerOptions) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
const args = ['install', '--frozen-lockfile', '--non-interactive'];
// Convert supported packagerOptions
if (packagerOptions === null || packagerOptions === void 0 ? void 0 : packagerOptions.ignoreScripts) {
args.push('--ignore-scripts');
}
utils_1.spawnProcess(command, args, { cwd });
}
// "Yarn install" prunes automatically
prune(cwd, packagerOptions) {
return this.install(cwd, packagerOptions);
}
runScripts(cwd, scriptNames) {
const command = /^win/.test(process.platform) ? 'yarn.cmd' : 'yarn';
scriptNames.forEach(scriptName => utils_1.spawnProcess(command, ['run', scriptName], { cwd }));
}
}
exports.Yarn = Yarn;