UNPKG

@process-engine/ci_tools

Version:
136 lines (133 loc) 5.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.printHelp = exports.getShortDoc = exports.run = void 0; const chalk = require("chalk"); const fs_1 = require("fs"); const shell_1 = require("../cli/shell"); const COMMAND_NAME = 'npm-install-only'; const BADGE = `[${COMMAND_NAME}]\t`; const DOC = ` Uses \`npm install [--save-exact] <PACKAGE_NAME>\` on all dependencies matching the given patterns. Example: ci_tools ${COMMAND_NAME} @process-engine/ installs only deps starting with \`@process-engine/\`, honoring their '~' and '^' requirements. `; // DOC: see above async function run(...args) { const isDryRun = args.indexOf('--dry') !== -1; const allPackageNamesWithNoStrictVersion = getAllPackageNamesWithNoStrictVersion(); const allPackageNamesWithStrictVersion = getAllPackageNamesWithStrictVersion(); const patternList = args.filter((arg) => !arg.startsWith('-')); const foundPatternMatchingPackagesWithNoStrictVersion = getPackageNamesMatchingPattern(allPackageNamesWithNoStrictVersion, patternList); const foundPatternMatchingPackagesWithStrictVersion = getPackageNamesMatchingPattern(allPackageNamesWithStrictVersion, patternList); const npmInstallArguments = foundPatternMatchingPackagesWithNoStrictVersion.join(' '); const npmInstallSaveExactArguments = foundPatternMatchingPackagesWithStrictVersion.join(' '); console.log(`${BADGE}`); console.log(`${BADGE}allPackageNamesWithNoStrictVersion:`, allPackageNamesWithNoStrictVersion); console.log(`${BADGE}allPackageNamesWithStrictVersion:`, allPackageNamesWithStrictVersion); console.log(`${BADGE}patternList:`, patternList); console.log(`${BADGE}foundPatternMatchingPackagesWithNoStrictVersion:`, foundPatternMatchingPackagesWithNoStrictVersion); console.log(`${BADGE}foundPatternMatchingPackagesWithStrictVersion:`, foundPatternMatchingPackagesWithStrictVersion); console.log(`${BADGE}`); if (npmInstallArguments.length !== 0) { await annotatedSh(`npm install ${npmInstallArguments}`, isDryRun); } if (npmInstallSaveExactArguments.length !== 0) { await annotatedSh(`npm install --save-exact ${npmInstallSaveExactArguments}`, isDryRun); } return true; } exports.run = run; function getShortDoc() { return DOC.trim().split('\n')[0]; } exports.getShortDoc = getShortDoc; function printHelp() { console.log(`Usage: ci_tools ${COMMAND_NAME} <package-pattern> [<package-pattern>...] [--dry]`); console.log(''); console.log(DOC.trim()); } exports.printHelp = printHelp; async function annotatedSh(command, isDryRun) { console.log(`${BADGE}`); console.log(`${BADGE}Running: ${chalk.cyan(command)}`); if (isDryRun) { console.log(chalk.yellow('\n [skipping execution due to --dry]\n')); return; } const output = await (0, shell_1.asyncSh)(command); console.log(output); } function getPackageNamesMatchingPattern(allPackageNames, patternList) { let foundPatternMatchingPackages = []; patternList.forEach((nameStart) => { const packages = allPackageNames.filter((packageName) => { return packageName.startsWith(nameStart); }); foundPatternMatchingPackages = foundPatternMatchingPackages.concat(packages); }); return foundPatternMatchingPackages; } function getAllPackageNamesWithNoStrictVersion() { const content = (0, fs_1.readFileSync)('package.json').toString(); const json = JSON.parse(content); let dependencies = []; let devDependencies = []; if (json.dependencies) { dependencies = Object.keys(json.dependencies) .filter((dependency) => { const version = json.dependencies[dependency]; const versionIsNotStrict = version.startsWith('^') || version.startsWith('~'); return versionIsNotStrict; }) .map((dependency) => { const version = json.dependencies[dependency]; return `${dependency}@${version}`; }); } if (json.devDependencies) { devDependencies = Object.keys(json.devDependencies) .filter((devDependency) => { const version = json.devDependencies[devDependency]; const versionIsNotStrict = version.startsWith('^') || version.startsWith('~'); return versionIsNotStrict; }) .map((devDependency) => { return `${devDependency}@${json.devDependencies[devDependency]}`; }); } const allPackageNames = [...dependencies].concat(devDependencies).sort(); return allPackageNames; } function getAllPackageNamesWithStrictVersion() { const content = (0, fs_1.readFileSync)('package.json').toString(); const json = JSON.parse(content); let dependencies = []; let devDependencies = []; if (json.dependencies) { dependencies = Object.keys(json.dependencies) .filter((dependency) => { const version = json.dependencies[dependency]; const versionIsStrict = !(version.startsWith('^') || version.startsWith('~')); return versionIsStrict; }) .map((dependency) => { const version = json.dependencies[dependency]; return `${dependency}@${version}`; }); } if (json.devDependencies) { devDependencies = Object.keys(json.devDependencies) .filter((devDependency) => { const version = json.devDependencies[devDependency]; const versionIsNotStrict = !(version.startsWith('^') || version.startsWith('~')); return versionIsNotStrict; }) .map((devDependency) => { return `${devDependency}@${json.devDependencies[devDependency]}`; }); } const allPackageNames = [...dependencies].concat(devDependencies).sort(); return allPackageNames; } //# sourceMappingURL=npm-install-only.js.map