UNPKG

@pnpm/package-bins

Version:
60 lines 2.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBinsFromPackageManifest = getBinsFromPackageManifest; const path_1 = __importDefault(require("path")); const tinyglobby_1 = require("tinyglobby"); const is_subdir_1 = __importDefault(require("is-subdir")); async function getBinsFromPackageManifest(manifest, pkgPath) { if (manifest.bin) { return commandsFromBin(manifest.bin, manifest.name, pkgPath); } if (manifest.directories?.bin) { const binDir = path_1.default.join(pkgPath, manifest.directories.bin); const files = await findFiles(binDir); return files.map((file) => ({ name: path_1.default.basename(file), path: path_1.default.join(binDir, file), })); } return []; } async function findFiles(dir) { try { return await (0, tinyglobby_1.glob)('**', { cwd: dir, onlyFiles: true, followSymbolicLinks: false, expandDirectories: false, }); } catch (err) { // eslint-disable-line if (err.code !== 'ENOENT') { throw err; } return []; } } function commandsFromBin(bin, pkgName, pkgPath) { if (typeof bin === 'string') { return [ { name: normalizeBinName(pkgName), path: path_1.default.join(pkgPath, bin), }, ]; } return Object.keys(bin) .filter((commandName) => encodeURIComponent(commandName) === commandName || commandName === '$' || commandName[0] === '@') .map((commandName) => ({ name: normalizeBinName(commandName), path: path_1.default.join(pkgPath, bin[commandName]), })) .filter((cmd) => (0, is_subdir_1.default)(pkgPath, cmd.path)); } function normalizeBinName(name) { return name[0] === '@' ? name.slice(name.indexOf('/') + 1) : name; } //# sourceMappingURL=index.js.map