From 2e648fd7005739286b718f1b3c97e1fd970f0f22 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sat, 26 Sep 2020 00:40:23 +0400 Subject: [PATCH] feat: let --npm-v7 flag be applied to regular repos --- README.md | 2 +- src/main/ts/index.ts | 2 +- src/main/ts/util.ts | 8 ++++++-- src/test/ts/util.ts | 13 +++++++------ 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 752d7cd4..b6b5200b 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ success Already up-to-date. |`--only` | Set package [updating scope](https://docs.npmjs.com/cli/audit): `dev`/`prod` |`--force` | Have audit fix install semver-major updates to toplevel dependencies, not just semver-compatible ones | false |`--audit-level` | Include a vulnerability with a level as defined or higher. Supported values: low, moderate, high, critical | low -|`--npm-v7` | Use the latest `npm@7.x.x-beta.x` version. Applicable to monorepos only | false +|`--npm-v7` | Use the latest `npm@7.x.x-beta.x` version. Recommended for monorepos | false ## Troubleshooting ### enoent: no such file or directory diff --git a/src/main/ts/index.ts b/src/main/ts/index.ts index ec6c00dd..d5177dfd 100644 --- a/src/main/ts/index.ts +++ b/src/main/ts/index.ts @@ -85,7 +85,7 @@ const yarnLockToPkgLock: TCallback = ({temp}) => { */ const npmAuditFix: TCallback = ({temp, flags, cwd, manifest}) => { const requireNpmBeta = !!manifest.workspaces - const npm = getNpm(requireNpmBeta, flags['npm-v7']) + const npm = getNpm(requireNpmBeta, flags['npm-v7'], flags.silent) const defaultFlags = { 'package-lock-only': true, } diff --git a/src/main/ts/util.ts b/src/main/ts/util.ts index cc8c48af..860033bd 100644 --- a/src/main/ts/util.ts +++ b/src/main/ts/util.ts @@ -64,10 +64,14 @@ export const getClosestNpm = (cmd: string): string => { }, {cwd: pkgRoot}) + '' } -export const getNpm = (requireNpmBeta?: boolean, allowNpmBeta?: boolean, isWin = isWindows()) => { +export const getNpm = (requireNpmBeta?: boolean, allowNpmBeta?: boolean, silent = false, isWin = isWindows()) => { const cmd = isWin ? 'npm.cmd' : 'npm' - return requireNpmBeta && allowNpmBeta + if (requireNpmBeta && !allowNpmBeta && !silent) { + console.warn('The project looks like monorepo, so it is recommended to use `--npm-v7` flag') + } + + return allowNpmBeta ? getClosestNpm(cmd) : cmd } diff --git a/src/test/ts/util.ts b/src/test/ts/util.ts index 3d7fd6c6..472b3567 100644 --- a/src/test/ts/util.ts +++ b/src/test/ts/util.ts @@ -57,14 +57,15 @@ describe('util', () => { it('properly resolves npm ref', () => { const cmd = isWindows() ? 'npm.cmd' : 'npm' const localNpm = resolve(__dirname, '../../../node_modules/.bin', cmd) - const cases: [boolean, boolean, string][] = [ - [true, true, localNpm], - [true, false, cmd], - [false, true, cmd], + const cases: [boolean, boolean, boolean, string][] = [ + [true, true, true, localNpm], + [false, true, true, localNpm], + [true, false, true, cmd], + [false, false, true, cmd], ] - cases.forEach(([requireNpm7, allowNpm7, result]) => { - expect(getNpm(requireNpm7, allowNpm7)).toBe(result) + cases.forEach(([requireNpm7, allowNpm7, silent, result]) => { + expect(getNpm(requireNpm7, allowNpm7, silent)).toBe(result) }) })