Skip to content

Commit

Permalink
Error on missing --configFileName (#1377).
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Mar 13, 2024
1 parent c0b47d6 commit 2914dbe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 18 additions & 16 deletions src/lib/getNcuRc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,37 @@ 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',
defaultExtension: ['.json', '.yml', '.js'],
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"
Expand Down
15 changes: 15 additions & 0 deletions test/rc-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 2914dbe

Please sign in to comment.