UNPKG

7.42 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const args_1 = require("./args");
13const args = args_1.argParser.parse();
14const path = require("path");
15const fs = require("fs");
16const ecmarkup = require("./ecmarkup");
17const utils = require("./utils");
18const debounce = require('promise-debounce');
19// back compat to old argument names
20if (args.css) {
21 args.cssOut = args.css;
22}
23if (args.js) {
24 args.jsOut = args.js;
25}
26if (args.strict && args.watch) {
27 fail('Cannot use --strict with --watch');
28}
29if (!args.outfile && (args.jsOut || args.cssOut)) {
30 fail('When using --js-out or --css-out you must specify an output file');
31}
32if (args.multipage) {
33 if (args.jsOut || args.cssOut) {
34 fail('Cannot use --multipage with --js-out or --css-out');
35 }
36 if (!args.outfile) {
37 fail('When using --multipage you must specify an output directory');
38 }
39 if (fs.existsSync(args.outfile) && !fs.lstatSync(args.outfile).isDirectory()) {
40 fail('When using --multipage, outfile (' + args.outfile + ') must be a directory');
41 }
42 fs.mkdirSync(path.resolve(args.outfile, 'multipage'), { recursive: true });
43}
44function fail(msg) {
45 console.error(msg);
46 process.exit(1);
47}
48const watching = new Map();
49const build = debounce(function build() {
50 return __awaiter(this, void 0, void 0, function* () {
51 try {
52 const opts = Object.assign({}, args);
53 if (args.verbose) {
54 opts.log = utils.logVerbose;
55 }
56 let warned = false;
57 let descriptor = `eslint/lib/cli-engine/formatters/${args.lintFormatter}.js`;
58 try {
59 require.resolve(descriptor);
60 }
61 catch (_a) {
62 descriptor = args.lintFormatter;
63 }
64 const formatter = require(descriptor);
65 const warnings = [];
66 opts.warn = err => {
67 var _a;
68 warned = true;
69 const file = normalizePath((_a = err.file) !== null && _a !== void 0 ? _a : args.infile);
70 // prettier-ignore
71 const message = `${args.strict ? 'Error' : 'Warning'}: ${file}:${err.line == null ? '' : `${err.line}:${err.column}:`} ${err.message}`;
72 utils.logWarning(message);
73 warnings.push(err);
74 };
75 const spec = yield ecmarkup.build(args.infile, utils.readFile, opts);
76 if (args.verbose) {
77 utils.logVerbose(warned ? 'Completed with errors.' : 'Done.');
78 }
79 const pending = [];
80 if (args.biblio) {
81 if (args.verbose) {
82 utils.logVerbose('Writing biblio file to ' + args.biblio);
83 }
84 pending.push(utils.writeFile(args.biblio, JSON.stringify(spec.exportBiblio())));
85 }
86 if (args.verbose && warned) {
87 warnings.sort((a, b) => {
88 var _a, _b;
89 const aPath = normalizePath((_a = a.file) !== null && _a !== void 0 ? _a : args.infile);
90 const bPath = normalizePath((_b = a.file) !== null && _b !== void 0 ? _b : args.infile);
91 if (aPath !== bPath) {
92 return aPath.localeCompare(bPath);
93 }
94 if (a.line === b.line) {
95 if (a.column === b.column) {
96 return 0;
97 }
98 if (a.column == null) {
99 return -1;
100 }
101 if (b.column == null) {
102 return 1;
103 }
104 return a.column - b.column;
105 }
106 if (a.line == null) {
107 return -1;
108 }
109 if (b.line == null) {
110 return 1;
111 }
112 return a.line - b.line;
113 });
114 const results = warnings.map(err => {
115 var _a;
116 return ({
117 filePath: normalizePath((_a = err.file) !== null && _a !== void 0 ? _a : args.infile),
118 messages: [Object.assign({ severity: args.strict ? 2 : 1 }, err)],
119 errorCount: args.strict ? 1 : 0,
120 warningCount: args.strict ? 0 : 1,
121 // for now, nothing is fixable
122 fixableErrorCount: 0,
123 fixableWarningCount: 0,
124 source: err.source,
125 });
126 });
127 console.error(formatter(results));
128 }
129 if (!args.strict || !warned) {
130 if (args.outfile) {
131 if (args.verbose) {
132 utils.logVerbose('Writing output...');
133 }
134 for (const [file, contents] of spec.generatedFiles) {
135 pending.push(utils.writeFile(file, contents));
136 }
137 }
138 else {
139 process.stdout.write(spec.generatedFiles.get(null));
140 }
141 }
142 yield Promise.all(pending);
143 if (args.strict && warned) {
144 utils.logVerbose('Exiting with an error due to errors (omit --strict to write output anyway)');
145 if (!args.verbose) {
146 utils.logVerbose('Rerun with --verbose to see detailed error information');
147 }
148 process.exit(1);
149 }
150 if (args.watch) {
151 const toWatch = new Set(spec.imports.map(i => i.importLocation).concat(args.infile));
152 // remove any files that we're no longer watching
153 for (const [file, watcher] of watching) {
154 if (!toWatch.has(file)) {
155 watcher.close();
156 watching.delete(file);
157 }
158 }
159 // watch any new files
160 for (const file of toWatch) {
161 if (!watching.has(file)) {
162 watching.set(file, fs.watch(file, build));
163 }
164 }
165 }
166 }
167 catch (e) {
168 if (args.watch) {
169 process.stderr.write(e.stack);
170 }
171 else {
172 throw e;
173 }
174 }
175 });
176});
177build().catch(e => {
178 console.error(e);
179 process.exit(1);
180});
181function normalizePath(absolute) {
182 return path.relative(process.cwd(), absolute);
183}