maxlath / wikibase-cli

read and edit a Wikibase instance from the command line
MIT License
226 stars 24 forks source link

Support HJSON as input format #71

Closed nichtich closed 4 years ago

nichtich commented 5 years ago

Editing input files for wd edit-item and wd create-item could be simplified by support of an HJSON as input format. Alternatives might be JSON5 and YAML.

maxlath commented 5 years ago

what would you think of simply using a JS file?

// item.js
module.exports = {
  id: 'Q4115189',
  labels: { nl: 'foo' }
}
wd edit-item ./item.js

we could then even export a function that could accept arguments:

// multilabel.js
module.exports = (id, label) => ({
  id,
  labels: { de: label, en: label, fr: label, nl: label }
})
wd edit-item ./multilabel.js Q2013 wikidata
nichtich commented 5 years ago

Hey, the idea with optional arguments is great. I recommend a flag to preview the result (e.g. wd edit-item --dry ./multilabel.js Q2013 wikidata could output the JSON result of require('./multilabel.js')("Q2013", "wikidata")) and/or document recommended form of the input JSON files as:

#!/usr/bin/env node
module.exports = (id, label) => ({
  id,
  labels: { de: label, en: label, fr: label, nl: label }
})
if (require.main === module) {
  let entity = module.exports(...process.argv.slice(2))
  console.log(JSON.stringify(entity, null, 4))
}

I'd prefer not to require this kind of boilerplate code to just check back before actually doing an edit.

maxlath commented 5 years ago

not to say that option shouldn't exist, but this can be done from command-line with a oneliner:

node -p "require('./multilabel.js')(...process.argv.slice(1))" Q2013 wikidata
nichtich commented 5 years ago

Great, than including this line in the documentation should be enough.

maxlath commented 5 years ago

I have been working on this JS template/function feature, if you want to try it, its on branch wd-edit-item-function. I also implemented the --dry option, as it was indeed convenient

maxlath commented 5 years ago

I just published v7.3.0, which includes dynamic JS templates \o/