Skip to content

Commit 979b1ed

Browse files
committed
Chore: improve watch script
1 parent 412f2bc commit 979b1ed

File tree

4 files changed

+119
-5
lines changed

4 files changed

+119
-5
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
"@types/mocha": "^2.2.41",
2828
"@types/node": "^6.0.85",
2929
"babel-eslint": "^7.1.1",
30-
"chokidar-cli": "^1.2.0",
30+
"chokidar": "^1.7.0",
3131
"codecov": "^2.1.0",
32+
"cross-spawn": "^5.1.0",
3233
"dts-bundle": "^0.7.3",
3334
"eslint": "^4.1.1",
3435
"eslint-config-mysticatea": "^11.0.0",
@@ -58,16 +59,16 @@
5859
"pretest": "run-s build lint",
5960
"test": "nyc npm run _mocha",
6061
"preupdate-fixtures": "npm run -s build",
61-
"update-fixtures": "node test/tools/update-template-ast.js",
62+
"update-fixtures": "node test/tools/update-fixtures-ast.js",
6263
"preversion": "npm test",
6364
"version": "npm run -s build",
6465
"postversion": "git push && git push --tags",
6566
"prewatch": "npm run -s clean",
6667
"watch": "run-p watch:*",
6768
"watch:tsc": "tsc --watch",
6869
"watch:rollup": "wait-on .temp/index.js && rollup -c -o index.js --watch",
69-
"watch:test": "wait-on index.js && chokidar index.js \"test/*.js\" \"test/fixtures/ast/*/*.json\" \"test/fixtures/*\" --command \"nyc -r lcov npm run -s _mocha\"",
70-
"watch:update-ast": "wait-on index.js && chokidar index.js \"test/fixtures/ast/*/*.vue\" --command \"node test/tools/update-template-ast.js\"",
70+
"watch:test": "wait-on index.js && node test/tools/watch.js index.js \"test/*.js\" \"test/fixtures/ast/*/*.json\" \"test/fixtures/*\" -- nyc -r lcov npm run -s _mocha",
71+
"watch:update-ast": "wait-on index.js && node test/tools/watch.js index.js \"test/fixtures/ast/*/*.vue\" -- node test/tools/update-fixtures-ast.js",
7172
"watch:coverage-report": "wait-on coverage/lcov-report/index.html && opener coverage/lcov-report/index.html"
7273
},
7374
"repository": {

test/tools/.eslintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": ["mysticatea", "mysticatea/node"]
2+
"extends": ["mysticatea", "mysticatea/node"],
3+
"rules": {
4+
"no-console": "off"
5+
}
36
}
File renamed without changes.

test/tools/watch.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)