Skip to content
This repository was archived by the owner on Dec 19, 2024. It is now read-only.

Commit 502c69c

Browse files
committed
Fixed: CLI --watch doesn't make some duplicate rebuild anymore. (1.3.1)
1 parent 7babec6 commit 502c69c

File tree

5 files changed

+110
-78
lines changed

5 files changed

+110
-78
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.3.1 - 2015-05-01
2+
3+
- Fixed: CLI `--watch` doesn't make some duplicate rebuild anymore.
4+
15
# 1.3.0 - 2015-04-15
26

37
- Added: hexadecimal fallback for rgba() color

bin/cssnext.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ if (input && !fs.existsSync(input)) {
8383
}
8484

8585
config.from = input
86-
8786
// init & adjust watcher with postcss-import dependencies
8887
var watcher
8988
if (config.watch) {
@@ -104,11 +103,18 @@ if (config.watch) {
104103
})
105104
}
106105

106+
const rebaseFile = function(file) {
107+
return path.relative(process.cwd(), file)
108+
}
109+
107110
var watcherOnImport = function(imported) {
108-
var filesToUnWatch = arrayDiff(importedFiles, imported)
109-
watcher.unwatch(filesToUnWatch)
111+
arrayDiff(importedFiles, imported).forEach(function(file) {
112+
watcher.unwatch(rebaseFile(file))
113+
})
114+
arrayDiff(imported, importedFiles).forEach(function(file) {
115+
watcher.add(rebaseFile(file))
116+
})
110117
importedFiles = imported
111-
watcher.add(importedFiles)
112118
}
113119

114120
// import need an object so we can pass onImport() cb
@@ -171,7 +177,8 @@ if (watcher) {
171177
if (verbose) {
172178
log(colors.cyan("Watching"), input)
173179
}
174-
watcher.on("all", transform)
180+
181+
watcher.on("change", transform)
175182
})
176183
}
177184

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cssnext",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "Use tomorrow's CSS syntax, today",
55
"keywords": [
66
"css",

test/cli.js

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Test dependencies
33
*/
44
var exec = require("child_process").exec
5-
var fs = require("fs")
65

76
var test = require("tape")
87

@@ -127,76 +126,5 @@ test("cli", function(t) {
127126
})
128127
planned+=2
129128

130-
// I don't success to call the kill() process from node and both Travis CI and Appveyor
131-
// so we avoid this test on this environnements
132-
if (!process.env.TRAVIS && !process.env.APPVEYOR) {
133-
var watchProcess = exec(cssnextBin + " --watch test/fixtures/cli.error.css test/fixtures/cli.output--watch.css", function(err) {
134-
t.ok(err && err.signal === "SIGTERM", "should only be killed by an interrupt when `--watch` option passed")
135-
if (err && !err.killed) { throw err }
136-
})
137-
138-
var msgWatch = "should output error messages when `--watch` option passed"
139-
var watchTimeout = setTimeout(function() {
140-
t.fail(msgWatch)
141-
watchProcess.kill()
142-
}, 5000)
143-
watchProcess.stderr.on("data", function(data) {
144-
if (utils.contains(data, "encounters an error")) {
145-
t.pass(msgWatch)
146-
clearTimeout(watchTimeout)
147-
watchProcess.kill()
148-
}
149-
})
150-
planned+=2
151-
152-
// watch/import tests
153-
var watchOut = "test/fixtures/cli.output--watch-import.css"
154-
var watchImportProcess = exec(cssnextBin + " --watch test/fixtures/cli.watch-import.css " + watchOut, function(err) {
155-
if (err && !err.killed) { throw err }
156-
})
157-
158-
// watch an empty file doesn't seems to work great, so I am using
159-
// /**/ to get a content
160-
// yeah...
161-
162-
// trigger a change in cli.import.css to add a new watched file cli.import2.css
163-
fs.writeFileSync("test/fixtures/cli.watch-import.css", "/**/ @import 'cli.watch-import-import.css';")
164-
165-
// we are using setTimeout for the watcher to do his job
166-
setTimeout(function() {
167-
// check the output has been updated (watcher works)
168-
t.equal(fs.readFileSync(watchOut, {encoding:"utf8"}), "/**/ watch{}", "should update the file")
169-
170-
// remove this newly imported file
171-
fs.writeFileSync("test/fixtures/cli.watch-import.css", "/**/")
172-
173-
// check the output has been update
174-
setTimeout(function() {
175-
t.equal(fs.readFileSync(watchOut, {encoding:"utf8"}), "/**/", "should update the file, again")
176-
177-
// previously imported file should not be watched anymore
178-
// to check that we read output mtime, modify the file that should not be watched
179-
// and check back that the output file has the same mtime
180-
var outStat = fs.statSync(watchOut)
181-
setTimeout(function() {
182-
// trigger a change in previously imported file
183-
var now = (new Date()).getTime()
184-
fs.utimesSync("test/fixtures/cli.watch-import-import.css", now, now)
185-
186-
setTimeout(function() {
187-
// this time, it should not trigger anything
188-
var outStatAfter = fs.statSync(watchOut)
189-
t.equal(outStat.mtime.getTime(), outStatAfter.mtime.getTime(), "should not modify a file if a previously imported file is modified")
190-
191-
utils.remove("cli.output--watch-import")
192-
watchImportProcess.kill()
193-
}, 2000)
194-
}, 2000)
195-
}, 2000)
196-
}, 4000)
197-
198-
planned+=3
199-
}
200-
201129
t.plan(planned)
202130
})

