christmas/src/manager.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-11-02 16:07:12 -08:00
const { exec } = require('child-process-promise')
const { spawn } = require('child_process')
const PACKAGENAME = 'get-product-name'
2020-11-08 13:54:08 -08:00
async function isOutdated () {
const command = `npm outdated ${PACKAGENAME} --json || true`
2020-11-02 16:07:12 -08:00
const npm = await exec(command)
const data = JSON.parse(npm.stdout)
return data[PACKAGENAME]?.current !== data[PACKAGENAME]?.wanted
}
2020-11-08 13:54:08 -08:00
async function updateGPD () {
2020-11-05 10:50:20 -08:00
// https://blog.cloud66.com/using-node-with-docker/
const command = `mv ./node_modules ./node_modules.tmp && mv ./node_modules.tmp ./node_modules && npm update ${PACKAGENAME}`
2020-11-02 16:07:12 -08:00
await exec(command)
}
;(async () => {
let cc = null
2020-11-08 13:54:08 -08:00
function spawnCC () {
cc = spawn('node', ['src/index.js'], { env: process.env })
2020-11-02 16:07:12 -08:00
cc.on('exit', spawnCC)
cc.stdout.pipe(process.stdout)
cc.stderr.pipe(process.stderr)
}
if (process.env.UPDATE_GPD !== 'false') {
2020-11-08 13:54:08 -08:00
async function update () {
2020-11-02 16:07:12 -08:00
if (await isOutdated()) {
2020-11-05 10:50:20 -08:00
try {
await updateGPD()
} catch {}
2020-11-02 16:07:12 -08:00
if (cc) cc.kill('sigint')
cc.once('exit', () => console.log(`Updated ${PACKAGENAME}`))
}
}
update()
setInterval(update, 1000 * 60 * 60) // 1 hour
}
2020-11-08 13:54:08 -08:00
2020-11-02 16:07:12 -08:00
spawnCC()
})()