forked from Wingysam/Christmas-Community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.js
43 lines (37 loc) · 1.16 KB
/
manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { exec } = require('child-process-promise')
const { spawn } = require('child_process')
const PACKAGENAME = 'get-product-name'
async function isOutdated () {
const command = `npm outdated ${PACKAGENAME} --json`
const npm = await exec(command)
const data = JSON.parse(npm.stdout)
return data[PACKAGENAME]?.current !== data[PACKAGENAME]?.wanted
}
async function updateGPD () {
// 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}`
await exec(command)
}
;(async () => {
let cc = null
function spawnCC () {
cc = spawn('node', ['index.js'], { env: process.env })
cc.on('exit', spawnCC)
cc.stdout.pipe(process.stdout)
cc.stderr.pipe(process.stderr)
}
if (process.env.UPDATE_GPD !== 'false') {
async function update () {
if (await isOutdated()) {
try {
await updateGPD()
} catch {}
if (cc) cc.kill('sigint')
cc.once('exit', () => console.log(`Updated ${PACKAGENAME}`))
}
}
update()
setInterval(update, 1000 * 60 * 60) // 1 hour
}
spawnCC()
})()