UNPKG

11.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.buildWatch = exports.buildOnce = exports.build = exports.cmd = exports.args = exports.description = exports.name = void 0;
4var tslib_1 = require("tslib");
5var rxjs_1 = require("rxjs");
6var operators_1 = require("rxjs/operators");
7var common_1 = require("../common");
8var listCommand = require("./ls.cmd");
9var syncCommand = require("./sync.cmd");
10exports.name = 'build';
11exports.description = 'Builds and syncs all typescript modules in order.';
12exports.args = {
13 '-i': 'Include ignored modules.',
14 '-w': 'Sync on changes to files.',
15 '-v': 'Verbose mode. Prints all error details.',
16};
17function cmd(args) {
18 return tslib_1.__awaiter(this, void 0, void 0, function () {
19 var options, watch, includeIgnored, verbose;
20 return tslib_1.__generator(this, function (_a) {
21 switch (_a.label) {
22 case 0:
23 options = (args && args.options) || {};
24 watch = options.w || false;
25 includeIgnored = options.i || false;
26 verbose = options.v || false;
27 return [4, build({ includeIgnored: includeIgnored, watch: watch, verbose: verbose })];
28 case 1:
29 _a.sent();
30 return [2];
31 }
32 });
33 });
34}
35exports.cmd = cmd;
36function build(options) {
37 if (options === void 0) { options = {}; }
38 return tslib_1.__awaiter(this, void 0, void 0, function () {
39 var _a, includeIgnored, _b, watch, _c, verbose, settings, modules;
40 return tslib_1.__generator(this, function (_d) {
41 switch (_d.label) {
42 case 0:
43 _a = options.includeIgnored, includeIgnored = _a === void 0 ? false : _a, _b = options.watch, watch = _b === void 0 ? false : _b, _c = options.verbose, verbose = _c === void 0 ? false : _c;
44 return [4, common_1.loadSettings()];
45 case 1:
46 settings = _d.sent();
47 if (!settings) {
48 common_1.log.warn.yellow(common_1.constants.CONFIG_NOT_FOUND_ERROR);
49 return [2];
50 }
51 modules = settings.modules
52 .filter(function (pkg) { return common_1.filter.includeIgnored(pkg, includeIgnored); })
53 .filter(function (pkg) { return pkg.isTypeScript; });
54 if (watch) {
55 return [2, buildWatch(modules, includeIgnored, verbose)];
56 }
57 else {
58 return [2, buildOnce(modules)];
59 }
60 return [2];
61 }
62 });
63 });
64}
65exports.build = build;
66var tscCommand = function (pkg) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
67 var path;
68 return tslib_1.__generator(this, function (_a) {
69 switch (_a.label) {
70 case 0:
71 path = common_1.fs.join(pkg.dir, 'node_modules/typescript/bin/tsc');
72 return [4, common_1.fs.pathExists(path)];
73 case 1: return [2, (_a.sent()) ? path : 'tsc'];
74 }
75 });
76}); };
77function buildOnce(modules) {
78 return tslib_1.__awaiter(this, void 0, void 0, function () {
79 var startedAt, tasks, taskList, error_1;
80 var _this = this;
81 return tslib_1.__generator(this, function (_a) {
82 switch (_a.label) {
83 case 0:
84 startedAt = new Date();
85 tasks = modules.map(function (pkg) {
86 return {
87 title: common_1.log.magenta(pkg.name) + " " + common_1.log.gray('=> sync'),
88 task: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
89 var tsc, cmd;
90 return tslib_1.__generator(this, function (_a) {
91 switch (_a.label) {
92 case 0: return [4, tscCommand(pkg)];
93 case 1:
94 tsc = _a.sent();
95 cmd = "cd " + pkg.dir + " && " + tsc;
96 return [4, common_1.exec.cmd.run(cmd, { silent: true })];
97 case 2:
98 _a.sent();
99 return [4, syncCommand.sync({
100 includeIgnored: false,
101 updateVersions: false,
102 silent: true,
103 })];
104 case 3:
105 _a.sent();
106 return [2];
107 }
108 });
109 }); },
110 };
111 });
112 _a.label = 1;
113 case 1:
114 _a.trys.push([1, 3, , 4]);
115 taskList = common_1.listr(tasks, { concurrent: false, exitOnError: false });
116 return [4, taskList.run()];
117 case 2:
118 _a.sent();
119 common_1.log.info.gray('', common_1.elapsed(startedAt));
120 common_1.log.info();
121 return [3, 4];
122 case 3:
123 error_1 = _a.sent();
124 return [3, 4];
125 case 4: return [2];
126 }
127 });
128 });
129}
130exports.buildOnce = buildOnce;
131function buildWatch(modules, includeIgnored, verbose) {
132 return tslib_1.__awaiter(this, void 0, void 0, function () {
133 var state, updates$;
134 var _this = this;
135 return tslib_1.__generator(this, function (_a) {
136 common_1.log.info.magenta('\nBuild watching:');
137 listCommand.printTable(modules, { includeIgnored: includeIgnored });
138 common_1.log.info();
139 state = {};
140 updates$ = new rxjs_1.Subject();
141 updates$.pipe(operators_1.debounceTime(100)).subscribe(function () {
142 common_1.log.clear();
143 var items = Object.keys(state)
144 .sort()
145 .map(function (key) { return ({ key: key, value: state[key] }); });
146 items.forEach(function (_a) {
147 var key = _a.key, value = _a.value;
148 var hasErrors = value.errors.length > 0;
149 var bullet = hasErrors ? common_1.log.red('✘') : value.isBuilding ? common_1.log.gray('✎') : common_1.log.green('✔');
150 common_1.log.info(bullet + " " + common_1.log.cyan(key) + " " + value.message);
151 });
152 var errors = items.filter(function (_a) {
153 var value = _a.value;
154 return value.errors.length > 0;
155 });
156 if (verbose && errors.length > 0) {
157 common_1.log.info();
158 errors.forEach(function (_a) {
159 var key = _a.key, value = _a.value;
160 value.errors.forEach(function (error) {
161 common_1.log
162 .table()
163 .add([common_1.log.yellow(key), formatError(error)])
164 .log();
165 });
166 });
167 }
168 });
169 modules.forEach(function (pkg) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
170 var tsc, cmd;
171 return tslib_1.__generator(this, function (_a) {
172 switch (_a.label) {
173 case 0: return [4, tscCommand(pkg)];
174 case 1:
175 tsc = _a.sent();
176 cmd = "cd " + pkg.dir + " && " + tsc + " --watch";
177 common_1.exec.cmd.run(cmd).output$.subscribe(function (e) {
178 var text = e.text;
179 var isBuilding = text.includes('Starting compilation in watch') ||
180 text.includes('Starting incremental compilation');
181 var isError = text.includes('error') && !text.includes('Found 0 errors.');
182 var isSuccess = text.includes('Found 0 errors.');
183 var isBuilt = text.includes('Watching for file changes.');
184 text = text.replace(/\n*$/, '');
185 var key = pkg.name;
186 var obj = state[key] || { count: 0, errors: [] };
187 obj.isBuilding = isBuilding;
188 if (isBuilding || isBuilt) {
189 var count = isBuilding ? obj.count + 1 : obj.count;
190 var status_1 = isBuilding ? 'Building...' : 'Built';
191 var countStatus = isBuilding ? common_1.log.gray(count) : common_1.log.green(count);
192 var message = common_1.log.gray(status_1 + " (" + countStatus + ")");
193 obj = tslib_1.__assign(tslib_1.__assign({}, obj), { count: count, message: message });
194 }
195 if (isError) {
196 if (!isBuilt) {
197 obj.errors = tslib_1.__spreadArrays(obj.errors, [text]);
198 }
199 var error = obj.errors.length === 1 ? 'Error' : 'Errors';
200 obj.message = common_1.log.red(obj.errors.length + " " + error);
201 }
202 if (isSuccess) {
203 obj.errors = [];
204 }
205 state[key] = obj;
206 updates$.next();
207 });
208 return [2];
209 }
210 });
211 }); });
212 return [2];
213 });
214 });
215}
216exports.buildWatch = buildWatch;
217var formatError = function (error) {
218 var MAX = 80;
219 var lines = [];
220 error.split('\n').forEach(function (line) {
221 line = line.length <= MAX ? line : splitLines(line);
222 lines.push(line);
223 });
224 return lines.join('\n');
225};
226var splitLines = function (line) {
227 var MAX = 80;
228 var words = [];
229 var count = 0;
230 line.split('').forEach(function (word) {
231 count += word.length;
232 if (count > MAX) {
233 word = word + "\n";
234 count = 0;
235 }
236 words.push(word);
237 });
238 return words.join('');
239};