wikibase-cli 18.2.2 → 18.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ import { access, writeFile } from 'fs/promises'
2
+ import read from 'read'
3
+
4
+ export async function checkConfigFileExistance (configFilePath) {
5
+ try {
6
+ await access(configFilePath)
7
+ } catch (err) {
8
+ if (err.code === 'ENOENT') {
9
+ await offerToCreateMissingConfigFile(configFilePath)
10
+ } else {
11
+ throw err
12
+ }
13
+ }
14
+ }
15
+
16
+ async function offerToCreateMissingConfigFile (configFilePath) {
17
+ try {
18
+ const res = await read({ prompt: `Config file not found at ${configFilePath}. Create? y/N` })
19
+ if (res.trim() === 'y') {
20
+ await writeFile(configFilePath, '{}', { mode: 0o600 })
21
+ } else {
22
+ process.exit(1)
23
+ }
24
+ } catch (err) {
25
+ if (err.message === 'canceled') process.exit(1)
26
+ else throw err
27
+ }
28
+ }
@@ -1,20 +1,29 @@
1
- import { accessSync, writeFileSync } from 'node:fs'
1
+ import { join, resolve } from '#lib/platform_agnostic_path'
2
2
  import configFolderFactory from '../get_folder_path.js'
3
- import path from '../path.js'
4
-
5
- const configFolder = configFolderFactory('config')
3
+ import { checkConfigFileExistance } from './check_config_file_existance.js'
6
4
 
7
5
  export let configFilePath
8
6
 
9
- if (configFolder != null) {
10
- configFilePath = path.join(configFolder, 'config.json')
7
+ const configFolder = configFolderFactory('config')
11
8
 
12
- try {
13
- accessSync(configFilePath)
14
- } catch (err) {
15
- // Initialize if it doesn't exist
16
- writeFileSync(configFilePath, '{}', { mode: 0o600 })
9
+ // Arguments parsing needs to be done manually as relying on lib/program
10
+ // would be a circular dependecy
11
+ const args = process.argv.slice(2)
12
+ const configArg = args.find(arg => arg.startsWith('--config'))
13
+ if (configArg) {
14
+ let configFilePathFromArgs
15
+ if (configArg.split('=')[1]) {
16
+ configFilePathFromArgs = configArg.split('=')[1]
17
+ } else {
18
+ configFilePathFromArgs = args[args.indexOf(configArg) + 1]
17
19
  }
20
+ configFilePath = resolve(process.cwd(), configFilePathFromArgs)
21
+ } else if (process.env.WB_CONFIG) {
22
+ configFilePath = process.env.WB_CONFIG
23
+ } else if (configFolder != null) {
24
+ configFilePath = join(configFolder, 'config.json')
18
25
  } else {
19
26
  configFilePath = null
20
27
  }
28
+
29
+ if (configFilePath) await checkConfigFileExistance(configFilePath)
@@ -1,4 +1,8 @@
1
- export default {
1
+ export const globalOptions = {
2
+ config: [ '--config <path>', 'Path to the config.json file' ],
3
+ }
4
+
5
+ export const commandSpecificOptions = {
2
6
  lang: [ '-l, --lang <lang>', 'specify the language to use' ],
3
7
  verbose: [ '-v, --verbose', 'make the output more verbose' ],
4
8
  dry: [ '-d, --dry', 'output the generated data, do not run the query' ],
@@ -0,0 +1,6 @@
1
+ import path from 'node:path'
2
+
3
+ const platformAgnosticPath = process.platform === 'win32' ? path.win32 : path
4
+
5
+ export const resolve = platformAgnosticPath.resolve
6
+ export const join = platformAgnosticPath.join
package/lib/program.js CHANGED
@@ -3,7 +3,7 @@
3
3
  import program from 'commander'
4
4
  import { debug } from '#lib/debug'
5
5
  import { applyEnvAndConfigDefault } from './apply_env_and_config_default.js'
6
- import globalOptionsHelp from './global_options_help.js'
6
+ import { globalOptions, commandSpecificOptions } from './global_options_help.js'
7
7
  import logCommandExamples from './log_command_examples.js'
8
8
 
9
9
  program.process = async command => {
@@ -14,9 +14,12 @@ program.process = async command => {
14
14
  ;({ options } = metadata)
15
15
  program.arguments(args)
16
16
  program.description(description)
17
- Object.keys(globalOptionsHelp).forEach(key => {
18
- if (options[key]) program.option(...globalOptionsHelp[key])
19
- })
17
+ for (const [ name, params ] of Object.entries(commandSpecificOptions)) {
18
+ if (options[name]) program.option(...params)
19
+ }
20
+ for (const params of Object.values(globalOptions)) {
21
+ program.option(...params)
22
+ }
20
23
  program.on('--help', () => logCommandExamples(command, examples, doc))
21
24
  }
22
25
  const isCommandsWithCustomHelpMenu = program.customHelpOption != null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikibase-cli",
3
- "version": "18.2.2",
3
+ "version": "18.3.0",
4
4
  "description": "A command-line interface to Wikibase",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/lib/path.js DELETED
@@ -1,3 +0,0 @@
1
- import path from 'node:path'
2
-
3
- export default process.platform === 'win32' ? path.win32 : path