Skip to content

Commit 19e27cb

Browse files
committed
Consolidate doctor help text.
1 parent 9d65860 commit 19e27cb

File tree

6 files changed

+60
-59
lines changed

6 files changed

+60
-59
lines changed

src/cli-options.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import _ from 'lodash'
22
import Table from 'cli-table'
33
import chalk from 'chalk'
4-
import { deepPatternPrefix } from './constants'
54
import { Index } from './types/IndexType'
65

76
export interface CLIOption<T = any> {
@@ -163,6 +162,50 @@ my-registry.json:
163162
`
164163
}
165164

165+
/** Extended help for the --doctor option. */
166+
const extendedHelpDoctor = () => `Iteratively installs upgrades and runs tests to identify breaking upgrades.
167+
168+
${chalk.bold('Add "-u" to execute')} (modifies your package file, lock file, and node_modules)
169+
170+
To be more precise:
171+
1. Runs "npm install" and "npm test" to ensure tests are currently passing.
172+
2. Runs "ncu -u" to optimistically upgrade all dependencies.
173+
3. If tests pass, hurray!
174+
4. If tests fail, restores package file and lock file.
175+
5. For each dependency, install upgrade and run tests.
176+
6. Prints broken upgrades with test error.
177+
7. Saves working upgrades to package.json.
178+
179+
Example:
180+
181+
$ ncu --doctor -u
182+
Running tests before upgrading
183+
npm install
184+
npm run test
185+
Upgrading all dependencies and re-running tests
186+
ncu -u
187+
npm install
188+
npm run test
189+
Tests failed
190+
Identifying broken dependencies
191+
npm install
192+
npm install --no-save react@16.0.0
193+
npm run test
194+
✓ react 15.0.0 → 16.0.0
195+
npm install --no-save react-redux@7.0.0
196+
npm run test
197+
✗ react-redux 6.0.0 → 7.0.0
198+
199+
/projects/myproject/test.js:13
200+
throw new Error('Test failed!')
201+
^
202+
203+
npm install --no-save react-dnd@11.1.3
204+
npm run test
205+
✓ react-dnd 10.0.0 → 11.1.3
206+
Saving partially upgraded package.json
207+
`
208+
166209
// store CLI options separately from bin file so that they can be used to build type definitions
167210
const cliOptions: CLIOption[] = [
168211
{
@@ -198,7 +241,7 @@ const cliOptions: CLIOption[] = [
198241
},
199242
{
200243
long: 'deep',
201-
description: `Run recursively in current working directory. Alias of (--packageFile '${deepPatternPrefix}package.json').`,
244+
description: `Run recursively in current working directory. Alias of (--packageFile '**/package.json').`,
202245
type: 'boolean',
203246
},
204247
{
@@ -217,8 +260,9 @@ const cliOptions: CLIOption[] = [
217260
{
218261
long: 'doctor',
219262
description:
220-
'Iteratively installs upgrades and runs tests to identify breaking upgrades. Run "ncu --doctor" for detailed help. Add "-u" to execute.',
263+
'Iteratively installs upgrades and runs tests to identify breaking upgrades. Requires "-u" to execute. Run "ncu --help --doctor" for details.',
221264
type: 'boolean',
265+
help: extendedHelpDoctor(),
222266
},
223267
{
224268
long: 'doctorInstall',

src/constants.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import globby from 'globby'
44
import _ from 'lodash'
55
import Chalk from 'chalk'
66
import packageManagers from './package-managers'
7-
import { doctorHelpText } from './constants'
87
import { print, printJson } from './logging'
8+
import { cliOptionsMap } from './cli-options'
99
import findPackage from './lib/findPackage'
1010
import doctor from './lib/doctor'
1111
import getNcuRc from './lib/getNcuRc'
@@ -165,7 +165,7 @@ export async function run(
165165
}
166166
// print help otherwise
167167
else {
168-
print(options, doctorHelpText, 'warn')
168+
print(options, `Usage: ncu --doctor\n\n${cliOptionsMap.doctor.help}`, 'warn')
169169
}
170170
}
171171
// normal mode

src/lib/initOptions.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import _ from 'lodash'
22
import fs from 'fs'
33
import Chalk from 'chalk'
44
import cliOptions from '../cli-options'
5-
import { deepPatternPrefix } from '../constants'
65
import programError from './programError'
76
import getPackageFileName from './getPackageFileName'
87
import { print } from '../logging'
@@ -92,9 +91,7 @@ function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } = {}): O
9291
else if (options.packageFile && options.deep) {
9392
programError(
9493
options,
95-
chalk.red(
96-
`Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '${deepPatternPrefix}package.json'`,
97-
),
94+
chalk.red(`Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '**/package.json'`),
9895
)
9996
}
10097

@@ -128,7 +125,7 @@ function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } = {}): O
128125

129126
return {
130127
...options,
131-
...(options.deep ? { packageFile: `${deepPatternPrefix}${getPackageFileName(options)}` } : null),
128+
...(options.deep ? { packageFile: `**/${getPackageFileName(options)}` } : null),
132129
...((options.args || []).length > 0 ? { filter: options.args!.join(' ') } : null),
133130
...(format.length > 0 ? { format } : null),
134131
// add shortcut for any keys that start with 'json'

src/lib/queryVersions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import chalk from 'chalk'
22
import pMap from 'p-map'
33
import ProgressBar from 'progress'
44
import { parseRange } from 'semver-utils'
5-
import { supportedVersionTargets } from '../constants'
65
import getPackageManager from './getPackageManager'
76
import packageManagers from '../package-managers'
87
import { createNpmAlias, isGithubUrl, isPre, parseNpmAlias } from '../version-util'
@@ -14,6 +13,8 @@ import { Version } from '../types/Version'
1413
import { VersionResult } from '../types/VersionResult'
1514
import { VersionSpec } from '../types/VersionSpec'
1615

16+
const supportedVersionTargets = ['latest', 'newest', 'greatest', 'minor', 'patch']
17+
1718
/**
1819
* Get the latest or greatest versions from the NPM repository based on the version target.
1920
*

test/doctor.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import fs from 'fs'
22
import path from 'path'
33
import chai from 'chai'
4+
import chalk from 'chalk'
45
import chaiAsPromised from 'chai-as-promised'
56
import spawn from 'spawn-please'
67
import rimraf from 'rimraf'
78
import stripAnsi from 'strip-ansi'
8-
import { doctorHelpText } from '../src/constants'
9+
import { cliOptionsMap } from '../src/cli-options'
910

1011
chai.should()
1112
chai.use(chaiAsPromised)
@@ -138,7 +139,11 @@ describe('doctor', function () {
138139
describe('npm', () => {
139140
it('print instructions when -u is not specified', async () => {
140141
const cwd = path.join(doctorTests, 'nopackagefile')
141-
return ncu(['--doctor'], { cwd }).should.eventually.equal(doctorHelpText + '\n')
142+
const output = await ncu(['--doctor'], { cwd })
143+
// output does not include chalk formatting
144+
// chalk.reset does not seem to work, so just format the expected bold text in the output before comparing
145+
const outputFormatted = output.replace('Add "-u" to execute', chalk.bold('Add "-u" to execute'))
146+
return outputFormatted.should.equal(`Usage: ncu --doctor\n\n${cliOptionsMap.doctor.help}\n`)
142147
})
143148

144149
it('throw an error if there is no package file', async () => {

0 commit comments

Comments
 (0)