Last active
May 7, 2020 11:17
-
-
Save kigiri/677414277477f52b0c9ea016501a2cdb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileStr } from 'https://deno.land/[email protected]/fs/read_file_str.ts' | |
import { writeFileStr } from 'https://deno.land/[email protected]/fs/write_file_str.ts' | |
const toUTF8 = buff => { | |
const decoder = new TextDecoder() | |
return decoder.decode(buff) | |
} | |
export const bootstrap = async ({ debug = false } = {}) => { | |
const pid = await readFileStr('.pid') | |
.catch(err => err instanceof Deno.errors.NotFound ? '' : Promise.reject(err)) | |
const log = debug ? console.log : () => {} | |
if (pid) { | |
const killProcess = await Deno.run({ | |
cmd: [ 'taskkill', '/F', '/PID', pid ], | |
stdout: 'piped', | |
stderr: 'piped', | |
}) | |
const killStatus = await killProcess.status() | |
if (!killStatus.success) { | |
const reason = toUTF8(await killProcess.stderrOutput()).trim() | |
if (reason.endsWith('not found.')) { | |
log(`pid [${pid}] not found`) | |
} else { | |
log(`Unable to kill [${pid}]: ${reason}`) | |
} | |
} else { | |
log(toUTF8(await killProcess.output()).trim()) | |
await Deno.remove('.pid') | |
} | |
} else { | |
log('nothing to kill') | |
} | |
await writeFileStr('.pid', String(Deno.pid)) | |
window.addEventListener('unload', async () => { | |
log('closing... cleanup .pid file') | |
await Deno.remove('.pid').catch(log) | |
}) | |
log('prev pid:', pid, Deno.pid) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment