-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- update cli to esm
- Loading branch information
Showing
1 changed file
with
164 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,164 @@ | ||
#!/usr/bin/env node | ||
|
||
import program from "commander" | ||
import { readFileSync } from "fs" | ||
import pkg from "../package.json" assert {type: 'json'} | ||
import { red } from "chalk" | ||
import wdk from "../lib/wdk.js" | ||
import { WikidataService } from "../lib/wikidata-wrapper.js" | ||
import ndjson from "../lib/ndjson.js" | ||
import suggest from "../lib/suggest.js" | ||
import { loadMappingSchemes, updateMappingSchemes } from "../lib/mapping-schemes.js" | ||
import getConcepts from "../lib/queries/get-concepts.js" | ||
|
||
|
||
program | ||
.version(pkg.version) | ||
.description("Access Wikidata in JSKOS format") | ||
.option("-l, --lang <lang>", "language(s), comma-separated") | ||
.option("-s, --scheme <id>", "limit mappings to selected scheme") | ||
.option("-i, --simplify", "simplify Wikidata JSON") | ||
.option("-f, --force", "force reloading mapping schemes") | ||
.option("-b, --both", "include both mapping directions") | ||
|
||
// TODO: option to not include subjectOf | ||
|
||
const error = (message, showHelp) => { | ||
console.error(red(message)) | ||
if (showHelp) { program.help() } | ||
process.exit(1) | ||
} | ||
|
||
function searchCommand (query) { | ||
return suggest({search: query.join(" ")}) | ||
.then(suggest => { | ||
console.log(suggest) | ||
}) | ||
} | ||
|
||
function conceptCommand (ids) { | ||
const language = program.lang || "en" | ||
|
||
getMappingSchemes() | ||
.then(schemes => new WikidataService(schemes)) | ||
.then(service => getConcepts({ uri: ids.join("|"), language })) | ||
.then(ndjson.write) | ||
} | ||
|
||
async function getMappingSchemes() { | ||
if (program.force) { | ||
return updateMappingSchemes() | ||
} else { | ||
return Promise.resolve(loadMappingSchemes()) | ||
} | ||
} | ||
|
||
function schemesCommand () { | ||
getMappingSchemes().then(ndjson.write) | ||
} | ||
|
||
function mappingsCommand (from, to) { | ||
let direction = program.both || to === undefined ? "both" : "forward" | ||
|
||
if (from === "-") from = undefined | ||
if (to === "-") to = undefined | ||
|
||
let languages = program.lang | ||
let toScheme = (from || direction !== "forward") ? program.scheme : null | ||
getMappingSchemes() | ||
.then(schemes => new WikidataService(schemes)) | ||
.then(service => service.getMappings({ from, to, toScheme, direction, languages })) | ||
.then(ndjson.write) | ||
.catch(e => error(e.message)) | ||
} | ||
|
||
function mapMappingCommand(mapping) { | ||
const { simplify } = program | ||
|
||
return getMappingSchemes() | ||
.then(schemes => new WikidataService(schemes)) | ||
.then(service => { | ||
try { | ||
return service.mapMapping(mapping, { simplify }) | ||
} catch(e) { | ||
error(e) | ||
} | ||
}) | ||
} | ||
|
||
program | ||
.command("find <query...>").alias("f") | ||
.description("search an entity") | ||
.action(searchCommand) | ||
|
||
program | ||
.command("concept <id...>").alias("c") | ||
.description("get Wikidata entity as JSKOS concept") | ||
.action(conceptCommand) | ||
|
||
program | ||
.command("schemes").alias("s") | ||
.description("get mapping properties as concept schemes") | ||
.action(schemesCommand) | ||
|
||
program | ||
.command("update").alias("u") | ||
.description("update mapping properties from Wikidata") | ||
.action(updateMappingSchemes) | ||
|
||
program | ||
.command("mappings [from] [to]").alias("m") | ||
.description("get mappings between Wikidata and other schemes") | ||
.action(mappingsCommand) | ||
|
||
program | ||
.command("mapping-item [file]").alias("i") | ||
.description("convert a JSKOS mapping to a Wikidata item") | ||
.action((file) => { | ||
const json = readFileSync(file || 0, "utf-8") | ||
const mapping = JSON.parse(json) | ||
|
||
mapMappingCommand(mapping) | ||
.then(item => console.log(JSON.stringify(item, null, " "))) | ||
}) | ||
|
||
program | ||
.command("add-mapping [file]").alias("a") | ||
.description("add or update a mapping (not implemented yet)") | ||
.action((file) => { | ||
const json = readFileSync(file || 0, "utf-8") | ||
const mapping = JSON.parse(json) | ||
|
||
mapMappingCommand(mapping).then(item => { | ||
if (!item.uri) { | ||
// TODO: check whether statement exists | ||
} | ||
|
||
console.log(JSON.stringify(item, null, " ")) | ||
// TODO: validate, check whether uri exists | ||
// then upateMapping, else insertMapping | ||
|
||
}) | ||
}) | ||
|
||
program | ||
.on("command:*", () => { | ||
const arg = program.args[0] | ||
if (wdk.isEntityId(arg)) { | ||
conceptCommand([arg]) | ||
} else { | ||
error(`unknown command '${arg}'`, true) | ||
} | ||
}) | ||
|
||
// execute | ||
program.parse(process.argv) | ||
|
||
// Show help by default | ||
if (!program.args.length) program.help() | ||
|
||
// Prevent logging an EPIPE error when piping the output | ||
process.stdout.on("error", err => { | ||
if (err.code !== "EPIPE") throw err | ||
}) | ||
#!/usr/bin/env node | ||
|
||
import { Command } from "commander" | ||
import { readFileSync } from "fs" | ||
import pkg from "../package.json" with {type: 'json'} | ||
import chalk from "chalk" | ||
import wdk from "../lib/wdk.js" | ||
import { WikidataService } from "../lib/wikidata-wrapper.js" | ||
import * as ndjson from "../lib/ndjson.js" | ||
import suggest from "../lib/suggest.js" | ||
import { loadMappingSchemes, updateMappingSchemes } from "../lib/mapping-schemes.js" | ||
import getConcepts from "../lib/queries/get-concepts.js" | ||
|
||
const program = new Command(); | ||
|
||
program | ||
.version(pkg.version) | ||
.description("Access Wikidata in JSKOS format") | ||
.option("-l, --lang <lang>", "language(s), comma-separated") | ||
.option("-s, --scheme <id>", "limit mappings to selected scheme") | ||
.option("-i, --simplify", "simplify Wikidata JSON") | ||
.option("-f, --force", "force reloading mapping schemes") | ||
.option("-b, --both", "include both mapping directions") | ||
|
||
// TODO: option to not include subjectOf | ||
|
||
const error = (message, showHelp) => { | ||
console.error(chalk.red(message)) | ||
if (showHelp) { program.help() } | ||
process.exit(1) | ||
} | ||
|
||
function searchCommand (query) { | ||
return suggest({search: query.join(" ")}) | ||
.then(suggest => { | ||
console.log(suggest) | ||
}) | ||
} | ||
|
||
function conceptCommand (ids) { | ||
const language = program.lang || "en" | ||
|
||
getMappingSchemes() | ||
.then(schemes => new WikidataService(schemes)) | ||
.then(service => getConcepts({ uri: ids.join("|"), language })) | ||
.then(ndjson.write) | ||
} | ||
|
||
async function getMappingSchemes() { | ||
if (program.force) { | ||
return updateMappingSchemes() | ||
} else { | ||
return Promise.resolve(loadMappingSchemes()) | ||
} | ||
} | ||
|
||
function schemesCommand () { | ||
getMappingSchemes().then(ndjson.write) | ||
} | ||
|
||
function mappingsCommand (from, to) { | ||
let direction = program.both || to === undefined ? "both" : "forward" | ||
|
||
if (from === "-") from = undefined | ||
if (to === "-") to = undefined | ||
|
||
let languages = program.lang | ||
let toScheme = (from || direction !== "forward") ? program.scheme : null | ||
getMappingSchemes() | ||
.then(schemes => new WikidataService(schemes)) | ||
.then(service => service.getMappings({ from, to, toScheme, direction, languages })) | ||
.then(ndjson.write) | ||
.catch(e => error(e.message)) | ||
} | ||
|
||
function mapMappingCommand(mapping) { | ||
const { simplify } = program | ||
|
||
return getMappingSchemes() | ||
.then(schemes => new WikidataService(schemes)) | ||
.then(service => { | ||
try { | ||
return service.mapMapping(mapping, { simplify }) | ||
} catch(e) { | ||
error(e) | ||
} | ||
}) | ||
} | ||
|
||
program | ||
.command("find <query...>").alias("f") | ||
.description("search an entity") | ||
.action(searchCommand) | ||
|
||
program | ||
.command("concept <id...>").alias("c") | ||
.description("get Wikidata entity as JSKOS concept") | ||
.action(conceptCommand) | ||
|
||
program | ||
.command("schemes").alias("s") | ||
.description("get mapping properties as concept schemes") | ||
.action(schemesCommand) | ||
|
||
program | ||
.command("update").alias("u") | ||
.description("update mapping properties from Wikidata") | ||
.action(updateMappingSchemes) | ||
|
||
program | ||
.command("mappings [from] [to]").alias("m") | ||
.description("get mappings between Wikidata and other schemes") | ||
.action(mappingsCommand) | ||
|
||
program | ||
.command("mapping-item [file]").alias("i") | ||
.description("convert a JSKOS mapping to a Wikidata item") | ||
.action((file) => { | ||
const json = readFileSync(file || 0, "utf-8") | ||
const mapping = JSON.parse(json) | ||
|
||
mapMappingCommand(mapping) | ||
.then(item => console.log(JSON.stringify(item, null, " "))) | ||
}) | ||
|
||
program | ||
.command("add-mapping [file]").alias("a") | ||
.description("add or update a mapping (not implemented yet)") | ||
.action((file) => { | ||
const json = readFileSync(file || 0, "utf-8") | ||
const mapping = JSON.parse(json) | ||
|
||
mapMappingCommand(mapping).then(item => { | ||
if (!item.uri) { | ||
// TODO: check whether statement exists | ||
} | ||
|
||
console.log(JSON.stringify(item, null, " ")) | ||
// TODO: validate, check whether uri exists | ||
// then upateMapping, else insertMapping | ||
|
||
}) | ||
}) | ||
|
||
program | ||
.on("command:*", () => { | ||
const arg = program.args[0] | ||
if (wdk.isEntityId(arg)) { | ||
conceptCommand([arg]) | ||
} else { | ||
error(`unknown command '${arg}'`, true) | ||
} | ||
}) | ||
|
||
// execute | ||
program.parse(process.argv) | ||
|
||
// Show help by default | ||
if (!program.args.length) program.help() | ||
|
||
// Prevent logging an EPIPE error when piping the output | ||
process.stdout.on("error", err => { | ||
if (err.code !== "EPIPE") throw err | ||
}) |