forked from graphql/graphql.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.js
110 lines (96 loc) · 2.71 KB
/
watch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
var fs = require('fs');
var path = require('path');
var sane = require('sane');
var build = require('./build');
var pwd = process.env.PWD;
var sourceDir = process.env.npm_package_site_source || './';
var buildDir = process.env.npm_package_site_build || path.join(sourceDir, '_build');
var SITE_ROOT = path.resolve(pwd, sourceDir);
var BUILD_ROOT = path.resolve(pwd, buildDir);
var buildWithinSite = path.relative(SITE_ROOT, BUILD_ROOT);
if (buildWithinSite[0] === '.') {
buildWithinSite = null;
} else if (buildWithinSite.indexOf('/') !== -1) {
// Note: minimatch would not be able to safely ignore watching this directory
// if nested deeply within source.
throw new Error('Cannot watch safely. Build deeply within Source');
}
module.exports = watch;
function watch() {
return build().then(() => {
var globIgnore =
'node_modules' + (buildWithinSite ? '|' + buildWithinSite : '');
var watcher = sane(SITE_ROOT, {
// Note: minimatch erroneously negates the entire pattern if the first
// character is a !. This prefix should cause it to not match.
// glob: ['?(%)!(' + globIgnore + ')/**/*', '?(%)!(' + globIgnore + ')'],
// handle node v0.10 bug
watchman: /^v0.10/.test(process.version)
})
.on('ready', startWatch)
.on('add', changeFile)
.on('delete', deleteFile)
.on('change', changeFile);
});
}
function startWatch() {
console.log('watching...');
}
function changeFile(fileName) {
enqueue(fileName);
}
function deleteFile(fileName) {
enqueue(fileName);
}
const queue = [];
function enqueue(fileName) {
queue.push(fileName);
if (queue.length === 1) {
rebuild();
} else {
console.log('queue', fileName);
}
}
function rebuild() {
const fileName = queue[0];
const filter =
/_core\//.test(fileName) ? /\.(js|md)$/ :
/\.less$/.test(fileName) ? /\.less$/ :
fileName ? SITE_ROOT + '/' + fileName :
null;
clearCache(fileName);
return build(filter).then(
() => {
queue.shift();
if (queue.length) {
return rebuild();
}
},
error => {
console.error(error.stack || error);
queue.shift();
if (queue.length) {
return rebuild();
}
}
);
}
function clearCache(causeFileName) {
if (path.extname(causeFileName) === '.js') {
for (var fileName in require.cache) {
if (fileName.indexOf('/node_modules/') === -1) {
delete require.cache[fileName];
}
}
}
}
if (require.main === module) {
watch().catch(error => console.error(error.stack || error));
}