-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
'use strict'; | ||
const { | ||
ArrayPrototypeJoin, | ||
ArrayPrototypeMap, | ||
ArrayPrototypePushApply, | ||
ArrayPrototypeReduce, | ||
ArrayPrototypeSlice, | ||
ArrayPrototypeSome, | ||
StringPrototypeSplit, | ||
StringPrototypeStartsWith, | ||
} = primordials; | ||
const { | ||
prepareMainThreadExecution, | ||
markBootstrapComplete | ||
} = require('internal/process/pre_execution'); | ||
const { getOptionValue } = require('internal/options'); | ||
const { green, blue, red, white } = require('internal/util/colors'); | ||
|
||
const { spawn } = require('child_process'); | ||
const { watch } = require('fs/promises'); | ||
const { inspect } = require('util'); | ||
const { setTimeout, clearTimeout } = require('timers'); | ||
const { dirname, sep, resolve } = require('path'); | ||
const { once } = require('events'); | ||
|
||
|
||
prepareMainThreadExecution(false); | ||
markBootstrapComplete(); | ||
|
||
// TODO(MoLow): Make kill signal configurable | ||
const kKillSignal = 'SIGTERM'; | ||
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path)); | ||
const kCommand = ArrayPrototypeSlice(process.argv, 1); | ||
const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' ')); | ||
const args = ArrayPrototypeReduce(process.execArgv, (acc, flag, i, arr) => { | ||
if (arr[i] !== '--watch-path' && arr[i - 1] !== '--watch-path' && arr[i] !== '--watch') { | ||
acc.push(arr[i]); | ||
} | ||
return acc; | ||
}, []); | ||
ArrayPrototypePushApply(args, kCommand); | ||
|
||
function isWatchedFile(filename) { | ||
if (kWatchedPaths.length > 0) { | ||
return ArrayPrototypeSome(kWatchedPaths, (path) => StringPrototypeStartsWith(filename, path)); | ||
} | ||
|
||
const directory = dirname(filename); | ||
if (directory === '.') { | ||
return true; | ||
} | ||
|
||
const dirs = StringPrototypeSplit(directory, sep); | ||
return !ArrayPrototypeSome(dirs, (dir) => dir[0] === '.' || dir === 'node_modules'); | ||
} | ||
|
||
function debounce(fn, duration = 100) { | ||
let timeout; | ||
return () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
|
||
timeout = setTimeout(fn, duration).unref(); | ||
}; | ||
} | ||
|
||
function exitHandler(code) { | ||
if (code === 0) { | ||
process.stdout.write(`${blue}Completed running ${kCommandStr}${white}\n`); | ||
} else { | ||
process.stdout.write(`${red}Failed running ${kCommandStr}${white}\n`); | ||
} | ||
} | ||
|
||
let graceTimer; | ||
function reportGracefulTermination() { | ||
let reported = false; | ||
clearTimeout(graceTimer); | ||
graceTimer = setTimeout(() => { | ||
reported = true; | ||
process.stdout.write(`${blue}Waiting for graceful termination${white}\n`); | ||
}, 1000).unref(); | ||
return () => { | ||
clearTimeout(graceTimer); | ||
if (reported) { | ||
process.stdout.write(`${green}gracefully terminated${white}\n`); | ||
} | ||
}; | ||
} | ||
|
||
|
||
let childProcess; | ||
async function run(restarting) { | ||
if (restarting) { | ||
process.stdout.write('\u001Bc'); | ||
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`); | ||
} | ||
if (childProcess && !childProcess.killed) { | ||
childProcess.removeListener('exit', exitHandler); | ||
const onExit = once(childProcess, 'exit'); | ||
const clearGraceReport = reportGracefulTermination(); | ||
childProcess.kill(kKillSignal); | ||
await onExit; | ||
clearGraceReport(); | ||
} | ||
|
||
childProcess = spawn(process.execPath, args, { stdio: ['inherit', 'inherit', 'inherit'] }); | ||
childProcess.once('exit', exitHandler); | ||
} | ||
|
||
const restart = debounce(() => run(true)); | ||
|
||
(async () => { | ||
run(); | ||
const watcher = watch(process.cwd(), { recursive: true }); | ||
for await (const event of watcher) { | ||
if (isWatchedFile(resolve(event.filename))) { | ||
restart(); | ||
} | ||
} | ||
})(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
blue: '', | ||
green: '', | ||
white: '', | ||
red: '', | ||
refresh() { | ||
if (process.stderr.isTTY && process.stderr.hasColors()) { | ||
module.exports.blue = '\u001b[34m'; | ||
module.exports.green = '\u001b[32m'; | ||
module.exports.white = '\u001b[39m'; | ||
module.exports.red = '\u001b[31m'; | ||
} | ||
} | ||
}; | ||
|
||
module.exports.refresh(); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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