wikibase-cli 16.3.2 → 17.0.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.
Files changed (218) hide show
  1. package/bin/wb-add-alias.js +12 -0
  2. package/bin/wb-add-badge.js +12 -0
  3. package/bin/wb-add-claim.js +14 -0
  4. package/bin/wb-add-qualifier.js +14 -0
  5. package/bin/wb-add-reference.js +14 -0
  6. package/bin/wb-aliases.js +4 -0
  7. package/bin/wb-badges.js +18 -0
  8. package/bin/{wb-claims → wb-claims.js} +19 -14
  9. package/bin/{wb-config → wb-config.js} +8 -6
  10. package/bin/{wb-convert → wb-convert.js} +11 -12
  11. package/bin/wb-create-entity.js +10 -0
  12. package/bin/{wb-data → wb-data.js} +15 -11
  13. package/bin/wb-delete-entity.js +10 -0
  14. package/bin/wb-description.js +4 -0
  15. package/bin/wb-edit-entity.js +10 -0
  16. package/bin/{wb-generate-template → wb-generate-template.js} +24 -13
  17. package/bin/{wb-hub → wb-hub.js} +10 -8
  18. package/bin/{wb-id → wb-id.js} +14 -11
  19. package/bin/wb-label.js +4 -0
  20. package/bin/{wb-lang → wb-lang.js} +8 -8
  21. package/bin/wb-lemma.js +4 -0
  22. package/bin/{wb-merge-entity → wb-merge-entity.js} +6 -4
  23. package/bin/wb-move-claim.js +20 -0
  24. package/bin/wb-move-qualifier.js +21 -0
  25. package/bin/{wb-open → wb-open.js} +23 -22
  26. package/bin/{wb-props → wb-props.js} +8 -9
  27. package/bin/{wb-query → wb-query.js} +13 -9
  28. package/bin/wb-remove-alias.js +12 -0
  29. package/bin/wb-remove-badge.js +12 -0
  30. package/bin/wb-remove-claim.js +11 -0
  31. package/bin/wb-remove-description.js +16 -0
  32. package/bin/wb-remove-label.js +16 -0
  33. package/bin/wb-remove-qualifier.js +15 -0
  34. package/bin/wb-remove-reference.js +15 -0
  35. package/bin/wb-remove-sitelink.js +16 -0
  36. package/bin/{wb-revisions → wb-revisions.js} +15 -11
  37. package/bin/{wb-search → wb-search.js} +17 -15
  38. package/bin/wb-set-alias.js +12 -0
  39. package/bin/wb-set-description.js +12 -0
  40. package/bin/wb-set-label.js +12 -0
  41. package/bin/wb-set-sitelink.js +17 -0
  42. package/bin/{wb-sparql → wb-sparql.js} +16 -12
  43. package/bin/{wb-summary → wb-summary.js} +3 -3
  44. package/bin/{wb-update-claim → wb-update-claim.js} +9 -6
  45. package/bin/wb-update-qualifier.js +14 -0
  46. package/bin/wb.js +27 -0
  47. package/bin/{wd → wd.js} +2 -3
  48. package/index.js +7 -1
  49. package/lib/apply_env_and_config_default.js +7 -5
  50. package/lib/chalk.js +12 -0
  51. package/lib/command_with_template_custom_help.js +7 -7
  52. package/lib/common_command_args_parser.js +7 -10
  53. package/lib/common_errors.js +5 -7
  54. package/lib/common_options.js +23 -24
  55. package/lib/config/config.js +10 -5
  56. package/lib/config/credentials_config.js +7 -6
  57. package/lib/config/file_operations.js +9 -8
  58. package/lib/config/file_path.js +11 -8
  59. package/lib/config/parameters.js +11 -10
  60. package/lib/convert_ids.js +10 -8
  61. package/lib/copy.js +2 -2
  62. package/lib/drop_non_selected_subprops.js +3 -3
  63. package/lib/edit/assert_credentials.js +3 -3
  64. package/lib/edit/edit_command.js +19 -19
  65. package/lib/edit/init_credentials.js +11 -11
  66. package/lib/edit/parse_batch_line.js +2 -2
  67. package/lib/edit/parse_object_value.js +6 -5
  68. package/lib/edit/prompt.js +7 -5
  69. package/lib/edit/validate_instance_credentials.js +2 -2
  70. package/lib/entity_attribute_command.js +10 -10
  71. package/lib/entity_attribute_parser.js +8 -8
  72. package/lib/entity_data_parser.js +14 -11
  73. package/lib/errors.js +3 -3
  74. package/lib/execute_function.js +4 -4
  75. package/lib/exit_on_missing.js +11 -12
  76. package/lib/fetch_and_log_entity_revision.js +18 -15
  77. package/lib/fetch_and_log_guid_data.js +19 -11
  78. package/lib/fetch_and_log_ids_data.js +17 -16
  79. package/lib/fetch_and_log_property_claims_data.js +18 -10
  80. package/lib/fetch_lang_props.js +13 -12
  81. package/lib/filter_claims_properties.js +4 -4
  82. package/lib/format_properties_data.js +2 -2
  83. package/lib/format_statement_element_value.js +3 -3
  84. package/lib/fs.js +27 -25
  85. package/lib/generate_describe_sparql_query.js +3 -3
  86. package/lib/generate_sample_sparql_query.js +1 -1
  87. package/lib/generate_select_sparql_query.js +9 -6
  88. package/lib/get_cache_folder_path.js +5 -5
  89. package/lib/get_claims_texts.js +8 -7
  90. package/lib/get_entities.js +6 -5
  91. package/lib/get_entities_by_batches.js +12 -7
  92. package/lib/get_entities_labels.js +3 -5
  93. package/lib/get_entities_schemas.js +7 -7
  94. package/lib/get_folder_path.js +8 -6
  95. package/lib/get_lang_props.js +17 -16
  96. package/lib/get_pattern_filter.js +18 -9
  97. package/lib/get_properties_data.js +5 -5
  98. package/lib/get_redirected_sitelink_title.js +4 -4
  99. package/lib/get_sitelink_url.js +9 -8
  100. package/lib/get_stdin_input.js +1 -1
  101. package/lib/get_ttl_entities.js +8 -8
  102. package/lib/global_options_help.js +1 -1
  103. package/lib/hash_string.js +4 -2
  104. package/lib/json.js +5 -0
  105. package/lib/local_lang.js +2 -1
  106. package/lib/log_claims.js +12 -12
  107. package/lib/log_command_examples.js +3 -3
  108. package/lib/log_ndjson.js +1 -1
  109. package/lib/make_sparql_query.js +29 -25
  110. package/lib/matching_langs.js +4 -6
  111. package/lib/minimize_claims.js +3 -5
  112. package/lib/object_arg_parser.js +12 -17
  113. package/lib/open.js +7 -7
  114. package/lib/output.js +37 -35
  115. package/lib/output_templates.js +41 -31
  116. package/lib/parse_command_utils.js +14 -15
  117. package/lib/parse_entity_type.js +2 -2
  118. package/lib/parse_props.js +7 -4
  119. package/lib/parse_simplify_options.js +1 -1
  120. package/lib/path.js +3 -2
  121. package/lib/pluralize.js +2 -2
  122. package/lib/polymorphic_command_args_parser.js +11 -9
  123. package/lib/program.js +9 -9
  124. package/lib/properties.js +22 -23
  125. package/lib/queries/all_instances.js +2 -2
  126. package/lib/queries/all_properties.js +1 -1
  127. package/lib/read_ids_from_stdin.js +3 -3
  128. package/lib/request.js +22 -23
  129. package/lib/request_headers.js +8 -2
  130. package/lib/reset_properties.js +4 -4
  131. package/lib/sparql_query_command.js +10 -8
  132. package/lib/stringify_as_js_function.js +3 -3
  133. package/lib/summary_parser.js +43 -13
  134. package/lib/tabularize.js +7 -7
  135. package/lib/tolerant_id_parser.js +21 -18
  136. package/lib/types.js +4 -6
  137. package/lib/utils.js +11 -13
  138. package/lib/validate.js +2 -0
  139. package/lib/validate_function_args.js +2 -2
  140. package/lib/value_parsers.js +2 -2
  141. package/lib/wbk.js +4 -3
  142. package/lib/wellknown_queries.js +6 -5
  143. package/metadata/add-alias.js +6 -4
  144. package/metadata/add-badge.js +5 -3
  145. package/metadata/add-claim.js +5 -5
  146. package/metadata/add-qualifier.js +6 -4
  147. package/metadata/add-reference.js +6 -4
  148. package/metadata/aliases.js +6 -4
  149. package/metadata/badges.js +3 -3
  150. package/metadata/claims.js +4 -4
  151. package/metadata/config.js +5 -5
  152. package/metadata/convert.js +4 -4
  153. package/metadata/create-entity.js +8 -8
  154. package/metadata/data.js +4 -4
  155. package/metadata/delete-entity.js +7 -7
  156. package/metadata/description.js +6 -4
  157. package/metadata/edit-entity.js +10 -10
  158. package/metadata/generate-template.js +4 -4
  159. package/metadata/hub.js +3 -3
  160. package/metadata/id.js +4 -4
  161. package/metadata/label.js +5 -3
  162. package/metadata/lang.js +4 -4
  163. package/metadata/lemma.js +6 -4
  164. package/metadata/merge-entity.js +6 -6
  165. package/metadata/move-claim.js +6 -4
  166. package/metadata/move-qualifier.js +6 -4
  167. package/metadata/open.js +4 -4
  168. package/metadata/props.js +4 -4
  169. package/metadata/query.js +3 -3
  170. package/metadata/remove-alias.js +6 -4
  171. package/metadata/remove-badge.js +5 -3
  172. package/metadata/remove-claim.js +6 -4
  173. package/metadata/remove-description.js +5 -3
  174. package/metadata/remove-label.js +5 -3
  175. package/metadata/remove-qualifier.js +6 -4
  176. package/metadata/remove-reference.js +6 -4
  177. package/metadata/remove-sitelink.js +5 -3
  178. package/metadata/revisions.js +4 -4
  179. package/metadata/search.js +3 -3
  180. package/metadata/set-alias.js +6 -4
  181. package/metadata/set-description.js +6 -4
  182. package/metadata/set-label.js +6 -4
  183. package/metadata/set-sitelink.js +5 -3
  184. package/metadata/sparql.js +4 -4
  185. package/metadata/summary.js +5 -5
  186. package/metadata/update-claim.js +5 -5
  187. package/metadata/update-qualifier.js +6 -4
  188. package/package.json +24 -16
  189. package/bin/wb +0 -22
  190. package/bin/wb-add-alias +0 -9
  191. package/bin/wb-add-badge +0 -9
  192. package/bin/wb-add-claim +0 -11
  193. package/bin/wb-add-qualifier +0 -11
  194. package/bin/wb-add-reference +0 -11
  195. package/bin/wb-aliases +0 -2
  196. package/bin/wb-badges +0 -18
  197. package/bin/wb-create-entity +0 -5
  198. package/bin/wb-delete-entity +0 -7
  199. package/bin/wb-description +0 -2
  200. package/bin/wb-edit-entity +0 -5
  201. package/bin/wb-label +0 -2
  202. package/bin/wb-lemma +0 -2
  203. package/bin/wb-move-claim +0 -17
  204. package/bin/wb-move-qualifier +0 -18
  205. package/bin/wb-remove-alias +0 -9
  206. package/bin/wb-remove-badge +0 -9
  207. package/bin/wb-remove-claim +0 -8
  208. package/bin/wb-remove-description +0 -13
  209. package/bin/wb-remove-label +0 -13
  210. package/bin/wb-remove-qualifier +0 -12
  211. package/bin/wb-remove-reference +0 -12
  212. package/bin/wb-remove-sitelink +0 -13
  213. package/bin/wb-set-alias +0 -9
  214. package/bin/wb-set-description +0 -9
  215. package/bin/wb-set-label +0 -9
  216. package/bin/wb-set-sitelink +0 -14
  217. package/bin/wb-update-qualifier +0 -11
  218. package/lib/valide.js +0 -4
