pmex
Version:
Run dynamic NPM or YARN or PNPM
44 lines (43 loc) • 1.89 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a, _b;
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = require("node:fs");
const constants_1 = require("./constants");
const pmex_1 = __importDefault(require("./pmex"));
const [, , ...args] = process.argv;
const isPackageScript = Boolean(process.env.npm_lifecycle_event);
let command = args.join(' ');
if (!isPackageScript) {
let runner = null;
if ((0, node_fs_1.existsSync)('package.json')) {
const pkg = JSON.parse((0, node_fs_1.readFileSync)('package.json', 'utf8'));
if (pkg.packageManager) {
runner = (_b = (_a = pkg.packageManager) === null || _a === void 0 ? void 0 : _a.split('@')) === null || _b === void 0 ? void 0 : _b[0];
}
}
if (!constants_1.RUNNERS.some((runner) => command.startsWith(`${runner} `))) {
let mtimeMs = 0;
const locks = {
bun: (0, node_fs_1.existsSync)('bun.lockb') ? (0, node_fs_1.statSync)('bun.lockb').mtimeMs : 0,
pnpm: (0, node_fs_1.existsSync)('pnpm-lock.yaml') ? (0, node_fs_1.statSync)('pnpm-lock.yaml').mtimeMs : 0,
yarn: (0, node_fs_1.existsSync)('yarn.lock') ? (0, node_fs_1.statSync)('yarn.lock').mtimeMs : 0,
npm: (0, node_fs_1.existsSync)('package-lock.json') ? (0, node_fs_1.statSync)('package-lock.json').mtimeMs : 0,
};
for (const attr in locks) {
// @ts-expect-error
const currentModifiedAt = locks[attr];
if (currentModifiedAt && currentModifiedAt > mtimeMs) {
runner = attr;
mtimeMs = currentModifiedAt;
}
}
}
if (runner) {
command = `${runner} ${command}`;
}
}
(0, pmex_1.default)(command);