From de0c48c76132f7942eee58db1910c34c88aa7398 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Tue, 6 Aug 2024 17:01:05 +0200 Subject: [PATCH] refactor: move cancellation to top level --- bin/cmd.js | 70 ++++++++++++++++++++++++++++---------------------- lib/signals.js | 6 ++--- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index c7ccc8d..13ca0d7 100644 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -3,43 +3,51 @@ 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' +import { trap } from './../lib/signals.js' -Promise.race([listen('SIGINT', 'SIGTERM', 'SIGQUIT'), (async () => { - const args = process.argv.slice(2) - if (args.length < 3) { - throw new Error( - `Expected at least 3 positional arguments to be given, but got ${args.length}, cannot continue.` - ) - } - const [source, target, ...entities] = args - - const oauth = { - consumer_key: process.env.TARGET_WIKI_OAUTH_CONSUMER_TOKEN, - consumer_secret: process.env.TARGET_WIKI_OAUTH_CONSUMER_SECRET, - token: process.env.TARGET_WIKI_OAUTH_ACCESS_TOKEN, - token_secret: process.env.TARGET_WIKI_OAUTH_ACCESS_SECRET - } - if (!Object.values(oauth).every(v => v)) { - throw new Error( - 'Unable to find full set of OAuth credentials in environment variables, cannot continue.' - ) - } +Promise.race([ + (async () => { + const args = process.argv.slice(2) + if (args.length < 3) { + throw new Error( + `Expected at least 3 positional arguments to be given, but got ${args.length}, cannot continue.` + ) + } + const [source, target, ...entities] = args - const sourceRepo = new WikibaseRepo(source) - const targetRepo = new WikibaseRepo(target, { oauth }) + const oauth = { + consumer_key: process.env.TARGET_WIKI_OAUTH_CONSUMER_TOKEN, + consumer_secret: process.env.TARGET_WIKI_OAUTH_CONSUMER_SECRET, + token: process.env.TARGET_WIKI_OAUTH_ACCESS_TOKEN, + token_secret: process.env.TARGET_WIKI_OAUTH_ACCESS_SECRET + } + if (!Object.values(oauth).every(v => v)) { + throw new Error( + 'Unable to find full set of OAuth credentials in environment variables, cannot continue.' + ) + } + + const sourceRepo = new WikibaseRepo(source) + const targetRepo = new WikibaseRepo(target, { oauth }) - const contentLanguages = await targetRepo.getContentLanguages() + const contentLanguages = await targetRepo.getContentLanguages() - let data = await sourceRepo.getEntities(...entities) - data = data - .map(e => pickKeys(e, 'type', 'labels', 'descriptions', 'aliases', 'datatype')) - .map(e => pickLanguages(e, ...contentLanguages)) + let data = await sourceRepo.getEntities(...entities) + data = data + .map(e => pickKeys(e, 'type', 'labels', 'descriptions', 'aliases', 'datatype')) + .map(e => pickLanguages(e, ...contentLanguages)) - await targetRepo.createEntities(...data) + await targetRepo.createEntities(...data) - console.log(`Sucessfully transferred ${entities.length} entities from ${source} to ${target}.`) -})()]) + console.log( + `Sucessfully transferred ${entities.length} entities from ${source} to ${target}.` + ) + })(), + (async () => { + const signal = await trap('SIGINT', 'SIGTERM', 'SIGQUIT') + throw new Error(`Received ${signal}, program will exit before finishing.`) + })() +]) .then((result) => { process.exitCode = 0 }) diff --git a/lib/signals.js b/lib/signals.js index 1b20e76..cbac111 100644 --- a/lib/signals.js +++ b/lib/signals.js @@ -1,9 +1,7 @@ -export const listen = (...signals) => { +export const trap = (...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.`)) - }) + process.on(s, () => resolve(s)) }) })) }