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
@@ -1,12 +1,17 @@
1
- const program = require('./program')
2
- require('./exit_on_missing').instance(program.instance)
3
- const { isEntityId, getManyEntities } = require('./wbk')(program)
4
- const errors_ = require('./errors')
5
- const { get } = require('./request')
6
- const { wait } = require('./utils')
1
+ import { isEntityId } from 'wikibase-sdk'
2
+ import { exitOnMissingInstance } from '#lib/exit_on_missing'
3
+ import errors_ from './errors.js'
4
+ import program from './program.js'
5
+ import { get } from './request.js'
6
+ import { wait } from './utils.js'
7
+ import wbk from './wbk.js'
8
+
7
9
  const noop = () => {}
8
10
 
9
- module.exports = ({ ids, props, languages, urls, onResponse, onDone = noop }) => {
11
+ export async function getEntitiesByBatches ({ ids, props, languages, urls, onResponse, onDone = noop }) {
12
+ exitOnMissingInstance(program.instance)
13
+ const { getManyEntities } = wbk(program)
14
+
10
15
  if (ids != null) {
11
16
  ids.forEach(id => {
12
17
  if (!isEntityId(id)) throw new Error(`invalid entity id: ${id}`)
@@ -1,6 +1,6 @@
1
- const getEntities = require('./get_entities')
1
+ import getEntities from './get_entities.js'
2
2
 
3
- const getEntitiesLabels = async (ids, lang = 'en') => {
3
+ export async function getEntitiesLabels (ids, lang = 'en') {
4
4
  const entities = await getEntities({ ids, languages: lang, props: 'labels' })
5
5
  return parseEntitiesLabels(entities, lang)
6
6
  }
@@ -17,11 +17,9 @@ const parseEntitiesLabels = (entities, lang) => {
17
17
 
18
18
  const parseLangLabel = (labels, lang) => labels && labels[lang] && labels[lang].value
19
19
 
20
- const getEntityLabel = async (id, lang) => {
20
+ export async function getEntityLabel (id, lang) {
21
21
  const labels = await getEntitiesLabels([ id ], lang)
22
22
  const label = labels[id]
23
23
  if (!label) throw new Error(`label not found: ${id}`)
24
24
  return label
25
25
  }
26
-
27
- module.exports = { getEntitiesLabels, getEntityLabel }
@@ -1,12 +1,12 @@
1
- const fetch = require('node-fetch')
2
- const program = require('./program')
3
- const { isEntitySchemaId } = require('wikibase-sdk')
1
+ import fetch from 'node-fetch'
2
+ import { isEntitySchemaId } from 'wikibase-sdk'
3
+ import { exitOnMissingInstance } from '#lib/exit_on_missing'
4
+ import program from './program.js'
4
5
 
5
- const { instance } = program
6
- if (!instance) throw new Error('no instance set')
7
- const instanceDomain = instance.replace('/w/api.php', '')
6
+ export function getEntitiesSchemas (ids) {
7
+ exitOnMissingInstance(program.instance)
8
+ const instanceDomain = program.instance.replace('/w/api.php', '')
8
9
 
9
- module.exports = ids => {
10
10
  ids.forEach(id => {
11
11
  if (!isEntitySchemaId(id)) throw new Error(`invalid entity schema id: ${id}`)
12
12
  })
@@ -1,9 +1,11 @@
1
- const path = require('path')
2
- const fs = require('./fs')
3
- const mkdirp = require('mkdirp')
4
- const homePath = require('os').homedir()
1
+ import os from 'node:os'
2
+ import path from 'node:path'
3
+ import mkdirp from 'mkdirp'
4
+ import { writeAccessSync } from './fs.js'
5
5
 
6
- module.exports = (category, subfolderName) => {
6
+ const homePath = os.homedir()
7
+
8
+ export default (category, subfolderName) => {
7
9
  let folderPath
8
10
  if (process.platform === 'win32') {
9
11
  folderPath = path.win32.join(homePath, 'wikibase-cli', category || subfolderName)
@@ -15,7 +17,7 @@ module.exports = (category, subfolderName) => {
15
17
  }
16
18
  mkdirp.sync(folderPath)
17
19
  // Make sure we have write access right in case the folder already existed
18
- fs.writeAccessSync(folderPath)
20
+ writeAccessSync(folderPath)
19
21
 
20
22
  return folderPath
21
23
  }
@@ -1,38 +1,39 @@
1
- const fs = require('./fs')
2
- const { URL } = require('url')
3
- const getCacheFolderPath = require('../lib/get_cache_folder_path')
4
- const fetchLangProps = require('./fetch_lang_props')
1
+ import { URL } from 'node:url'
2
+ import getCacheFolderPath from '#lib/get_cache_folder_path'
3
+ import { hashString } from '#lib/hash_string'
4
+ import { readJsonFile } from '#lib/json'
5
+ import fetchLangProps from './fetch_lang_props.js'
6
+ import { writeFile } from './fs.js'
5
7
 
6
- module.exports = async program => {
8
+ export default async program => {
7
9
  const { lang, sparqlEndpoint, noCache } = program
8
10
  const sparqlEndpointName = getSparqlEndpointPart(sparqlEndpoint)
9
11
 
10
12
  const propsDir = await getCacheFolderPath('props')
11
- const file = `${propsDir}/${lang}${sparqlEndpointName}.v2.json`
13
+ const langJsonFilePath = `${propsDir}/${lang}${sparqlEndpointName}.v2.json`
12
14
 
13
- if (noCache) return refreshProps(program, file)
15
+ if (noCache) return refreshProps(program, langJsonFilePath)
14
16
 
15
17
  try {
16
- const propsSync = require(file)
17
- return propsSync
18
+ return readJsonFile(langJsonFilePath)
18
19
  } catch (err) {
19
- return refreshProps(program, file)
20
+ return refreshProps(program, langJsonFilePath)
20
21
  }
21
22
  }
22
23
 
23
- const refreshProps = async (program, file) => {
24
- await fetchAndSaveProps(program, file)
25
- return require(file)
24
+ const refreshProps = async (program, langJsonFilePath) => {
25
+ await fetchAndSaveProps(program, langJsonFilePath)
26
+ return readJsonFile(langJsonFilePath)
26
27
  }
27
28
 
28
- const fetchAndSaveProps = async (program, filepath) => {
29
+ const fetchAndSaveProps = async (program, langJsonFilePath) => {
29
30
  const props = await fetchLangProps(program)
30
- return fs.writeFile(filepath, JSON.stringify(props))
31
+ return writeFile(langJsonFilePath, JSON.stringify(props))
31
32
  }
32
33
 
33
34
  const getSparqlEndpointPart = sparqlEndpoint => {
34
35
  if (!sparqlEndpoint) return ''
35
36
  const { host } = new URL(sparqlEndpoint)
36
- const hash = require('./hash_string')(sparqlEndpoint)
37
+ const hash = hashString(sparqlEndpoint)
37
38
  return `-${host}-${hash}`
38
39
  }
@@ -1,19 +1,28 @@
1
- const program = require('./program')
2
- const { propTypes } = require('./properties')
1
+ import program from './program.js'
2
+ import { propTypes } from './properties.js'
3
3
 
4
- module.exports = (pattern, includeTypes) => {
4
+ const normalizeTypeName = name => name.replace(/-/g, '').toLowerCase()
5
+ const normalizedTypesNames = Object.keys(propTypes).map(normalizeTypeName)
6
+
7
+ export default (pattern, includeTypes) => {
5
8
  const { type } = program
9
+ let normalizedType, isKnownType
10
+ // If --type is called without argument, `type` will be a boolean
11
+ if (typeof type === 'string') {
12
+ normalizedType = normalizeTypeName(type)
13
+ isKnownType = normalizedTypesNames.includes(normalizedType)
14
+ }
6
15
  if (!(pattern && pattern.length > 0)) {
7
- if (typeof type === 'string') {
16
+ if (normalizedType) {
8
17
  // Consider that what was passed as a type argument was actually a pattern
9
- if (!(type.toLowerCase() in propTypes)) pattern = type
18
+ if (!isKnownType) pattern = type
10
19
  } else {
11
20
  return
12
21
  }
13
22
  }
14
23
 
15
- if (includeTypes && pattern.toLowerCase() in propTypes) {
16
- return propData => propData.type === pattern
24
+ if (includeTypes && isKnownType) {
25
+ return propData => normalizeTypeName(propData.type) === normalizedType
17
26
  }
18
27
 
19
28
  // Set the insensitive case flag only if only lowercased was used
@@ -23,8 +32,8 @@ module.exports = (pattern, includeTypes) => {
23
32
  const filterRegex = new RegExp(pattern, flag)
24
33
 
25
34
  let typeFilter
26
- if (typeof type === 'string' && type.toLowerCase() in propTypes) {
27
- typeFilter = propData => propData.type === type
35
+ if (normalizedType && isKnownType) {
36
+ typeFilter = propData => normalizeTypeName(propData.type) === normalizedType
28
37
  } else {
29
38
  typeFilter = () => true
30
39
  }
@@ -1,11 +1,11 @@
1
- const getEntities = require('./get_entities')
2
- const { simplify } = require('wikibase-sdk')
1
+ import { simplify } from 'wikibase-sdk'
2
+ import getEntities from './get_entities.js'
3
3
 
4
- module.exports = async (ids, lang = 'en') => {
4
+ export default async (ids, lang = 'en') => {
5
5
  const properties = await getEntities({
6
6
  ids,
7
7
  languages: [ lang, 'en' ],
8
- props: [ 'info', 'labels' ]
8
+ props: [ 'info', 'labels' ],
9
9
  })
10
10
  const propertiesData = {}
11
11
  Object.keys(properties).forEach(id => {
@@ -13,7 +13,7 @@ module.exports = async (ids, lang = 'en') => {
13
13
  const labels = simplify.labels(property.labels)
14
14
  propertiesData[id] = {
15
15
  type: property.datatype,
16
- label: labels[lang] || labels.en
16
+ label: labels[lang] || labels.en,
17
17
  }
18
18
  })
19
19
  return propertiesData
@@ -1,9 +1,9 @@
1
- const { get } = require('../lib/request')
2
- const _ = require('lodash')
1
+ import { values } from 'lodash-es'
2
+ import { get } from '#lib/request'
3
3
 
4
- module.exports = (lang, site, title) => {
4
+ export default (lang, site, title) => {
5
5
  title = encodeURIComponent(title)
6
6
  const url = `https://${lang}.${site}.org/w/api.php?format=json&action=query&redirects&titles=${title}`
7
7
  return get(url)
8
- .then(body => _.values(body.query.pages)[0].title)
8
+ .then(body => values(body.query.pages)[0].title)
9
9
  }
@@ -1,11 +1,12 @@
1
- const { get } = require('../lib/request')
2
- const exitOnMissing = require('../lib/exit_on_missing')
3
- const errors_ = require('../lib/errors')
1
+ import errors_ from '#lib/errors'
2
+ import { exitOnMissingInstance } from '#lib/exit_on_missing'
3
+ import { get } from '#lib/request'
4
+ import wbk from '#lib/wbk'
4
5
 
5
- module.exports = program => {
6
- exitOnMissing.instance(program.instance)
7
- const { getEntities } = require('../lib/wbk')(program)
8
- return async (id, lang, sitelinkSuffix) => {
6
+ export function getSitelinkUrlFactory (program) {
7
+ exitOnMissingInstance(program.instance)
8
+ const { getEntities } = wbk(program)
9
+ return async function (id, lang, sitelinkSuffix) {
9
10
  if (id[0] === 'P') {
10
11
  console.error("Wikibase properties don't have sitelinks")
11
12
  process.exit(1)
@@ -25,7 +26,7 @@ module.exports = program => {
25
26
  }
26
27
 
27
28
  const sitelinkDomain = {
28
- wiki: 'wikipedia.org'
29
+ wiki: 'wikipedia.org',
29
30
  }
30
31
 
31
32
  const formatTitle = title => {
@@ -1,4 +1,4 @@
1
- module.exports = () => {
1
+ export function getStdinInput () {
2
2
  let input = ''
3
3
  return new Promise((resolve, reject) => {
4
4
  process.stdin
@@ -1,13 +1,13 @@
1
- const fetch = require('node-fetch')
2
- const program = require('../lib/program')
3
- const crypto = require('crypto')
4
- const { isEntityId } = require('wikibase-sdk')
1
+ import crypto from 'node:crypto'
2
+ import fetch from 'node-fetch'
3
+ import { isEntityId } from 'wikibase-sdk'
4
+ import { exitOnMissingInstance } from '#lib/exit_on_missing'
5
+ import program from '#lib/program'
5
6
 
6
- const { instance } = program
7
- if (!instance) throw new Error('no instance set')
8
- const instanceDomain = instance.replace('/w/api.php', '')
7
+ export default ids => {
8
+ exitOnMissingInstance(program.instance)
9
+ const instanceDomain = program.instance.replace('/w/api.php', '')
9
10
 
10
- module.exports = ids => {
11
11
  ids.forEach(id => {
12
12
  if (!isEntityId(id)) throw new Error(`invalid entity id: ${id}`)
13
13
  })
@@ -1,4 +1,4 @@
1
- module.exports = {
1
+ export default {
2
2
  lang: [ '-l, --lang <lang>', 'specify the language to use' ],
3
3
  verbose: [ '-v, --verbose', 'make the output more verbose' ],
4
4
  dry: [ '-d, --dry', 'output the generated data, do not run the query' ],
@@ -1,3 +1,5 @@
1
- const crypto = require('crypto')
1
+ import { createHash } from 'node:crypto'
2
2
 
3
- module.exports = str => crypto.createHash('md5').update(str).digest('hex')
3
+ export function hashString (str) {
4
+ return createHash('md5').update(str).digest('hex')
5
+ }
package/lib/json.js ADDED
@@ -0,0 +1,5 @@
1
+ import { readFileSync } from 'node:fs'
2
+
3
+ export function readJsonFile (jsonFilePath) {
4
+ return JSON.parse(readFileSync(jsonFilePath, 'utf-8'))
5
+ }
package/lib/local_lang.js CHANGED
@@ -1,3 +1,4 @@
1
1
  const { LANG } = process.env
2
+
2
3
  // process.env.LANG is apparently not defined on Windows
3
- module.exports = (LANG && LANG.split(/[\W_-]/)[0]) || 'en'
4
+ export const localLang = (LANG && LANG.split(/[\W_-]/)[0]) || 'en'
package/lib/log_claims.js CHANGED
@@ -1,12 +1,12 @@
1
- const _ = require('lodash')
2
- const { isEntityId } = require('wikibase-sdk')
3
- const filterClaimsProperties = require('../lib/filter_claims_properties')
4
- const getClaimsTexts = require('../lib/get_claims_texts')
5
- const { getEntitiesLabels } = require('./get_entities_labels')
6
- const getPropertiesData = require('./get_properties_data')
7
- const { bgMagenta, grey } = require('chalk')
1
+ import { chain, max, padEnd } from 'lodash-es'
2
+ import { isEntityId } from 'wikibase-sdk'
3
+ import { bgMagenta, grey } from '#lib/chalk'
4
+ import filterClaimsProperties from '#lib/filter_claims_properties'
5
+ import getClaimsTexts from '#lib/get_claims_texts'
6
+ import { getEntitiesLabels } from './get_entities_labels.js'
7
+ import getPropertiesData from './get_properties_data.js'
8
8
 
9
- module.exports = async ({ program, simplifiedClaims, extraData = {}, pattern, resort }) => {
9
+ export default async ({ program, simplifiedClaims, extraData = {}, pattern, resort }) => {
10
10
  const { lang } = program
11
11
  const ids = collectEntityIds(simplifiedClaims, extraData)
12
12
  const labels = await getEntitiesLabels(ids, lang)
@@ -24,11 +24,11 @@ module.exports = async ({ program, simplifiedClaims, extraData = {}, pattern, re
24
24
  labels,
25
25
  simplifiedClaims,
26
26
  resort,
27
- lang
27
+ lang,
28
28
  })
29
29
 
30
30
  const propTextLength = claimsTexts.map(data => data.propText.length)
31
- const longestPropText = _.max(propTextLength) || 0
31
+ const longestPropText = max(propTextLength) || 0
32
32
 
33
33
  const claimsText = claimsTexts.map(propClaimsText(longestPropText))
34
34
  const lineBreaks = claimsTexts.length > 3 ? '\n\n' : '\n'
@@ -36,7 +36,7 @@ module.exports = async ({ program, simplifiedClaims, extraData = {}, pattern, re
36
36
  }
37
37
 
38
38
  const collectEntityIds = (claims, extraData) => {
39
- const claimIds = _(claims)
39
+ const claimIds = chain(claims)
40
40
  .values()
41
41
  .flatten()
42
42
  .filter(isEntityId)
@@ -53,7 +53,7 @@ const propClaimsText = targetLength => claimsTextData => {
53
53
  }
54
54
 
55
55
  const harmonizedLength = (str, minLength) => {
56
- if (minLength && str.length < minLength) str = _.padEnd(str, minLength)
56
+ if (minLength && str.length < minLength) str = padEnd(str, minLength)
57
57
  return str
58
58
  }
59
59
 
@@ -1,7 +1,7 @@
1
- const { grey } = require('chalk')
2
- const { execSync } = require('child_process')
1
+ import { execSync } from 'node:child_process'
2
+ import { grey } from '#lib/chalk'
3
3
 
4
- module.exports = (command, examples, doc, dryRun = false) => {
4
+ export default (command, examples, doc, dryRun = false) => {
5
5
  if (examples && examples.length > 0) {
6
6
  console.log('\nExamples:')
7
7
  for (const example of examples) {
package/lib/log_ndjson.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Log as newline-delimited JSON: one obj JSON per line. No comma.
2
2
 
3
- module.exports = array => {
3
+ export default array => {
4
4
  for (const obj of array) {
5
5
  process.stdout.write(JSON.stringify(obj))
6
6
  process.stdout.write('\n')
@@ -1,18 +1,17 @@
1
- const { customGet, post } = require('../lib/request')
2
- const errors_ = require('../lib/errors')
3
- const { grey } = require('chalk')
4
- const program = require('../lib/program')
5
- const output = require('../lib/output')(program)
6
- const { simplify } = require('wikibase-sdk')
1
+ import { simplify } from 'wikibase-sdk'
2
+ import { grey } from '#lib/chalk'
3
+ import errors_ from '#lib/errors'
4
+ import { exitOnMissingSparqlEndpoint } from '#lib/exit_on_missing'
5
+ import { outputFactory } from '#lib/output'
6
+ import program from '#lib/program'
7
+ import { customGet, post } from '#lib/request'
8
+ import wbk from '#lib/wbk'
7
9
 
8
- const simplifyOption = !program.raw
9
- const { index: indexAttribute } = program
10
-
11
- module.exports = (sparql, format = 'json') => {
10
+ export default (sparql, format = 'json') => {
12
11
  // JIT require to be sure program.process was already called
13
12
  // and program.sparqlEndpoint is set
14
- require('../lib/exit_on_missing').sparqlEndpoint(program.sparqlEndpoint)
15
- const { sparqlQuery } = require('../lib/wbk')(program)
13
+ exitOnMissingSparqlEndpoint(program.sparqlEndpoint)
14
+ const { sparqlQuery } = wbk(program)
16
15
 
17
16
  if (localFormatsDerivedFromJSON.includes(format)) format = 'json'
18
17
 
@@ -22,7 +21,8 @@ module.exports = (sparql, format = 'json') => {
22
21
  .catch(errors_.exit)
23
22
  }
24
23
 
25
- const makeRequest = (url, format) => {
24
+ function makeRequest (url, format) {
25
+ const output = outputFactory(program)
26
26
  const acceptHeader = formatAcceptHeader[format]
27
27
  if (!acceptHeader) throw new Error(`unknown format: ${format}`)
28
28
  // Avoid making a POST request when not necessary as those aren't cached
@@ -32,8 +32,8 @@ const makeRequest = (url, format) => {
32
32
  return customGet({
33
33
  url,
34
34
  headers: {
35
- Accept: formatAcceptHeader[format]
36
- }
35
+ Accept: formatAcceptHeader[format],
36
+ },
37
37
  })
38
38
  } else {
39
39
  const [ postUrl, urlencodedSparql ] = url.split('?')
@@ -44,7 +44,7 @@ const makeRequest = (url, format) => {
44
44
  headers: {
45
45
  'Content-type': 'application/sparql-query',
46
46
  Accept: formatAcceptHeader[format],
47
- }
47
+ },
48
48
  })
49
49
  }
50
50
  }
@@ -63,16 +63,20 @@ const formatAcceptHeader = {
63
63
  binrdf: 'application/x-binary-rdf-results-table',
64
64
  }
65
65
 
66
- const parseResult = format => results => {
67
- try {
68
- if (format === 'json' && simplifyOption) {
69
- results = simplify.sparqlResults(results, { minimize: true })
70
- if (indexAttribute) results = indexBy(results, indexAttribute)
66
+ function parseResult (format) {
67
+ const simplifyOption = !program.raw
68
+ const { index: indexAttribute } = program
69
+ return results => {
70
+ try {
71
+ if (format === 'json' && simplifyOption) {
72
+ results = simplify.sparqlResults(results, { minimize: true })
73
+ if (indexAttribute) results = indexBy(results, indexAttribute)
74
+ }
75
+ return results
76
+ } catch (err) {
77
+ err.context = { results }
78
+ throw err
71
79
  }
72
- return results
73
- } catch (err) {
74
- err.context = { results }
75
- throw err
76
80
  }
77
81
  }
78
82
 
@@ -1,7 +1,7 @@
1
- const wdLang = require('wikidata-lang')
2
- const { values, padEnd } = require('lodash')
1
+ import { values, padEnd } from 'lodash-es'
2
+ import wdLang from 'wikidata-lang'
3
3
 
4
- const find = input => {
4
+ export const findMatchingLang = input => {
5
5
  const regex = new RegExp(input, 'i')
6
6
  return values(wdLang.byCode)
7
7
  .filter(matches(regex))
@@ -12,11 +12,9 @@ const matches = regex => langData => {
12
12
  return label.match(regex) || native.match(regex)
13
13
  }
14
14
 
15
- const format = langsData => langsData.map(formatLangData).join('\n')
15
+ export const formatLangs = langsData => langsData.map(formatLangData).join('\n')
16
16
 
17
17
  const formatLangData = langData => {
18
18
  const { code, label, native, wd } = langData
19
19
  return `${padEnd(code, 5)} ${padEnd(wd, 10)} ${padEnd(label, 10)} ${padEnd(native, 10)} `
20
20
  }
21
-
22
- module.exports = { find, format }
@@ -1,4 +1,4 @@
1
- const minimizeSnaks = snaks => {
1
+ export function minimizeClaimsOrSnaks (snaks) {
2
2
  if (!snaks) return
3
3
  // Support references nested snaks
4
4
  if (snaks.snaks) snaks = snaks.snaks
@@ -11,11 +11,11 @@ const minimizeSnaks = snaks => {
11
11
  if (snak.snaktype === 'value') delete snak.snaktype
12
12
  if (snak.rank === 'normal') delete snak.rank
13
13
  if (snak.qualifiers) {
14
- minimizeSnaks(snak.qualifiers)
14
+ minimizeClaimsOrSnaks(snak.qualifiers)
15
15
  if (Object.keys(snak.qualifiers).length === 0) delete snak.qualifiers
16
16
  }
17
17
  if (snak.references) {
18
- snak.references.forEach(minimizeSnaks)
18
+ snak.references.forEach(minimizeClaimsOrSnaks)
19
19
  if (snak.references.length === 0) delete snak.references
20
20
  }
21
21
  const keys = Object.keys(snak)
@@ -25,5 +25,3 @@ const minimizeSnaks = snaks => {
25
25
  if (snaks[property].length === 1) snaks[property] = snaks[property][0]
26
26
  })
27
27
  }
28
-
29
- module.exports = minimizeSnaks
@@ -1,12 +1,12 @@
1
- const fs = require('fs')
2
- const { red } = require('chalk')
3
- const program = require('./program')
4
- const validateFunctionArgs = require('./validate_function_args')
5
- const { parseGuid } = require('./parse_command_utils')
6
- const { isFilePathSync, isJsonString, getAbsoluteFilePath, validateTemplateCommand } = require('../lib/utils')
1
+ import { red } from '#lib/chalk'
2
+ import { readJsonFile } from '#lib/json'
3
+ import { isFilePathSync, isJsonString, getAbsoluteFilePath, validateTemplateCommand } from '#lib/utils'
4
+ import { parseGuid } from './parse_command_utils.js'
5
+ import program from './program.js'
6
+ import validateFunctionArgs from './validate_function_args.js'
7
7
 
8
- module.exports = args => {
9
- const data = getData(args)
8
+ export async function objectArgParser (args) {
9
+ const data = await getData(args)
10
10
  if (data == null) return []
11
11
  if (data.guid != null) data.guid = parseGuid(data.guid)
12
12
  return [ data ]
@@ -31,7 +31,7 @@ const getData = args => {
31
31
 
32
32
  try {
33
33
  // Try to parse it as a JSON file
34
- return getJsonFile(filepath)
34
+ return readJsonFile(filepath)
35
35
  } catch (err1) {
36
36
  // Try to parse it as a JS module
37
37
  try {
@@ -50,13 +50,8 @@ const getData = args => {
50
50
  }
51
51
  }
52
52
 
53
- const getJsonFile = filepath => {
54
- const file = fs.readFileSync(filepath).toString()
55
- return JSON.parse(file)
56
- }
57
-
58
- const getDataFromJsModule = (filepath, args) => {
59
- const jsModule = require(filepath)
53
+ async function getDataFromJsModule (filepath, args) {
54
+ const { default: jsModule } = await import(filepath)
60
55
  if (typeof jsModule === 'function') {
61
56
  const inputArgs = args.slice(1)
62
57
  validateFunctionArgs(jsModule, inputArgs, jsModule)
@@ -64,7 +59,7 @@ const getDataFromJsModule = (filepath, args) => {
64
59
  } else if (jsModule && typeof jsModule.template === 'function') {
65
60
  validateTemplateCommand({
66
61
  commandName: program._name.replace('wb-', ''),
67
- validCommands: jsModule.commands
62
+ validCommands: jsModule.commands,
68
63
  })
69
64
  const inputArgs = args.slice(1)
70
65
  validateFunctionArgs(jsModule.template, inputArgs, jsModule)
package/lib/open.js CHANGED
@@ -1,13 +1,13 @@
1
- const copy = require('./copy')
2
- const open = require('open')
3
- const program = require('./program')
4
- const { url: urlOnly, clipboard } = program
1
+ import open from 'open'
2
+ import { copyAndExit } from './copy.js'
3
+ import program from './program.js'
5
4
 
6
- module.exports = url => {
5
+ export function openUrl (url) {
6
+ const { url: urlOnly, clipboard } = program
7
7
  if (urlOnly) {
8
- clipboard ? copy(url) : console.log(url)
8
+ clipboard ? copyAndExit(url) : console.log(url)
9
9
  } else {
10
- if (clipboard) copy(url)
10
+ if (clipboard) copyAndExit(url)
11
11
  open(url, { url: true })
12
12
  }
13
13
  }