create watcher script to restart changed scripts
This commit is contained in:
parent
14b51dad96
commit
b7ae6c6464
@ -33,4 +33,4 @@ To update your Netscript Definitions, run `npm run defs` in a terminal
|
||||
Press F1 and Select `Bitburner: Enable File Watcher` to enable auto uploading to the game
|
||||
|
||||
## Todo
|
||||
Create a bitburner script which detects updates to files and restarts the running script automatically
|
||||
~~Create a bitburner script which detects updates to files and restarts the running script automatically~~
|
42
src/watcher.ts
Normal file
42
src/watcher.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { NS, ProcessInfo } from "../NetscriptDefinitions"
|
||||
|
||||
export async function main(ns : NS) : Promise<void> {
|
||||
const hashes : any = {}
|
||||
|
||||
const files = ns.ls('home', '.js')
|
||||
for ( const file of files ) {
|
||||
const contents = ns.read(file)
|
||||
hashes[file] = getHash(contents)
|
||||
}
|
||||
|
||||
while(true) {
|
||||
const files = ns.ls('home', '.js')
|
||||
|
||||
for(const file of files ) {
|
||||
const contents = ns.read(file)
|
||||
const hash = getHash(contents)
|
||||
|
||||
if(hash != hashes[file]) {
|
||||
ns.tprint(`INFO: Detected change in ${file}`)
|
||||
|
||||
const processes = ns.ps().filter((p : ProcessInfo) => {
|
||||
return p.filename == file
|
||||
})
|
||||
|
||||
for(const process of processes) {
|
||||
ns.tprint(`INFO: Restarting ${process.filename} ${process.args} -t ${process.threads}`)
|
||||
ns.kill(process.pid, ns.getHostname())
|
||||
ns.run(process.filename, process.threads, ...process.args)
|
||||
}
|
||||
|
||||
hashes[file] = hash
|
||||
}
|
||||
}
|
||||
|
||||
await ns.sleep(1000)
|
||||
}
|
||||
}
|
||||
|
||||
function getHash(input : string) : number {
|
||||
return input.split("").reduce( (a, b) => ((a << 5) - 1) + b.charCodeAt(0)|0, 0)
|
||||
}
|
Loading…
Reference in New Issue
Block a user