Skip to content

Commit

Permalink
refactor: run user callbacks from within node
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Aug 1, 2024
1 parent 5b368bb commit 0a6e132
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ RUN apk add --no-cache curl=8.9.0-r0

COPY . /pkg
RUN npm i -g /pkg
COPY --chmod=755 docker-entrypoint.sh /usr/bin/docker-entrypoint

ENTRYPOINT ["docker-entrypoint"]
ENTRYPOINT ["transferbot"]
15 changes: 14 additions & 1 deletion bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import WikibaseRepo from './../lib/repo.js'
import { pickLanguages, pickKeys } from './../lib/util.js'
import { execCmd } from './../lib/exec.js'

;(async () => {
const [source, target, ...entities] = process.argv.slice(2)
Expand Down Expand Up @@ -32,5 +33,17 @@ import { pickLanguages, pickKeys } from './../lib/util.js'
})
.catch((err) => {
console.error(err)
process.exit(1)
process.exitCode = 1
})
.then(() => {
if (process.env.CALLBACK_ON_SUCCESS && process.exitCode === 0) {
return execCmd(process.env.CALLBACK_ON_SUCCESS)
}
if (process.env.CALLBACK_ON_FAILURE && process.exitCode !== 0) {
return execCmd(process.env.CALLBACK_ON_FAILURE)
}
})
.catch((err) => {
console.error('An error occurred executing the given callbacks:')
console.error(err)
})
17 changes: 0 additions & 17 deletions docker-entrypoint.sh

This file was deleted.

15 changes: 15 additions & 0 deletions lib/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { exec } from 'node:child_process'

export const execCmd = (cmd) => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
process.stdout.write(stdout)
process.stderr.write(stderr)
if (error) {
reject(error)
return
}
resolve()
})
})
}
4 changes: 2 additions & 2 deletions lib/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export default class WikibaseRepository {
.then(body => Object.keys(body.query.wbcontentlanguages))
}

async createEntities (...entities) {
createEntities (...entities) {
if (!this.edit) {
throw new Error('Cannot edit a read only instance.')
return Promise.reject(new Error('Cannot edit a read only instance.'))
}
return Promise.all(entities.map(async entity => {
return this.edit.entity.create(entity)
Expand Down

0 comments on commit 0a6e132

Please sign in to comment.