@makano/rew 1.1.8 → 1.1.9

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