UNPKG

pmex

Version:

Run dynamic NPM or YARN or PNPM

100 lines (99 loc) 4.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_child_process_1 = require("node:child_process"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const constants_1 = require("./constants"); const resolver_1 = __importDefault(require("./resolver")); function pmex(command, options) { var _a, _b, _c, _d, _e; const execPath = `${((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.npm_execpath) || ''}`.toLowerCase(); let runner = execPath.includes('bun') ? 'bun' : execPath.includes('pnpm') ? 'pnpm' : execPath.includes('yarn') ? 'yarn' : execPath.includes('npm') ? 'npm' : null; // @ts-expect-error let cmd = `${typeof command === 'string' ? command : (_b = command === null || command === void 0 ? void 0 : command[runner]) !== null && _b !== void 0 ? _b : command === null || command === void 0 ? void 0 : command.default}`.trim(); // Check if command replaces package manager detection if (constants_1.RUNNERS.some((runner) => cmd.startsWith(`${runner} `))) { // @ts-expect-error runner = cmd.split(' ', 1).shift(); } cmd = cmd .replace(new RegExp(`^(${constants_1.RUNNERS.join('|')})\\s+`), '') .replace(/^(run)\s+/, '') .trim(); // Detect global binaries const npmGlobalDir = (0, node_child_process_1.execSync)('npm root -g').toString().trim(); const globalBins = (0, node_fs_1.existsSync)(npmGlobalDir) ? (0, node_fs_1.readdirSync)(npmGlobalDir) .filter((file) => { return (0, node_fs_1.existsSync)((0, node_path_1.join)(npmGlobalDir, file, 'package.json')); }) .flatMap((file) => { var _a; const pkgJSON = require((0, node_path_1.join)(npmGlobalDir, file, 'package.json')); const pkgBin = (_a = pkgJSON.bin) !== null && _a !== void 0 ? _a : {}; return typeof pkgBin === 'string' ? pkgJSON.name : Object.keys(pkgBin); }) : []; // Detect local binaries const binPath = (0, node_path_1.join)(`${process.cwd()}`, 'node_modules', '.bin'); const localBins = (0, node_fs_1.existsSync)(binPath) ? (0, node_fs_1.readdirSync)(binPath).map((file) => file.split('.').shift()) : []; // All bins const binScripts = [...globalBins, ...localBins]; // Detect scripts const pkgPath = (0, node_path_1.join)(`${process.cwd()}`, 'package.json'); const pkgScripts = (0, node_fs_1.existsSync)(pkgPath) ? Object.keys((_d = (_c = require(pkgPath)) === null || _c === void 0 ? void 0 : _c.scripts) !== null && _d !== void 0 ? _d : {}) : []; const cmdArg = (_e = cmd.split(' ').shift()) !== null && _e !== void 0 ? _e : ''; const isBinScript = binScripts.includes(cmdArg); const isPkgScript = pkgScripts.includes(cmdArg); const runScript = (0, resolver_1.default)(`${isBinScript ? '' : `${runner} `}${isPkgScript ? 'run ' : ''}${cmd}`); const splitScript = runScript.split(' '); const printRunner = isBinScript ? runner : splitScript.shift(); const printCommand = (isPkgScript ? splitScript.slice(1) : splitScript).join(' '); process.stdout.write('\n'); process.stdout.write(colors.bgBlue); process.stdout.write(` ${printRunner} `); if (isBinScript) { process.stdout.write(colors.bgGreen); process.stdout.write(' bin '); } if (isPkgScript) { process.stdout.write(colors.bgYellow); process.stdout.write(' run '); } process.stdout.write(colors.reset); process.stdout.write(` ${printCommand}`); process.stdout.write('\n\n'); return (0, node_child_process_1.execSync)(runScript, Object.assign({ stdio: 'inherit', encoding: 'utf-8' }, options)); } exports.default = pmex; const colors = { // Defaults reset: '\x1b[0m', bright: '\x1b[1m', dim: '\x1b[2m', underscore: '\x1b[4m', blink: '\x1b[5m', reverse: '\x1b[7m', hidden: '\x1b[8m', // Background bgBlack: '\x1b[40m', bgRed: '\x1b[41m', bgGreen: '\x1b[42m', bgYellow: '\x1b[43m', bgBlue: '\x1b[44m', bgMagenta: '\x1b[45m', bgCyan: '\x1b[46m', bgWhite: '\x1b[47m', bgGray: '\x1b[100m', };