diff --git a/bin/cmd.js b/bin/cmd.js index 652ec9c..c7ccc8d 100644 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -3,8 +3,9 @@ import WikibaseRepo from './../lib/repo.js' import { pickLanguages, pickKeys } from './../lib/util.js' import { execCmd } from './../lib/exec.js' +import { listen } from './../lib/signals.js' -;(async () => { +Promise.race([listen('SIGINT', 'SIGTERM', 'SIGQUIT'), (async () => { const args = process.argv.slice(2) if (args.length < 3) { throw new Error( @@ -37,10 +38,9 @@ import { execCmd } from './../lib/exec.js' await targetRepo.createEntities(...data) - return `Sucessfully transferred ${entities.length} entities from ${source} to ${target}.` -})() + console.log(`Sucessfully transferred ${entities.length} entities from ${source} to ${target}.`) +})()]) .then((result) => { - console.log(result) process.exitCode = 0 }) .catch((err) => { diff --git a/lib/signals.js b/lib/signals.js new file mode 100644 index 0000000..1b20e76 --- /dev/null +++ b/lib/signals.js @@ -0,0 +1,9 @@ +export const listen = (...signals) => { + return Promise.race(signals.map((s) => { + return new Promise((resolve, reject) => { + process.on(s, () => { + reject(new Error(`Received ${s}, program will exit before finishing.`)) + }) + }) + })) +}