Skip to content

Commit

Permalink
refactor: move cancellation to top level
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Aug 6, 2024
1 parent 214be4d commit de0c48c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
70 changes: 39 additions & 31 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
6 changes: 2 additions & 4 deletions lib/signals.js
Original file line number Diff line number Diff line change
@@ -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))
})
}))
}

0 comments on commit de0c48c

Please sign in to comment.