-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
5 changed files
with
123 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict'; | ||
const { | ||
ArrayPrototypeConcat, | ||
ArrayPrototypeMap, | ||
ArrayPrototypeReduce, | ||
ArrayPrototypeSlice, | ||
ArrayPrototypeSome, | ||
StringPrototypeSplit, | ||
StringPrototypeStartsWith, | ||
} = primordials; | ||
const { | ||
prepareMainThreadExecution, | ||
markBootstrapComplete | ||
} = require('internal/process/pre_execution'); | ||
const { getOptionValue } = require('internal/options'); | ||
|
||
const { spawn } = require('child_process'); | ||
const { watch } = require('fs/promises'); | ||
const { setTimeout, clearTimeout } = require('timers'); | ||
const { dirname, sep, resolve } = require('path'); | ||
|
||
|
||
prepareMainThreadExecution(false); | ||
markBootstrapComplete(); | ||
|
||
const kWatchedFiles = ArrayPrototypeMap(getOptionValue('--watch-file'), (file) => resolve(file)); | ||
const args = ArrayPrototypeReduce(ArrayPrototypeConcat( | ||
ArrayPrototypeSlice(process.execArgv), | ||
ArrayPrototypeSlice(process.argv, 1), | ||
), (acc, flag, i, arr) => { | ||
if (arr[i] !== '--watch-file' && arr[i - 1] !== '--watch-file' && arr[i] !== '--watch') { | ||
acc.push(arr[i]); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
function isWatchedFile(filename) { | ||
if (kWatchedFiles.length > 0) { | ||
return ArrayPrototypeSome(kWatchedFiles, (file) => StringPrototypeStartsWith(filename, file)); | ||
} | ||
|
||
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(); | ||
}; | ||
} | ||
|
||
let childProcess; | ||
function run(restarting) { | ||
if (childProcess && !childProcess.killed) { | ||
childProcess.kill(); | ||
} | ||
if (restarting) { | ||
process.stdout.write('\u001Bc'); | ||
process.stdout.write('\u001b[32mrestarting process\u001b[39m\n'); | ||
} | ||
|
||
childProcess = spawn(process.execPath, args, { stdio: ['inherit', 'inherit', 'inherit'] }); | ||
} | ||
|
||
const restart = debounce(run.bind(null, 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
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