Skip to content

Commit

Permalink
Consolidate doctor help text.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Jun 23, 2022
1 parent 9d65860 commit 19e27cb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 59 deletions.
50 changes: 47 additions & 3 deletions src/cli-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import _ from 'lodash'
import Table from 'cli-table'
import chalk from 'chalk'
import { deepPatternPrefix } from './constants'
import { Index } from './types/IndexType'

export interface CLIOption<T = any> {
Expand Down Expand Up @@ -163,6 +162,50 @@ my-registry.json:
`
}

/** Extended help for the --doctor option. */
const extendedHelpDoctor = () => `Iteratively installs upgrades and runs tests to identify breaking upgrades.
${chalk.bold('Add "-u" to execute')} (modifies your package file, lock file, and node_modules)
To be more precise:
1. Runs "npm install" and "npm test" to ensure tests are currently passing.
2. Runs "ncu -u" to optimistically upgrade all dependencies.
3. If tests pass, hurray!
4. If tests fail, restores package file and lock file.
5. For each dependency, install upgrade and run tests.
6. Prints broken upgrades with test error.
7. Saves working upgrades to package.json.
Example:
$ ncu --doctor -u
Running tests before upgrading
npm install
npm run test
Upgrading all dependencies and re-running tests
ncu -u
npm install
npm run test
Tests failed
Identifying broken dependencies
npm install
npm install --no-save react@16.0.0
npm run test
✓ react 15.0.0 → 16.0.0
npm install --no-save react-redux@7.0.0
npm run test
✗ react-redux 6.0.0 → 7.0.0
/projects/myproject/test.js:13
throw new Error('Test failed!')
^
npm install --no-save react-dnd@11.1.3
npm run test
✓ react-dnd 10.0.0 → 11.1.3
Saving partially upgraded package.json
`

// store CLI options separately from bin file so that they can be used to build type definitions
const cliOptions: CLIOption[] = [
{
Expand Down Expand Up @@ -198,7 +241,7 @@ const cliOptions: CLIOption[] = [
},
{
long: 'deep',
description: `Run recursively in current working directory. Alias of (--packageFile '${deepPatternPrefix}package.json').`,
description: `Run recursively in current working directory. Alias of (--packageFile '**/package.json').`,
type: 'boolean',
},
{
Expand All @@ -217,8 +260,9 @@ const cliOptions: CLIOption[] = [
{
long: 'doctor',
description:
'Iteratively installs upgrades and runs tests to identify breaking upgrades. Run "ncu --doctor" for detailed help. Add "-u" to execute.',
'Iteratively installs upgrades and runs tests to identify breaking upgrades. Requires "-u" to execute. Run "ncu --help --doctor" for details.',
type: 'boolean',
help: extendedHelpDoctor(),
},
{
long: 'doctorInstall',
Expand Down
46 changes: 0 additions & 46 deletions src/constants.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import globby from 'globby'
import _ from 'lodash'
import Chalk from 'chalk'
import packageManagers from './package-managers'
import { doctorHelpText } from './constants'
import { print, printJson } from './logging'
import { cliOptionsMap } from './cli-options'
import findPackage from './lib/findPackage'
import doctor from './lib/doctor'
import getNcuRc from './lib/getNcuRc'
Expand Down Expand Up @@ -165,7 +165,7 @@ export async function run(
}
// print help otherwise
else {
print(options, doctorHelpText, 'warn')
print(options, `Usage: ncu --doctor\n\n${cliOptionsMap.doctor.help}`, 'warn')
}
}
// normal mode
Expand Down
7 changes: 2 additions & 5 deletions src/lib/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import _ from 'lodash'
import fs from 'fs'
import Chalk from 'chalk'
import cliOptions from '../cli-options'
import { deepPatternPrefix } from '../constants'
import programError from './programError'
import getPackageFileName from './getPackageFileName'
import { print } from '../logging'
Expand Down Expand Up @@ -92,9 +91,7 @@ function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } = {}): O
else if (options.packageFile && options.deep) {
programError(
options,
chalk.red(
`Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '${deepPatternPrefix}package.json'`,
),
chalk.red(`Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '**/package.json'`),
)
}

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

return {
...options,
...(options.deep ? { packageFile: `${deepPatternPrefix}${getPackageFileName(options)}` } : null),
...(options.deep ? { packageFile: `**/${getPackageFileName(options)}` } : null),
...((options.args || []).length > 0 ? { filter: options.args!.join(' ') } : null),
...(format.length > 0 ? { format } : null),
// add shortcut for any keys that start with 'json'
Expand Down
3 changes: 2 additions & 1 deletion src/lib/queryVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import chalk from 'chalk'
import pMap from 'p-map'
import ProgressBar from 'progress'
import { parseRange } from 'semver-utils'
import { supportedVersionTargets } from '../constants'
import getPackageManager from './getPackageManager'
import packageManagers from '../package-managers'
import { createNpmAlias, isGithubUrl, isPre, parseNpmAlias } from '../version-util'
Expand All @@ -14,6 +13,8 @@ import { Version } from '../types/Version'
import { VersionResult } from '../types/VersionResult'
import { VersionSpec } from '../types/VersionSpec'

const supportedVersionTargets = ['latest', 'newest', 'greatest', 'minor', 'patch']

/**
* Get the latest or greatest versions from the NPM repository based on the version target.
*
Expand Down
9 changes: 7 additions & 2 deletions test/doctor.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'fs'
import path from 'path'
import chai from 'chai'
import chalk from 'chalk'
import chaiAsPromised from 'chai-as-promised'
import spawn from 'spawn-please'
import rimraf from 'rimraf'
import stripAnsi from 'strip-ansi'
import { doctorHelpText } from '../src/constants'
import { cliOptionsMap } from '../src/cli-options'

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

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

0 comments on commit 19e27cb

Please sign in to comment.