1 | import http from 'http';
|
2 | import fs from 'fs';
|
3 | import path from 'path';
|
4 | import { parse, URLSearchParams } from 'url';
|
5 | import require$$0, { EventEmitter } from 'events';
|
6 | import CheapWatch from 'cheap-watch';
|
7 | import util from 'util';
|
8 | import os from 'os';
|
9 | import https from 'https';
|
10 | import require$$1 from 'child_process';
|
11 | import require$$0$1 from 'domain';
|
12 | import 'querystring';
|
13 | import vm from 'vm';
|
14 | import vite from 'vite';
|
15 | import { c as create_manifest_data, a as create_app } from './index2.js';
|
16 | import { r as rimraf, c as copy_assets } from './utils.js';
|
17 | import { ssr } from '../ssr.js';
|
18 | import { g as get_body } from './index3.js';
|
19 | import svelte from '@svitejs/vite-plugin-svelte';
|
20 | import './standard.js';
|
21 | import '../cli.js';
|
22 | import 'sade';
|
23 | import 'crypto';
|
24 | import 'zlib';
|
25 | import 'stream';
|
26 |
|
27 | function createCommonjsModule(fn) {
|
28 | var module = { exports: {} };
|
29 | return fn(module, module.exports), module.exports;
|
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 | var styles_1 = createCommonjsModule(function (module) {
|
58 | var styles = {};
|
59 | module['exports'] = styles;
|
60 |
|
61 | var codes = {
|
62 | reset: [0, 0],
|
63 |
|
64 | bold: [1, 22],
|
65 | dim: [2, 22],
|
66 | italic: [3, 23],
|
67 | underline: [4, 24],
|
68 | inverse: [7, 27],
|
69 | hidden: [8, 28],
|
70 | strikethrough: [9, 29],
|
71 |
|
72 | black: [30, 39],
|
73 | red: [31, 39],
|
74 | green: [32, 39],
|
75 | yellow: [33, 39],
|
76 | blue: [34, 39],
|
77 | magenta: [35, 39],
|
78 | cyan: [36, 39],
|
79 | white: [37, 39],
|
80 | gray: [90, 39],
|
81 | grey: [90, 39],
|
82 |
|
83 | brightRed: [91, 39],
|
84 | brightGreen: [92, 39],
|
85 | brightYellow: [93, 39],
|
86 | brightBlue: [94, 39],
|
87 | brightMagenta: [95, 39],
|
88 | brightCyan: [96, 39],
|
89 | brightWhite: [97, 39],
|
90 |
|
91 | bgBlack: [40, 49],
|
92 | bgRed: [41, 49],
|
93 | bgGreen: [42, 49],
|
94 | bgYellow: [43, 49],
|
95 | bgBlue: [44, 49],
|
96 | bgMagenta: [45, 49],
|
97 | bgCyan: [46, 49],
|
98 | bgWhite: [47, 49],
|
99 | bgGray: [100, 49],
|
100 | bgGrey: [100, 49],
|
101 |
|
102 | bgBrightRed: [101, 49],
|
103 | bgBrightGreen: [102, 49],
|
104 | bgBrightYellow: [103, 49],
|
105 | bgBrightBlue: [104, 49],
|
106 | bgBrightMagenta: [105, 49],
|
107 | bgBrightCyan: [106, 49],
|
108 | bgBrightWhite: [107, 49],
|
109 |
|
110 |
|
111 | blackBG: [40, 49],
|
112 | redBG: [41, 49],
|
113 | greenBG: [42, 49],
|
114 | yellowBG: [43, 49],
|
115 | blueBG: [44, 49],
|
116 | magentaBG: [45, 49],
|
117 | cyanBG: [46, 49],
|
118 | whiteBG: [47, 49],
|
119 |
|
120 | };
|
121 |
|
122 | Object.keys(codes).forEach(function(key) {
|
123 | var val = codes[key];
|
124 | var style = styles[key] = [];
|
125 | style.open = '\u001b[' + val[0] + 'm';
|
126 | style.close = '\u001b[' + val[1] + 'm';
|
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 | var hasFlag = function(flag, argv) {
|
155 | argv = argv || process.argv;
|
156 |
|
157 | var terminatorPos = argv.indexOf('--');
|
158 | var prefix = /^-{1,2}/.test(flag) ? '' : '--';
|
159 | var pos = argv.indexOf(prefix + flag);
|
160 |
|
161 | return pos !== -1 && (terminatorPos === -1 ? true : pos < terminatorPos);
|
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 | var env = process.env;
|
193 |
|
194 | var forceColor = void 0;
|
195 | if (hasFlag('no-color') || hasFlag('no-colors') || hasFlag('color=false')) {
|
196 | forceColor = false;
|
197 | } else if (hasFlag('color') || hasFlag('colors') || hasFlag('color=true')
|
198 | || hasFlag('color=always')) {
|
199 | forceColor = true;
|
200 | }
|
201 | if ('FORCE_COLOR' in env) {
|
202 | forceColor = env.FORCE_COLOR.length === 0
|
203 | || parseInt(env.FORCE_COLOR, 10) !== 0;
|
204 | }
|
205 |
|
206 | function translateLevel(level) {
|
207 | if (level === 0) {
|
208 | return false;
|
209 | }
|
210 |
|
211 | return {
|
212 | level: level,
|
213 | hasBasic: true,
|
214 | has256: level >= 2,
|
215 | has16m: level >= 3,
|
216 | };
|
217 | }
|
218 |
|
219 | function supportsColor(stream) {
|
220 | if (forceColor === false) {
|
221 | return 0;
|
222 | }
|
223 |
|
224 | if (hasFlag('color=16m') || hasFlag('color=full')
|
225 | || hasFlag('color=truecolor')) {
|
226 | return 3;
|
227 | }
|
228 |
|
229 | if (hasFlag('color=256')) {
|
230 | return 2;
|
231 | }
|
232 |
|
233 | if (stream && !stream.isTTY && forceColor !== true) {
|
234 | return 0;
|
235 | }
|
236 |
|
237 | var min = forceColor ? 1 : 0;
|
238 |
|
239 | if (process.platform === 'win32') {
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | var osRelease = os.release().split('.');
|
247 | if (Number(process.versions.node.split('.')[0]) >= 8
|
248 | && Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
|
249 | return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
250 | }
|
251 |
|
252 | return 1;
|
253 | }
|
254 |
|
255 | if ('CI' in env) {
|
256 | if (['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'].some(function(sign) {
|
257 | return sign in env;
|
258 | }) || env.CI_NAME === 'codeship') {
|
259 | return 1;
|
260 | }
|
261 |
|
262 | return min;
|
263 | }
|
264 |
|
265 | if ('TEAMCITY_VERSION' in env) {
|
266 | return (/^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0
|
267 | );
|
268 | }
|
269 |
|
270 | if ('TERM_PROGRAM' in env) {
|
271 | var version = parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
|
272 |
|
273 | switch (env.TERM_PROGRAM) {
|
274 | case 'iTerm.app':
|
275 | return version >= 3 ? 3 : 2;
|
276 | case 'Hyper':
|
277 | return 3;
|
278 | case 'Apple_Terminal':
|
279 | return 2;
|
280 |
|
281 | }
|
282 | }
|
283 |
|
284 | if (/-256(color)?$/i.test(env.TERM)) {
|
285 | return 2;
|
286 | }
|
287 |
|
288 | if (/^screen|^xterm|^vt100|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
|
289 | return 1;
|
290 | }
|
291 |
|
292 | if ('COLORTERM' in env) {
|
293 | return 1;
|
294 | }
|
295 |
|
296 | if (env.TERM === 'dumb') {
|
297 | return min;
|
298 | }
|
299 |
|
300 | return min;
|
301 | }
|
302 |
|
303 | function getSupportLevel(stream) {
|
304 | var level = supportsColor(stream);
|
305 | return translateLevel(level);
|
306 | }
|
307 |
|
308 | var supportsColors = {
|
309 | supportsColor: getSupportLevel,
|
310 | stdout: getSupportLevel(process.stdout),
|
311 | stderr: getSupportLevel(process.stderr),
|
312 | };
|
313 |
|
314 | var trap = createCommonjsModule(function (module) {
|
315 | module['exports'] = function runTheTrap(text, options) {
|
316 | var result = '';
|
317 | text = text || 'Run the trap, drop the bass';
|
318 | text = text.split('');
|
319 | var trap = {
|
320 | a: ['\u0040', '\u0104', '\u023a', '\u0245', '\u0394', '\u039b', '\u0414'],
|
321 | b: ['\u00df', '\u0181', '\u0243', '\u026e', '\u03b2', '\u0e3f'],
|
322 | c: ['\u00a9', '\u023b', '\u03fe'],
|
323 | d: ['\u00d0', '\u018a', '\u0500', '\u0501', '\u0502', '\u0503'],
|
324 | e: ['\u00cb', '\u0115', '\u018e', '\u0258', '\u03a3', '\u03be', '\u04bc',
|
325 | '\u0a6c'],
|
326 | f: ['\u04fa'],
|
327 | g: ['\u0262'],
|
328 | h: ['\u0126', '\u0195', '\u04a2', '\u04ba', '\u04c7', '\u050a'],
|
329 | i: ['\u0f0f'],
|
330 | j: ['\u0134'],
|
331 | k: ['\u0138', '\u04a0', '\u04c3', '\u051e'],
|
332 | l: ['\u0139'],
|
333 | m: ['\u028d', '\u04cd', '\u04ce', '\u0520', '\u0521', '\u0d69'],
|
334 | n: ['\u00d1', '\u014b', '\u019d', '\u0376', '\u03a0', '\u048a'],
|
335 | o: ['\u00d8', '\u00f5', '\u00f8', '\u01fe', '\u0298', '\u047a', '\u05dd',
|
336 | '\u06dd', '\u0e4f'],
|
337 | p: ['\u01f7', '\u048e'],
|
338 | q: ['\u09cd'],
|
339 | r: ['\u00ae', '\u01a6', '\u0210', '\u024c', '\u0280', '\u042f'],
|
340 | s: ['\u00a7', '\u03de', '\u03df', '\u03e8'],
|
341 | t: ['\u0141', '\u0166', '\u0373'],
|
342 | u: ['\u01b1', '\u054d'],
|
343 | v: ['\u05d8'],
|
344 | w: ['\u0428', '\u0460', '\u047c', '\u0d70'],
|
345 | x: ['\u04b2', '\u04fe', '\u04fc', '\u04fd'],
|
346 | y: ['\u00a5', '\u04b0', '\u04cb'],
|
347 | z: ['\u01b5', '\u0240'],
|
348 | };
|
349 | text.forEach(function(c) {
|
350 | c = c.toLowerCase();
|
351 | var chars = trap[c] || [' '];
|
352 | var rand = Math.floor(Math.random() * chars.length);
|
353 | if (typeof trap[c] !== 'undefined') {
|
354 | result += trap[c][rand];
|
355 | } else {
|
356 | result += c;
|
357 | }
|
358 | });
|
359 | return result;
|
360 | };
|
361 | });
|
362 |
|
363 | var zalgo = createCommonjsModule(function (module) {
|
364 |
|
365 | module['exports'] = function zalgo(text, options) {
|
366 | text = text || ' he is here ';
|
367 | var soul = {
|
368 | 'up': [
|
369 | '̍', '̎', '̄', '̅',
|
370 | '̿', '̑', '̆', '̐',
|
371 | '͒', '͗', '͑', '̇',
|
372 | '̈', '̊', '͂', '̓',
|
373 | '̈', '͊', '͋', '͌',
|
374 | '̃', '̂', '̌', '͐',
|
375 | '̀', '́', '̋', '̏',
|
376 | '̒', '̓', '̔', '̽',
|
377 | '̉', 'ͣ', 'ͤ', 'ͥ',
|
378 | 'ͦ', 'ͧ', 'ͨ', 'ͩ',
|
379 | 'ͪ', 'ͫ', 'ͬ', 'ͭ',
|
380 | 'ͮ', 'ͯ', '̾', '͛',
|
381 | '͆', '̚',
|
382 | ],
|
383 | 'down': [
|
384 | '̖', '̗', '̘', '̙',
|
385 | '̜', '̝', '̞', '̟',
|
386 | '̠', '̤', '̥', '̦',
|
387 | '̩', '̪', '̫', '̬',
|
388 | '̭', '̮', '̯', '̰',
|
389 | '̱', '̲', '̳', '̹',
|
390 | '̺', '̻', '̼', 'ͅ',
|
391 | '͇', '͈', '͉', '͍',
|
392 | '͎', '͓', '͔', '͕',
|
393 | '͖', '͙', '͚', '̣',
|
394 | ],
|
395 | 'mid': [
|
396 | '̕', '̛', '̀', '́',
|
397 | '͘', '̡', '̢', '̧',
|
398 | '̨', '̴', '̵', '̶',
|
399 | '͜', '͝', '͞',
|
400 | '͟', '͠', '͢', '̸',
|
401 | '̷', '͡', ' ҉',
|
402 | ],
|
403 | };
|
404 | var all = [].concat(soul.up, soul.down, soul.mid);
|
405 |
|
406 | function randomNumber(range) {
|
407 | var r = Math.floor(Math.random() * range);
|
408 | return r;
|
409 | }
|
410 |
|
411 | function isChar(character) {
|
412 | var bool = false;
|
413 | all.filter(function(i) {
|
414 | bool = (i === character);
|
415 | });
|
416 | return bool;
|
417 | }
|
418 |
|
419 |
|
420 | function heComes(text, options) {
|
421 | var result = '';
|
422 | var counts;
|
423 | var l;
|
424 | options = options || {};
|
425 | options['up'] =
|
426 | typeof options['up'] !== 'undefined' ? options['up'] : true;
|
427 | options['mid'] =
|
428 | typeof options['mid'] !== 'undefined' ? options['mid'] : true;
|
429 | options['down'] =
|
430 | typeof options['down'] !== 'undefined' ? options['down'] : true;
|
431 | options['size'] =
|
432 | typeof options['size'] !== 'undefined' ? options['size'] : 'maxi';
|
433 | text = text.split('');
|
434 | for (l in text) {
|
435 | if (isChar(l)) {
|
436 | continue;
|
437 | }
|
438 | result = result + text[l];
|
439 | counts = {'up': 0, 'down': 0, 'mid': 0};
|
440 | switch (options.size) {
|
441 | case 'mini':
|
442 | counts.up = randomNumber(8);
|
443 | counts.mid = randomNumber(2);
|
444 | counts.down = randomNumber(8);
|
445 | break;
|
446 | case 'maxi':
|
447 | counts.up = randomNumber(16) + 3;
|
448 | counts.mid = randomNumber(4) + 1;
|
449 | counts.down = randomNumber(64) + 3;
|
450 | break;
|
451 | default:
|
452 | counts.up = randomNumber(8) + 1;
|
453 | counts.mid = randomNumber(6) / 2;
|
454 | counts.down = randomNumber(8) + 1;
|
455 | break;
|
456 | }
|
457 |
|
458 | var arr = ['up', 'mid', 'down'];
|
459 | for (var d in arr) {
|
460 | var index = arr[d];
|
461 | for (var i = 0; i <= counts[index]; i++) {
|
462 | if (options[index]) {
|
463 | result = result + soul[index][randomNumber(soul[index].length)];
|
464 | }
|
465 | }
|
466 | }
|
467 | }
|
468 | return result;
|
469 | }
|
470 |
|
471 | return heComes(text, options);
|
472 | };
|
473 | });
|
474 |
|
475 | var america = createCommonjsModule(function (module) {
|
476 | module['exports'] = function(colors) {
|
477 | return function(letter, i, exploded) {
|
478 | if (letter === ' ') return letter;
|
479 | switch (i%3) {
|
480 | case 0: return colors.red(letter);
|
481 | case 1: return colors.white(letter);
|
482 | case 2: return colors.blue(letter);
|
483 | }
|
484 | };
|
485 | };
|
486 | });
|
487 |
|
488 | var zebra = createCommonjsModule(function (module) {
|
489 | module['exports'] = function(colors) {
|
490 | return function(letter, i, exploded) {
|
491 | return i % 2 === 0 ? letter : colors.inverse(letter);
|
492 | };
|
493 | };
|
494 | });
|
495 |
|
496 | var rainbow = createCommonjsModule(function (module) {
|
497 | module['exports'] = function(colors) {
|
498 |
|
499 | var rainbowColors = ['red', 'yellow', 'green', 'blue', 'magenta'];
|
500 | return function(letter, i, exploded) {
|
501 | if (letter === ' ') {
|
502 | return letter;
|
503 | } else {
|
504 | return colors[rainbowColors[i++ % rainbowColors.length]](letter);
|
505 | }
|
506 | };
|
507 | };
|
508 | });
|
509 |
|
510 | var random = createCommonjsModule(function (module) {
|
511 | module['exports'] = function(colors) {
|
512 | var available = ['underline', 'inverse', 'grey', 'yellow', 'red', 'green',
|
513 | 'blue', 'white', 'cyan', 'magenta', 'brightYellow', 'brightRed',
|
514 | 'brightGreen', 'brightBlue', 'brightWhite', 'brightCyan', 'brightMagenta'];
|
515 | return function(letter, i, exploded) {
|
516 | return letter === ' ' ? letter :
|
517 | colors[
|
518 | available[Math.round(Math.random() * (available.length - 2))]
|
519 | ](letter);
|
520 | };
|
521 | };
|
522 | });
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 | var colors_1 = createCommonjsModule(function (module) {
|
555 | var colors = {};
|
556 | module['exports'] = colors;
|
557 |
|
558 | colors.themes = {};
|
559 |
|
560 |
|
561 | var ansiStyles = colors.styles = styles_1;
|
562 | var defineProps = Object.defineProperties;
|
563 | var newLineRegex = new RegExp(/[\r\n]+/g);
|
564 |
|
565 | colors.supportsColor = supportsColors.supportsColor;
|
566 |
|
567 | if (typeof colors.enabled === 'undefined') {
|
568 | colors.enabled = colors.supportsColor() !== false;
|
569 | }
|
570 |
|
571 | colors.enable = function() {
|
572 | colors.enabled = true;
|
573 | };
|
574 |
|
575 | colors.disable = function() {
|
576 | colors.enabled = false;
|
577 | };
|
578 |
|
579 | colors.stripColors = colors.strip = function(str) {
|
580 | return ('' + str).replace(/\x1B\[\d+m/g, '');
|
581 | };
|
582 |
|
583 |
|
584 | colors.stylize = function stylize(str, style) {
|
585 | if (!colors.enabled) {
|
586 | return str+'';
|
587 | }
|
588 |
|
589 | var styleMap = ansiStyles[style];
|
590 |
|
591 |
|
592 | if(!styleMap && style in colors){
|
593 |
|
594 |
|
595 | return colors[style](str);
|
596 | }
|
597 |
|
598 | return styleMap.open + str + styleMap.close;
|
599 | };
|
600 |
|
601 | var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
|
602 | var escapeStringRegexp = function(str) {
|
603 | if (typeof str !== 'string') {
|
604 | throw new TypeError('Expected a string');
|
605 | }
|
606 | return str.replace(matchOperatorsRe, '\\$&');
|
607 | };
|
608 |
|
609 | function build(_styles) {
|
610 | var builder = function builder() {
|
611 | return applyStyle.apply(builder, arguments);
|
612 | };
|
613 | builder._styles = _styles;
|
614 |
|
615 |
|
616 | builder.__proto__ = proto;
|
617 | return builder;
|
618 | }
|
619 |
|
620 | var styles = (function() {
|
621 | var ret = {};
|
622 | ansiStyles.grey = ansiStyles.gray;
|
623 | Object.keys(ansiStyles).forEach(function(key) {
|
624 | ansiStyles[key].closeRe =
|
625 | new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
626 | ret[key] = {
|
627 | get: function() {
|
628 | return build(this._styles.concat(key));
|
629 | },
|
630 | };
|
631 | });
|
632 | return ret;
|
633 | })();
|
634 |
|
635 | var proto = defineProps(function colors() {}, styles);
|
636 |
|
637 | function applyStyle() {
|
638 | var args = Array.prototype.slice.call(arguments);
|
639 |
|
640 | var str = args.map(function(arg) {
|
641 |
|
642 | if (arg != null && arg.constructor === String) {
|
643 | return arg;
|
644 | } else {
|
645 | return util.inspect(arg);
|
646 | }
|
647 | }).join(' ');
|
648 |
|
649 | if (!colors.enabled || !str) {
|
650 | return str;
|
651 | }
|
652 |
|
653 | var newLinesPresent = str.indexOf('\n') != -1;
|
654 |
|
655 | var nestedStyles = this._styles;
|
656 |
|
657 | var i = nestedStyles.length;
|
658 | while (i--) {
|
659 | var code = ansiStyles[nestedStyles[i]];
|
660 | str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
661 | if (newLinesPresent) {
|
662 | str = str.replace(newLineRegex, function(match) {
|
663 | return code.close + match + code.open;
|
664 | });
|
665 | }
|
666 | }
|
667 |
|
668 | return str;
|
669 | }
|
670 |
|
671 | colors.setTheme = function(theme) {
|
672 | if (typeof theme === 'string') {
|
673 | console.log('colors.setTheme now only accepts an object, not a string. ' +
|
674 | 'If you are trying to set a theme from a file, it is now your (the ' +
|
675 | 'caller\'s) responsibility to require the file. The old syntax ' +
|
676 | 'looked like colors.setTheme(__dirname + ' +
|
677 | '\'/../themes/generic-logging.js\'); The new syntax looks like '+
|
678 | 'colors.setTheme(require(__dirname + ' +
|
679 | '\'/../themes/generic-logging.js\'));');
|
680 | return;
|
681 | }
|
682 | for (var style in theme) {
|
683 | (function(style) {
|
684 | colors[style] = function(str) {
|
685 | if (typeof theme[style] === 'object') {
|
686 | var out = str;
|
687 | for (var i in theme[style]) {
|
688 | out = colors[theme[style][i]](out);
|
689 | }
|
690 | return out;
|
691 | }
|
692 | return colors[theme[style]](str);
|
693 | };
|
694 | })(style);
|
695 | }
|
696 | };
|
697 |
|
698 | function init() {
|
699 | var ret = {};
|
700 | Object.keys(styles).forEach(function(name) {
|
701 | ret[name] = {
|
702 | get: function() {
|
703 | return build([name]);
|
704 | },
|
705 | };
|
706 | });
|
707 | return ret;
|
708 | }
|
709 |
|
710 | var sequencer = function sequencer(map, str) {
|
711 | var exploded = str.split('');
|
712 | exploded = exploded.map(map);
|
713 | return exploded.join('');
|
714 | };
|
715 |
|
716 |
|
717 | colors.trap = trap;
|
718 | colors.zalgo = zalgo;
|
719 |
|
720 |
|
721 | colors.maps = {};
|
722 | colors.maps.america = america(colors);
|
723 | colors.maps.zebra = zebra(colors);
|
724 | colors.maps.rainbow = rainbow(colors);
|
725 | colors.maps.random = random(colors);
|
726 |
|
727 | for (var map in colors.maps) {
|
728 | (function(map) {
|
729 | colors[map] = function(str) {
|
730 | return sequencer(colors.maps[map], str);
|
731 | };
|
732 | })(map);
|
733 | }
|
734 |
|
735 | defineProps(colors, init());
|
736 | });
|
737 |
|
738 | var safe = createCommonjsModule(function (module) {
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 | module['exports'] = colors_1;
|
749 | });
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 | var commander = createCommonjsModule(function (module, exports) {
|
756 | var EventEmitter = require$$0.EventEmitter;
|
757 | var spawn = require$$1.spawn;
|
758 |
|
759 | var dirname = path.dirname;
|
760 | var basename = path.basename;
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 | util.inherits(Command, EventEmitter);
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 | exports = module.exports = new Command();
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 | exports.Command = Command;
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 | exports.Option = Option;
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 | function Option(flags, description) {
|
796 | this.flags = flags;
|
797 | this.required = ~flags.indexOf('<');
|
798 | this.optional = ~flags.indexOf('[');
|
799 | this.bool = !~flags.indexOf('-no-');
|
800 | flags = flags.split(/[ ,|]+/);
|
801 | if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
|
802 | this.long = flags.shift();
|
803 | this.description = description || '';
|
804 | }
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 | Option.prototype.name = function() {
|
814 | return this.long
|
815 | .replace('--', '')
|
816 | .replace('no-', '');
|
817 | };
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 |
|
827 | Option.prototype.attributeName = function() {
|
828 | return camelcase(this.name());
|
829 | };
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 | Option.prototype.is = function(arg) {
|
840 | return this.short === arg || this.long === arg;
|
841 | };
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 | function Command(name) {
|
851 | this.commands = [];
|
852 | this.options = [];
|
853 | this._execs = {};
|
854 | this._allowUnknownOption = false;
|
855 | this._args = [];
|
856 | this._name = name || '';
|
857 | }
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 |
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 | Command.prototype.command = function(name, desc, opts) {
|
921 | if (typeof desc === 'object' && desc !== null) {
|
922 | opts = desc;
|
923 | desc = null;
|
924 | }
|
925 | opts = opts || {};
|
926 | var args = name.split(/ +/);
|
927 | var cmd = new Command(args.shift());
|
928 |
|
929 | if (desc) {
|
930 | cmd.description(desc);
|
931 | this.executables = true;
|
932 | this._execs[cmd._name] = true;
|
933 | if (opts.isDefault) this.defaultExecutable = cmd._name;
|
934 | }
|
935 | cmd._noHelp = !!opts.noHelp;
|
936 | this.commands.push(cmd);
|
937 | cmd.parseExpectedArgs(args);
|
938 | cmd.parent = this;
|
939 |
|
940 | if (desc) return this;
|
941 | return cmd;
|
942 | };
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 | Command.prototype.arguments = function(desc) {
|
951 | return this.parseExpectedArgs(desc.split(/ +/));
|
952 | };
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 | Command.prototype.addImplicitHelpCommand = function() {
|
962 | this.command('help [cmd]', 'display help for [cmd]');
|
963 | };
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 | Command.prototype.parseExpectedArgs = function(args) {
|
976 | if (!args.length) return;
|
977 | var self = this;
|
978 | args.forEach(function(arg) {
|
979 | var argDetails = {
|
980 | required: false,
|
981 | name: '',
|
982 | variadic: false
|
983 | };
|
984 |
|
985 | switch (arg[0]) {
|
986 | case '<':
|
987 | argDetails.required = true;
|
988 | argDetails.name = arg.slice(1, -1);
|
989 | break;
|
990 | case '[':
|
991 | argDetails.name = arg.slice(1, -1);
|
992 | break;
|
993 | }
|
994 |
|
995 | if (argDetails.name.length > 3 && argDetails.name.slice(-3) === '...') {
|
996 | argDetails.variadic = true;
|
997 | argDetails.name = argDetails.name.slice(0, -3);
|
998 | }
|
999 | if (argDetails.name) {
|
1000 | self._args.push(argDetails);
|
1001 | }
|
1002 | });
|
1003 | return this;
|
1004 | };
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 |
|
1023 | Command.prototype.action = function(fn) {
|
1024 | var self = this;
|
1025 | var listener = function(args, unknown) {
|
1026 |
|
1027 | args = args || [];
|
1028 | unknown = unknown || [];
|
1029 |
|
1030 | var parsed = self.parseOptions(unknown);
|
1031 |
|
1032 |
|
1033 | outputHelpIfNecessary(self, parsed.unknown);
|
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 | if (parsed.unknown.length > 0) {
|
1039 | self.unknownOption(parsed.unknown[0]);
|
1040 | }
|
1041 |
|
1042 |
|
1043 | if (parsed.args.length) args = parsed.args.concat(args);
|
1044 |
|
1045 | self._args.forEach(function(arg, i) {
|
1046 | if (arg.required && args[i] == null) {
|
1047 | self.missingArgument(arg.name);
|
1048 | } else if (arg.variadic) {
|
1049 | if (i !== self._args.length - 1) {
|
1050 | self.variadicArgNotLast(arg.name);
|
1051 | }
|
1052 |
|
1053 | args[i] = args.splice(i);
|
1054 | }
|
1055 | });
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 | if (self._args.length) {
|
1061 | args[self._args.length] = self;
|
1062 | } else {
|
1063 | args.push(self);
|
1064 | }
|
1065 |
|
1066 | fn.apply(self, args);
|
1067 | };
|
1068 | var parent = this.parent || this;
|
1069 | var name = parent === this ? '*' : this._name;
|
1070 | parent.on('command:' + name, listener);
|
1071 | if (this._alias) parent.on('command:' + this._alias, listener);
|
1072 | return this;
|
1073 | };
|
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 |
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 |
|
1102 |
|
1103 |
|
1104 |
|
1105 |
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 |
|
1115 |
|
1116 |
|
1117 |
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 | Command.prototype.option = function(flags, description, fn, defaultValue) {
|
1125 | var self = this,
|
1126 | option = new Option(flags, description),
|
1127 | oname = option.name(),
|
1128 | name = option.attributeName();
|
1129 |
|
1130 |
|
1131 | if (typeof fn !== 'function') {
|
1132 | if (fn instanceof RegExp) {
|
1133 | var regex = fn;
|
1134 | fn = function(val, def) {
|
1135 | var m = regex.exec(val);
|
1136 | return m ? m[0] : def;
|
1137 | };
|
1138 | } else {
|
1139 | defaultValue = fn;
|
1140 | fn = null;
|
1141 | }
|
1142 | }
|
1143 |
|
1144 |
|
1145 | if (!option.bool || option.optional || option.required) {
|
1146 |
|
1147 | if (!option.bool) defaultValue = true;
|
1148 |
|
1149 | if (defaultValue !== undefined) {
|
1150 | self[name] = defaultValue;
|
1151 | option.defaultValue = defaultValue;
|
1152 | }
|
1153 | }
|
1154 |
|
1155 |
|
1156 | this.options.push(option);
|
1157 |
|
1158 |
|
1159 |
|
1160 | this.on('option:' + oname, function(val) {
|
1161 |
|
1162 | if (val !== null && fn) {
|
1163 | val = fn(val, self[name] === undefined ? defaultValue : self[name]);
|
1164 | }
|
1165 |
|
1166 |
|
1167 | if (typeof self[name] === 'boolean' || typeof self[name] === 'undefined') {
|
1168 |
|
1169 | if (val == null) {
|
1170 | self[name] = option.bool
|
1171 | ? defaultValue || true
|
1172 | : false;
|
1173 | } else {
|
1174 | self[name] = val;
|
1175 | }
|
1176 | } else if (val !== null) {
|
1177 |
|
1178 | self[name] = val;
|
1179 | }
|
1180 | });
|
1181 |
|
1182 | return this;
|
1183 | };
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 | Command.prototype.allowUnknownOption = function(arg) {
|
1193 | this._allowUnknownOption = arguments.length === 0 || arg;
|
1194 | return this;
|
1195 | };
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 | Command.prototype.parse = function(argv) {
|
1206 |
|
1207 | if (this.executables) this.addImplicitHelpCommand();
|
1208 |
|
1209 |
|
1210 | this.rawArgs = argv;
|
1211 |
|
1212 |
|
1213 | this._name = this._name || basename(argv[1], '.js');
|
1214 |
|
1215 |
|
1216 | if (this.executables && argv.length < 3 && !this.defaultExecutable) {
|
1217 |
|
1218 | argv.push('--help');
|
1219 | }
|
1220 |
|
1221 |
|
1222 | var parsed = this.parseOptions(this.normalize(argv.slice(2)));
|
1223 | var args = this.args = parsed.args;
|
1224 |
|
1225 | var result = this.parseArgs(this.args, parsed.unknown);
|
1226 |
|
1227 |
|
1228 | var name = result.args[0];
|
1229 |
|
1230 | var aliasCommand = null;
|
1231 |
|
1232 | if (name) {
|
1233 | aliasCommand = this.commands.filter(function(command) {
|
1234 | return command.alias() === name;
|
1235 | })[0];
|
1236 | }
|
1237 |
|
1238 | if (this._execs[name] && typeof this._execs[name] !== 'function') {
|
1239 | return this.executeSubCommand(argv, args, parsed.unknown);
|
1240 | } else if (aliasCommand) {
|
1241 |
|
1242 | args[0] = aliasCommand._name;
|
1243 | return this.executeSubCommand(argv, args, parsed.unknown);
|
1244 | } else if (this.defaultExecutable) {
|
1245 |
|
1246 | args.unshift(this.defaultExecutable);
|
1247 | return this.executeSubCommand(argv, args, parsed.unknown);
|
1248 | }
|
1249 |
|
1250 | return result;
|
1251 | };
|
1252 |
|
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 |
|
1260 |
|
1261 |
|
1262 | Command.prototype.executeSubCommand = function(argv, args, unknown) {
|
1263 | args = args.concat(unknown);
|
1264 |
|
1265 | if (!args.length) this.help();
|
1266 | if (args[0] === 'help' && args.length === 1) this.help();
|
1267 |
|
1268 |
|
1269 | if (args[0] === 'help') {
|
1270 | args[0] = args[1];
|
1271 | args[1] = '--help';
|
1272 | }
|
1273 |
|
1274 |
|
1275 | var f = argv[1];
|
1276 |
|
1277 | var bin = basename(f, '.js') + '-' + args[0];
|
1278 |
|
1279 |
|
1280 |
|
1281 | var baseDir,
|
1282 | link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f;
|
1283 |
|
1284 |
|
1285 | if (link !== f && link.charAt(0) !== '/') {
|
1286 | link = path.join(dirname(f), link);
|
1287 | }
|
1288 | baseDir = dirname(link);
|
1289 |
|
1290 |
|
1291 | var localBin = path.join(baseDir, bin);
|
1292 |
|
1293 |
|
1294 | var isExplicitJS = false;
|
1295 | if (exists(localBin + '.js')) {
|
1296 | bin = localBin + '.js';
|
1297 | isExplicitJS = true;
|
1298 | } else if (exists(localBin)) {
|
1299 | bin = localBin;
|
1300 | }
|
1301 |
|
1302 | args = args.slice(1);
|
1303 |
|
1304 | var proc;
|
1305 | if (process.platform !== 'win32') {
|
1306 | if (isExplicitJS) {
|
1307 | args.unshift(bin);
|
1308 |
|
1309 | args = (process.execArgv || []).concat(args);
|
1310 |
|
1311 | proc = spawn(process.argv[0], args, { stdio: 'inherit', customFds: [0, 1, 2] });
|
1312 | } else {
|
1313 | proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] });
|
1314 | }
|
1315 | } else {
|
1316 | args.unshift(bin);
|
1317 | proc = spawn(process.execPath, args, { stdio: 'inherit' });
|
1318 | }
|
1319 |
|
1320 | var signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
|
1321 | signals.forEach(function(signal) {
|
1322 | process.on(signal, function() {
|
1323 | if (proc.killed === false && proc.exitCode === null) {
|
1324 | proc.kill(signal);
|
1325 | }
|
1326 | });
|
1327 | });
|
1328 | proc.on('close', process.exit.bind(process));
|
1329 | proc.on('error', function(err) {
|
1330 | if (err.code === 'ENOENT') {
|
1331 | console.error('\n %s(1) does not exist, try --help\n', bin);
|
1332 | } else if (err.code === 'EACCES') {
|
1333 | console.error('\n %s(1) not executable. try chmod or run with root\n', bin);
|
1334 | }
|
1335 | process.exit(1);
|
1336 | });
|
1337 |
|
1338 |
|
1339 | this.runningCommand = proc;
|
1340 | };
|
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 | Command.prototype.normalize = function(args) {
|
1353 | var ret = [],
|
1354 | arg,
|
1355 | lastOpt,
|
1356 | index;
|
1357 |
|
1358 | for (var i = 0, len = args.length; i < len; ++i) {
|
1359 | arg = args[i];
|
1360 | if (i > 0) {
|
1361 | lastOpt = this.optionFor(args[i - 1]);
|
1362 | }
|
1363 |
|
1364 | if (arg === '--') {
|
1365 |
|
1366 | ret = ret.concat(args.slice(i));
|
1367 | break;
|
1368 | } else if (lastOpt && lastOpt.required) {
|
1369 | ret.push(arg);
|
1370 | } else if (arg.length > 1 && arg[0] === '-' && arg[1] !== '-') {
|
1371 | arg.slice(1).split('').forEach(function(c) {
|
1372 | ret.push('-' + c);
|
1373 | });
|
1374 | } else if (/^--/.test(arg) && ~(index = arg.indexOf('='))) {
|
1375 | ret.push(arg.slice(0, index), arg.slice(index + 1));
|
1376 | } else {
|
1377 | ret.push(arg);
|
1378 | }
|
1379 | }
|
1380 |
|
1381 | return ret;
|
1382 | };
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 |
|
1390 |
|
1391 |
|
1392 |
|
1393 |
|
1394 |
|
1395 |
|
1396 | Command.prototype.parseArgs = function(args, unknown) {
|
1397 | var name;
|
1398 |
|
1399 | if (args.length) {
|
1400 | name = args[0];
|
1401 | if (this.listeners('command:' + name).length) {
|
1402 | this.emit('command:' + args.shift(), args, unknown);
|
1403 | } else {
|
1404 | this.emit('command:*', args);
|
1405 | }
|
1406 | } else {
|
1407 | outputHelpIfNecessary(this, unknown);
|
1408 |
|
1409 |
|
1410 |
|
1411 | if (unknown.length > 0) {
|
1412 | this.unknownOption(unknown[0]);
|
1413 | }
|
1414 | }
|
1415 |
|
1416 | return this;
|
1417 | };
|
1418 |
|
1419 |
|
1420 |
|
1421 |
|
1422 |
|
1423 |
|
1424 |
|
1425 |
|
1426 |
|
1427 | Command.prototype.optionFor = function(arg) {
|
1428 | for (var i = 0, len = this.options.length; i < len; ++i) {
|
1429 | if (this.options[i].is(arg)) {
|
1430 | return this.options[i];
|
1431 | }
|
1432 | }
|
1433 | };
|
1434 |
|
1435 |
|
1436 |
|
1437 |
|
1438 |
|
1439 |
|
1440 |
|
1441 |
|
1442 |
|
1443 |
|
1444 | Command.prototype.parseOptions = function(argv) {
|
1445 | var args = [],
|
1446 | len = argv.length,
|
1447 | literal,
|
1448 | option,
|
1449 | arg;
|
1450 |
|
1451 | var unknownOptions = [];
|
1452 |
|
1453 |
|
1454 | for (var i = 0; i < len; ++i) {
|
1455 | arg = argv[i];
|
1456 |
|
1457 |
|
1458 | if (literal) {
|
1459 | args.push(arg);
|
1460 | continue;
|
1461 | }
|
1462 |
|
1463 | if (arg === '--') {
|
1464 | literal = true;
|
1465 | continue;
|
1466 | }
|
1467 |
|
1468 |
|
1469 | option = this.optionFor(arg);
|
1470 |
|
1471 |
|
1472 | if (option) {
|
1473 |
|
1474 | if (option.required) {
|
1475 | arg = argv[++i];
|
1476 | if (arg == null) return this.optionMissingArgument(option);
|
1477 | this.emit('option:' + option.name(), arg);
|
1478 |
|
1479 | } else if (option.optional) {
|
1480 | arg = argv[i + 1];
|
1481 | if (arg == null || (arg[0] === '-' && arg !== '-')) {
|
1482 | arg = null;
|
1483 | } else {
|
1484 | ++i;
|
1485 | }
|
1486 | this.emit('option:' + option.name(), arg);
|
1487 |
|
1488 | } else {
|
1489 | this.emit('option:' + option.name());
|
1490 | }
|
1491 | continue;
|
1492 | }
|
1493 |
|
1494 |
|
1495 | if (arg.length > 1 && arg[0] === '-') {
|
1496 | unknownOptions.push(arg);
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 | if ((i + 1) < argv.length && argv[i + 1][0] !== '-') {
|
1502 | unknownOptions.push(argv[++i]);
|
1503 | }
|
1504 | continue;
|
1505 | }
|
1506 |
|
1507 |
|
1508 | args.push(arg);
|
1509 | }
|
1510 |
|
1511 | return { args: args, unknown: unknownOptions };
|
1512 | };
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 | Command.prototype.opts = function() {
|
1521 | var result = {},
|
1522 | len = this.options.length;
|
1523 |
|
1524 | for (var i = 0; i < len; i++) {
|
1525 | var key = this.options[i].attributeName();
|
1526 | result[key] = key === this._versionOptionName ? this._version : this[key];
|
1527 | }
|
1528 | return result;
|
1529 | };
|
1530 |
|
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 |
|
1536 |
|
1537 |
|
1538 | Command.prototype.missingArgument = function(name) {
|
1539 | console.error();
|
1540 | console.error(" error: missing required argument `%s'", name);
|
1541 | console.error();
|
1542 | process.exit(1);
|
1543 | };
|
1544 |
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 |
|
1550 |
|
1551 |
|
1552 |
|
1553 | Command.prototype.optionMissingArgument = function(option, flag) {
|
1554 | console.error();
|
1555 | if (flag) {
|
1556 | console.error(" error: option `%s' argument missing, got `%s'", option.flags, flag);
|
1557 | } else {
|
1558 | console.error(" error: option `%s' argument missing", option.flags);
|
1559 | }
|
1560 | console.error();
|
1561 | process.exit(1);
|
1562 | };
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 | Command.prototype.unknownOption = function(flag) {
|
1572 | if (this._allowUnknownOption) return;
|
1573 | console.error();
|
1574 | console.error(" error: unknown option `%s'", flag);
|
1575 | console.error();
|
1576 | process.exit(1);
|
1577 | };
|
1578 |
|
1579 |
|
1580 |
|
1581 |
|
1582 |
|
1583 |
|
1584 |
|
1585 |
|
1586 | Command.prototype.variadicArgNotLast = function(name) {
|
1587 | console.error();
|
1588 | console.error(" error: variadic arguments must be last `%s'", name);
|
1589 | console.error();
|
1590 | process.exit(1);
|
1591 | };
|
1592 |
|
1593 |
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 |
|
1603 |
|
1604 |
|
1605 | Command.prototype.version = function(str, flags) {
|
1606 | if (arguments.length === 0) return this._version;
|
1607 | this._version = str;
|
1608 | flags = flags || '-V, --version';
|
1609 | var versionOption = new Option(flags, 'output the version number');
|
1610 | this._versionOptionName = versionOption.long.substr(2) || 'version';
|
1611 | this.options.push(versionOption);
|
1612 | this.on('option:' + this._versionOptionName, function() {
|
1613 | process.stdout.write(str + '\n');
|
1614 | process.exit(0);
|
1615 | });
|
1616 | return this;
|
1617 | };
|
1618 |
|
1619 |
|
1620 |
|
1621 |
|
1622 |
|
1623 |
|
1624 |
|
1625 |
|
1626 |
|
1627 |
|
1628 | Command.prototype.description = function(str, argsDescription) {
|
1629 | if (arguments.length === 0) return this._description;
|
1630 | this._description = str;
|
1631 | this._argsDescription = argsDescription;
|
1632 | return this;
|
1633 | };
|
1634 |
|
1635 |
|
1636 |
|
1637 |
|
1638 |
|
1639 |
|
1640 |
|
1641 |
|
1642 |
|
1643 | Command.prototype.alias = function(alias) {
|
1644 | var command = this;
|
1645 | if (this.commands.length !== 0) {
|
1646 | command = this.commands[this.commands.length - 1];
|
1647 | }
|
1648 |
|
1649 | if (arguments.length === 0) return command._alias;
|
1650 |
|
1651 | if (alias === command._name) throw new Error('Command alias can\'t be the same as its name');
|
1652 |
|
1653 | command._alias = alias;
|
1654 | return this;
|
1655 | };
|
1656 |
|
1657 |
|
1658 |
|
1659 |
|
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 |
|
1665 | Command.prototype.usage = function(str) {
|
1666 | var args = this._args.map(function(arg) {
|
1667 | return humanReadableArgName(arg);
|
1668 | });
|
1669 |
|
1670 | var usage = '[options]' +
|
1671 | (this.commands.length ? ' [command]' : '') +
|
1672 | (this._args.length ? ' ' + args.join(' ') : '');
|
1673 |
|
1674 | if (arguments.length === 0) return this._usage || usage;
|
1675 | this._usage = str;
|
1676 |
|
1677 | return this;
|
1678 | };
|
1679 |
|
1680 |
|
1681 |
|
1682 |
|
1683 |
|
1684 |
|
1685 |
|
1686 |
|
1687 |
|
1688 | Command.prototype.name = function(str) {
|
1689 | if (arguments.length === 0) return this._name;
|
1690 | this._name = str;
|
1691 | return this;
|
1692 | };
|
1693 |
|
1694 |
|
1695 |
|
1696 |
|
1697 |
|
1698 |
|
1699 |
|
1700 |
|
1701 | Command.prototype.prepareCommands = function() {
|
1702 | return this.commands.filter(function(cmd) {
|
1703 | return !cmd._noHelp;
|
1704 | }).map(function(cmd) {
|
1705 | var args = cmd._args.map(function(arg) {
|
1706 | return humanReadableArgName(arg);
|
1707 | }).join(' ');
|
1708 |
|
1709 | return [
|
1710 | cmd._name +
|
1711 | (cmd._alias ? '|' + cmd._alias : '') +
|
1712 | (cmd.options.length ? ' [options]' : '') +
|
1713 | (args ? ' ' + args : ''),
|
1714 | cmd._description
|
1715 | ];
|
1716 | });
|
1717 | };
|
1718 |
|
1719 |
|
1720 |
|
1721 |
|
1722 |
|
1723 |
|
1724 |
|
1725 |
|
1726 | Command.prototype.largestCommandLength = function() {
|
1727 | var commands = this.prepareCommands();
|
1728 | return commands.reduce(function(max, command) {
|
1729 | return Math.max(max, command[0].length);
|
1730 | }, 0);
|
1731 | };
|
1732 |
|
1733 |
|
1734 |
|
1735 |
|
1736 |
|
1737 |
|
1738 |
|
1739 |
|
1740 | Command.prototype.largestOptionLength = function() {
|
1741 | var options = [].slice.call(this.options);
|
1742 | options.push({
|
1743 | flags: '-h, --help'
|
1744 | });
|
1745 | return options.reduce(function(max, option) {
|
1746 | return Math.max(max, option.flags.length);
|
1747 | }, 0);
|
1748 | };
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 | Command.prototype.largestArgLength = function() {
|
1758 | return this._args.reduce(function(max, arg) {
|
1759 | return Math.max(max, arg.name.length);
|
1760 | }, 0);
|
1761 | };
|
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 |
|
1768 |
|
1769 |
|
1770 | Command.prototype.padWidth = function() {
|
1771 | var width = this.largestOptionLength();
|
1772 | if (this._argsDescription && this._args.length) {
|
1773 | if (this.largestArgLength() > width) {
|
1774 | width = this.largestArgLength();
|
1775 | }
|
1776 | }
|
1777 |
|
1778 | if (this.commands && this.commands.length) {
|
1779 | if (this.largestCommandLength() > width) {
|
1780 | width = this.largestCommandLength();
|
1781 | }
|
1782 | }
|
1783 |
|
1784 | return width;
|
1785 | };
|
1786 |
|
1787 |
|
1788 |
|
1789 |
|
1790 |
|
1791 |
|
1792 |
|
1793 |
|
1794 | Command.prototype.optionHelp = function() {
|
1795 | var width = this.padWidth();
|
1796 |
|
1797 |
|
1798 | return this.options.map(function(option) {
|
1799 | return pad(option.flags, width) + ' ' + option.description +
|
1800 | ((option.bool && option.defaultValue !== undefined) ? ' (default: ' + option.defaultValue + ')' : '');
|
1801 | }).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
|
1802 | .join('\n');
|
1803 | };
|
1804 |
|
1805 |
|
1806 |
|
1807 |
|
1808 |
|
1809 |
|
1810 |
|
1811 |
|
1812 | Command.prototype.commandHelp = function() {
|
1813 | if (!this.commands.length) return '';
|
1814 |
|
1815 | var commands = this.prepareCommands();
|
1816 | var width = this.padWidth();
|
1817 |
|
1818 | return [
|
1819 | ' Commands:',
|
1820 | '',
|
1821 | commands.map(function(cmd) {
|
1822 | var desc = cmd[1] ? ' ' + cmd[1] : '';
|
1823 | return (desc ? pad(cmd[0], width) : cmd[0]) + desc;
|
1824 | }).join('\n').replace(/^/gm, ' '),
|
1825 | ''
|
1826 | ].join('\n');
|
1827 | };
|
1828 |
|
1829 |
|
1830 |
|
1831 |
|
1832 |
|
1833 |
|
1834 |
|
1835 |
|
1836 | Command.prototype.helpInformation = function() {
|
1837 | var desc = [];
|
1838 | if (this._description) {
|
1839 | desc = [
|
1840 | ' ' + this._description,
|
1841 | ''
|
1842 | ];
|
1843 |
|
1844 | var argsDescription = this._argsDescription;
|
1845 | if (argsDescription && this._args.length) {
|
1846 | var width = this.padWidth();
|
1847 | desc.push(' Arguments:');
|
1848 | desc.push('');
|
1849 | this._args.forEach(function(arg) {
|
1850 | desc.push(' ' + pad(arg.name, width) + ' ' + argsDescription[arg.name]);
|
1851 | });
|
1852 | desc.push('');
|
1853 | }
|
1854 | }
|
1855 |
|
1856 | var cmdName = this._name;
|
1857 | if (this._alias) {
|
1858 | cmdName = cmdName + '|' + this._alias;
|
1859 | }
|
1860 | var usage = [
|
1861 | '',
|
1862 | ' Usage: ' + cmdName + ' ' + this.usage(),
|
1863 | ''
|
1864 | ];
|
1865 |
|
1866 | var cmds = [];
|
1867 | var commandHelp = this.commandHelp();
|
1868 | if (commandHelp) cmds = [commandHelp];
|
1869 |
|
1870 | var options = [
|
1871 | ' Options:',
|
1872 | '',
|
1873 | '' + this.optionHelp().replace(/^/gm, ' '),
|
1874 | ''
|
1875 | ];
|
1876 |
|
1877 | return usage
|
1878 | .concat(desc)
|
1879 | .concat(options)
|
1880 | .concat(cmds)
|
1881 | .join('\n');
|
1882 | };
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 |
|
1890 | Command.prototype.outputHelp = function(cb) {
|
1891 | if (!cb) {
|
1892 | cb = function(passthru) {
|
1893 | return passthru;
|
1894 | };
|
1895 | }
|
1896 | process.stdout.write(cb(this.helpInformation()));
|
1897 | this.emit('--help');
|
1898 | };
|
1899 |
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 |
|
1905 |
|
1906 | Command.prototype.help = function(cb) {
|
1907 | this.outputHelp(cb);
|
1908 | process.exit();
|
1909 | };
|
1910 |
|
1911 |
|
1912 |
|
1913 |
|
1914 |
|
1915 |
|
1916 |
|
1917 |
|
1918 |
|
1919 | function camelcase(flag) {
|
1920 | return flag.split('-').reduce(function(str, word) {
|
1921 | return str + word[0].toUpperCase() + word.slice(1);
|
1922 | });
|
1923 | }
|
1924 |
|
1925 |
|
1926 |
|
1927 |
|
1928 |
|
1929 |
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 | function pad(str, width) {
|
1935 | var len = Math.max(0, width - str.length);
|
1936 | return str + Array(len + 1).join(' ');
|
1937 | }
|
1938 |
|
1939 |
|
1940 |
|
1941 |
|
1942 |
|
1943 |
|
1944 |
|
1945 |
|
1946 |
|
1947 | function outputHelpIfNecessary(cmd, options) {
|
1948 | options = options || [];
|
1949 | for (var i = 0; i < options.length; i++) {
|
1950 | if (options[i] === '--help' || options[i] === '-h') {
|
1951 | cmd.outputHelp();
|
1952 | process.exit(0);
|
1953 | }
|
1954 | }
|
1955 | }
|
1956 |
|
1957 |
|
1958 |
|
1959 |
|
1960 |
|
1961 |
|
1962 |
|
1963 |
|
1964 |
|
1965 | function humanReadableArgName(arg) {
|
1966 | var nameOutput = arg.name + (arg.variadic === true ? '...' : '');
|
1967 |
|
1968 | return arg.required
|
1969 | ? '<' + nameOutput + '>'
|
1970 | : '[' + nameOutput + ']';
|
1971 | }
|
1972 |
|
1973 |
|
1974 | function exists(file) {
|
1975 | try {
|
1976 | if (fs.statSync(file).isFile()) {
|
1977 | return true;
|
1978 | }
|
1979 | } catch (e) {
|
1980 | return false;
|
1981 | }
|
1982 | }
|
1983 | });
|
1984 |
|
1985 | var domain;
|
1986 | var hasSetImmediate = typeof setImmediate === "function";
|
1987 |
|
1988 |
|
1989 |
|
1990 |
|
1991 |
|
1992 |
|
1993 |
|
1994 |
|
1995 |
|
1996 |
|
1997 | var raw = rawAsap;
|
1998 | function rawAsap(task) {
|
1999 | if (!queue.length) {
|
2000 | requestFlush();
|
2001 | flushing = true;
|
2002 | }
|
2003 |
|
2004 | queue[queue.length] = task;
|
2005 | }
|
2006 |
|
2007 | var queue = [];
|
2008 |
|
2009 |
|
2010 | var flushing = false;
|
2011 |
|
2012 |
|
2013 |
|
2014 | var index = 0;
|
2015 |
|
2016 |
|
2017 |
|
2018 | var capacity = 1024;
|
2019 |
|
2020 |
|
2021 |
|
2022 |
|
2023 |
|
2024 |
|
2025 |
|
2026 | function flush() {
|
2027 | while (index < queue.length) {
|
2028 | var currentIndex = index;
|
2029 |
|
2030 |
|
2031 | index = index + 1;
|
2032 | queue[currentIndex].call();
|
2033 |
|
2034 |
|
2035 |
|
2036 |
|
2037 |
|
2038 | if (index > capacity) {
|
2039 |
|
2040 |
|
2041 | for (var scan = 0, newLength = queue.length - index; scan < newLength; scan++) {
|
2042 | queue[scan] = queue[scan + index];
|
2043 | }
|
2044 | queue.length -= index;
|
2045 | index = 0;
|
2046 | }
|
2047 | }
|
2048 | queue.length = 0;
|
2049 | index = 0;
|
2050 | flushing = false;
|
2051 | }
|
2052 |
|
2053 | rawAsap.requestFlush = requestFlush;
|
2054 | function requestFlush() {
|
2055 |
|
2056 |
|
2057 |
|
2058 | var parentDomain = process.domain;
|
2059 | if (parentDomain) {
|
2060 | if (!domain) {
|
2061 |
|
2062 |
|
2063 | domain = require$$0$1;
|
2064 | }
|
2065 | domain.active = process.domain = null;
|
2066 | }
|
2067 |
|
2068 |
|
2069 |
|
2070 |
|
2071 |
|
2072 |
|
2073 |
|
2074 | if (flushing && hasSetImmediate) {
|
2075 | setImmediate(flush);
|
2076 | } else {
|
2077 | process.nextTick(flush);
|
2078 | }
|
2079 |
|
2080 | if (parentDomain) {
|
2081 | domain.active = process.domain = parentDomain;
|
2082 | }
|
2083 | }
|
2084 |
|
2085 | function noop() {}
|
2086 |
|
2087 |
|
2088 |
|
2089 |
|
2090 |
|
2091 |
|
2092 |
|
2093 |
|
2094 |
|
2095 |
|
2096 |
|
2097 |
|
2098 |
|
2099 |
|
2100 |
|
2101 |
|
2102 |
|
2103 |
|
2104 | var LAST_ERROR = null;
|
2105 | var IS_ERROR = {};
|
2106 | function getThen(obj) {
|
2107 | try {
|
2108 | return obj.then;
|
2109 | } catch (ex) {
|
2110 | LAST_ERROR = ex;
|
2111 | return IS_ERROR;
|
2112 | }
|
2113 | }
|
2114 |
|
2115 | function tryCallOne(fn, a) {
|
2116 | try {
|
2117 | return fn(a);
|
2118 | } catch (ex) {
|
2119 | LAST_ERROR = ex;
|
2120 | return IS_ERROR;
|
2121 | }
|
2122 | }
|
2123 | function tryCallTwo(fn, a, b) {
|
2124 | try {
|
2125 | fn(a, b);
|
2126 | } catch (ex) {
|
2127 | LAST_ERROR = ex;
|
2128 | return IS_ERROR;
|
2129 | }
|
2130 | }
|
2131 |
|
2132 | var core = Promise$1;
|
2133 |
|
2134 | function Promise$1(fn) {
|
2135 | if (typeof this !== 'object') {
|
2136 | throw new TypeError('Promises must be constructed via new');
|
2137 | }
|
2138 | if (typeof fn !== 'function') {
|
2139 | throw new TypeError('Promise constructor\'s argument is not a function');
|
2140 | }
|
2141 | this._75 = 0;
|
2142 | this._83 = 0;
|
2143 | this._18 = null;
|
2144 | this._38 = null;
|
2145 | if (fn === noop) return;
|
2146 | doResolve(fn, this);
|
2147 | }
|
2148 | Promise$1._47 = null;
|
2149 | Promise$1._71 = null;
|
2150 | Promise$1._44 = noop;
|
2151 |
|
2152 | Promise$1.prototype.then = function(onFulfilled, onRejected) {
|
2153 | if (this.constructor !== Promise$1) {
|
2154 | return safeThen(this, onFulfilled, onRejected);
|
2155 | }
|
2156 | var res = new Promise$1(noop);
|
2157 | handle(this, new Handler(onFulfilled, onRejected, res));
|
2158 | return res;
|
2159 | };
|
2160 |
|
2161 | function safeThen(self, onFulfilled, onRejected) {
|
2162 | return new self.constructor(function (resolve, reject) {
|
2163 | var res = new Promise$1(noop);
|
2164 | res.then(resolve, reject);
|
2165 | handle(self, new Handler(onFulfilled, onRejected, res));
|
2166 | });
|
2167 | }
|
2168 | function handle(self, deferred) {
|
2169 | while (self._83 === 3) {
|
2170 | self = self._18;
|
2171 | }
|
2172 | if (Promise$1._47) {
|
2173 | Promise$1._47(self);
|
2174 | }
|
2175 | if (self._83 === 0) {
|
2176 | if (self._75 === 0) {
|
2177 | self._75 = 1;
|
2178 | self._38 = deferred;
|
2179 | return;
|
2180 | }
|
2181 | if (self._75 === 1) {
|
2182 | self._75 = 2;
|
2183 | self._38 = [self._38, deferred];
|
2184 | return;
|
2185 | }
|
2186 | self._38.push(deferred);
|
2187 | return;
|
2188 | }
|
2189 | handleResolved(self, deferred);
|
2190 | }
|
2191 |
|
2192 | function handleResolved(self, deferred) {
|
2193 | raw(function() {
|
2194 | var cb = self._83 === 1 ? deferred.onFulfilled : deferred.onRejected;
|
2195 | if (cb === null) {
|
2196 | if (self._83 === 1) {
|
2197 | resolve(deferred.promise, self._18);
|
2198 | } else {
|
2199 | reject(deferred.promise, self._18);
|
2200 | }
|
2201 | return;
|
2202 | }
|
2203 | var ret = tryCallOne(cb, self._18);
|
2204 | if (ret === IS_ERROR) {
|
2205 | reject(deferred.promise, LAST_ERROR);
|
2206 | } else {
|
2207 | resolve(deferred.promise, ret);
|
2208 | }
|
2209 | });
|
2210 | }
|
2211 | function resolve(self, newValue) {
|
2212 |
|
2213 | if (newValue === self) {
|
2214 | return reject(
|
2215 | self,
|
2216 | new TypeError('A promise cannot be resolved with itself.')
|
2217 | );
|
2218 | }
|
2219 | if (
|
2220 | newValue &&
|
2221 | (typeof newValue === 'object' || typeof newValue === 'function')
|
2222 | ) {
|
2223 | var then = getThen(newValue);
|
2224 | if (then === IS_ERROR) {
|
2225 | return reject(self, LAST_ERROR);
|
2226 | }
|
2227 | if (
|
2228 | then === self.then &&
|
2229 | newValue instanceof Promise$1
|
2230 | ) {
|
2231 | self._83 = 3;
|
2232 | self._18 = newValue;
|
2233 | finale(self);
|
2234 | return;
|
2235 | } else if (typeof then === 'function') {
|
2236 | doResolve(then.bind(newValue), self);
|
2237 | return;
|
2238 | }
|
2239 | }
|
2240 | self._83 = 1;
|
2241 | self._18 = newValue;
|
2242 | finale(self);
|
2243 | }
|
2244 |
|
2245 | function reject(self, newValue) {
|
2246 | self._83 = 2;
|
2247 | self._18 = newValue;
|
2248 | if (Promise$1._71) {
|
2249 | Promise$1._71(self, newValue);
|
2250 | }
|
2251 | finale(self);
|
2252 | }
|
2253 | function finale(self) {
|
2254 | if (self._75 === 1) {
|
2255 | handle(self, self._38);
|
2256 | self._38 = null;
|
2257 | }
|
2258 | if (self._75 === 2) {
|
2259 | for (var i = 0; i < self._38.length; i++) {
|
2260 | handle(self, self._38[i]);
|
2261 | }
|
2262 | self._38 = null;
|
2263 | }
|
2264 | }
|
2265 |
|
2266 | function Handler(onFulfilled, onRejected, promise){
|
2267 | this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
|
2268 | this.onRejected = typeof onRejected === 'function' ? onRejected : null;
|
2269 | this.promise = promise;
|
2270 | }
|
2271 |
|
2272 |
|
2273 |
|
2274 |
|
2275 |
|
2276 |
|
2277 |
|
2278 | function doResolve(fn, promise) {
|
2279 | var done = false;
|
2280 | var res = tryCallTwo(fn, function (value) {
|
2281 | if (done) return;
|
2282 | done = true;
|
2283 | resolve(promise, value);
|
2284 | }, function (reason) {
|
2285 | if (done) return;
|
2286 | done = true;
|
2287 | reject(promise, reason);
|
2288 | });
|
2289 | if (!done && res === IS_ERROR) {
|
2290 | done = true;
|
2291 | reject(promise, LAST_ERROR);
|
2292 | }
|
2293 | }
|
2294 |
|
2295 | core.prototype.done = function (onFulfilled, onRejected) {
|
2296 | var self = arguments.length ? this.then.apply(this, arguments) : this;
|
2297 | self.then(null, function (err) {
|
2298 | setTimeout(function () {
|
2299 | throw err;
|
2300 | }, 0);
|
2301 | });
|
2302 | };
|
2303 |
|
2304 | core.prototype['finally'] = function (f) {
|
2305 | return this.then(function (value) {
|
2306 | return core.resolve(f()).then(function () {
|
2307 | return value;
|
2308 | });
|
2309 | }, function (err) {
|
2310 | return core.resolve(f()).then(function () {
|
2311 | throw err;
|
2312 | });
|
2313 | });
|
2314 | };
|
2315 |
|
2316 |
|
2317 |
|
2318 | var TRUE = valuePromise(true);
|
2319 | var FALSE = valuePromise(false);
|
2320 | var NULL = valuePromise(null);
|
2321 | var UNDEFINED = valuePromise(undefined);
|
2322 | var ZERO = valuePromise(0);
|
2323 | var EMPTYSTRING = valuePromise('');
|
2324 |
|
2325 | function valuePromise(value) {
|
2326 | var p = new core(core._44);
|
2327 | p._83 = 1;
|
2328 | p._18 = value;
|
2329 | return p;
|
2330 | }
|
2331 | core.resolve = function (value) {
|
2332 | if (value instanceof core) return value;
|
2333 |
|
2334 | if (value === null) return NULL;
|
2335 | if (value === undefined) return UNDEFINED;
|
2336 | if (value === true) return TRUE;
|
2337 | if (value === false) return FALSE;
|
2338 | if (value === 0) return ZERO;
|
2339 | if (value === '') return EMPTYSTRING;
|
2340 |
|
2341 | if (typeof value === 'object' || typeof value === 'function') {
|
2342 | try {
|
2343 | var then = value.then;
|
2344 | if (typeof then === 'function') {
|
2345 | return new core(then.bind(value));
|
2346 | }
|
2347 | } catch (ex) {
|
2348 | return new core(function (resolve, reject) {
|
2349 | reject(ex);
|
2350 | });
|
2351 | }
|
2352 | }
|
2353 | return valuePromise(value);
|
2354 | };
|
2355 |
|
2356 | core.all = function (arr) {
|
2357 | var args = Array.prototype.slice.call(arr);
|
2358 |
|
2359 | return new core(function (resolve, reject) {
|
2360 | if (args.length === 0) return resolve([]);
|
2361 | var remaining = args.length;
|
2362 | function res(i, val) {
|
2363 | if (val && (typeof val === 'object' || typeof val === 'function')) {
|
2364 | if (val instanceof core && val.then === core.prototype.then) {
|
2365 | while (val._83 === 3) {
|
2366 | val = val._18;
|
2367 | }
|
2368 | if (val._83 === 1) return res(i, val._18);
|
2369 | if (val._83 === 2) reject(val._18);
|
2370 | val.then(function (val) {
|
2371 | res(i, val);
|
2372 | }, reject);
|
2373 | return;
|
2374 | } else {
|
2375 | var then = val.then;
|
2376 | if (typeof then === 'function') {
|
2377 | var p = new core(then.bind(val));
|
2378 | p.then(function (val) {
|
2379 | res(i, val);
|
2380 | }, reject);
|
2381 | return;
|
2382 | }
|
2383 | }
|
2384 | }
|
2385 | args[i] = val;
|
2386 | if (--remaining === 0) {
|
2387 | resolve(args);
|
2388 | }
|
2389 | }
|
2390 | for (var i = 0; i < args.length; i++) {
|
2391 | res(i, args[i]);
|
2392 | }
|
2393 | });
|
2394 | };
|
2395 |
|
2396 | core.reject = function (value) {
|
2397 | return new core(function (resolve, reject) {
|
2398 | reject(value);
|
2399 | });
|
2400 | };
|
2401 |
|
2402 | core.race = function (values) {
|
2403 | return new core(function (resolve, reject) {
|
2404 | values.forEach(function(value){
|
2405 | core.resolve(value).then(resolve, reject);
|
2406 | });
|
2407 | });
|
2408 | };
|
2409 |
|
2410 |
|
2411 |
|
2412 | core.prototype['catch'] = function (onRejected) {
|
2413 | return this.then(null, onRejected);
|
2414 | };
|
2415 |
|
2416 | var freeTasks = [];
|
2417 |
|
2418 |
|
2419 |
|
2420 |
|
2421 |
|
2422 |
|
2423 |
|
2424 |
|
2425 |
|
2426 |
|
2427 |
|
2428 | var asap_1 = asap;
|
2429 | function asap(task) {
|
2430 | var rawTask;
|
2431 | if (freeTasks.length) {
|
2432 | rawTask = freeTasks.pop();
|
2433 | } else {
|
2434 | rawTask = new RawTask();
|
2435 | }
|
2436 | rawTask.task = task;
|
2437 | rawTask.domain = process.domain;
|
2438 | raw(rawTask);
|
2439 | }
|
2440 |
|
2441 | function RawTask() {
|
2442 | this.task = null;
|
2443 | this.domain = null;
|
2444 | }
|
2445 |
|
2446 | RawTask.prototype.call = function () {
|
2447 | if (this.domain) {
|
2448 | this.domain.enter();
|
2449 | }
|
2450 | var threw = true;
|
2451 | try {
|
2452 | this.task.call();
|
2453 | threw = false;
|
2454 |
|
2455 |
|
2456 | if (this.domain) {
|
2457 | this.domain.exit();
|
2458 | }
|
2459 | } finally {
|
2460 |
|
2461 |
|
2462 | if (threw) {
|
2463 |
|
2464 |
|
2465 |
|
2466 |
|
2467 |
|
2468 | raw.requestFlush();
|
2469 | }
|
2470 |
|
2471 |
|
2472 | this.task = null;
|
2473 | this.domain = null;
|
2474 | freeTasks.push(this);
|
2475 | }
|
2476 | };
|
2477 |
|
2478 |
|
2479 |
|
2480 | core.denodeify = function (fn, argumentCount) {
|
2481 | if (
|
2482 | typeof argumentCount === 'number' && argumentCount !== Infinity
|
2483 | ) {
|
2484 | return denodeifyWithCount(fn, argumentCount);
|
2485 | } else {
|
2486 | return denodeifyWithoutCount(fn);
|
2487 | }
|
2488 | };
|
2489 |
|
2490 | var callbackFn = (
|
2491 | 'function (err, res) {' +
|
2492 | 'if (err) { rj(err); } else { rs(res); }' +
|
2493 | '}'
|
2494 | );
|
2495 | function denodeifyWithCount(fn, argumentCount) {
|
2496 | var args = [];
|
2497 | for (var i = 0; i < argumentCount; i++) {
|
2498 | args.push('a' + i);
|
2499 | }
|
2500 | var body = [
|
2501 | 'return function (' + args.join(',') + ') {',
|
2502 | 'var self = this;',
|
2503 | 'return new Promise(function (rs, rj) {',
|
2504 | 'var res = fn.call(',
|
2505 | ['self'].concat(args).concat([callbackFn]).join(','),
|
2506 | ');',
|
2507 | 'if (res &&',
|
2508 | '(typeof res === "object" || typeof res === "function") &&',
|
2509 | 'typeof res.then === "function"',
|
2510 | ') {rs(res);}',
|
2511 | '});',
|
2512 | '};'
|
2513 | ].join('');
|
2514 | return Function(['Promise', 'fn'], body)(core, fn);
|
2515 | }
|
2516 | function denodeifyWithoutCount(fn) {
|
2517 | var fnLength = Math.max(fn.length - 1, 3);
|
2518 | var args = [];
|
2519 | for (var i = 0; i < fnLength; i++) {
|
2520 | args.push('a' + i);
|
2521 | }
|
2522 | var body = [
|
2523 | 'return function (' + args.join(',') + ') {',
|
2524 | 'var self = this;',
|
2525 | 'var args;',
|
2526 | 'var argLength = arguments.length;',
|
2527 | 'if (arguments.length > ' + fnLength + ') {',
|
2528 | 'args = new Array(arguments.length + 1);',
|
2529 | 'for (var i = 0; i < arguments.length; i++) {',
|
2530 | 'args[i] = arguments[i];',
|
2531 | '}',
|
2532 | '}',
|
2533 | 'return new Promise(function (rs, rj) {',
|
2534 | 'var cb = ' + callbackFn + ';',
|
2535 | 'var res;',
|
2536 | 'switch (argLength) {',
|
2537 | args.concat(['extra']).map(function (_, index) {
|
2538 | return (
|
2539 | 'case ' + (index) + ':' +
|
2540 | 'res = fn.call(' + ['self'].concat(args.slice(0, index)).concat('cb').join(',') + ');' +
|
2541 | 'break;'
|
2542 | );
|
2543 | }).join(''),
|
2544 | 'default:',
|
2545 | 'args[argLength] = cb;',
|
2546 | 'res = fn.apply(self, args);',
|
2547 | '}',
|
2548 |
|
2549 | 'if (res &&',
|
2550 | '(typeof res === "object" || typeof res === "function") &&',
|
2551 | 'typeof res.then === "function"',
|
2552 | ') {rs(res);}',
|
2553 | '});',
|
2554 | '};'
|
2555 | ].join('');
|
2556 |
|
2557 | return Function(
|
2558 | ['Promise', 'fn'],
|
2559 | body
|
2560 | )(core, fn);
|
2561 | }
|
2562 |
|
2563 | core.nodeify = function (fn) {
|
2564 | return function () {
|
2565 | var args = Array.prototype.slice.call(arguments);
|
2566 | var callback =
|
2567 | typeof args[args.length - 1] === 'function' ? args.pop() : null;
|
2568 | var ctx = this;
|
2569 | try {
|
2570 | return fn.apply(this, arguments).nodeify(callback, ctx);
|
2571 | } catch (ex) {
|
2572 | if (callback === null || typeof callback == 'undefined') {
|
2573 | return new core(function (resolve, reject) {
|
2574 | reject(ex);
|
2575 | });
|
2576 | } else {
|
2577 | asap_1(function () {
|
2578 | callback.call(ctx, ex);
|
2579 | });
|
2580 | }
|
2581 | }
|
2582 | }
|
2583 | };
|
2584 |
|
2585 | core.prototype.nodeify = function (callback, ctx) {
|
2586 | if (typeof callback != 'function') return this;
|
2587 |
|
2588 | this.then(function (value) {
|
2589 | asap_1(function () {
|
2590 | callback.call(ctx, null, value);
|
2591 | });
|
2592 | }, function (err) {
|
2593 | asap_1(function () {
|
2594 | callback.call(ctx, err);
|
2595 | });
|
2596 | });
|
2597 | };
|
2598 |
|
2599 | core.enableSynchronous = function () {
|
2600 | core.prototype.isPending = function() {
|
2601 | return this.getState() == 0;
|
2602 | };
|
2603 |
|
2604 | core.prototype.isFulfilled = function() {
|
2605 | return this.getState() == 1;
|
2606 | };
|
2607 |
|
2608 | core.prototype.isRejected = function() {
|
2609 | return this.getState() == 2;
|
2610 | };
|
2611 |
|
2612 | core.prototype.getValue = function () {
|
2613 | if (this._83 === 3) {
|
2614 | return this._18.getValue();
|
2615 | }
|
2616 |
|
2617 | if (!this.isFulfilled()) {
|
2618 | throw new Error('Cannot get a value of an unfulfilled promise.');
|
2619 | }
|
2620 |
|
2621 | return this._18;
|
2622 | };
|
2623 |
|
2624 | core.prototype.getReason = function () {
|
2625 | if (this._83 === 3) {
|
2626 | return this._18.getReason();
|
2627 | }
|
2628 |
|
2629 | if (!this.isRejected()) {
|
2630 | throw new Error('Cannot get a rejection reason of a non-rejected promise.');
|
2631 | }
|
2632 |
|
2633 | return this._18;
|
2634 | };
|
2635 |
|
2636 | core.prototype.getState = function () {
|
2637 | if (this._83 === 3) {
|
2638 | return this._18.getState();
|
2639 | }
|
2640 | if (this._83 === -1 || this._83 === -2) {
|
2641 | return 0;
|
2642 | }
|
2643 |
|
2644 | return this._83;
|
2645 | };
|
2646 | };
|
2647 |
|
2648 | core.disableSynchronous = function() {
|
2649 | core.prototype.isPending = undefined;
|
2650 | core.prototype.isFulfilled = undefined;
|
2651 | core.prototype.isRejected = undefined;
|
2652 | core.prototype.getValue = undefined;
|
2653 | core.prototype.getReason = undefined;
|
2654 | core.prototype.getState = undefined;
|
2655 | };
|
2656 |
|
2657 | var lib = core;
|
2658 |
|
2659 | var promise = lib;
|
2660 |
|
2661 |
|
2662 |
|
2663 |
|
2664 |
|
2665 |
|
2666 |
|
2667 |
|
2668 |
|
2669 |
|
2670 |
|
2671 |
|
2672 |
|
2673 |
|
2674 |
|
2675 |
|
2676 |
|
2677 |
|
2678 |
|
2679 |
|
2680 |
|
2681 |
|
2682 |
|
2683 |
|
2684 |
|
2685 |
|
2686 |
|
2687 |
|
2688 |
|
2689 |
|
2690 | const DEFAULT_USER_AGENT = 'amphtml-validator';
|
2691 |
|
2692 |
|
2693 |
|
2694 |
|
2695 |
|
2696 |
|
2697 |
|
2698 | function hasPrefix(str, prefix) {
|
2699 | return str.indexOf(prefix) == 0;
|
2700 | }
|
2701 |
|
2702 |
|
2703 |
|
2704 |
|
2705 |
|
2706 |
|
2707 |
|
2708 | function isHttpOrHttpsUrl(url) {
|
2709 | return hasPrefix(url, 'http://') || hasPrefix(url, 'https://');
|
2710 | }
|
2711 |
|
2712 |
|
2713 |
|
2714 |
|
2715 |
|
2716 |
|
2717 | function readFromFile(name) {
|
2718 | return new promise(function(resolve, reject) {
|
2719 | fs.readFile(name, 'utf8', function(err, data) {
|
2720 | if (err) {
|
2721 | reject(err);
|
2722 | } else {
|
2723 | resolve(data.trim());
|
2724 | }
|
2725 | });
|
2726 | });
|
2727 | }
|
2728 |
|
2729 |
|
2730 |
|
2731 |
|
2732 |
|
2733 |
|
2734 |
|
2735 | function readFromReadable(name, readable) {
|
2736 | return new promise(function(resolve, reject) {
|
2737 | const chunks = [];
|
2738 | readable.setEncoding('utf8');
|
2739 | readable.on('data', function(chunk) {
|
2740 | chunks.push(chunk);
|
2741 | });
|
2742 | readable.on('end', function() {
|
2743 | resolve(chunks.join(''));
|
2744 | });
|
2745 | readable.on('error', function(error) {
|
2746 | reject(new Error('Could not read from ' + name + ' - ' + error.message));
|
2747 | });
|
2748 | });
|
2749 | }
|
2750 |
|
2751 |
|
2752 |
|
2753 |
|
2754 |
|
2755 |
|
2756 |
|
2757 | function readFromStdin() {
|
2758 | return readFromReadable('stdin', process.stdin).then(function(data) {
|
2759 | process.stdin.resume();
|
2760 | return data;
|
2761 | });
|
2762 | }
|
2763 |
|
2764 |
|
2765 |
|
2766 |
|
2767 |
|
2768 |
|
2769 |
|
2770 |
|
2771 |
|
2772 | function readFromUrl(url, userAgent) {
|
2773 | return new promise(function(resolve, reject) {
|
2774 | const clientModule = hasPrefix(url, 'http://') ? http : https;
|
2775 | const req = clientModule.request(url, function(response) {
|
2776 | if (response.statusCode !== 200) {
|
2777 |
|
2778 |
|
2779 |
|
2780 |
|
2781 |
|
2782 |
|
2783 | response.resume();
|
2784 | reject(new Error(
|
2785 | 'Unable to fetch ' + url + ' - HTTP Status ' +
|
2786 | response.statusCode));
|
2787 | } else {
|
2788 | resolve(response);
|
2789 | }
|
2790 | });
|
2791 | req.setHeader('User-Agent', userAgent);
|
2792 | req.on('error', function(error) {
|
2793 | reject(
|
2794 | new Error('Unable to fetch ' + url + ' - ' + error.message));
|
2795 | });
|
2796 | req.end();
|
2797 | })
|
2798 | .then(readFromReadable.bind(null, url));
|
2799 | }
|
2800 |
|
2801 |
|
2802 |
|
2803 |
|
2804 |
|
2805 |
|
2806 |
|
2807 |
|
2808 |
|
2809 | function ValidationResult() {
|
2810 | |
2811 |
|
2812 |
|
2813 |
|
2814 | this.status = 'UNKNOWN';
|
2815 |
|
2816 | this.errors = [];
|
2817 | }
|
2818 |
|
2819 |
|
2820 |
|
2821 |
|
2822 |
|
2823 |
|
2824 |
|
2825 |
|
2826 | function ValidationError() {
|
2827 | |
2828 |
|
2829 |
|
2830 |
|
2831 | this.severity = 'UNKNOWN_SEVERITY';
|
2832 | |
2833 |
|
2834 |
|
2835 | this.line = 1;
|
2836 | |
2837 |
|
2838 |
|
2839 | this.col = 0;
|
2840 | |
2841 |
|
2842 |
|
2843 |
|
2844 |
|
2845 |
|
2846 |
|
2847 | this.message = '';
|
2848 | |
2849 |
|
2850 |
|
2851 |
|
2852 |
|
2853 |
|
2854 | this.specUrl = null;
|
2855 | |
2856 |
|
2857 |
|
2858 |
|
2859 |
|
2860 |
|
2861 |
|
2862 |
|
2863 |
|
2864 |
|
2865 |
|
2866 | this.code = 'UNKNOWN_CODE';
|
2867 | |
2868 |
|
2869 |
|
2870 |
|
2871 |
|
2872 |
|
2873 | this.params = [];
|
2874 | }
|
2875 |
|
2876 |
|
2877 |
|
2878 |
|
2879 |
|
2880 |
|
2881 |
|
2882 |
|
2883 |
|
2884 |
|
2885 | function Validator(scriptContents) {
|
2886 |
|
2887 |
|
2888 |
|
2889 |
|
2890 |
|
2891 |
|
2892 |
|
2893 |
|
2894 |
|
2895 |
|
2896 | this.sandbox = vm.createContext();
|
2897 | try {
|
2898 | new vm.Script(scriptContents).runInContext(this.sandbox);
|
2899 | } catch (error) {
|
2900 | throw new Error('Could not instantiate validator.js - ' + error.message);
|
2901 | }
|
2902 | }
|
2903 |
|
2904 |
|
2905 |
|
2906 |
|
2907 |
|
2908 |
|
2909 |
|
2910 |
|
2911 |
|
2912 | Validator.prototype.validateString = function(inputString, htmlFormat) {
|
2913 | const internalResult =
|
2914 | this.sandbox.amp.validator.validateString(inputString, htmlFormat);
|
2915 | const result = new ValidationResult();
|
2916 | result.status = internalResult.status;
|
2917 | for (let ii = 0; ii < internalResult.errors.length; ii++) {
|
2918 | const internalError = internalResult.errors[ii];
|
2919 | const error = new ValidationError();
|
2920 | error.severity = internalError.severity;
|
2921 | error.line = internalError.line;
|
2922 | error.col = internalError.col;
|
2923 | error.message =
|
2924 | this.sandbox.amp.validator.renderErrorMessage(internalError);
|
2925 | error.specUrl = internalError.specUrl;
|
2926 | error.code = internalError.code;
|
2927 | error.params = internalError.params;
|
2928 | result.errors.push(error);
|
2929 | }
|
2930 | return result;
|
2931 | };
|
2932 |
|
2933 |
|
2934 |
|
2935 |
|
2936 |
|
2937 |
|
2938 | const instanceByValidatorJs = {};
|
2939 |
|
2940 |
|
2941 |
|
2942 |
|
2943 |
|
2944 |
|
2945 |
|
2946 |
|
2947 |
|
2948 |
|
2949 |
|
2950 |
|
2951 |
|
2952 | function getInstance(opt_validatorJs, opt_userAgent) {
|
2953 | const validatorJs =
|
2954 | opt_validatorJs || 'https://cdn.ampproject.org/v0/validator.js';
|
2955 | const userAgent = opt_userAgent || DEFAULT_USER_AGENT;
|
2956 | if (instanceByValidatorJs.hasOwnProperty(validatorJs)) {
|
2957 | return promise.resolve(instanceByValidatorJs[validatorJs]);
|
2958 | }
|
2959 | const validatorJsPromise = isHttpOrHttpsUrl(validatorJs) ?
|
2960 | readFromUrl(validatorJs, userAgent) :
|
2961 | readFromFile(validatorJs);
|
2962 | return validatorJsPromise.then(function(scriptContents) {
|
2963 | let instance;
|
2964 | try {
|
2965 | instance = new Validator(scriptContents);
|
2966 | } catch (error) {
|
2967 |
|
2968 |
|
2969 |
|
2970 |
|
2971 | throw error;
|
2972 | }
|
2973 | instanceByValidatorJs[validatorJs] = instance;
|
2974 | return instance;
|
2975 | });
|
2976 | }
|
2977 | var getInstance_1 = getInstance;
|
2978 |
|
2979 |
|
2980 |
|
2981 |
|
2982 |
|
2983 |
|
2984 |
|
2985 |
|
2986 |
|
2987 |
|
2988 |
|
2989 |
|
2990 |
|
2991 | function newInstance(validatorJsContents) {
|
2992 | return new Validator(validatorJsContents);
|
2993 | }
|
2994 | var newInstance_1 = newInstance;
|
2995 |
|
2996 |
|
2997 |
|
2998 |
|
2999 |
|
3000 |
|
3001 |
|
3002 |
|
3003 |
|
3004 |
|
3005 |
|
3006 |
|
3007 |
|
3008 |
|
3009 |
|
3010 |
|
3011 |
|
3012 |
|
3013 | function logValidationResult(filename, validationResult, color) {
|
3014 | if (validationResult.status === 'PASS') {
|
3015 | process.stdout.write(
|
3016 | filename + ': ' + (color ? safe.green('PASS') : 'PASS') + '\n');
|
3017 | }
|
3018 | for (let ii = 0; ii < validationResult.errors.length; ii++) {
|
3019 | const error = validationResult.errors[ii];
|
3020 | let msg = filename + ':' + error.line + ':' + error.col + ' ';
|
3021 | if (color) {
|
3022 | msg += (error.severity === 'ERROR' ? safe.red : safe.magenta)(
|
3023 | error.message);
|
3024 | } else {
|
3025 | msg += error.message;
|
3026 | }
|
3027 | if (error.specUrl) {
|
3028 | msg += ' (see ' + error.specUrl + ')';
|
3029 | }
|
3030 |
|
3031 | process.stderr.write(msg + '\n');
|
3032 | }
|
3033 | }
|
3034 |
|
3035 |
|
3036 |
|
3037 |
|
3038 | function main() {
|
3039 | commander
|
3040 | .usage(
|
3041 | '[options] <fileOrUrlOrMinus...>\n\n' +
|
3042 | ' Validates the files or urls provided as arguments. If "-" is\n' +
|
3043 | ' specified, reads from stdin instead.')
|
3044 | .option(
|
3045 | '--validator_js <fileOrUrl>',
|
3046 | 'The Validator Javascript.\n' +
|
3047 | ' Latest published version by default, or\n' +
|
3048 | ' dist/validator_minified.js (built with build.py)\n' +
|
3049 | ' for development.',
|
3050 | 'https://cdn.ampproject.org/v0/validator.js')
|
3051 | .option(
|
3052 | '--user-agent <userAgent>', 'User agent string to use in requests.',
|
3053 | DEFAULT_USER_AGENT)
|
3054 | .option(
|
3055 | '--html_format <AMP|AMP4ADS|AMP4EMAIL>',
|
3056 | 'The input format to be validated.\n' +
|
3057 | ' AMP by default.',
|
3058 | 'AMP')
|
3059 | .option(
|
3060 | '--format <color|text|json>',
|
3061 | 'How to format the output.\n' +
|
3062 | ' "color" displays errors/warnings/success in\n' +
|
3063 | ' red/orange/green.\n' +
|
3064 | ' "text" avoids color (e.g., useful in terminals not\n' +
|
3065 | ' supporting color).\n' +
|
3066 | ' "json" emits json corresponding to the ValidationResult\n' +
|
3067 | ' message in validator.proto.',
|
3068 | 'color')
|
3069 | .parse(process.argv);
|
3070 | if (commander.args.length === 0) {
|
3071 | commander.outputHelp();
|
3072 | process.exit(1);
|
3073 | }
|
3074 | if (commander.html_format !== 'AMP' && commander.html_format !== 'AMP4ADS' &&
|
3075 | commander.html_format !== 'AMP4EMAIL') {
|
3076 | process.stderr.write(
|
3077 | '--html_format must be set to "AMP", "AMP4ADS", or "AMP4EMAIL".\n',
|
3078 | function() {
|
3079 | process.exit(1);
|
3080 | });
|
3081 | }
|
3082 | if (commander.format !== 'color' && commander.format !== 'text' &&
|
3083 | commander.format !== 'json') {
|
3084 | process.stderr.write(
|
3085 | '--format must be set to "color", "text", or "json".\n', function() {
|
3086 | process.exit(1);
|
3087 | });
|
3088 | }
|
3089 | const inputs = [];
|
3090 | for (let ii = 0; ii < commander.args.length; ii++) {
|
3091 | const item = commander.args[ii];
|
3092 | if (item === '-') {
|
3093 | inputs.push(readFromStdin());
|
3094 | } else if (isHttpOrHttpsUrl(item)) {
|
3095 | inputs.push(readFromUrl(item, commander.userAgent));
|
3096 | } else {
|
3097 | inputs.push(readFromFile(item));
|
3098 | }
|
3099 | }
|
3100 | getInstance(commander.validator_js, commander.userAgent)
|
3101 | .then(function(validator) {
|
3102 | promise.all(inputs)
|
3103 | .then(function(resolvedInputs) {
|
3104 | const jsonOut = {};
|
3105 | let hasError = false;
|
3106 | for (let ii = 0; ii < resolvedInputs.length; ii++) {
|
3107 | const validationResult = validator.validateString(
|
3108 | resolvedInputs[ii], commander.html_format);
|
3109 | if (commander.format === 'json') {
|
3110 | jsonOut[commander.args[ii]] = validationResult;
|
3111 | } else {
|
3112 | logValidationResult(
|
3113 | commander.args[ii], validationResult,
|
3114 | commander.format === 'color' ? true : false);
|
3115 | }
|
3116 | if (validationResult.status !== 'PASS') {
|
3117 | hasError = true;
|
3118 | }
|
3119 | }
|
3120 | if (commander.format === 'json') {
|
3121 | process.stdout.write(
|
3122 | JSON.stringify(jsonOut) + '\n', function() {
|
3123 | process.exit(hasError ? 1 : 0);
|
3124 | });
|
3125 | } else if (hasError) {
|
3126 | process.stderr.write('', function() {
|
3127 | process.exit(1);
|
3128 | });
|
3129 | } else {
|
3130 | process.stdout.write('', function() {
|
3131 | process.exit(0);
|
3132 | });
|
3133 | }
|
3134 | })
|
3135 | .catch(function(error) {
|
3136 | process.stderr.write(
|
3137 | (commander.format == 'color' ? safe.red(error.message) :
|
3138 | error.message) +
|
3139 | '\n',
|
3140 | function() {
|
3141 | process.exit(1);
|
3142 | });
|
3143 | });
|
3144 | })
|
3145 | .catch(function(error) {
|
3146 | process.stderr.write(
|
3147 | (commander.format == 'color' ? safe.red(error.message) :
|
3148 | error.message) +
|
3149 | '\n',
|
3150 | function() {
|
3151 | process.exit(1);
|
3152 | });
|
3153 | });
|
3154 | }
|
3155 |
|
3156 | var main_1 = main;
|
3157 |
|
3158 | var amphtmlValidator = {
|
3159 | getInstance: getInstance_1,
|
3160 | newInstance: newInstance_1,
|
3161 | main: main_1
|
3162 | };
|
3163 |
|
3164 |
|
3165 |
|
3166 |
|
3167 | function dev(opts) {
|
3168 | return new Watcher(opts).init();
|
3169 | }
|
3170 |
|
3171 | class Watcher extends EventEmitter {
|
3172 |
|
3173 | constructor({ cwd = process.cwd(), port, config }) {
|
3174 | super();
|
3175 |
|
3176 | this.cwd = cwd;
|
3177 | this.dir = path.resolve(cwd, '.svelte/dev');
|
3178 |
|
3179 | this.port = port;
|
3180 | this.config = config;
|
3181 |
|
3182 | process.env.NODE_ENV = 'development';
|
3183 |
|
3184 | process.on('exit', () => {
|
3185 | this.close();
|
3186 | });
|
3187 | }
|
3188 |
|
3189 | async init() {
|
3190 | rimraf(this.dir);
|
3191 | copy_assets(this.dir);
|
3192 | process.env.VITE_SVELTEKIT_AMP = this.config.kit.amp ? 'true' : '';
|
3193 |
|
3194 | await this.init_filewatcher();
|
3195 | await this.init_server();
|
3196 |
|
3197 | this.update();
|
3198 |
|
3199 | return this;
|
3200 | }
|
3201 |
|
3202 | async init_filewatcher() {
|
3203 | this.cheapwatch = new CheapWatch({
|
3204 | dir: this.config.kit.files.routes,
|
3205 |
|
3206 | filter: ({ path }) => path.split('/').every((part) => !part.startsWith('_'))
|
3207 | });
|
3208 |
|
3209 | await this.cheapwatch.init();
|
3210 |
|
3211 |
|
3212 | this.cheapwatch.on('+', ({ isNew }) => {
|
3213 | if (isNew) this.update();
|
3214 | });
|
3215 |
|
3216 | this.cheapwatch.on('-', () => {
|
3217 | this.update();
|
3218 | });
|
3219 | }
|
3220 |
|
3221 | async init_server() {
|
3222 | |
3223 |
|
3224 |
|
3225 | this.viteDevServer = await vite.createServer({
|
3226 | root: this.cwd,
|
3227 | resolve: {
|
3228 | alias: {
|
3229 | $app: path.resolve(`${this.dir}/runtime/app`),
|
3230 | $lib: this.config.kit.files.lib
|
3231 | }
|
3232 | },
|
3233 | plugins: [
|
3234 | svelte({
|
3235 | extensions: this.config.extensions
|
3236 | })
|
3237 | ],
|
3238 | publicDir: this.config.kit.files.assets,
|
3239 | server: {
|
3240 | middlewareMode: true
|
3241 | }
|
3242 | });
|
3243 |
|
3244 | const validator = this.config.kit.amp && (await amphtmlValidator.getInstance());
|
3245 |
|
3246 | this.server = http.createServer((req, res) => {
|
3247 | this.viteDevServer.middlewares(req, res, async () => {
|
3248 | try {
|
3249 | const parsed = parse(req.url);
|
3250 |
|
3251 | if (req.url === '/favicon.ico') return;
|
3252 |
|
3253 |
|
3254 | const template = fs.readFileSync(this.config.kit.files.template, 'utf-8');
|
3255 |
|
3256 | const setup = await this.viteDevServer
|
3257 | .ssrLoadModule(`/${this.config.kit.files.setup}`)
|
3258 | .catch(() => ({}));
|
3259 |
|
3260 | let root;
|
3261 |
|
3262 | try {
|
3263 | root = (await this.viteDevServer.ssrLoadModule(`/${this.dir}/generated/root.svelte`))
|
3264 | .default;
|
3265 | } catch (e) {
|
3266 | res.statusCode = 500;
|
3267 | res.end(e.stack);
|
3268 | return;
|
3269 | }
|
3270 |
|
3271 | const body = await get_body(req);
|
3272 |
|
3273 | const rendered = await ssr(
|
3274 | {
|
3275 | headers: req.headers,
|
3276 | method: req.method,
|
3277 | host: null,
|
3278 | path: parsed.pathname,
|
3279 | query: new URLSearchParams(parsed.query),
|
3280 | body
|
3281 | },
|
3282 | {
|
3283 | paths: this.config.kit.paths,
|
3284 | template: ({ head, body }) => {
|
3285 | let rendered = template
|
3286 | .replace('%svelte.head%', () => head)
|
3287 | .replace('%svelte.body%', () => body);
|
3288 |
|
3289 | if (this.config.kit.amp) {
|
3290 | const result = validator.validateString(rendered);
|
3291 |
|
3292 | if (result.status !== 'PASS') {
|
3293 | const lines = rendered.split('\n');
|
3294 |
|
3295 |
|
3296 | const escape = (str) =>
|
3297 | str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
3298 |
|
3299 | rendered = `<!doctype html>
|
3300 | <head>
|
3301 | <meta charset="utf-8" />
|
3302 | <meta name="viewport" content="width=device-width, initial-scale=1" />
|
3303 | <style>
|
3304 | body {
|
3305 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
3306 | color: #333;
|
3307 | }
|
3308 |
|
3309 | pre {
|
3310 | background: #f4f4f4;
|
3311 | padding: 1em;
|
3312 | overflow-x: auto;
|
3313 | }
|
3314 | </style>
|
3315 | </head>
|
3316 | <h1>AMP validation failed</h1>
|
3317 |
|
3318 | ${result.errors
|
3319 | .map(
|
3320 | (error) => `
|
3321 | <h2>${error.severity}</h2>
|
3322 | <p>Line ${error.line}, column ${error.col}: ${error.message} (<a href="${error.specUrl}">${
|
3323 | error.code
|
3324 | }</a>)</p>
|
3325 | <pre>${escape(lines[error.line - 1])}</pre>
|
3326 | `
|
3327 | )
|
3328 | .join('\n\n')}
|
3329 | `;
|
3330 | }
|
3331 | }
|
3332 |
|
3333 | return rendered;
|
3334 | },
|
3335 | manifest: this.manifest,
|
3336 | target: this.config.kit.target,
|
3337 | entry: '/.svelte/dev/runtime/internal/start.js',
|
3338 | dev: true,
|
3339 | amp: this.config.kit.amp,
|
3340 | root,
|
3341 | setup,
|
3342 | only_prerender: false,
|
3343 | start_global: this.config.kit.startGlobal,
|
3344 | host: this.config.kit.host,
|
3345 | host_header: this.config.kit.hostHeader,
|
3346 | get_stack: (error) => {
|
3347 | this.viteDevServer.ssrFixStacktrace(error);
|
3348 | return error.stack;
|
3349 | },
|
3350 | get_static_file: (file) =>
|
3351 | fs.readFileSync(path.join(this.config.kit.files.assets, file)),
|
3352 | get_amp_css: (url) => ''
|
3353 | }
|
3354 | );
|
3355 |
|
3356 | if (rendered) {
|
3357 | res.writeHead(rendered.status, rendered.headers);
|
3358 | res.end(rendered.body);
|
3359 | } else {
|
3360 | res.statusCode = 404;
|
3361 | res.end('Not found');
|
3362 | }
|
3363 | } catch (e) {
|
3364 | this.viteDevServer.ssrFixStacktrace(e);
|
3365 | res.end(e.stack);
|
3366 | }
|
3367 | });
|
3368 | });
|
3369 |
|
3370 | this.server.listen(this.port);
|
3371 | }
|
3372 |
|
3373 | update() {
|
3374 | const manifest_data = create_manifest_data({
|
3375 | config: this.config,
|
3376 | output: this.dir,
|
3377 | cwd: this.cwd
|
3378 | });
|
3379 |
|
3380 | create_app({
|
3381 | manifest_data,
|
3382 | output: this.dir,
|
3383 | cwd: this.cwd
|
3384 | });
|
3385 |
|
3386 | const common_css_deps = new Set();
|
3387 |
|
3388 | |
3389 |
|
3390 |
|
3391 | const load = async (file) => {
|
3392 | const url = path.resolve(this.cwd, file);
|
3393 |
|
3394 | const mod = await this.viteDevServer.ssrLoadModule(url);
|
3395 | const node = await this.viteDevServer.moduleGraph.getModuleByUrl(url);
|
3396 |
|
3397 | const deps = new Set();
|
3398 | find_deps(node, deps);
|
3399 |
|
3400 | const css = new Set();
|
3401 | for (const dep of deps) {
|
3402 |
|
3403 | if (dep.file.endsWith('.css')) {
|
3404 | try {
|
3405 | const mod = await this.viteDevServer.ssrLoadModule(dep.url);
|
3406 | css.add(mod.default);
|
3407 | } catch {
|
3408 |
|
3409 |
|
3410 |
|
3411 | }
|
3412 | }
|
3413 | }
|
3414 |
|
3415 | return { mod, css };
|
3416 | };
|
3417 |
|
3418 | |
3419 |
|
3420 |
|
3421 |
|
3422 | const find_deps = (node, deps) => {
|
3423 | for (const dep of node.importedModules) {
|
3424 | if (!deps.has(dep)) {
|
3425 | deps.add(dep);
|
3426 | find_deps(dep, deps);
|
3427 | }
|
3428 | }
|
3429 | };
|
3430 |
|
3431 | this.manifest = {
|
3432 | assets: manifest_data.assets,
|
3433 | layout: async () => {
|
3434 | const { mod, css } = await load(manifest_data.layout);
|
3435 | css.forEach((mod) => {
|
3436 | common_css_deps.add(mod);
|
3437 | });
|
3438 | return mod;
|
3439 | },
|
3440 | error: async () => {
|
3441 | const { mod, css } = await load(manifest_data.error);
|
3442 | css.forEach((mod) => {
|
3443 | common_css_deps.add(mod);
|
3444 | });
|
3445 | return mod;
|
3446 | },
|
3447 | pages: manifest_data.pages.map((data) => {
|
3448 |
|
3449 |
|
3450 | const css_deps = new Set();
|
3451 |
|
3452 | return {
|
3453 | pattern: data.pattern,
|
3454 | params: get_params(data.params),
|
3455 | parts: data.parts.map((file) => async () => {
|
3456 | const { mod, css } = await load(file);
|
3457 |
|
3458 | css.forEach((mod) => {
|
3459 | css_deps.add(mod);
|
3460 | });
|
3461 |
|
3462 | return mod;
|
3463 | }),
|
3464 | get style() {
|
3465 |
|
3466 |
|
3467 | return [...common_css_deps, ...css_deps].join('\n');
|
3468 | },
|
3469 | css: [],
|
3470 | js: []
|
3471 | };
|
3472 | }),
|
3473 | endpoints: manifest_data.endpoints.map((data) => ({
|
3474 | pattern: data.pattern,
|
3475 | params: get_params(data.params),
|
3476 | load: async () => {
|
3477 | const url = path.resolve(this.cwd, data.file);
|
3478 | return await this.viteDevServer.ssrLoadModule(url);
|
3479 | }
|
3480 | }))
|
3481 | };
|
3482 | }
|
3483 |
|
3484 | close() {
|
3485 | if (this.closed) return;
|
3486 | this.closed = true;
|
3487 |
|
3488 | this.viteDevServer.close();
|
3489 | this.server.close();
|
3490 | this.cheapwatch.close();
|
3491 | }
|
3492 | }
|
3493 |
|
3494 |
|
3495 | function get_params(array) {
|
3496 |
|
3497 |
|
3498 |
|
3499 |
|
3500 |
|
3501 | const fn = (match) => {
|
3502 |
|
3503 | const params = {};
|
3504 | array.forEach((key, i) => {
|
3505 | if (key.startsWith('...')) {
|
3506 | params[key.slice(3)] = decodeURIComponent(match[i + 1]).split('/');
|
3507 | } else {
|
3508 | params[key] = decodeURIComponent(match[i + 1]);
|
3509 | }
|
3510 | });
|
3511 | return params;
|
3512 | };
|
3513 |
|
3514 | return fn;
|
3515 | }
|
3516 |
|
3517 | export { dev };
|