test/cli.watcher.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Test dependencies
3+
*/
4+
var exec = require("child_process").exec
5+
var spawn = require("child_process").spawn
6+
var fs = require("fs")
7+
8+
var test = require("tape")
9+
10+
var utils = require("./utils")
11+
12+
var cssnextBin = "node bin/cssnext" // node bin is used to help for windows
13+
14+
test("cli/watcher", function(t) {
15+
var planned = 0
16+
17+
// I don't success to call the kill() process from node and both Travis CI and Appveyor
18+
// so we avoid this test on this environnements
19+
if (!process.env.TRAVIS && !process.env.APPVEYOR) {
20+
var watchProcess = exec(cssnextBin + " --watch test/fixtures/cli.error.css test/fixtures/cli.output--watch.css", function(err) {
21+
t.ok(err && err.signal === "SIGTERM", "should only be killed by an interrupt when `--watch` option passed")
22+
if (err && !err.killed) { throw err }
23+
})
24+
25+
var msgWatch = "should output error messages when `--watch` option passed"
26+
var watchTimeout = setTimeout(function() {
27+
t.fail(msgWatch)
28+
watchProcess.kill()
29+
}, 5000)
30+
watchProcess.stderr.on("data", function(data) {
31+
if (utils.contains(data, "encounters an error")) {
32+
t.pass(msgWatch)
33+
clearTimeout(watchTimeout)
34+
watchProcess.kill()
35+
}
36+
})
37+
planned+=2
38+
39+
// watch/import tests
40+
var watchOut = "test/fixtures/cli.output--watch-import.css"
41+
42+
var watchImportProcess = spawn("node", ["bin/cssnext", "--watch", "test/fixtures/cli.watch-import.css", watchOut], {stdio: "inherit"})
43+
44+
// watch an empty file doesn't seems to work great, so I am using
45+
// /**/ to get a content
46+
// yeah...
47+
48+
// trigger a change in cli.import.css to add a new watched file cli.import2.css
49+
fs.writeFileSync("test/fixtures/cli.watch-import.css", "/**/ @import 'cli.watch-import-import.css';")
50+
51+
// we are using setTimeout for the watcher to do his job
52+
setTimeout(function() {
53+
// check the output has been updated (watcher works)
54+
t.equal(fs.readFileSync(watchOut, {encoding:"utf8"}), "/**/ watch{}", "should update the file")
55+
56+
// remove this newly imported file
57+
fs.writeFileSync("test/fixtures/cli.watch-import.css", "/**/")
58+
59+
// check the output has been update
60+
setTimeout(function() {
61+
t.equal(fs.readFileSync(watchOut, {encoding:"utf8"}), "/**/", "should update the file, again")
62+
63+
setTimeout(function() {
64+
// previously imported file should not be watched anymore
65+
// to check that we read output mtime, modify the file that should not be watched
66+
// and check back that the output file has the same mtime
67+
68+
// trigger a change in previously imported file
69+
var now = (new Date()).getTime()
70+
fs.utimesSync("test/fixtures/cli.watch-import-import.css", now, now)
71+
72+
// not sure why but it's better with the statSync on the watched file in this delayed call
73+
setTimeout(function() {
74+
var outStat = fs.statSync(watchOut)
75+
76+
setTimeout(function() {
77+
// this time, it should not trigger anything
78+
var outStatAfter = fs.statSync(watchOut)
79+
t.equal(outStat.mtime.getTime(), outStatAfter.mtime.getTime(), "should not modify a file if a previously imported file is modified")
80+
81+
utils.remove("cli.output--watch-import")
82+
watchImportProcess.kill()
83+
}, 1000)
84+
}, 1000)
85+
}, 1000)
86+
}, 1000)
87+
}, 1000)
88+
89+
planned+=3
90+
}
91+
92+
t.plan(planned)
93+
})

0 commit comments

Comments
 (0)