Skip to content

Commit

Permalink
working on bcoe#436 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcknasty committed Jan 23, 2024
1 parent 22a9c08 commit 4e0eef7
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,37 @@ function buildYargs (withCommands = false) {
return yargs
}

// Loads any kind of configuration file that has
// a .js, .cjs, .yml, .yaml extension
// will also attempt to load anything with
// an rc at the end of the filename. The rc
// files will always be loaded as json
function loadConfigFile (path) {
let config = {}
const jsExts = ['.js', '.cjs']
const ymlExts = ['.yml', '.yaml']
const fileName = basename(path)

const ext = extname(path).toLowerCase()
if (jsExts.includes(ext)) {
config = require(path)
} else if (ymlExts.includes(ext)) {
config = require('js-yaml').load(readFileSync(path, 'utf8'))
} else if (ext === '.json' || fileName.slice(-2) === 'rc') {
config = JSON.parse(readFileSync(path, 'utf8'))
}

// Should the process die if none of these filename extensions are found?
if (Object.keys(config).length === 0) {
throw new Error(`Unsupported file type ${ext} while reading file ${path}`)
const errorMessage = `Unsupported file type ${ext} while reading file ${path}`

// Need to write unit tests for these.
try {
if (jsExts.includes(ext)) {
config = require(path)
} else if (ymlExts.includes(ext)) {
config = require('js-yaml').load(readFileSync(path, 'utf8'))
} else if (ext === '.json' || fileName.slice(-2) === 'rc') {
config = JSON.parse(readFileSync(path, 'utf8'))
} else {
// Should the process die if none of these filename extensions are found?
// I think yes, because we need to know how to parse the file passed.
throw new Error(errorMessage)
}
} catch (e) {
console.error(String(e))
process.exit()
}

return config
Expand Down

0 comments on commit 4e0eef7

Please sign in to comment.