|
| 1 | +'use strict'; |
| 2 | +const { |
| 3 | + ArrayPrototypeConcat, |
| 4 | + ArrayPrototypeMap, |
| 5 | + ArrayPrototypeReduce, |
| 6 | + ArrayPrototypeSlice, |
| 7 | + ArrayPrototypeSome, |
| 8 | + StringPrototypeSplit, |
| 9 | + StringPrototypeStartsWith, |
| 10 | +} = primordials; |
| 11 | +const { |
| 12 | + prepareMainThreadExecution, |
| 13 | + markBootstrapComplete |
| 14 | +} = require('internal/process/pre_execution'); |
| 15 | +const { getOptionValue } = require('internal/options'); |
| 16 | + |
| 17 | +const { spawn } = require('child_process'); |
| 18 | +const { watch } = require('fs/promises'); |
| 19 | +const { setTimeout, clearTimeout } = require('timers'); |
| 20 | +const { dirname, sep, resolve } = require('path'); |
| 21 | + |
| 22 | + |
| 23 | +prepareMainThreadExecution(false); |
| 24 | +markBootstrapComplete(); |
| 25 | + |
| 26 | +const kWatchedFiles = ArrayPrototypeMap(getOptionValue('--watch-file'), (file) => resolve(file)); |
| 27 | +const args = ArrayPrototypeReduce(ArrayPrototypeConcat( |
| 28 | + ArrayPrototypeSlice(process.execArgv), |
| 29 | + ArrayPrototypeSlice(process.argv, 1), |
| 30 | +), (acc, flag, i, arr) => { |
| 31 | + if (arr[i] !== '--watch-file' && arr[i - 1] !== '--watch-file' && arr[i] !== '--watch') { |
| 32 | + acc.push(arr[i]); |
| 33 | + } |
| 34 | + return acc; |
| 35 | +}, []); |
| 36 | + |
| 37 | +function isWatchedFile(filename) { |
| 38 | + if (kWatchedFiles.length > 0) { |
| 39 | + return ArrayPrototypeSome(kWatchedFiles, (file) => StringPrototypeStartsWith(filename, file)); |
| 40 | + } |
| 41 | + |
| 42 | + const directory = dirname(filename); |
| 43 | + if (directory === '.') { |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + const dirs = StringPrototypeSplit(directory, sep); |
| 48 | + return !ArrayPrototypeSome(dirs, (dir) => dir[0] === '.' || dir === 'node_modules'); |
| 49 | +} |
| 50 | + |
| 51 | +function debounce(fn, duration = 100) { |
| 52 | + let timeout; |
| 53 | + return () => { |
| 54 | + if (timeout) { |
| 55 | + clearTimeout(timeout); |
| 56 | + } |
| 57 | + |
| 58 | + timeout = setTimeout(fn, duration).unref(); |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +let childProcess; |
| 63 | +function run(restarting) { |
| 64 | + if (childProcess && !childProcess.killed) { |
| 65 | + childProcess.kill(); |
| 66 | + } |
| 67 | + if (restarting) { |
| 68 | + process.stdout.write('\u001Bc'); |
| 69 | + process.stdout.write('\u001b[32mrestarting process\u001b[39m\n'); |
| 70 | + } |
| 71 | + |
| 72 | + childProcess = spawn(process.execPath, args, { stdio: ['inherit', 'inherit', 'inherit'] }); |
| 73 | +} |
| 74 | + |
| 75 | +const restart = debounce(run.bind(null, true)); |
| 76 | + |
| 77 | +(async () => { |
| 78 | + run(); |
| 79 | + const watcher = watch(process.cwd(), { recursive: true }); |
| 80 | + for await (const event of watcher) { |
| 81 | + if (isWatchedFile(resolve(event.filename))) { |
| 82 | + restart(); |
| 83 | + } |
| 84 | + } |
| 85 | +})(); |
0 commit comments