-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelecterm.home.watch.js
53 lines (48 loc) · 1.44 KB
/
electerm.home.watch.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
44
45
46
47
48
49
50
51
52
53
import fs from 'fs'
import { exec } from 'child_process'
import { resolve } from 'path'
import { cwd } from './bin/common.js'
const filePath = resolve(cwd, 'data/electerm-github-release.json') // Replace with the actual path to the file
const commandToRun = 'npm run b' // Replace with the command you want to run
function checkVersion () {
try {
const d = JSON.parse(fs.readFileSync(filePath, 'utf8'))
const tag = d.release.name.toLowerCase()
return d.action === 'published' &&
!tag.includes('b') &&
!tag.includes('a') &&
d.release.assets &&
d.release.assets.length > 0 &&
d.release.body &&
d.release.body.length > 10
} catch (err) {
console.error('Failed to load file:', err)
}
return false
}
// Watch for changes in the file
fs.watchFile(filePath, (curr, prev) => {
if (curr.mtime !== prev.mtime) {
console.log(new Date(), 'File has changed. Running command...')
if (checkVersion()) {
executeCommand(commandToRun)
} else {
console.log('it is beta, no need to do anything')
}
}
})
// Execute the specified command
function executeCommand (command) {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`)
return
}
if (stderr) {
console.error(`Command stderr: ${stderr}`)
return
}
console.log(`Command output: ${stdout}`)
})
}
console.log(`Watching file: ${filePath}`)