aws-lambda-nodejs-esbuild
Version:
λ💨 AWS CDK Construct to bundle JavaScript and TypeScript AWS lambdas using extremely fast esbuild
69 lines (68 loc) • 2.01 kB
JavaScript
;
/**
* Factory for supported packagers.
*
* All packagers must implement the following interface:
*
* interface Packager {
*
* static get lockfileName(): string;
* static get copyPackageSectionNames(): Array<string>;
* static get mustCopyModules(): boolean;
* static getProdDependencies(cwd: string, depth: number = 1): Object;
* static rebaseLockfile(pathToPackageRoot: string, lockfile: Object): void;
* static install(cwd: string): void;
* static prune(cwd: string): void;
* static runScripts(cwd: string, scriptNames): void;
*
* }
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.get = void 0;
const npm_1 = require("./npm");
const yarn_1 = require("./yarn");
const utils_1 = require("../utils");
const registeredPackagers = {
npm: new npm_1.NPM(),
yarn: new yarn_1.Yarn()
};
/**
* Factory method.
* @param {string} packagerId - Well known packager id.
*/
function get(cwd, packagerId) {
const pkger = findPackager(cwd, packagerId);
if (!(pkger in registeredPackagers)) {
const message = `Could not find packager '${pkger}'`;
console.log(`ERROR: ${message}`);
throw new Error(message);
}
return registeredPackagers[pkger];
}
exports.get = get;
/**
* Determine what package manager to use based on what preference is set,
* and whether it's currently running in a yarn/npm script
*
* @export
* @param {InstallConfig} config
* @returns {SupportedPackageManagers}
*/
function findPackager(cwd, prefer) {
let pkgManager = prefer || utils_1.getCurrentPackager();
if (!pkgManager) {
pkgManager = utils_1.getPackagerFromLockfile(cwd);
}
if (!pkgManager) {
for (const pkg in registeredPackagers) {
if (registeredPackagers[pkg].isManagerInstalled(cwd)) {
pkgManager = pkg;
break;
}
}
}
if (!pkgManager) {
throw new Error('No supported package manager found');
}
return pkgManager;
}