wikibase-cli 18.2.0 → 18.2.1
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.
|
@@ -14,7 +14,7 @@ program.acceptsArgsOnStdin = true
|
|
|
14
14
|
await program
|
|
15
15
|
.option('-p, --props <props>', 'request only certain properties (info, sitelinks, aliases, labels, descriptions, claims, datatype)')
|
|
16
16
|
.option('-r, --revision <id>', 'request a specific revision')
|
|
17
|
-
.option('-f, --format <format>', 'Default: js when fetching a single entity, json otherwise. Note that the -j, --json option is equivalent to "--format json"')
|
|
17
|
+
.option('-f, --format <format>', 'Options: js, json, mjs. Default: js when fetching a single entity, json otherwise. Note that the -j, --json option is equivalent to "--format json"')
|
|
18
18
|
.option('-m, --create-mode', 'optimize for creating an entity from a previously existing one, namely dropping ids from the existing entity used as template')
|
|
19
19
|
.option('-z, --no-minimize', 'disable claims minimization, making the output format more predictable; i.e. single claims will still be in arrays')
|
|
20
20
|
.process('generate-template')
|
|
@@ -49,7 +49,7 @@ const handleIds = async ids => {
|
|
|
49
49
|
|
|
50
50
|
const batchMode = ids.length > 1
|
|
51
51
|
|
|
52
|
-
if (batchMode && format
|
|
52
|
+
if (batchMode && format.endsWith('js')) {
|
|
53
53
|
throw new Error("js format can't be used when several entities are requested")
|
|
54
54
|
}
|
|
55
55
|
|
package/lib/object_arg_parser.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
1
2
|
import { red } from '#lib/chalk'
|
|
2
3
|
import { readJsonFile } from '#lib/json'
|
|
3
|
-
import {
|
|
4
|
+
import { isJsonString, getAbsoluteFileUrl, validateTemplateCommand } from '#lib/utils'
|
|
4
5
|
import { parseGuid } from './parse_command_utils.js'
|
|
5
6
|
import program from './program.js'
|
|
6
7
|
import validateFunctionArgs from './validate_function_args.js'
|
|
@@ -20,22 +21,22 @@ const getData = args => {
|
|
|
20
21
|
return JSON.parse(arg)
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
const
|
|
24
|
+
const fileUrl = getAbsoluteFileUrl(arg)
|
|
24
25
|
|
|
25
|
-
if (!
|
|
26
|
+
if (!existsSync(fileUrl)) {
|
|
26
27
|
console.error(red('the argument should be a valid JSON or a JSON file path or a JS function file path'))
|
|
27
28
|
console.error("- it doesn't look like inline JSON")
|
|
28
|
-
console.error(`couldn't parse arguments: ${
|
|
29
|
+
console.error(`couldn't parse arguments: ${fileUrl} is not the path to an existing file`)
|
|
29
30
|
process.exit(1)
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
try {
|
|
33
34
|
// Try to parse it as a JSON file
|
|
34
|
-
return readJsonFile(
|
|
35
|
+
return readJsonFile(fileUrl)
|
|
35
36
|
} catch (err1) {
|
|
36
37
|
// Try to parse it as a JS module
|
|
37
38
|
try {
|
|
38
|
-
return getDataFromJsModule(
|
|
39
|
+
return getDataFromJsModule(fileUrl, args)
|
|
39
40
|
} catch (err2) {
|
|
40
41
|
if (err2 === 'SyntaxError') {
|
|
41
42
|
console.error(red('the argument should be a valid JSON or a JSON file path or a JS function file path'))
|
|
@@ -50,8 +51,8 @@ const getData = args => {
|
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
async function getDataFromJsModule (
|
|
54
|
-
const { default: jsModule } = await import(
|
|
54
|
+
async function getDataFromJsModule (fileUrl, args) {
|
|
55
|
+
const { default: jsModule } = await import(fileUrl)
|
|
55
56
|
if (typeof jsModule === 'function') {
|
|
56
57
|
const inputArgs = args.slice(1)
|
|
57
58
|
validateFunctionArgs(jsModule, inputArgs, jsModule)
|
package/lib/output_templates.js
CHANGED
|
@@ -10,8 +10,8 @@ export function outputTemplatesFactory ({ batchMode, format, propsToPick, reques
|
|
|
10
10
|
const formatEntity = FormatEntity(batchMode, propsToPick, requestedPropsAndSubProps, minimize)
|
|
11
11
|
return async function outputTemplates (entities) {
|
|
12
12
|
entities = entities.map(formatEntity)
|
|
13
|
-
if (format
|
|
14
|
-
const jsFile = await stringifyAsJsFunction(entities[0], program.lang)
|
|
13
|
+
if (format.endsWith('js')) {
|
|
14
|
+
const jsFile = await stringifyAsJsFunction(entities[0], program.lang, format)
|
|
15
15
|
console.log(jsFile)
|
|
16
16
|
} else {
|
|
17
17
|
const newLines = entities.map(entity => JSON.stringify(entity)).join('\n')
|
|
@@ -70,11 +70,12 @@ const getPatternComment = (line, pattern, entitiesLabels) => {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
export default async (entity, lang) => {
|
|
73
|
+
export default async (entity, lang, format) => {
|
|
74
74
|
const json = JSON.stringify(entity, null, 2)
|
|
75
75
|
// Use the classic syntax function to avoid the implicit return syntax
|
|
76
76
|
// which might be confusion to users unfamiliar with JS
|
|
77
|
-
const
|
|
77
|
+
const moduleExport = format === 'mjs' ? 'export default function' : 'module.exports = function'
|
|
78
|
+
const file = `${moduleExport} () {
|
|
78
79
|
return ${formatJsObj(json)}
|
|
79
80
|
}`
|
|
80
81
|
const entitiesIds = collectEntitiesIds(json)
|
package/lib/utils.js
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs'
|
|
2
2
|
import path from 'node:path'
|
|
3
|
+
import { pathToFileURL } from 'node:url'
|
|
3
4
|
import errors_ from './errors.js'
|
|
4
5
|
|
|
5
6
|
export const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
|
|
6
7
|
|
|
7
8
|
export const sum = (a, b) => a + b
|
|
8
9
|
|
|
9
|
-
export
|
|
10
|
+
export function average (values) {
|
|
10
11
|
if (values.length > 0) return values.reduce(sum, 0) / values.length
|
|
11
12
|
else return 0
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
export const
|
|
15
|
+
export const getAbsoluteFileUrl = filepath => pathToFileURL(path.resolve(process.cwd(), filepath))
|
|
15
16
|
|
|
16
|
-
export
|
|
17
|
-
const
|
|
18
|
-
return existsSync(
|
|
17
|
+
export function isFilePathSync (arg) {
|
|
18
|
+
const possibleFileUrl = getAbsoluteFileUrl(arg)
|
|
19
|
+
return existsSync(possibleFileUrl)
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
export
|
|
22
|
+
export function isJsonString (str) {
|
|
22
23
|
if (typeof str !== 'string') return false
|
|
23
24
|
else return (str.trim()[0] === '{' || str.trim()[0] === '[')
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
export
|
|
27
|
+
export function validateTemplateCommand ({ commandName, validCommands }) {
|
|
27
28
|
if (!validCommands) return
|
|
28
29
|
if (validCommands && !validCommands.includes(commandName)) {
|
|
29
30
|
throw errors_.exitMessage('wrong command for this template', { commandName, validCommands })
|