|
| 1 | +const log = require('proc-log') |
| 2 | +const npa = require('npm-package-arg') |
| 3 | +const { partition } = require('lodash') |
1 | 4 | const hasPackage = require('../util/has-package.js')
|
2 |
| -const { groupBy } = require('lodash') |
3 |
| - |
4 |
| -const run = ({ pkg, config: { requiredPackages = {} } }) => { |
5 |
| - // ensure required packages are present in the correct place |
6 |
| - |
7 |
| - const mustHave = Object.entries(requiredPackages).flatMap(([location, pkgs]) => |
8 |
| - // first make a flat array of {name, version, location} |
9 |
| - Object.entries(pkgs).map(([name, version]) => ({ |
10 |
| - name, |
11 |
| - version, |
12 |
| - location, |
13 |
| - }))) |
14 |
| - .filter(({ name, version, location }) => !hasPackage(pkg, name, version, [location])) |
15 |
| - |
16 |
| - if (mustHave.length) { |
17 |
| - return Object.entries(groupBy(mustHave, 'location')).map(([location, specs]) => { |
18 |
| - const rm = specs.map(({ name }) => name).join(' ') |
19 |
| - const install = specs.map(({ name, version }) => `${name}@${version}`).join(' ') |
20 |
| - const installLocation = hasPackage.flags[location] |
| 5 | + |
| 6 | +const rmCommand = (specs) => |
| 7 | + `npm rm ${specs.map((s) => s.name).join(' ')}`.trim() |
| 8 | + |
| 9 | +const installCommand = (specs, flags) => specs.length ? |
| 10 | + `npm i ${specs.map((s) => `${s.name}@${s.fetchSpec}`).join(' ')} ${flags.join(' ')}`.trim() : '' |
| 11 | + |
| 12 | +// ensure required packages are present in the correct place |
| 13 | +const run = ({ pkg, path, config: { requiredPackages = {} } }) => { |
| 14 | + // keys are the dependency location in package.json |
| 15 | + // values are a filtered list of parsed specs that dont exist in the current package |
| 16 | + // { [location]: [spec1, spec2] } |
| 17 | + const requiredByLocation = Object.entries(requiredPackages) |
| 18 | + .reduce((acc, [location, pkgs]) => { |
| 19 | + acc[location] = pkgs |
| 20 | + .filter((spec) => !hasPackage(pkg, spec, [location], path)) |
| 21 | + .map((spec) => npa(spec)) |
| 22 | + log.verbose(location, pkg, pkgs) |
| 23 | + return acc |
| 24 | + }, {}) |
| 25 | + |
| 26 | + const requiredEntries = Object.entries(requiredByLocation) |
| 27 | + |
| 28 | + log.verbose('check-required', requiredEntries) |
| 29 | + |
| 30 | + if (requiredEntries.flatMap(([, specs]) => specs).length) { |
| 31 | + return requiredEntries.map(([location, specs]) => { |
| 32 | + const locationFlag = hasPackage.flags[location] |
| 33 | + const [exactSpecs, saveSpecs] = partition(specs, (s) => s.type === 'version') |
| 34 | + |
| 35 | + log.verbose('check-required', location, specs) |
21 | 36 |
|
22 | 37 | return {
|
23 | 38 | title: `The following required ${location} were not found:`,
|
24 |
| - body: specs.map(({ name, version }) => `${name}@${version}`), |
| 39 | + body: specs.map((s) => s.rawSpec ? `${s.name}@${s.rawSpec}` : s.name), |
25 | 40 | // solution is to remove any existing all at once but add back in by --save-<location>
|
26 |
| - solution: [`npm rm ${rm}`, `npm i ${install} ${installLocation}`].join(' && '), |
| 41 | + solution: [ |
| 42 | + rmCommand(specs), |
| 43 | + installCommand(saveSpecs, [locationFlag]), |
| 44 | + installCommand(exactSpecs, [locationFlag, '--save-exact']), |
| 45 | + ].filter(Boolean).join(' && '), |
27 | 46 | }
|
28 | 47 | })
|
29 | 48 | }
|
|
0 commit comments