@makano/rew 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/rew +9 -0
- package/bin/ui +0 -0
- package/bin/webkit_app +0 -0
- package/lib/coffeescript/browser.js +151 -0
- package/lib/coffeescript/cake.js +135 -0
- package/lib/coffeescript/coffeescript.js +409 -0
- package/lib/coffeescript/command.js +750 -0
- package/lib/coffeescript/grammar.js +2496 -0
- package/lib/coffeescript/helpers.js +477 -0
- package/lib/coffeescript/index.js +217 -0
- package/lib/coffeescript/lexer.js +1943 -0
- package/lib/coffeescript/nodes.js +9204 -0
- package/lib/coffeescript/optparse.js +230 -0
- package/lib/coffeescript/parser.js +1344 -0
- package/lib/coffeescript/register.js +100 -0
- package/lib/coffeescript/repl.js +305 -0
- package/lib/coffeescript/rewriter.js +1138 -0
- package/lib/coffeescript/scope.js +187 -0
- package/lib/coffeescript/sourcemap.js +229 -0
- package/lib/rew/cli/cli.js +117 -0
- package/lib/rew/cli/log.js +40 -0
- package/lib/rew/cli/run.js +20 -0
- package/lib/rew/cli/utils.js +122 -0
- package/lib/rew/const/default.js +35 -0
- package/lib/rew/const/files.js +15 -0
- package/lib/rew/css/theme.css +3 -0
- package/lib/rew/functions/core.js +85 -0
- package/lib/rew/functions/emitter.js +57 -0
- package/lib/rew/functions/export.js +9 -0
- package/lib/rew/functions/future.js +22 -0
- package/lib/rew/functions/id.js +13 -0
- package/lib/rew/functions/import.js +57 -0
- package/lib/rew/functions/map.js +17 -0
- package/lib/rew/functions/match.js +34 -0
- package/lib/rew/functions/sleep.js +5 -0
- package/lib/rew/functions/types.js +96 -0
- package/lib/rew/html/ui.html +223 -0
- package/lib/rew/main.js +17 -0
- package/lib/rew/models/enum.js +14 -0
- package/lib/rew/models/struct.js +41 -0
- package/lib/rew/modules/compiler.js +17 -0
- package/lib/rew/modules/context.js +50 -0
- package/lib/rew/modules/fs.js +19 -0
- package/lib/rew/modules/runtime.js +24 -0
- package/lib/rew/modules/utils.js +0 -0
- package/lib/rew/modules/yaml.js +36 -0
- package/lib/rew/pkgs/conf.js +92 -0
- package/lib/rew/pkgs/data.js +8 -0
- package/lib/rew/pkgs/date.js +98 -0
- package/lib/rew/pkgs/modules/data/bintree.js +66 -0
- package/lib/rew/pkgs/modules/data/doublylinked.js +100 -0
- package/lib/rew/pkgs/modules/data/linkedList.js +88 -0
- package/lib/rew/pkgs/modules/data/queue.js +28 -0
- package/lib/rew/pkgs/modules/data/stack.js +27 -0
- package/lib/rew/pkgs/modules/ui/classes.js +171 -0
- package/lib/rew/pkgs/pkgs.js +13 -0
- package/lib/rew/pkgs/ui.js +108 -0
- package/package.json +41 -0
@@ -0,0 +1,750 @@
|
|
1
|
+
// Generated by CoffeeScript 2.7.0
|
2
|
+
(function() {
|
3
|
+
// The `coffee` utility. Handles command-line compilation of CoffeeScript
|
4
|
+
// into various forms: saved into `.js` files or printed to stdout
|
5
|
+
// or recompiled every time the source is saved,
|
6
|
+
// printed as a token stream or as the syntax tree, or launch an
|
7
|
+
// interactive REPL.
|
8
|
+
|
9
|
+
// External dependencies.
|
10
|
+
var BANNER, CoffeeScript, EventEmitter, SWITCHES, buildCSOptionParser, compileJoin, compileOptions, compilePath, compileScript, compileStdio, exec, findDirectoryIndex, forkNode, fs, helpers, hidden, joinTimeout, makePrelude, mkdirp, notSources, optionParser, optparse, opts, outputPath, parseOptions, path, printLine, printTokens, printWarn, removeSource, removeSourceDir, silentUnlink, sourceCode, sources, spawn, timeLog, usage, useWinPathSep, version, wait, watch, watchDir, watchedDirs, writeJs,
|
11
|
+
indexOf = [].indexOf;
|
12
|
+
|
13
|
+
fs = require('fs');
|
14
|
+
|
15
|
+
path = require('path');
|
16
|
+
|
17
|
+
helpers = require('./helpers');
|
18
|
+
|
19
|
+
optparse = require('./optparse');
|
20
|
+
|
21
|
+
CoffeeScript = require('./');
|
22
|
+
|
23
|
+
({spawn, exec} = require('child_process'));
|
24
|
+
|
25
|
+
({EventEmitter} = require('events'));
|
26
|
+
|
27
|
+
useWinPathSep = path.sep === '\\';
|
28
|
+
|
29
|
+
// Allow CoffeeScript to emit Node.js events.
|
30
|
+
helpers.extend(CoffeeScript, new EventEmitter());
|
31
|
+
|
32
|
+
printLine = function(line) {
|
33
|
+
return process.stdout.write(line + '\n');
|
34
|
+
};
|
35
|
+
|
36
|
+
printWarn = function(line) {
|
37
|
+
return process.stderr.write(line + '\n');
|
38
|
+
};
|
39
|
+
|
40
|
+
hidden = function(file) {
|
41
|
+
return /^\.|~$/.test(file);
|
42
|
+
};
|
43
|
+
|
44
|
+
// The help banner that is printed in conjunction with `-h`/`--help`.
|
45
|
+
BANNER = `Usage: coffee [options] path/to/script.coffee [args]
|
46
|
+
|
47
|
+
If called without options, \`coffee\` will run your script.`;
|
48
|
+
|
49
|
+
// The list of all the valid option flags that `coffee` knows how to handle.
|
50
|
+
SWITCHES = [['--ast', 'generate an abstract syntax tree of nodes'], ['-b', '--bare', 'compile without a top-level function wrapper'], ['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-e', '--eval', 'pass a string from the command line as input'], ['-h', '--help', 'display this help message'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling'], ['-l', '--literate', 'treat stdio as literate style coffeescript'], ['-m', '--map', 'generate source map and save as .js.map files'], ['-M', '--inline-map', 'generate source map and include it directly in output'], ['-n', '--nodes', 'print out the parse tree that the parser produces'], ['--nodejs [ARGS]', 'pass options directly to the "node" binary'], ['--no-header', 'suppress the "Generated by" header'], ['-o', '--output [PATH]', 'set the output path or path/filename for compiled JavaScript'], ['-p', '--print', 'print out the compiled JavaScript'], ['-r', '--require [MODULE*]', 'require the given module before eval or REPL'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-t', '--transpile', 'pipe generated JavaScript through Babel'], ['--tokens', 'print out the tokens that the lexer/rewriter produce'], ['-v', '--version', 'display the version number'], ['-w', '--watch', 'watch scripts for changes and rerun commands']];
|
51
|
+
|
52
|
+
// Top-level objects shared by all the functions.
|
53
|
+
opts = {};
|
54
|
+
|
55
|
+
sources = [];
|
56
|
+
|
57
|
+
sourceCode = [];
|
58
|
+
|
59
|
+
notSources = {};
|
60
|
+
|
61
|
+
watchedDirs = {};
|
62
|
+
|
63
|
+
optionParser = null;
|
64
|
+
|
65
|
+
exports.buildCSOptionParser = buildCSOptionParser = function() {
|
66
|
+
return new optparse.OptionParser(SWITCHES, BANNER);
|
67
|
+
};
|
68
|
+
|
69
|
+
// Run `coffee` by parsing passed options and determining what action to take.
|
70
|
+
// Many flags cause us to divert before compiling anything. Flags passed after
|
71
|
+
// `--` will be passed verbatim to your script as arguments in `process.argv`
|
72
|
+
exports.run = function() {
|
73
|
+
var err, i, len, literals, outputBasename, ref, replCliOpts, results, source;
|
74
|
+
optionParser = buildCSOptionParser();
|
75
|
+
try {
|
76
|
+
parseOptions();
|
77
|
+
} catch (error) {
|
78
|
+
err = error;
|
79
|
+
console.error(`option parsing error: ${err.message}`);
|
80
|
+
process.exit(1);
|
81
|
+
}
|
82
|
+
if ((!opts.doubleDashed) && (opts.arguments[1] === '--')) {
|
83
|
+
printWarn(`coffee was invoked with '--' as the second positional argument, which is
|
84
|
+
now deprecated. To pass '--' as an argument to a script to run, put an
|
85
|
+
additional '--' before the path to your script.
|
86
|
+
|
87
|
+
'--' will be removed from the argument list.`);
|
88
|
+
printWarn(`The positional arguments were: ${JSON.stringify(opts.arguments)}`);
|
89
|
+
opts.arguments = [opts.arguments[0]].concat(opts.arguments.slice(2));
|
90
|
+
}
|
91
|
+
// Make the REPL *CLI* use the global context so as to (a) be consistent with the
|
92
|
+
// `node` REPL CLI and, therefore, (b) make packages that modify native prototypes
|
93
|
+
// (such as 'colors' and 'sugar') work as expected.
|
94
|
+
replCliOpts = {
|
95
|
+
useGlobal: true
|
96
|
+
};
|
97
|
+
if (opts.require) {
|
98
|
+
opts.prelude = makePrelude(opts.require);
|
99
|
+
}
|
100
|
+
replCliOpts.prelude = opts.prelude;
|
101
|
+
replCliOpts.transpile = opts.transpile;
|
102
|
+
if (opts.nodejs) {
|
103
|
+
return forkNode();
|
104
|
+
}
|
105
|
+
if (opts.help) {
|
106
|
+
return usage();
|
107
|
+
}
|
108
|
+
if (opts.version) {
|
109
|
+
return version();
|
110
|
+
}
|
111
|
+
if (opts.interactive) {
|
112
|
+
return require('./repl').start(replCliOpts);
|
113
|
+
}
|
114
|
+
if (opts.stdio) {
|
115
|
+
return compileStdio();
|
116
|
+
}
|
117
|
+
if (opts.eval) {
|
118
|
+
return compileScript(null, opts.arguments[0]);
|
119
|
+
}
|
120
|
+
if (!opts.arguments.length) {
|
121
|
+
return require('./repl').start(replCliOpts);
|
122
|
+
}
|
123
|
+
literals = opts.run ? opts.arguments.splice(1) : [];
|
124
|
+
process.argv = process.argv.slice(0, 2).concat(literals);
|
125
|
+
process.argv[0] = 'coffee';
|
126
|
+
if (opts.output) {
|
127
|
+
outputBasename = path.basename(opts.output);
|
128
|
+
if (indexOf.call(outputBasename, '.') >= 0 && (outputBasename !== '.' && outputBasename !== '..') && !helpers.ends(opts.output, path.sep)) {
|
129
|
+
// An output filename was specified, e.g. `/dist/scripts.js`.
|
130
|
+
opts.outputFilename = outputBasename;
|
131
|
+
opts.outputPath = path.resolve(path.dirname(opts.output));
|
132
|
+
} else {
|
133
|
+
// An output path was specified, e.g. `/dist`.
|
134
|
+
opts.outputFilename = null;
|
135
|
+
opts.outputPath = path.resolve(opts.output);
|
136
|
+
}
|
137
|
+
}
|
138
|
+
if (opts.join) {
|
139
|
+
opts.join = path.resolve(opts.join);
|
140
|
+
console.error(`
|
141
|
+
The --join option is deprecated and will be removed in a future version.
|
142
|
+
|
143
|
+
If for some reason it's necessary to share local variables between files,
|
144
|
+
replace...
|
145
|
+
|
146
|
+
$ coffee --compile --join bundle.js -- a.coffee b.coffee c.coffee
|
147
|
+
|
148
|
+
with...
|
149
|
+
|
150
|
+
$ cat a.coffee b.coffee c.coffee | coffee --compile --stdio > bundle.js
|
151
|
+
`);
|
152
|
+
}
|
153
|
+
ref = opts.arguments;
|
154
|
+
results = [];
|
155
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
156
|
+
source = ref[i];
|
157
|
+
source = path.resolve(source);
|
158
|
+
results.push(compilePath(source, true, source));
|
159
|
+
}
|
160
|
+
return results;
|
161
|
+
};
|
162
|
+
|
163
|
+
makePrelude = function(requires) {
|
164
|
+
return requires.map(function(module) {
|
165
|
+
var full, match, name;
|
166
|
+
if (match = module.match(/^(.*)=(.*)$/)) {
|
167
|
+
[full, name, module] = match;
|
168
|
+
}
|
169
|
+
name || (name = helpers.baseFileName(module, true, useWinPathSep));
|
170
|
+
return `global['${name}'] = require('${module}')`;
|
171
|
+
}).join(';');
|
172
|
+
};
|
173
|
+
|
174
|
+
// Compile a path, which could be a script or a directory. If a directory
|
175
|
+
// is passed, recursively compile all '.coffee', '.litcoffee', and '.coffee.md'
|
176
|
+
// extension source files in it and all subdirectories.
|
177
|
+
compilePath = function(source, topLevel, base) {
|
178
|
+
var code, err, file, files, i, len, results, stats;
|
179
|
+
if (indexOf.call(sources, source) >= 0 || watchedDirs[source] || !topLevel && (notSources[source] || hidden(source))) {
|
180
|
+
return;
|
181
|
+
}
|
182
|
+
try {
|
183
|
+
stats = fs.statSync(source);
|
184
|
+
} catch (error) {
|
185
|
+
err = error;
|
186
|
+
if (err.code === 'ENOENT') {
|
187
|
+
console.error(`File not found: ${source}`);
|
188
|
+
process.exit(1);
|
189
|
+
}
|
190
|
+
throw err;
|
191
|
+
}
|
192
|
+
if (stats.isDirectory()) {
|
193
|
+
if (path.basename(source) === 'node_modules') {
|
194
|
+
notSources[source] = true;
|
195
|
+
return;
|
196
|
+
}
|
197
|
+
if (opts.run) {
|
198
|
+
compilePath(findDirectoryIndex(source), topLevel, base);
|
199
|
+
return;
|
200
|
+
}
|
201
|
+
if (opts.watch) {
|
202
|
+
watchDir(source, base);
|
203
|
+
}
|
204
|
+
try {
|
205
|
+
files = fs.readdirSync(source);
|
206
|
+
} catch (error) {
|
207
|
+
err = error;
|
208
|
+
if (err.code === 'ENOENT') {
|
209
|
+
return;
|
210
|
+
} else {
|
211
|
+
throw err;
|
212
|
+
}
|
213
|
+
}
|
214
|
+
results = [];
|
215
|
+
for (i = 0, len = files.length; i < len; i++) {
|
216
|
+
file = files[i];
|
217
|
+
results.push(compilePath(path.join(source, file), false, base));
|
218
|
+
}
|
219
|
+
return results;
|
220
|
+
} else if (topLevel || helpers.isCoffee(source)) {
|
221
|
+
sources.push(source);
|
222
|
+
sourceCode.push(null);
|
223
|
+
delete notSources[source];
|
224
|
+
if (opts.watch) {
|
225
|
+
watch(source, base);
|
226
|
+
}
|
227
|
+
try {
|
228
|
+
code = fs.readFileSync(source);
|
229
|
+
} catch (error) {
|
230
|
+
err = error;
|
231
|
+
if (err.code === 'ENOENT') {
|
232
|
+
return;
|
233
|
+
} else {
|
234
|
+
throw err;
|
235
|
+
}
|
236
|
+
}
|
237
|
+
return compileScript(source, code.toString(), base);
|
238
|
+
} else {
|
239
|
+
return notSources[source] = true;
|
240
|
+
}
|
241
|
+
};
|
242
|
+
|
243
|
+
findDirectoryIndex = function(source) {
|
244
|
+
var err, ext, i, index, len, ref;
|
245
|
+
ref = CoffeeScript.FILE_EXTENSIONS;
|
246
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
247
|
+
ext = ref[i];
|
248
|
+
index = path.join(source, `index${ext}`);
|
249
|
+
try {
|
250
|
+
if ((fs.statSync(index)).isFile()) {
|
251
|
+
return index;
|
252
|
+
}
|
253
|
+
} catch (error) {
|
254
|
+
err = error;
|
255
|
+
if (err.code !== 'ENOENT') {
|
256
|
+
throw err;
|
257
|
+
}
|
258
|
+
}
|
259
|
+
}
|
260
|
+
console.error(`Missing index.coffee or index.litcoffee in ${source}`);
|
261
|
+
return process.exit(1);
|
262
|
+
};
|
263
|
+
|
264
|
+
// Compile a single source script, containing the given code, according to the
|
265
|
+
// requested options. If evaluating the script directly, set `__filename`,
|
266
|
+
// `__dirname` and `module.filename` to be correct relative to the script's path.
|
267
|
+
compileScript = function(file, input, base = null) {
|
268
|
+
var compiled, err, message, options, saveTo, task;
|
269
|
+
options = compileOptions(file, base);
|
270
|
+
try {
|
271
|
+
task = {file, input, options};
|
272
|
+
CoffeeScript.emit('compile', task);
|
273
|
+
if (opts.tokens) {
|
274
|
+
return printTokens(CoffeeScript.tokens(task.input, task.options));
|
275
|
+
} else if (opts.nodes) {
|
276
|
+
return printLine(CoffeeScript.nodes(task.input, task.options).toString().trim());
|
277
|
+
} else if (opts.ast) {
|
278
|
+
compiled = CoffeeScript.compile(task.input, task.options);
|
279
|
+
return printLine(JSON.stringify(compiled, null, 2));
|
280
|
+
} else if (opts.run) {
|
281
|
+
CoffeeScript.register();
|
282
|
+
if (opts.prelude) {
|
283
|
+
CoffeeScript.eval(opts.prelude, task.options);
|
284
|
+
}
|
285
|
+
return CoffeeScript.run(task.input, task.options);
|
286
|
+
} else if (opts.join && task.file !== opts.join) {
|
287
|
+
if (helpers.isLiterate(file)) {
|
288
|
+
task.input = helpers.invertLiterate(task.input);
|
289
|
+
}
|
290
|
+
sourceCode[sources.indexOf(task.file)] = task.input;
|
291
|
+
return compileJoin();
|
292
|
+
} else {
|
293
|
+
compiled = CoffeeScript.compile(task.input, task.options);
|
294
|
+
task.output = compiled;
|
295
|
+
if (opts.map) {
|
296
|
+
task.output = compiled.js;
|
297
|
+
task.sourceMap = compiled.v3SourceMap;
|
298
|
+
}
|
299
|
+
CoffeeScript.emit('success', task);
|
300
|
+
if (opts.print) {
|
301
|
+
return printLine(task.output.trim());
|
302
|
+
} else if (opts.compile || opts.map) {
|
303
|
+
saveTo = opts.outputFilename && sources.length === 1 ? path.join(opts.outputPath, opts.outputFilename) : options.jsPath;
|
304
|
+
return writeJs(base, task.file, task.output, saveTo, task.sourceMap);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
} catch (error) {
|
308
|
+
err = error;
|
309
|
+
CoffeeScript.emit('failure', err, task);
|
310
|
+
if (CoffeeScript.listeners('failure').length) {
|
311
|
+
return;
|
312
|
+
}
|
313
|
+
message = (err != null ? err.stack : void 0) || `${err}`;
|
314
|
+
if (opts.watch) {
|
315
|
+
return printLine(message + '\x07');
|
316
|
+
} else {
|
317
|
+
printWarn(message);
|
318
|
+
return process.exit(1);
|
319
|
+
}
|
320
|
+
}
|
321
|
+
};
|
322
|
+
|
323
|
+
// Attach the appropriate listeners to compile scripts incoming over **stdin**,
|
324
|
+
// and write them back to **stdout**.
|
325
|
+
compileStdio = function() {
|
326
|
+
var buffers, stdin;
|
327
|
+
if (opts.map) {
|
328
|
+
console.error('--stdio and --map cannot be used together');
|
329
|
+
process.exit(1);
|
330
|
+
}
|
331
|
+
buffers = [];
|
332
|
+
stdin = process.openStdin();
|
333
|
+
stdin.on('data', function(buffer) {
|
334
|
+
if (buffer) {
|
335
|
+
return buffers.push(buffer);
|
336
|
+
}
|
337
|
+
});
|
338
|
+
return stdin.on('end', function() {
|
339
|
+
return compileScript(null, Buffer.concat(buffers).toString());
|
340
|
+
});
|
341
|
+
};
|
342
|
+
|
343
|
+
// If all of the source files are done being read, concatenate and compile
|
344
|
+
// them together.
|
345
|
+
joinTimeout = null;
|
346
|
+
|
347
|
+
compileJoin = function() {
|
348
|
+
if (!opts.join) {
|
349
|
+
return;
|
350
|
+
}
|
351
|
+
if (!sourceCode.some(function(code) {
|
352
|
+
return code === null;
|
353
|
+
})) {
|
354
|
+
clearTimeout(joinTimeout);
|
355
|
+
return joinTimeout = wait(100, function() {
|
356
|
+
return compileScript(opts.join, sourceCode.join('\n'), opts.join);
|
357
|
+
});
|
358
|
+
}
|
359
|
+
};
|
360
|
+
|
361
|
+
// Watch a source CoffeeScript file using `fs.watch`, recompiling it every
|
362
|
+
// time the file is updated. May be used in combination with other options,
|
363
|
+
// such as `--print`.
|
364
|
+
watch = function(source, base) {
|
365
|
+
var compile, compileTimeout, err, prevStats, rewatch, startWatcher, watchErr, watcher;
|
366
|
+
watcher = null;
|
367
|
+
prevStats = null;
|
368
|
+
compileTimeout = null;
|
369
|
+
watchErr = function(err) {
|
370
|
+
if (err.code !== 'ENOENT') {
|
371
|
+
throw err;
|
372
|
+
}
|
373
|
+
if (indexOf.call(sources, source) < 0) {
|
374
|
+
return;
|
375
|
+
}
|
376
|
+
try {
|
377
|
+
rewatch();
|
378
|
+
return compile();
|
379
|
+
} catch (error) {
|
380
|
+
removeSource(source, base);
|
381
|
+
return compileJoin();
|
382
|
+
}
|
383
|
+
};
|
384
|
+
compile = function() {
|
385
|
+
clearTimeout(compileTimeout);
|
386
|
+
return compileTimeout = wait(25, function() {
|
387
|
+
return fs.stat(source, function(err, stats) {
|
388
|
+
if (err) {
|
389
|
+
return watchErr(err);
|
390
|
+
}
|
391
|
+
if (prevStats && stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime()) {
|
392
|
+
return rewatch();
|
393
|
+
}
|
394
|
+
prevStats = stats;
|
395
|
+
return fs.readFile(source, function(err, code) {
|
396
|
+
if (err) {
|
397
|
+
return watchErr(err);
|
398
|
+
}
|
399
|
+
compileScript(source, code.toString(), base);
|
400
|
+
return rewatch();
|
401
|
+
});
|
402
|
+
});
|
403
|
+
});
|
404
|
+
};
|
405
|
+
startWatcher = function() {
|
406
|
+
return watcher = fs.watch(source).on('change', compile).on('error', function(err) {
|
407
|
+
if (err.code !== 'EPERM') {
|
408
|
+
throw err;
|
409
|
+
}
|
410
|
+
return removeSource(source, base);
|
411
|
+
});
|
412
|
+
};
|
413
|
+
rewatch = function() {
|
414
|
+
if (watcher != null) {
|
415
|
+
watcher.close();
|
416
|
+
}
|
417
|
+
return startWatcher();
|
418
|
+
};
|
419
|
+
try {
|
420
|
+
return startWatcher();
|
421
|
+
} catch (error) {
|
422
|
+
err = error;
|
423
|
+
return watchErr(err);
|
424
|
+
}
|
425
|
+
};
|
426
|
+
|
427
|
+
// Watch a directory of files for new additions.
|
428
|
+
watchDir = function(source, base) {
|
429
|
+
var err, readdirTimeout, startWatcher, stopWatcher, watcher;
|
430
|
+
watcher = null;
|
431
|
+
readdirTimeout = null;
|
432
|
+
startWatcher = function() {
|
433
|
+
return watcher = fs.watch(source).on('error', function(err) {
|
434
|
+
if (err.code !== 'EPERM') {
|
435
|
+
throw err;
|
436
|
+
}
|
437
|
+
return stopWatcher();
|
438
|
+
}).on('change', function() {
|
439
|
+
clearTimeout(readdirTimeout);
|
440
|
+
return readdirTimeout = wait(25, function() {
|
441
|
+
var err, file, files, i, len, results;
|
442
|
+
try {
|
443
|
+
files = fs.readdirSync(source);
|
444
|
+
} catch (error) {
|
445
|
+
err = error;
|
446
|
+
if (err.code !== 'ENOENT') {
|
447
|
+
throw err;
|
448
|
+
}
|
449
|
+
return stopWatcher();
|
450
|
+
}
|
451
|
+
results = [];
|
452
|
+
for (i = 0, len = files.length; i < len; i++) {
|
453
|
+
file = files[i];
|
454
|
+
results.push(compilePath(path.join(source, file), false, base));
|
455
|
+
}
|
456
|
+
return results;
|
457
|
+
});
|
458
|
+
});
|
459
|
+
};
|
460
|
+
stopWatcher = function() {
|
461
|
+
watcher.close();
|
462
|
+
return removeSourceDir(source, base);
|
463
|
+
};
|
464
|
+
watchedDirs[source] = true;
|
465
|
+
try {
|
466
|
+
return startWatcher();
|
467
|
+
} catch (error) {
|
468
|
+
err = error;
|
469
|
+
if (err.code !== 'ENOENT') {
|
470
|
+
throw err;
|
471
|
+
}
|
472
|
+
}
|
473
|
+
};
|
474
|
+
|
475
|
+
removeSourceDir = function(source, base) {
|
476
|
+
var file, i, len, sourcesChanged;
|
477
|
+
delete watchedDirs[source];
|
478
|
+
sourcesChanged = false;
|
479
|
+
for (i = 0, len = sources.length; i < len; i++) {
|
480
|
+
file = sources[i];
|
481
|
+
if (!(source === path.dirname(file))) {
|
482
|
+
continue;
|
483
|
+
}
|
484
|
+
removeSource(file, base);
|
485
|
+
sourcesChanged = true;
|
486
|
+
}
|
487
|
+
if (sourcesChanged) {
|
488
|
+
return compileJoin();
|
489
|
+
}
|
490
|
+
};
|
491
|
+
|
492
|
+
// Remove a file from our source list, and source code cache. Optionally remove
|
493
|
+
// the compiled JS version as well.
|
494
|
+
removeSource = function(source, base) {
|
495
|
+
var index;
|
496
|
+
index = sources.indexOf(source);
|
497
|
+
sources.splice(index, 1);
|
498
|
+
sourceCode.splice(index, 1);
|
499
|
+
if (!opts.join) {
|
500
|
+
silentUnlink(outputPath(source, base));
|
501
|
+
silentUnlink(outputPath(source, base, '.js.map'));
|
502
|
+
return timeLog(`removed ${source}`);
|
503
|
+
}
|
504
|
+
};
|
505
|
+
|
506
|
+
silentUnlink = function(path) {
|
507
|
+
var err, ref;
|
508
|
+
try {
|
509
|
+
return fs.unlinkSync(path);
|
510
|
+
} catch (error) {
|
511
|
+
err = error;
|
512
|
+
if ((ref = err.code) !== 'ENOENT' && ref !== 'EPERM') {
|
513
|
+
throw err;
|
514
|
+
}
|
515
|
+
}
|
516
|
+
};
|
517
|
+
|
518
|
+
// Get the corresponding output JavaScript path for a source file.
|
519
|
+
outputPath = function(source, base, extension = ".js") {
|
520
|
+
var basename, dir, srcDir;
|
521
|
+
basename = helpers.baseFileName(source, true, useWinPathSep);
|
522
|
+
srcDir = path.dirname(source);
|
523
|
+
dir = !opts.outputPath ? srcDir : source === base ? opts.outputPath : path.join(opts.outputPath, path.relative(base, srcDir));
|
524
|
+
return path.join(dir, basename + extension);
|
525
|
+
};
|
526
|
+
|
527
|
+
// Recursively mkdir, like `mkdir -p`.
|
528
|
+
mkdirp = function(dir, fn) {
|
529
|
+
var mkdirs, mode;
|
530
|
+
mode = 0o777 & ~process.umask();
|
531
|
+
return (mkdirs = function(p, fn) {
|
532
|
+
return fs.exists(p, function(exists) {
|
533
|
+
if (exists) {
|
534
|
+
return fn();
|
535
|
+
} else {
|
536
|
+
return mkdirs(path.dirname(p), function() {
|
537
|
+
return fs.mkdir(p, mode, function(err) {
|
538
|
+
if (err) {
|
539
|
+
return fn(err);
|
540
|
+
}
|
541
|
+
return fn();
|
542
|
+
});
|
543
|
+
});
|
544
|
+
}
|
545
|
+
});
|
546
|
+
})(dir, fn);
|
547
|
+
};
|
548
|
+
|
549
|
+
// Write out a JavaScript source file with the compiled code. By default, files
|
550
|
+
// are written out in `cwd` as `.js` files with the same name, but the output
|
551
|
+
// directory can be customized with `--output`.
|
552
|
+
|
553
|
+
// If `generatedSourceMap` is provided, this will write a `.js.map` file into the
|
554
|
+
// same directory as the `.js` file.
|
555
|
+
writeJs = function(base, sourcePath, js, jsPath, generatedSourceMap = null) {
|
556
|
+
var compile, jsDir, sourceMapPath;
|
557
|
+
sourceMapPath = `${jsPath}.map`;
|
558
|
+
jsDir = path.dirname(jsPath);
|
559
|
+
compile = function() {
|
560
|
+
if (opts.compile) {
|
561
|
+
if (js.length <= 0) {
|
562
|
+
js = ' ';
|
563
|
+
}
|
564
|
+
if (generatedSourceMap) {
|
565
|
+
js = `${js}\n//# sourceMappingURL=${helpers.baseFileName(sourceMapPath, false, useWinPathSep)}\n`;
|
566
|
+
}
|
567
|
+
fs.writeFile(jsPath, js, function(err) {
|
568
|
+
if (err) {
|
569
|
+
printLine(err.message);
|
570
|
+
return process.exit(1);
|
571
|
+
} else if (opts.compile && opts.watch) {
|
572
|
+
return timeLog(`compiled ${sourcePath}`);
|
573
|
+
}
|
574
|
+
});
|
575
|
+
}
|
576
|
+
if (generatedSourceMap) {
|
577
|
+
return fs.writeFile(sourceMapPath, generatedSourceMap, function(err) {
|
578
|
+
if (err) {
|
579
|
+
printLine(`Could not write source map: ${err.message}`);
|
580
|
+
return process.exit(1);
|
581
|
+
}
|
582
|
+
});
|
583
|
+
}
|
584
|
+
};
|
585
|
+
return fs.exists(jsDir, function(itExists) {
|
586
|
+
if (itExists) {
|
587
|
+
return compile();
|
588
|
+
} else {
|
589
|
+
return mkdirp(jsDir, compile);
|
590
|
+
}
|
591
|
+
});
|
592
|
+
};
|
593
|
+
|
594
|
+
// Convenience for cleaner setTimeouts.
|
595
|
+
wait = function(milliseconds, func) {
|
596
|
+
return setTimeout(func, milliseconds);
|
597
|
+
};
|
598
|
+
|
599
|
+
// When watching scripts, it's useful to log changes with the timestamp.
|
600
|
+
timeLog = function(message) {
|
601
|
+
return console.log(`${(new Date()).toLocaleTimeString()} - ${message}`);
|
602
|
+
};
|
603
|
+
|
604
|
+
// Pretty-print a stream of tokens, sans location data.
|
605
|
+
printTokens = function(tokens) {
|
606
|
+
var strings, tag, token, value;
|
607
|
+
strings = (function() {
|
608
|
+
var i, len, results;
|
609
|
+
results = [];
|
610
|
+
for (i = 0, len = tokens.length; i < len; i++) {
|
611
|
+
token = tokens[i];
|
612
|
+
tag = token[0];
|
613
|
+
value = token[1].toString().replace(/\n/, '\\n');
|
614
|
+
results.push(`[${tag} ${value}]`);
|
615
|
+
}
|
616
|
+
return results;
|
617
|
+
})();
|
618
|
+
return printLine(strings.join(' '));
|
619
|
+
};
|
620
|
+
|
621
|
+
// Use the [OptionParser module](optparse.html) to extract all options from
|
622
|
+
// `process.argv` that are specified in `SWITCHES`.
|
623
|
+
parseOptions = function() {
|
624
|
+
var o;
|
625
|
+
o = opts = optionParser.parse(process.argv.slice(2));
|
626
|
+
o.compile || (o.compile = !!o.output);
|
627
|
+
o.run = !(o.compile || o.print || o.map);
|
628
|
+
return o.print = !!(o.print || (o.eval || o.stdio && o.compile));
|
629
|
+
};
|
630
|
+
|
631
|
+
// The compile-time options to pass to the CoffeeScript compiler.
|
632
|
+
compileOptions = function(filename, base) {
|
633
|
+
var answer, cwd, jsDir, jsPath;
|
634
|
+
if (opts.transpile) {
|
635
|
+
try {
|
636
|
+
// The user has requested that the CoffeeScript compiler also transpile
|
637
|
+
// via Babel. We don’t include Babel as a dependency because we want to
|
638
|
+
// avoid dependencies in general, and most users probably won’t be relying
|
639
|
+
// on us to transpile for them; we assume most users will probably either
|
640
|
+
// run CoffeeScript’s output without transpilation (modern Node or evergreen
|
641
|
+
// browsers) or use a proper build chain like Gulp or Webpack.
|
642
|
+
require('@babel/core');
|
643
|
+
} catch (error) {
|
644
|
+
try {
|
645
|
+
require('babel-core');
|
646
|
+
} catch (error) {
|
647
|
+
// Give appropriate instructions depending on whether `coffee` was run
|
648
|
+
// locally or globally.
|
649
|
+
if (require.resolve('.').indexOf(process.cwd()) === 0) {
|
650
|
+
console.error(`To use --transpile, you must have @babel/core installed:
|
651
|
+
npm install --save-dev @babel/core
|
652
|
+
And you must save options to configure Babel in one of the places it looks to find its options.
|
653
|
+
See https://coffeescript.org/#transpilation`);
|
654
|
+
} else {
|
655
|
+
console.error(`To use --transpile with globally-installed CoffeeScript, you must have @babel/core installed globally:
|
656
|
+
npm install --global @babel/core
|
657
|
+
And you must save options to configure Babel in one of the places it looks to find its options, relative to the file being compiled or to the current folder.
|
658
|
+
See https://coffeescript.org/#transpilation`);
|
659
|
+
}
|
660
|
+
process.exit(1);
|
661
|
+
}
|
662
|
+
}
|
663
|
+
if (typeof opts.transpile !== 'object') {
|
664
|
+
opts.transpile = {};
|
665
|
+
}
|
666
|
+
// Pass a reference to Babel into the compiler, so that the transpile option
|
667
|
+
// is available for the CLI. We need to do this so that tools like Webpack
|
668
|
+
// can `require('coffeescript')` and build correctly, without trying to
|
669
|
+
// require Babel.
|
670
|
+
opts.transpile.transpile = CoffeeScript.transpile;
|
671
|
+
// Babel searches for its options (a `.babelrc` file, a `.babelrc.js` file,
|
672
|
+
// a `package.json` file with a `babel` key, etc.) relative to the path
|
673
|
+
// given to it in its `filename` option. Make sure we have a path to pass
|
674
|
+
// along.
|
675
|
+
if (!opts.transpile.filename) {
|
676
|
+
opts.transpile.filename = filename || path.resolve(base || process.cwd(), '<anonymous>');
|
677
|
+
}
|
678
|
+
} else {
|
679
|
+
opts.transpile = false;
|
680
|
+
}
|
681
|
+
answer = {
|
682
|
+
filename: filename,
|
683
|
+
literate: opts.literate || helpers.isLiterate(filename),
|
684
|
+
bare: opts.bare,
|
685
|
+
header: opts.compile && !opts['no-header'],
|
686
|
+
transpile: opts.transpile,
|
687
|
+
sourceMap: opts.map,
|
688
|
+
inlineMap: opts['inline-map'],
|
689
|
+
ast: opts.ast
|
690
|
+
};
|
691
|
+
if (filename) {
|
692
|
+
if (base) {
|
693
|
+
cwd = process.cwd();
|
694
|
+
jsPath = outputPath(filename, base);
|
695
|
+
jsDir = path.dirname(jsPath);
|
696
|
+
answer = helpers.merge(answer, {
|
697
|
+
jsPath,
|
698
|
+
sourceRoot: path.relative(jsDir, cwd) + path.sep,
|
699
|
+
sourceFiles: [path.relative(cwd, filename)],
|
700
|
+
generatedFile: helpers.baseFileName(jsPath, false, useWinPathSep)
|
701
|
+
});
|
702
|
+
} else {
|
703
|
+
answer = helpers.merge(answer, {
|
704
|
+
sourceRoot: "",
|
705
|
+
sourceFiles: [helpers.baseFileName(filename, false, useWinPathSep)],
|
706
|
+
generatedFile: helpers.baseFileName(filename, true, useWinPathSep) + ".js"
|
707
|
+
});
|
708
|
+
}
|
709
|
+
}
|
710
|
+
return answer;
|
711
|
+
};
|
712
|
+
|
713
|
+
// Start up a new Node.js instance with the arguments in `--nodejs` passed to
|
714
|
+
// the `node` binary, preserving the other options.
|
715
|
+
forkNode = function() {
|
716
|
+
var args, i, len, nodeArgs, p, ref, signal;
|
717
|
+
nodeArgs = opts.nodejs.split(/\s+/);
|
718
|
+
args = process.argv.slice(1);
|
719
|
+
args.splice(args.indexOf('--nodejs'), 2);
|
720
|
+
p = spawn(process.execPath, nodeArgs.concat(args), {
|
721
|
+
cwd: process.cwd(),
|
722
|
+
env: process.env,
|
723
|
+
stdio: [0, 1, 2]
|
724
|
+
});
|
725
|
+
ref = ['SIGINT', 'SIGTERM'];
|
726
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
727
|
+
signal = ref[i];
|
728
|
+
process.on(signal, (function(signal) {
|
729
|
+
return function() {
|
730
|
+
return p.kill(signal);
|
731
|
+
};
|
732
|
+
})(signal));
|
733
|
+
}
|
734
|
+
return p.on('exit', function(code) {
|
735
|
+
return process.exit(code);
|
736
|
+
});
|
737
|
+
};
|
738
|
+
|
739
|
+
// Print the `--help` usage message and exit. Deprecated switches are not
|
740
|
+
// shown.
|
741
|
+
usage = function() {
|
742
|
+
return printLine(optionParser.help());
|
743
|
+
};
|
744
|
+
|
745
|
+
// Print the `--version` message and exit.
|
746
|
+
version = function() {
|
747
|
+
return printLine(`CoffeeScript version ${CoffeeScript.VERSION}`);
|
748
|
+
};
|
749
|
+
|
750
|
+
}).call(this);
|