Skip to content

Commit

Permalink
programError: Default to chalk.red.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Apr 5, 2023
1 parent 347fbb6 commit 61b91e6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export async function run(
const error = `Exceeded global timeout of ${timeoutMs}ms`
reject(error)
try {
programError(options, chalk.red(error))
programError(options, error)
} catch (e) {
/* noop */
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/findPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ async function findPackage(options: Options): Promise<{
)} or ${chalk.cyan('--packageData')} options, or pipe a ${pkgFileName} to stdin and specify ${chalk.cyan(
'--stdin',
)}.`,
{ color: false },
)
}

return fs.readFile(pkgFile!, 'utf-8').catch(e => {
programError(options, chalk.red(e))
programError(options, e)
})
}

Expand Down
9 changes: 3 additions & 6 deletions src/lib/getAllPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import yaml from 'yaml'
import { Options } from '../types/Options'
import { PackageFile } from '../types/PackageFile'
import { PackageInfo } from '../types/PackageInfo'
import chalk from './chalk'
import findPackage from './findPackage'
import loadPackageInfoFromFile from './loadPackageInfoFromFile'
import programError from './programError'
Expand Down Expand Up @@ -46,11 +45,9 @@ async function getWorkspacePackageInfos(
if (!workspaces) {
programError(
options,
chalk.red(
`workspaces property missing from package.json. --workspace${
options.workspaces ? 's' : ''
} only works when you specify a "workspaces" property in your package.json.`,
),
`workspaces property missing from package.json. --workspace${
options.workspaces ? 's' : ''
} only works when you specify a "workspaces" property in your package.json.`,
)
}

Expand Down
28 changes: 10 additions & 18 deletions src/lib/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,13 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =
// make sure the option value is valid
// if an array of values is given, make sure each one is a valid choice
if (values.every(value => !choices.includes(value))) {
programError(
options,
chalk.red(`Invalid option value: --${long} ${value}. Valid values are: ${choices.join(', ')}.`),
)
programError(options, `Invalid option value: --${long} ${value}. Valid values are: ${choices.join(', ')}.`)
}
})

// validate options.cwd
if (options.cwd && !(await exists(options.cwd))) {
programError(options, `no such directory: ${options.cwd}`)
programError(options, `No such directory: ${options.cwd}`)
}

// trim filter args
Expand All @@ -118,48 +115,43 @@ async function initOptions(runOptions: RunOptions, { cli }: { cli?: boolean } =
if (options.filter && args && !isEqual(args.join(' '), Array.isArray(filter) ? filter.join(' ') : filter)) {
programError(
options,
chalk.red('Cannot specify a filter using both --filter and args. Did you forget to quote an argument?') +
'\nSee: https://github.com/raineorshine/npm-check-updates/issues/759#issuecomment-723587297',
'Cannot specify a filter using both --filter and args. Did you forget to quote an argument?\nSee: https://github.com/raineorshine/npm-check-updates/issues/759#issuecomment-723587297',
)
}
// disallow packageFile and --deep
else if (options.packageFile && options.deep) {
programError(
options,
chalk.red(`Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '**/package.json'`),
`Cannot specify both --packageFile and --deep. --deep is an alias for --packageFile '**/package.json'`,
)
}

// disallow --workspace and --workspaces
else if (options.workspace?.length && options.workspaces) {
programError(options, chalk.red('Cannot specify both --workspace and --workspaces.'))
programError(options, 'Cannot specify both --workspace and --workspaces.')
}

// disallow --workspace(s) and --deep
else if (options.deep && (options.workspace?.length || options.workspaces)) {
programError(options, chalk.red(`Cannot specify both --deep and --workspace${options.workspaces ? 's' : ''}.`))
programError(options, `Cannot specify both --deep and --workspace${options.workspaces ? 's' : ''}.`)
}

// disallow --workspace(s) and --doctor
else if (options.doctor && (options.workspace?.length || options.workspaces)) {
programError(
options,
chalk.red(`Doctor mode is not currently supported with --workspace${options.workspaces ? 's' : ''}.`),
)
programError(options, `Doctor mode is not currently supported with --workspace${options.workspaces ? 's' : ''}.`)
}

// disallow incorrect or missing registry path when selecting staticRegistry as packageManager
if (options.packageManager === 'staticRegistry') {
if (options.registry === undefined || options.registry === null) {
programError(
options,
chalk.red(
'When --package-manager staticRegistry is specified, you must provide the path for the registry file with --registry. Run "ncu --help --packageManager" for details.',
),

'When --package-manager staticRegistry is specified, you must provide the path for the registry file with --registry. Run "ncu --help --packageManager" for details.',
)
}
if (!(await exists(options.registry!))) {
programError(options, chalk.red(`The specified static registry file does not exist: ${options.registry}`))
programError(options, `The specified static registry file does not exist: ${options.registry}`)
}
}
const target: Target = options.target || 'latest'
Expand Down
15 changes: 13 additions & 2 deletions src/lib/programError.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { print } from '../lib/logging'
import { Options } from '../types/Options'
import chalk from './chalk'

/** Print an error. Exit the process if in CLI mode. */
function programError(options: Options, message: string): never {
function programError(
options: Options,
message: string,
{
color = true,
}: {
// defaults to true, which uses chalk.red on the whole error message.
// set to false to provide your own coloring.
color?: boolean
} = {},
): never {
if (options.cli) {
print(options, message, null, 'error')
print(options, color ? chalk.red(message) : message, null, 'error')
process.exit(1)
} else {
throw new Error(message)
Expand Down
1 change: 1 addition & 0 deletions src/lib/queryVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async function queryVersions(packageMap: Index<VersionSpec>, options: Options =
`\nSupported version targets are: ` +
packageManagerSupportedVersionTargets.join(', ') +
(!isGithubDependency ? ', and tags (e.g. @next)' : ''),
{ color: false },
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/runLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async function runLocal(
} catch (e: any) {
programError(
options,
await chalk.red(`Invalid package file${pkgFile ? `: ${pkgFile}` : ' from stdin'}. Error details:\n${e.message}`),
`Invalid package file${pkgFile ? `: ${pkgFile}` : ' from stdin'}. Error details:\n${e.message}`,
)
}

Expand Down
2 changes: 1 addition & 1 deletion test/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('bin', async function () {

it('throw error if --cwd does not exist', async () => {
return spawn('node', [bin, '--cwd', 'fnuoathufoawhtufonwauto']).should.eventually.be.rejectedWith(
'no such directory: fnuoathufoawhtufonwauto',
'No such directory: fnuoathufoawhtufonwauto',
)
})

Expand Down

0 comments on commit 61b91e6

Please sign in to comment.