UNPKG

aws-lambda-nodejs-esbuild

Version:

λ💨 AWS CDK Construct to bundle JavaScript and TypeScript AWS lambdas using extremely fast esbuild

156 lines (155 loc) 5.84 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPackagerFromLockfile = exports.getCurrentPackager = exports.nodeMajorVersion = exports.findProjectRoot = exports.findUp = exports.extractFileName = exports.splitLines = exports.safeJsonParse = exports.spawnProcess = exports.SpawnError = void 0; const childProcess = __importStar(require("child_process")); const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); const ramda_1 = require("ramda"); class SpawnError extends Error { constructor(message, stdout, stderr) { super(message); this.stdout = stdout; this.stderr = stderr; } toString() { return `${this.message}\n${this.stderr}`; } } exports.SpawnError = SpawnError; /** * Executes a child process without limitations on stdout and stderr. * On error (exit code is not 0), it rejects with a SpawnProcessError that contains the stdout and stderr streams, * on success it returns the streams in an object. * @param {string} command - Command * @param {string[]} [args] - Arguments * @param {Object} [options] - Options for child_process.spawn */ function spawnProcess(command, args, options) { var _a, _b; const child = childProcess.spawnSync(command, args, options); const stdout = (_a = child.stdout) === null || _a === void 0 ? void 0 : _a.toString('utf8'); const stderr = (_b = child.stderr) === null || _b === void 0 ? void 0 : _b.toString('utf8'); if (child.status !== 0) { throw new SpawnError(`${command} ${ramda_1.join(' ', args)} failed with code ${child.status}`, stdout, stderr); } return { stdout, stderr }; } exports.spawnProcess = spawnProcess; function safeJsonParse(str) { try { return JSON.parse(str); } catch (e) { return null; } } exports.safeJsonParse = safeJsonParse; function splitLines(str) { return str.split(/\r?\n/); } exports.splitLines = splitLines; /** * Extracts the file name from handler string. */ function extractFileName(cwd, handler) { const fnName = path.extname(handler); const fnNameLastAppearanceIndex = handler.lastIndexOf(fnName); // replace only last instance to allow the same name for file and handler const fileName = handler.substring(0, fnNameLastAppearanceIndex); // Check if the .ts files exists. If so return that to watch if (fs.existsSync(path.join(cwd, fileName + '.ts'))) { return fileName + '.ts'; } // Check if the .js files exists. If so return that to watch if (fs.existsSync(path.join(cwd, fileName + '.js'))) { return fileName + '.js'; } // Can't find the files. Watch will have an exception anyway. So throw one with error. console.log(`Cannot locate handler - ${fileName} not found`); throw new Error('Compilation failed. Please ensure handler exists with ext .ts or .js'); } exports.extractFileName = extractFileName; /** * Find a file by walking up parent directories */ function findUp(name, directory = process.cwd()) { const absoluteDirectory = path.resolve(directory); if (fs.existsSync(path.join(directory, name))) { return directory; } const { root } = path.parse(absoluteDirectory); if (absoluteDirectory === root) { return undefined; } return findUp(name, path.dirname(absoluteDirectory)); } exports.findUp = findUp; /** * Forwards `rootDir` or finds project root folder. */ function findProjectRoot(rootDir) { var _a, _b, _c; return ((_c = (_b = (_a = rootDir !== null && rootDir !== void 0 ? rootDir : findUp('yarn.lock')) !== null && _a !== void 0 ? _a : findUp('package-lock.json')) !== null && _b !== void 0 ? _b : findUp('package.json')) !== null && _c !== void 0 ? _c : findUp(`.git${path.sep}`)); } exports.findProjectRoot = findProjectRoot; /** * Returns the major version of node installation */ function nodeMajorVersion() { return parseInt(process.versions.node.split('.')[0], 10); } exports.nodeMajorVersion = nodeMajorVersion; /** * Returns the package manager currently active if the program is executed * through an npm or yarn script like: * ```bash * yarn run example * npm run example * ``` */ function getCurrentPackager() { const userAgent = process.env.npm_config_user_agent; if (!userAgent) { return null; } if (userAgent.startsWith('npm')) { return 'npm'; } if (userAgent.startsWith('yarn')) { return 'yarn'; } return null; } exports.getCurrentPackager = getCurrentPackager; /** * Checks for the presence of package-lock.json or yarn.lock to determine which package manager is being used */ function getPackagerFromLockfile(cwd) { if (fs.existsSync(path.join(cwd, 'package-lock.json'))) { return 'npm'; } if (fs.existsSync(path.join(cwd, 'yarn.lock'))) { return 'yarn'; } return null; } exports.getPackagerFromLockfile = getPackagerFromLockfile;