diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 1ec150f1..f63e3c6f 100755 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -157,7 +157,13 @@ ${chalk.dim.underline( // Do not load when tests are running (can be overridden if configFilePath is set explicitly, or --mergeConfig option specified) const rcResult = !process.env.NCU_TESTS || configFilePath || mergeConfig - ? await getNcuRc({ configFileName, configFilePath, global, packageFile, color }) + ? await getNcuRc({ + configFileName, + configFilePath, + global, + packageFile, + options: { ...programOpts, cli: true }, + }) : null // override rc args with program args diff --git a/src/index.ts b/src/index.ts index f31bdfc6..ce45a0fe 100755 --- a/src/index.ts +++ b/src/index.ts @@ -200,7 +200,7 @@ async function runUpgrades(options: Options, timeout?: NodeJS.Timeout): Promise< async (previousPromise, packageInfo: PackageInfo) => { const packages = await previousPromise // copy object to prevent share .ncurc options between different packageFile, to prevent unpredictable behavior - const rcResult = await getNcuRc({ packageFile: packageInfo.filepath, color: options.color }) + const rcResult = await getNcuRc({ packageFile: packageInfo.filepath, options }) let rcConfig = rcResult && rcResult.config ? rcResult.config : {} if (options.mergeConfig && Object.keys(rcConfig).length) { // Merge config options. diff --git a/src/lib/getNcuRc.ts b/src/lib/getNcuRc.ts index 38dd0535..205be93b 100644 --- a/src/lib/getNcuRc.ts +++ b/src/lib/getNcuRc.ts @@ -5,28 +5,26 @@ import os from 'os' import path from 'path' import { rcFile } from 'rc-config-loader' import { cliOptionsMap } from '../cli-options' +import { Options } from '../types/Options' +import programError from './programError' -interface Options { - color?: boolean +/** Loads the .ncurc config file. */ +async function getNcuRc({ + configFileName, + configFilePath, + packageFile, + global, + options, +}: { configFileName?: string configFilePath?: string - // if true, does not look in package directory + /** If true, does not look in package directory. */ global?: boolean packageFile?: string -} - -/** - * Loads the .ncurc config file. - * - * @param [cfg] - * @param [cfg.configFileName=.ncurc] - * @param [cfg.configFilePath] - * @param [cfg.packageFile] - * @returns - */ -async function getNcuRc({ color, configFileName, configFilePath, packageFile, global }: Options = {}) { + options: Options +}) { const { default: chalkDefault, Chalk } = await import('chalk') - const chalk = color ? new Chalk({ level: 1 }) : chalkDefault + const chalk = options?.color ? new Chalk({ level: 1 }) : chalkDefault const rawResult = rcFile('ncurc', { configFileName: configFileName || '.ncurc', @@ -34,6 +32,10 @@ async function getNcuRc({ color, configFileName, configFilePath, packageFile, gl cwd: configFilePath || (global ? os.homedir() : packageFile ? path.dirname(packageFile) : undefined), }) + if (configFileName && !rawResult?.filePath) { + programError(options, `Config file ${configFileName} not found in ${configFilePath || process.cwd()}`) + } + const result = { filePath: rawResult?.filePath, // Prevent the cli tool from choking because of an unknown option "$schema" diff --git a/test/rc-config.test.ts b/test/rc-config.test.ts index 46a39204..50ae1fcd 100644 --- a/test/rc-config.test.ts +++ b/test/rc-config.test.ts @@ -61,6 +61,21 @@ describe('rc-config', () => { } }) + it('error on missing --configFileName', async () => { + const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-')) + const configFileName = '.ncurc_missing.json' + try { + const result = spawn( + 'node', + [bin, '--stdin', '--configFilePath', tempDir, '--configFileName', configFileName], + JSON.stringify({ dependencies: { 'ncu-test-v2': '1', 'ncu-test-tag': '0.1.0' } }), + ) + await result.should.eventually.be.rejectedWith(`Config file ${configFileName} not found in ${tempDir}`) + } finally { + await fs.rm(tempDir, { recursive: true, force: true }) + } + }) + it('read --configFilePath', async () => { const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'npm-check-updates-')) const tempConfigFile = path.join(tempDir, '.ncurc.json')