forked from openlayers/openlayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-info.js
302 lines (264 loc) · 7.32 KB
/
generate-info.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var fs = require('fs-extra');
var path = require('path');
var spawn = require('child_process').spawn;
var async = require('async');
var walk = require('walk').walk;
var isWindows = process.platform.indexOf('win') === 0;
var sourceDir = path.join(__dirname, '..', 'src');
var externsDir = path.join(__dirname, '..', 'externs');
var externsPaths = [
path.join(externsDir, 'olx.js'),
path.join(externsDir, 'geojson.js')
];
var infoPath = path.join(__dirname, '..', 'build', 'info.json');
var jsdocResolved = require.resolve('jsdoc/jsdoc.js');
var jsdoc = path.resolve(path.dirname(jsdocResolved), '../.bin/jsdoc');
// on Windows, use jsdoc.cmd
if (isWindows) {
jsdoc += '.cmd';
}
var jsdocConfig = path.join(
__dirname, '..', 'config', 'jsdoc', 'info', 'conf.json');
/**
* Get the mtime of the info file.
* @param {function(Error, Date)} callback Callback called with any
* error and the mtime of the info file (zero date if it doesn't exist).
*/
function getInfoTime(callback) {
fs.stat(infoPath, function(err, stats) {
if (err) {
if (err.code === 'ENOENT') {
callback(null, new Date(0));
} else {
callback(err);
}
} else {
callback(null, stats.mtime);
}
});
}
/**
* Test whether any externs are newer than the provided date.
* @param {Date} date Modification time of info file.
* @param {function(Error, Date, boolen)} callback Called with any
* error, the mtime of the info file (zero date if it doesn't exist), and
* whether any externs are newer than that date.
*/
function getNewerExterns(date, callback) {
var newer = false;
var walker = walk(externsDir);
walker.on('file', function(root, stats, next) {
var sourcePath = path.join(root, stats.name);
externsPaths.forEach(function(path) {
if (sourcePath === path && stats.mtime > date) {
newer = true;
}
});
next();
});
walker.on('errors', function() {
callback(new Error('Trouble walking ' + externsDir));
});
walker.on('end', function() {
callback(null, date, newer);
});
}
/**
* Generate a list of all .js paths in the source directory if any are newer
* than the provided date.
* @param {Date} date Modification time of info file.
* @param {boolean} newer Whether any externs are newer than date.
* @param {function(Error, Array.<string>)} callback Called with any
* error and the array of source paths (empty if none newer).
*/
function getNewer(date, newer, callback) {
var paths = [].concat(externsPaths);
var walker = walk(sourceDir);
walker.on('file', function(root, stats, next) {
var sourcePath = path.join(root, stats.name);
if (/\.js$/.test(sourcePath)) {
paths.push(sourcePath);
if (stats.mtime > date) {
newer = true;
}
}
next();
});
walker.on('errors', function() {
callback(new Error('Trouble walking ' + sourceDir));
});
walker.on('end', function() {
/**
* Windows has restrictions on length of command line, so passing all the
* changed paths to a task will fail if this limit is exceeded.
* To get round this, if this is Windows and there are newer files, just
* pass the sourceDir to the task so it can do the walking.
*/
if (isWindows) {
paths = [sourceDir].concat(externsPaths);
}
callback(null, newer ? paths : []);
});
}
/**
* Parse the JSDoc output.
* @param {string} output JSDoc output
* @return {Object} Symbol and define info.
*/
function parseOutput(output) {
if (!output) {
throw new Error('Expected JSON output');
}
var info;
try {
info = JSON.parse(String(output));
} catch (err) {
throw new Error('Failed to parse output as JSON: ' + output);
}
if (!Array.isArray(info.symbols)) {
throw new Error('Expected symbols array: ' + output);
}
if (!Array.isArray(info.defines)) {
throw new Error('Expected defines array: ' + output);
}
return info;
}
/**
* Spawn JSDoc.
* @param {Array.<string>} paths Paths to source files.
* @param {function(Error, string)} callback Callback called with any error and
* the JSDoc output (new metadata). If provided with an empty list of paths
* the callback will be called with null.
*/
function spawnJSDoc(paths, callback) {
if (paths.length === 0) {
process.nextTick(function() {
callback(null, null);
});
return;
}
var output = '';
var errors = '';
var cwd = path.join(__dirname, '..');
var child = spawn(jsdoc, ['-c', jsdocConfig].concat(paths), {cwd: cwd});
child.stdout.on('data', function(data) {
output += String(data);
});
child.stderr.on('data', function(data) {
errors += String(data);
});
child.on('exit', function(code) {
if (code) {
callback(new Error(errors || 'JSDoc failed with no output'));
} else {
var info;
try {
info = parseOutput(output);
} catch (err) {
callback(err);
return;
}
callback(null, info);
}
});
}
/**
* Given the path to a source file, get the list of provides.
* @param {string} srcPath Path to source file.
* @param {function(Error, Array.<string>)} callback Called with a list of
* provides or any error.
*/
var getProvides = async.memoize(function(srcPath, callback) {
fs.readFile(srcPath, function(err, data) {
if (err) {
callback(err);
return;
}
var provides = [];
var matcher = /goog\.provide\('(.*)'\)/;
String(data).split('\n').forEach(function(line) {
var match = line.match(matcher);
if (match) {
provides.push(match[1]);
}
});
callback(null, provides);
});
});
/**
* Add provides data to new symbols.
* @param {Object} info Symbols and defines metadata.
* @param {function(Error, Object)} callback Updated metadata.
*/
function addSymbolProvides(info, callback) {
if (!info) {
process.nextTick(function() {
callback(null, null);
});
return;
}
function addProvides(symbol, callback) {
getProvides(symbol.path, function(err, provides) {
if (err) {
callback(err);
return;
}
symbol.provides = provides;
callback(null, symbol);
});
}
async.map(info.symbols, addProvides, function(err, newSymbols) {
info.symbols = newSymbols;
callback(err, info);
});
}
/**
* Write symbol and define metadata to the info file.
* @param {Object} info Symbol and define metadata.
* @param {function(Error)} callback Callback.
*/
function writeInfo(info, callback) {
if (info) {
var str = JSON.stringify(info, null, ' ');
fs.outputFile(infoPath, str, callback);
} else {
process.nextTick(function() {
callback(null);
});
}
}
/**
* Determine if source files have been changed, run JSDoc and write updated
* info if there are any changes.
*
* @param {function(Error)} callback Called when the info file has been written
* (or an error occurs).
*/
function main(callback) {
async.waterfall([
getInfoTime,
getNewerExterns,
getNewer,
spawnJSDoc,
addSymbolProvides,
writeInfo
], callback);
}
/**
* If running this module directly, read the config file and call the main
* function.
*/
if (require.main === module) {
main(function(err) {
if (err) {
process.stderr.write(err.message + '\n');
process.exit(1);
} else {
process.exit(0);
}
});
}
/**
* Export main function.
*/
module.exports = main;