|
| 1 | +/** |
| 2 | + * @author Toru Nagashima <https://github.com/mysticatea> |
| 3 | + * @copyright 2017 Toru Nagashima. All rights reserved. |
| 4 | + * See LICENSE file in root directory for full license. |
| 5 | + */ |
| 6 | +"use strict" |
| 7 | + |
| 8 | +const readline = require("readline") |
| 9 | +const chokidar = require("chokidar") |
| 10 | +const spawn = require("cross-spawn") |
| 11 | +const lodash = require("lodash") |
| 12 | + |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | +// Parse arguments |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +const args = (() => { |
| 18 | + const allArgs = process.argv.slice(2) |
| 19 | + const i = allArgs.indexOf("--") |
| 20 | + return { |
| 21 | + patterns: allArgs.slice(0, i), |
| 22 | + command: allArgs[i + 1], |
| 23 | + arguments: allArgs.slice(i + 2), |
| 24 | + } |
| 25 | +})() |
| 26 | + |
| 27 | +//------------------------------------------------------------------------------ |
| 28 | +// Normalize SIGINT |
| 29 | +//------------------------------------------------------------------------------ |
| 30 | + |
| 31 | +if (process.platform === "win32") { |
| 32 | + let rl = null |
| 33 | + process.on("newListener", (type) => { |
| 34 | + if (type === "SIGINT" && process.listenerCount("SIGINT") === 1) { |
| 35 | + rl = readline.createInterface({ |
| 36 | + input: process.stdin, |
| 37 | + output: process.stdout, |
| 38 | + }) |
| 39 | + rl.on("SIGINT", process.emit.bind(process, "SIGINT")) |
| 40 | + } |
| 41 | + }) |
| 42 | + process.on("removeListener", (type) => { |
| 43 | + if (type === "SIGINT" && rl && process.listenerCount("SIGINT") === 0) { |
| 44 | + rl.close() |
| 45 | + rl = null |
| 46 | + } |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +//------------------------------------------------------------------------------ |
| 51 | +// Define exec. |
| 52 | +//------------------------------------------------------------------------------ |
| 53 | + |
| 54 | +let running = false |
| 55 | +let dirty = false |
| 56 | +const requestCommand = lodash.debounce(() => { |
| 57 | + if (running) { |
| 58 | + dirty = true |
| 59 | + return |
| 60 | + } |
| 61 | + running = true |
| 62 | + |
| 63 | + /** |
| 64 | + * Finalize. |
| 65 | + * @param {any} x The exit code or error object. |
| 66 | + * @returns {void} |
| 67 | + */ |
| 68 | + function done(x) { |
| 69 | + running = false |
| 70 | + if (dirty) { |
| 71 | + dirty = false |
| 72 | + requestCommand() |
| 73 | + } |
| 74 | + if (x instanceof Error) { |
| 75 | + console.error("FAILED TO EXEC:", x.message) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + spawn(args.command, args.arguments, {stdio: "inherit"}) |
| 80 | + .on("exit", done) |
| 81 | + .on("error", done) |
| 82 | +}, 1000) |
| 83 | + |
| 84 | +//------------------------------------------------------------------------------ |
| 85 | +// Setup watcher. |
| 86 | +//------------------------------------------------------------------------------ |
| 87 | + |
| 88 | +const watcher = chokidar.watch(args.patterns, {ignoreInitial: true}) |
| 89 | +watcher.on("all", (event, path) => { |
| 90 | + console.log(`${event}:${path}`) |
| 91 | + requestCommand() |
| 92 | +}) |
| 93 | +watcher.on("error", (error) => { |
| 94 | + console.error("Error:", error) |
| 95 | + console.error(error.stack) |
| 96 | +}) |
| 97 | + |
| 98 | +watcher.once("ready", () => { |
| 99 | + const list = args.patterns.join("\", \"") |
| 100 | + console.log("Watching", `"${list}" ..`) |
| 101 | +}) |
| 102 | + |
| 103 | +//------------------------------------------------------------------------------ |
| 104 | +// Setup SIGINT |
| 105 | +//------------------------------------------------------------------------------ |
| 106 | + |
| 107 | +process.on("SIGINT", () => { |
| 108 | + console.log("<<SIGINT>> $", args.command, args.arguments.join(" ")) |
| 109 | + watcher.close() |
| 110 | +}) |
0 commit comments