Skip to content

Commit

Permalink
allow to set the config path with --config
Browse files Browse the repository at this point in the history
Addressing #132
  • Loading branch information
maxlath committed Nov 20, 2024
1 parent 7a46813 commit 67ba8c9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
28 changes: 28 additions & 0 deletions lib/config/check_config_file_existance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { access, writeFile } from 'fs/promises'
import read from 'read'

export async function checkConfigFileExistance (configFilePath) {
try {
await access(configFilePath)
} catch (err) {
if (err.code === 'ENOENT') {
await offerToCreateMissingConfigFile(configFilePath)
} else {
throw err
}
}
}

async function offerToCreateMissingConfigFile (configFilePath) {
try {
const res = await read({ prompt: `Config file not found at ${configFilePath}. Create? y/N` })
if (res.trim() === 'y') {
await writeFile(configFilePath, '{}', { mode: 0o600 })
} else {
process.exit(1)
}
} catch (err) {
if (err.message === 'canceled') process.exit(1)
else throw err
}
}
29 changes: 18 additions & 11 deletions lib/config/file_path.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { accessSync, writeFileSync } from 'node:fs'
import { join, resolve } from '#lib/platform_agnostic_path'
import configFolderFactory from '../get_folder_path.js'
import path from '../path.js'

const configFolder = configFolderFactory('config')
import { checkConfigFileExistance } from './check_config_file_existance.js'

export let configFilePath

if (configFolder != null) {
configFilePath = path.join(configFolder, 'config.json')
const configFolder = configFolderFactory('config')

try {
accessSync(configFilePath)
} catch (err) {
// Initialize if it doesn't exist
writeFileSync(configFilePath, '{}', { mode: 0o600 })
// Arguments parsing needs to be done manually as relying on lib/program
// would be a circular dependecy
const args = process.argv.slice(2)
const configArg = args.find(arg => arg.startsWith('--config'))
if (configArg) {
let configFilePathFromArgs
if (configArg.split('=')[1]) {
configFilePathFromArgs = configArg.split('=')[1]
} else {
configFilePathFromArgs = args[args.indexOf(configArg) + 1]
}
configFilePath = resolve(process.cwd(), configFilePathFromArgs)
} else if (configFolder != null) {
configFilePath = join(configFolder, 'config.json')
} else {
configFilePath = null
}

if (configFilePath) await checkConfigFileExistance(configFilePath)
6 changes: 5 additions & 1 deletion lib/global_options_help.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default {
export const globalOptions = {
config: [ '--config <path>', 'Path to the config.json file' ],
}

export const commandSpecificOptions = {
lang: [ '-l, --lang <lang>', 'specify the language to use' ],
verbose: [ '-v, --verbose', 'make the output more verbose' ],
dry: [ '-d, --dry', 'output the generated data, do not run the query' ],
Expand Down
3 changes: 0 additions & 3 deletions lib/path.js

This file was deleted.

6 changes: 6 additions & 0 deletions lib/platform_agnostic_path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import path from 'node:path'

const platformAgnosticPath = process.platform === 'win32' ? path.win32 : path

export const resolve = platformAgnosticPath.resolve
export const join = platformAgnosticPath.join
11 changes: 7 additions & 4 deletions lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import program from 'commander'
import { debug } from '#lib/debug'
import { applyEnvAndConfigDefault } from './apply_env_and_config_default.js'
import globalOptionsHelp from './global_options_help.js'
import { globalOptions, commandSpecificOptions } from './global_options_help.js'
import logCommandExamples from './log_command_examples.js'

program.process = async command => {
Expand All @@ -14,9 +14,12 @@ program.process = async command => {
;({ options } = metadata)
program.arguments(args)
program.description(description)
Object.keys(globalOptionsHelp).forEach(key => {
if (options[key]) program.option(...globalOptionsHelp[key])
})
for (const [ name, params ] of Object.entries(commandSpecificOptions)) {
if (options[name]) program.option(...params)
}
for (const params of Object.values(globalOptions)) {
program.option(...params)
}
program.on('--help', () => logCommandExamples(command, examples, doc))
}
const isCommandsWithCustomHelpMenu = program.customHelpOption != null
Expand Down

0 comments on commit 67ba8c9

Please sign in to comment.