package/lib/output.js CHANGED
@@ -1,45 +1,47 @@
1
- const types = require('./types')
2
- const { promisify } = require('util')
1
+ import { promisify } from 'node:util'
2
+ import { grey } from '#lib/chalk'
3
+ import { copyAndExit } from '#lib/copy'
4
+ import { tabularize } from '#lib/tabularize'
5
+ import { isArray, isCollection, isPlainObject } from '#lib/types'
6
+
3
7
  const stdoutWrite = promisify(process.stdout.write).bind(process.stdout)
4
8
 
5
9
  // Calling program just in time, so that code running
6
10
  // after this first level function can still change the option
7
- module.exports = program => async (data, optional, customColors) => {
8
- const isArray = types.isArray(data)
9
- const isPlainObject = types.isPlainObject(data)
10
- const isCollection = types.isCollection(data)
11
-
12
- program.json = program.json || program.format === 'json'
11
+ export function outputFactory (program) {
12
+ return async function (data, optional, customColors) {
13
+ program.json = program.json || program.format === 'json'
13
14
 
14
- // Flatten when data is one array in an array to get a chance
15
- // to have a shell-friendly output
16
- if (isArray && data.length === 1 && types.isArray(data[0])) data = data[0]
15
+ // Flatten when data is one array in an array to get a chance
16
+ // to have a shell-friendly output
17
+ if (isArray(data) && data.length === 1 && isArray(data[0])) data = data[0]
17
18
 
18
- if (optional) {
19
- if (!program.verbose) return
20
- if (!customColors) data = require('chalk').grey(data)
21
- await log(data)
22
- } else {
23
- if (isArray && !isCollection && !program.json) {
24
- if (program.format === 'inline') data = data.join(' ')
25
- else data = data.join('\n')
26
- } else if (isCollection && program.format === 'table') {
27
- data = require('./tabularize')(data)
28
- } else if (program.json || isCollection || isPlainObject) {
29
- // indent deep objects
30
- data = JSON.stringify(data, null, 2)
31
- // Do not copy to clipboard when the output is valid JSON
32
- program.clipboard = false
33
- }
34
- if (data == null || (isArray && data.length === 0)) {
35
- console.error('no result found')
36
- return process.exit(1)
37
- }
38
- if (program.clipboard) {
39
- require('./copy')(data)
40
- } else {
19
+ if (optional) {
20
+ if (!program.verbose) return
21
+ if (!customColors) data = grey(data)
41
22
  await log(data)
42
- process.exit(0)
23
+ } else {
24
+ if (isArray(data) && !isCollection(data) && !program.json) {
25
+ if (program.format === 'inline') data = data.join(' ')
26
+ else data = data.join('\n')
27
+ } else if (isCollection(data) && program.format === 'table') {
28
+ data = tabularize(data)
29
+ } else if (program.json || isCollection(data) || isPlainObject(data)) {
30
+ // indent deep objects
31
+ data = JSON.stringify(data, null, 2)
32
+ // Do not copy to clipboard when the output is valid JSON
33
+ program.clipboard = false
34
+ }
35
+ if (data == null || (isArray(data) && data.length === 0)) {
36
+ console.error('no result found')
37
+ return process.exit(1)
38
+ }
39
+ if (program.clipboard) {
40
+ copyAndExit(data)
41
+ } else {
42
+ await log(data)
43
+ process.exit(0)
44
+ }
43
45
  }
44
46
  }
45
47
  }
@@ -1,27 +1,14 @@
1
- const program = require('./program')
2
- const dropNonSelectedSubprops = require('./drop_non_selected_subprops')
3
- const minimizeClaims = require('./minimize_claims')
4
- const stringifyAsJsFunction = require('./stringify_as_js_function')
5
- const { pick } = require('lodash')
6
- const { simplify } = require('./wbk')(program)
7
- const { createMode = false } = program
8
- const simplifyOptions = {
9
- keepIds: !createMode,
10
- keepQualifiers: true,
11
- keepReferences: true,
12
- keepRichValues: true,
13
- keepNonTruthy: true,
14
- keepSnaktypes: true,
15
- keepRanks: true,
16
- keepBadges: true,
17
- // No need to keep the hashes as every edited claim (identified with a GUID)
18
- // will have it's qualifiers and references fully overriden
19
- keepHashes: false
20
- }
1
+ import { pick } from 'lodash-es'
2
+ import { getStatementsKey } from 'wikibase-edit/lib/parse_instance.js'
3
+ import { simplifyEntity } from 'wikibase-sdk'
4
+ import dropNonSelectedSubprops from './drop_non_selected_subprops.js'
5
+ import { minimizeClaimsOrSnaks } from './minimize_claims.js'
6
+ import program from './program.js'
7
+ import stringifyAsJsFunction from './stringify_as_js_function.js'
21
8
 
22
- module.exports = ({ batchMode, format, propsToPick, requestedPropsAndSubProps, minimize }) => {
9
+ export function outputTemplatesFactory ({ batchMode, format, propsToPick, requestedPropsAndSubProps, minimize }) {
23
10
  const formatEntity = FormatEntity(batchMode, propsToPick, requestedPropsAndSubProps, minimize)
24
- return async entities => {
11
+ return async function outputTemplates (entities) {
25
12
  entities = entities.map(formatEntity)
26
13
  if (format === 'js') {
27
14
  const jsFile = await stringifyAsJsFunction(entities[0], program.lang)
@@ -33,16 +20,39 @@ module.exports = ({ batchMode, format, propsToPick, requestedPropsAndSubProps, m
33
20
  }
34
21
  }
35
22
 
36
- const FormatEntity = (batchMode, propsToPick, requestedPropsAndSubProps, minimize) => entity => {
37
- entity = simplify.entity(entity, simplifyOptions)
38
- if (createMode) delete entity.id
39
- entity = pick(entity, propsToPick)
40
- dropNonSelectedSubprops(entity, requestedPropsAndSubProps)
41
- if (!batchMode && minimize !== false) {
42
- minimizeClaims(entity.claims)
43
- minimizeSitelinks(entity.sitelinks)
23
+ function FormatEntity (batchMode, propsToPick, requestedPropsAndSubProps, minimize) {
24
+ const { createMode = false } = program
25
+ const simplifyOptions = {
26
+ keepIds: !createMode,
27
+ keepQualifiers: true,
28
+ keepReferences: true,
29
+ keepRichValues: true,
30
+ keepNonTruthy: true,
31
+ keepSnaktypes: true,
32
+ keepRanks: true,
33
+ keepBadges: true,
34
+ // No need to keep the hashes as every edited claim (identified with a GUID)
35
+ // will have it's qualifiers and references fully overriden
36
+ keepHashes: false,
37
+ }
38
+ const statementsKey = getStatementsKey(program.instance)
39
+
40
+ return function formatEntity (entity) {
41
+ if (statementsKey !== 'claims') {
42
+ if (propsToPick.includes('claims')) {
43
+ propsToPick[propsToPick.indexOf('claims')] = statementsKey
44
+ }
45
+ }
46
+ entity = pick(entity, propsToPick)
47
+ entity = simplifyEntity(entity, simplifyOptions)
48
+ if (createMode) delete entity.id
49
+ dropNonSelectedSubprops(entity, requestedPropsAndSubProps)
50
+ if (!batchMode && minimize !== false) {
51
+ minimizeClaimsOrSnaks(entity[statementsKey])
52
+ minimizeSitelinks(entity.sitelinks)
53
+ }
54
+ return entity
44
55
  }
45
- return entity
46
56
  }
47
57
 
48
58
  const minimizeSitelinks = sitelinks => {
@@ -1,16 +1,15 @@
1
- module.exports = {
2
- parseGuid: guid => {
3
- return guid
4
- // Normalize prefixed statements GUIDs as returned by SPARQL queries:
5
- // to the format used by Wikibase API
6
- // - from: wds:Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C
7
- // - to: Q1827902$6706334E-D27E-4F4B-B6DA-ABE7544DF11C
8
- // Also accept Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C
9
- .replace(/^([a-z]+:)?([QPL]\d+)-(.*)/i, '$2$$$3')
10
- // Often required when getting guids from the command line, as passing a value with a $
11
- // can be challenging, and require different quoting strategies
12
- .replace(/("|')/g, '')
13
- .replace('\\$', '$')
14
- .trim()
15
- }
1
+ export const parseGuid = guid => {
2
+ guid = guid
3
+ // Normalize prefixed statements GUIDs as returned by SPARQL queries:
4
+ // to the format used by Wikibase API
5
+ // - from: wds:Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C
6
+ // - to: Q1827902$6706334E-D27E-4F4B-B6DA-ABE7544DF11C
7
+ // Also accept Q1827902-6706334E-D27E-4F4B-B6DA-ABE7544DF11C
8
+ .replace(/^([a-z]+:)?([QPLM]\d+)-(.*)/i, '$2$$$3')
9
+ // Often required when getting guids from the command line, as passing a value with a $
10
+ // can be challenging, and require different quoting strategies
11
+ .replace(/("|')/g, '')
12
+ .replace('\\$', '$')
13
+ .trim()
14
+ return guid
16
15
  }
@@ -3,10 +3,10 @@ const shortTypes = {
3
3
  i: 'item',
4
4
  p: 'property',
5
5
  l: 'lexeme',
6
- s: 'sense'
6
+ s: 'sense',
7
7
  }
8
8
 
9
- module.exports = (type = 'item') => {
9
+ export default (type = 'item') => {
10
10
  // Fix plural forms
11
11
  type = type.replace(/s$/, '')
12
12
  return shortTypes[type] || type
@@ -1,13 +1,14 @@
1
- const { isPropertyId, isSitelinkKey } = require('wikibase-sdk')
1
+ import { isPropertyId, isSitelinkKey } from 'wikibase-sdk'
2
+
2
3
  const langPattern = /^\w{2,3}(-\w{2,4})?$/
3
4
  const termSections = [
4
5
  'labels',
5
6
  'descriptions',
6
7
  'aliases',
7
- 'lemmas'
8
+ 'lemmas',
8
9
  ]
9
10
 
10
- module.exports = propsStr => {
11
+ export default propsStr => {
11
12
  if (!propsStr) return {}
12
13
 
13
14
  // The Wikibase API doesn't accept lemmas, forms, senses props
@@ -27,13 +28,15 @@ const getPropsAndSubKeys = propStr => {
27
28
  if (isPropertyId(prop)) return lazyProps.claims(prop)
28
29
  else if (prop.match(langPattern)) return lazyProps.terms(prop)
29
30
  else if (isSitelinkKey(prop)) return lazyProps.sitelinks(prop)
31
+ // Handle Wikimedia Commons weirdness: it uses entity.statements but wbgetentities expects a "claims" prop
32
+ else if (prop === 'statements') return [ { prop: 'claims', subkey } ]
30
33
  else return [ { prop, subkey } ]
31
34
  }
32
35
 
33
36
  const lazyProps = {
34
37
  claims: propertyId => [ { prop: 'claims', subkey: propertyId } ],
35
38
  terms: lang => termSections.map(section => ({ prop: section, subkey: lang })),
36
- sitelinks: site => [ { prop: 'sitelinks', subkey: site } ]
39
+ sitelinks: site => [ { prop: 'sitelinks', subkey: site } ],
37
40
  }
38
41
 
39
42
  const aggregate = (index, propsData) => {
@@ -13,7 +13,7 @@ const propsMap = {
13
13
  badges: 'keepBadges',
14
14
  }
15
15
 
16
- module.exports = ({ simplify: simplifyOption, keep: props, timeConverter }) => {
16
+ export default ({ simplify: simplifyOption, keep: props, timeConverter }) => {
17
17
  const options = {}
18
18
 
19
19
  if (props != null) {
package/lib/path.js CHANGED
@@ -1,2 +1,3 @@
1
- const path = require('path')
2
- module.exports = process.platform === 'win32' ? path.win32 : path
1
+ import path from 'node:path'
2
+
3
+ export default process.platform === 'win32' ? path.win32 : path
package/lib/pluralize.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Attributes that can't be pluralized with `${attribute}s`
2
2
  // or are already pluralized
3
3
  const plurals = {
4
- aliases: 'aliases'
4
+ aliases: 'aliases',
5
5
  }
6
6
 
7
- module.exports = attribute => plurals[attribute] || `${attribute}s`
7
+ export default attribute => plurals[attribute] || `${attribute}s`
@@ -1,17 +1,19 @@
1
- const objectArgParser = require('./object_arg_parser')
2
- const program = require('commander')
3
- const { isFilePathSync, isJsonString } = require('../lib/utils')
1
+ import program from 'commander'
2
+ import { objectArgParser } from '#lib/object_arg_parser'
3
+ import { isFilePathSync, isJsonString } from '#lib/utils'
4
4
 
5
5
  const inlineOptions = [
6
- 'rank'
6
+ 'rank',
7
7
  ]
8
8
 
9
9
  const noInlineOptions = () => inlineOptions.filter(option => program[option] != null).length === 0
10
10
 
11
- module.exports = ({ inlineArgsParser }) => args => {
12
- if ((args.length === 1 && isJsonString(args[0]) && noInlineOptions()) || isFilePathSync(args[0])) {
13
- return objectArgParser(args)
14
- } else {
15
- return inlineArgsParser(args)
11
+ export function polymorphicCommandArgsParser ({ inlineArgsParser }) {
12
+ return async function (args) {
13
+ if ((args.length === 1 && isJsonString(args[0]) && noInlineOptions()) || isFilePathSync(args[0])) {
14
+ return objectArgParser(args)
15
+ } else {
16
+ return inlineArgsParser(args)
17
+ }
16
18
  }
17
19
  }
package/lib/program.js CHANGED
@@ -1,13 +1,13 @@
1
1
  // Extends commander with options and functions used by all subcommands.
2
2
 
3
- const program = require('commander')
4
- const applyEnvAndConfigDefault = require('./apply_env_and_config_default')
5
- const logCommandExamples = require('./log_command_examples')
6
- const globalOptionsHelp = require('./global_options_help')
3
+ import program from 'commander'
4
+ import { applyEnvAndConfigDefault } from './apply_env_and_config_default.js'
5
+ import globalOptionsHelp from './global_options_help.js'
6
+ import logCommandExamples from './log_command_examples.js'
7
7
 
8
- program.process = command => {
8
+ program.process = async command => {
9
9
  if (command) {
10
- const metadata = require(`../metadata/${command}`)
10
+ const { default: metadata } = await import(`../metadata/${command}.js`)
11
11
  const { args, description, options, examples, doc } = metadata
12
12
  program.arguments(args)
13
13
  program.description(description)
@@ -42,12 +42,12 @@ const commandsAcceptingZeroArguments = [
42
42
  'query',
43
43
  'convert',
44
44
  // Needs to also log the current config
45
- 'config'
45
+ 'config',
46
46
  ]
47
47
 
48
48
  const commandsTakingArgsOnStdin = [
49
49
  'data',
50
- 'generate-template'
50
+ 'generate-template',
51
51
  ]
52
52
 
53
53
  const parseArgv = (argv, isCommandsWithCustomHelpMenu) => {
@@ -80,4 +80,4 @@ process.stdout.on('error', err => {
80
80
  else throw err
81
81
  })
82
82
 
83
- module.exports = program
83
+ export default program
package/lib/properties.js CHANGED
@@ -1,24 +1,23 @@
1
- module.exports = {
2
- mainProps: {
3
- 'https://www.wikidata.org': [ 'P31', 'P279' ]
4
- },
5
- propTypes: {
6
- 'external-id': { factor: 0.1, color: 'grey' },
7
- string: { factor: 1 },
8
- 'wikibase-item': { factor: 1 },
9
- time: { factor: 1 },
10
- monolingualtext: { factor: 2 },
11
- quantity: { factor: 1 },
12
- 'wikibase-property': { factor: 1 },
13
- url: { factor: 0.5 },
14
- commonsMedia: { factor: 0.5 },
15
- 'globe-coordinate': { factor: 0.5 },
16
- math: { factor: 0.5 },
17
- 'geo-shape': { factor: 0.5 },
18
- 'tabular-data': { factor: 0.5 },
19
- 'wikibase-lexeme': { factor: 0.5 },
20
- 'wikibase-form': { factor: 0.5 },
21
- 'wikibase-sense': { factor: 0.5 },
22
- 'musical-notation': { factor: 0.5 }
23
- }
1
+ export const mainProps = {
2
+ 'https://www.wikidata.org': [ 'P31', 'P279' ],
3
+ }
4
+
5
+ export const propTypes = {
6
+ 'external-id': { factor: 0.1, color: 'grey' },
7
+ string: { factor: 1 },
8
+ 'wikibase-item': { factor: 1 },
9
+ time: { factor: 1 },
10
+ monolingualtext: { factor: 2 },
11
+ quantity: { factor: 1 },
12
+ 'wikibase-property': { factor: 1 },
13
+ url: { factor: 0.5 },
14
+ commonsMedia: { factor: 0.5 },
15
+ 'globe-coordinate': { factor: 0.5 },
16
+ math: { factor: 0.5 },
17
+ 'geo-shape': { factor: 0.5 },
18
+ 'tabular-data': { factor: 0.5 },
19
+ 'wikibase-lexeme': { factor: 0.5 },
20
+ 'wikibase-form': { factor: 0.5 },
21
+ 'wikibase-sense': { factor: 0.5 },
22
+ 'musical-notation': { factor: 0.5 },
24
23
  }
@@ -1,6 +1,6 @@
1
- const { isItemId } = require('wikibase-sdk')
1
+ import { isItemId } from 'wikibase-sdk'
2
2
 
3
- module.exports = id => {
3
+ export default id => {
4
4
  if (!isItemId(id)) throw new Error(`invalid id: ${id}`)
5
5
 
6
6
  return `SELECT ?item WHERE {
@@ -1,4 +1,4 @@
1
- module.exports = lang => {
1
+ export default lang => {
2
2
  return `SELECT ?property ?propertyLabel ?propertyType ?propertyDescription ?propertyAltLabel WHERE {
3
3
  ?property wikibase:propertyType ?propertyType .
4
4
  SERVICE wikibase:label { bd:serviceParam wikibase:language "${lang},en". }
@@ -1,9 +1,9 @@
1
- const split = require('split')
2
- const through = require('through')
1
+ import split from 'split'
2
+ import through from 'through'
3
3
 
4
4
  let ids = []
5
5
 
6
- module.exports = handleIdsBatch => {
6
+ export function readIdsFromStdin (handleIdsBatch) {
7
7
  const write = function (id) {
8
8
  id = id.trim()
9
9
  if (id === '') return
package/lib/request.js CHANGED
@@ -1,10 +1,13 @@
1
- const fetch = require('node-fetch')
2
- const { logResponseHeaders } = require('./program')
3
1
  // Using a custom agent to set keepAlive=true
4
2
  // https://nodejs.org/api/http.html#http_class_http_agent
5
3
  // https://github.com/bitinn/node-fetch#custom-agent
6
- const http = require('http')
7
- const https = require('https')
4
+ import http from 'node:http'
5
+ import https from 'node:https'
6
+ import { pick } from 'lodash-es'
7
+ import fetch from 'node-fetch'
8
+ import program from './program.js'
9
+ import globalHeaders from './request_headers.js'
10
+
8
11
  const httpAgent = new http.Agent({ keepAlive: true })
9
12
  const httpsAgent = new https.Agent({ keepAlive: true })
10
13
  // Useful to:
@@ -12,39 +15,35 @@ const httpsAgent = new https.Agent({ keepAlive: true })
12
15
  // - accept certificates that would otherwise generate a UNABLE_TO_VERIFY_LEAF_SIGNATURE error
13
16
  const insecureHttpsAgent = new https.Agent({ keepAlive: true, rejectUnauthorized: false })
14
17
  const tlsErrorsHosts = new Set([ 'datos.bne.es' ])
15
- const { pick } = require('lodash')
16
18
 
17
19
  const agent = ({ host, protocol }) => {
18
20
  if (tlsErrorsHosts.has(host)) return protocol === 'http:' ? httpAgent : insecureHttpsAgent
19
21
  else return protocol === 'http:' ? httpAgent : httpsAgent
20
22
  }
21
-
22
- const globalHeaders = require('./request_headers')
23
23
  const buildHeaders = customHeaders => {
24
24
  if (customHeaders) return Object.assign({}, globalHeaders, customHeaders)
25
25
  else return globalHeaders
26
26
  }
27
27
 
28
- module.exports = {
29
- get: async url => {
30
- const res = await fetch(url, { headers: globalHeaders, agent })
31
- return handleResponse({ res, url })
32
- },
28
+ export const get = async url => {
29
+ const res = await fetch(url, { headers: globalHeaders, agent })
30
+ return handleResponse({ res, url })
31
+ }
33
32
 
34
- customGet: async ({ url, headers }) => {
35
- const res = await fetch(url, { headers: buildHeaders(headers), agent })
36
- return handleResponse({ res, url })
37
- },
33
+ export const customGet = async ({ url, headers }) => {
34
+ const res = await fetch(url, { headers: buildHeaders(headers), agent })
35
+ return handleResponse({ res, url })
36
+ }
38
37
 
39
- post: async ({ url, body, headers }) => {
40
- const method = 'post'
41
- const res = await fetch(url, { method, body, headers: buildHeaders(headers), agent })
42
- return handleResponse({ res, url, method })
43
- }
38
+ export const post = async ({ url, body, headers }) => {
39
+ const method = 'post'
40
+ const res = await fetch(url, { method, body, headers: buildHeaders(headers), agent })
41
+ return handleResponse({ res, url, method })
44
42
  }
45
43
 
46
- const handleResponse = async ({ res, url, method }) => {
44
+ async function handleResponse ({ res, url, method }) {
47
45
  let body = await res.text()
46
+ const { logResponseHeaders } = program
48
47
  if (logResponseHeaders) {
49
48
  let headers = res.headers.raw()
50
49
  if (typeof logResponseHeaders === 'string') {
@@ -81,5 +80,5 @@ const newError = (statusCode, body, statusMessage) => {
81
80
  }
82
81
 
83
82
  const statusCodeByErrorCode = {
84
- 'no-such-entity': 404
83
+ 'no-such-entity': 404,
85
84
  }
@@ -1,2 +1,8 @@
1
- const { name, version } = require('../package.json')
2
- module.exports = { 'User-Agent': `${name}/v${version}` }
1
+ import { resolve } from 'node:path'
2
+ import { getDirname } from '#lib/fs'
3
+ import { readJsonFile } from '#lib/json'
4
+
5
+ const dirname = getDirname(import.meta.url)
6
+ const { name, version } = readJsonFile(resolve(dirname, '../package.json'))
7
+
8
+ export default { 'User-Agent': `${name}/v${version}` }
@@ -1,8 +1,8 @@
1
- const { exec } = require('child_process')
2
- const { green } = require('chalk')
3
- const getCacheFolderPath = require('../lib/get_cache_folder_path')
1
+ import { exec } from 'node:child_process'
2
+ import { green } from '#lib/chalk'
3
+ import getCacheFolderPath from '#lib/get_cache_folder_path'
4
4
 
5
- module.exports = () => {
5
+ export default () => {
6
6
  return getCacheFolderPath('props')
7
7
  .then(propsDir => {
8
8
  // --force: ignore if there are no more files to delete
@@ -1,10 +1,11 @@
1
- const makeSparqlQuery = require('./make_sparql_query')
2
- const program = require('./program')
3
- const { sparqlQuery } = require('../lib/wbk')(program)
4
- const output = require('./output')(program)
5
- const open = require('../lib/open')
1
+ import { openUrl } from '#lib/open'
2
+ import wbk from '#lib/wbk'
3
+ import makeSparqlQuery from './make_sparql_query.js'
4
+ import { outputFactory } from './output.js'
5
+ import program from './program.js'
6
6
 
7
- module.exports = (sparql, format) => {
7
+ export function sparqlQueryCommand (sparql, format) {
8
+ const output = outputFactory(program)
8
9
  if (program.open) {
9
10
  openQueryServiceGUI(sparql)
10
11
  } else if (program.dry) {
@@ -16,7 +17,8 @@ module.exports = (sparql, format) => {
16
17
  }
17
18
  }
18
19
 
19
- const openQueryServiceGUI = sparql => {
20
+ function openQueryServiceGUI (sparql) {
21
+ const { sparqlQuery } = wbk(program)
20
22
  const queryServiceGUI = sparqlQuery(sparql).replace('sparql?format=json&query=', '#')
21
- open(queryServiceGUI)
23
+ openUrl(queryServiceGUI)
22
24
  }
@@ -1,5 +1,5 @@
1
- const { uniq } = require('lodash')
2
- const { getEntitiesLabels } = require('./get_entities_labels')
1
+ import { uniq } from 'lodash-es'
2
+ import { getEntitiesLabels } from './get_entities_labels.js'
3
3
 
4
4
  const formatJsObj = text => {
5
5
  return text
@@ -70,7 +70,7 @@ const getPatternComment = (line, pattern, entitiesLabels) => {
70
70
  }
71
71
  }
72
72
 
73
- module.exports = async (entity, lang) => {
73
+ export default async (entity, lang) => {
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