|
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +const NpmWithDiff = require('../lib/NpmWithDiff').NpmWithDiff; |
| 4 | + |
| 5 | +const argumentsWithValue = ['--searchedTreeDepth']; |
| 6 | + |
| 7 | +let isDebug = false; |
| 8 | +let forceExecution = false; |
| 9 | +let excludeDevPackagesFromDiff = false; |
| 10 | +let searchedTreeDepthForDiff = 10; |
| 11 | +let command = ''; |
| 12 | + |
| 13 | +async function main() { |
| 14 | + const arguments = process.argv.slice(2); // First two arguments are execPath and path of JS file. |
| 15 | + |
| 16 | + let internalArguments = []; // Arguments that are used for this script. |
| 17 | + let npmArguments = []; // Arguments that are passed to npm. |
| 18 | + |
| 19 | + let isReadingInternalArguments = true; |
| 20 | + arguments.forEach((argument) => { |
| 21 | + if (isReadingInternalArguments) { |
| 22 | + if (argument === '--') { // "--" marks the end of internal arguments. |
| 23 | + isReadingInternalArguments = false; |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + internalArguments.push(argument); |
| 28 | + } else { |
| 29 | + npmArguments.push(argument); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + // If no arguments were added for npm and no "--" was added to the arguments, assume that only npm arguments were |
| 34 | + // provided. This is a little "hack" around the double dash convention, but probably reflects most use cases better. |
| 35 | + if (isReadingInternalArguments) { |
| 36 | + npmArguments = internalArguments.slice(); |
| 37 | + internalArguments = []; |
| 38 | + } |
| 39 | + |
| 40 | + // If first npm argument doesn't start with a dash, assume it's an npm command. |
| 41 | + if (npmArguments.length > 0 && !npmArguments[0].startsWith('-')) { |
| 42 | + command = npmArguments.shift(); // All other npm arguments are treated as arguments for the command. |
| 43 | + } |
| 44 | + |
| 45 | + for (const internalArgument of internalArguments) { |
| 46 | + const splitArgument = internalArgument.split('='); |
| 47 | + const argument = splitArgument[0]; |
| 48 | + let value = null; |
| 49 | + |
| 50 | + if (splitArgument.length > 1) { |
| 51 | + splitArgument.shift(); |
| 52 | + value = splitArgument.join(''); |
| 53 | + |
| 54 | + if (!argumentsWithValue.includes(argument)) { |
| 55 | + console.error('Argument "' + argument + '" cannot be provided with a value.'); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (value === '') { |
| 60 | + console.error('Please provide a value for argument "' + argument + '".'); |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + switch (argument) { |
| 66 | + case '--dev-only-debug': |
| 67 | + isDebug = true; |
| 68 | + break; |
| 69 | + case '--force': |
| 70 | + case '-f': |
| 71 | + forceExecution = true; |
| 72 | + break; |
| 73 | + case '--exclude-dev': |
| 74 | + excludeDevPackagesFromDiff = true; |
| 75 | + break; |
| 76 | + case '--searchedTreeDepth': |
| 77 | + searchedTreeDepthForDiff = value; |
| 78 | + break; |
| 79 | + default: |
| 80 | + console.error('Unknown argument: "' + argument + '".'); |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + await (new NpmWithDiff(isDebug).runCommandWithDiff( |
| 87 | + command, |
| 88 | + npmArguments, |
| 89 | + searchedTreeDepthForDiff, |
| 90 | + excludeDevPackagesFromDiff, |
| 91 | + forceExecution |
| 92 | + )); |
| 93 | +} |
| 94 | + |
| 95 | +main(); |
0 commit comments