1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.processArgs = exports.build = exports.buildAs = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var common_1 = require("../common");
|
6 | var BUILD_FORMATS = ['COMMON_JS', 'ES_MODULE'];
|
7 | function buildAs(formats, args) {
|
8 | if (args === void 0) { args = {}; }
|
9 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
10 | var _a, silent, _b, outDir, code, error, _c, cwd, log, errorLog, tasks, res, ok;
|
11 | var _this = this;
|
12 | return tslib_1.__generator(this, function (_d) {
|
13 | switch (_d.label) {
|
14 | case 0: return [4, processArgs(args)];
|
15 | case 1:
|
16 | _a = _d.sent(), silent = _a.silent, _b = _a.outDir, outDir = _b === void 0 ? '' : _b, code = _a.code, error = _a.error, _c = _a.cwd, cwd = _c === void 0 ? '' : _c;
|
17 | log = (0, common_1.getLog)(silent);
|
18 | if (code !== 0) {
|
19 | return [2, common_1.result.formatResult({ code: code, error: error })];
|
20 | }
|
21 | return [4, deleteTempDirs()];
|
22 | case 2:
|
23 | _d.sent();
|
24 | return [4, common_1.fs.remove(outDir)];
|
25 | case 3:
|
26 | _d.sent();
|
27 | if (!formats.includes('ES_MODULE')) return [3, 5];
|
28 | return [4, ensureMainHasNoExtension(cwd, { silent: silent })];
|
29 | case 4:
|
30 | _d.sent();
|
31 | _d.label = 5;
|
32 | case 5:
|
33 | tasks = formats.map(function (format) {
|
34 | var title = format === 'ES_MODULE' ? '.mjs ESModule' : '.js CommonJS';
|
35 | return {
|
36 | title: "build ".concat(title),
|
37 | task: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
38 | var res;
|
39 | return tslib_1.__generator(this, function (_a) {
|
40 | switch (_a.label) {
|
41 | case 0: return [4, build(tslib_1.__assign(tslib_1.__assign({}, args), { as: format, silent: true }))];
|
42 | case 1:
|
43 | res = _a.sent();
|
44 | if (res.errorLog && !errorLog) {
|
45 | errorLog = res.errorLog;
|
46 | }
|
47 | if (!res.ok) {
|
48 | throw res.error;
|
49 | }
|
50 | return [2, res];
|
51 | }
|
52 | });
|
53 | }); },
|
54 | };
|
55 | });
|
56 | log.info();
|
57 | return [4, common_1.exec.tasks.run(tasks, { concurrent: true })];
|
58 | case 6:
|
59 | res = _d.sent();
|
60 | if (errorLog) {
|
61 | log.info("\n".concat(errorLog));
|
62 | }
|
63 | log.info();
|
64 | ok = res.ok;
|
65 | return [2, common_1.result.formatResult({ ok: ok, code: ok ? 0 : 1 })];
|
66 | }
|
67 | });
|
68 | });
|
69 | }
|
70 | exports.buildAs = buildAs;
|
71 | function build(args) {
|
72 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
73 | var _a, _b, cwd, _c, outDir, silent, watch, as, code, error, tsconfig, tmpDir, tsc, cmd, res, response, res, errorLog, error_1;
|
74 | return tslib_1.__generator(this, function (_d) {
|
75 | switch (_d.label) {
|
76 | case 0: return [4, processArgs(args)];
|
77 | case 1:
|
78 | _a = _d.sent(), _b = _a.cwd, cwd = _b === void 0 ? '' : _b, _c = _a.outDir, outDir = _c === void 0 ? '' : _c, silent = _a.silent, watch = _a.watch, as = _a.as, code = _a.code, error = _a.error, tsconfig = _a.tsconfig;
|
79 | if (code !== 0) {
|
80 | return [2, common_1.result.formatResult({ code: code, error: error })];
|
81 | }
|
82 | tmpDir = toTmpDir(as);
|
83 | tsc = 'tsc';
|
84 | cmd = "cd ".concat(common_1.fs.resolve(cwd), "\n");
|
85 | cmd += tsc;
|
86 | cmd += " --outDir ".concat(common_1.fs.resolve(watch ? outDir : tmpDir));
|
87 | cmd = watch ? "".concat(cmd, " --watch") : cmd;
|
88 | cmd = tsconfig ? "".concat(cmd, " --project ").concat(tsconfig) : cmd;
|
89 | switch (as) {
|
90 | case 'COMMON_JS':
|
91 | cmd += " --module commonjs";
|
92 | cmd += " --target es5";
|
93 | cmd += " --declaration";
|
94 | break;
|
95 | case 'ES_MODULE':
|
96 | cmd += " --module es2015";
|
97 | cmd += " --target ES2017";
|
98 | cmd += " --declaration false";
|
99 | break;
|
100 | }
|
101 | cmd += '\n';
|
102 | _d.label = 2;
|
103 | case 2:
|
104 | _d.trys.push([2, 13, , 15]);
|
105 | if (!watch) return [3, 5];
|
106 | return [4, deleteTempDirs()];
|
107 | case 3:
|
108 | _d.sent();
|
109 | return [4, common_1.exec.cmd.run(cmd, { silent: silent, cwd: cwd })];
|
110 | case 4:
|
111 | res = _d.sent();
|
112 | return [2, res];
|
113 | case 5:
|
114 | response = common_1.exec.cmd.runList(cmd, { silent: silent, cwd: cwd });
|
115 | return [4, response];
|
116 | case 6:
|
117 | res = _d.sent();
|
118 | if (res.code !== 0) {
|
119 | errorLog = res.errors.log({ log: null, header: false });
|
120 | return [2, tslib_1.__assign(tslib_1.__assign({}, common_1.result.fail("Build failed.", res.code)), { errorLog: errorLog })];
|
121 | }
|
122 | if (!(as === 'ES_MODULE')) return [3, 8];
|
123 | return [4, (0, common_1.changeExtensions)({ dir: tmpDir, from: '.js', to: '.mjs' })];
|
124 | case 7:
|
125 | _d.sent();
|
126 | _d.label = 8;
|
127 | case 8: return [4, common_1.fs.ensureDir(common_1.fs.resolve(outDir))];
|
128 | case 9:
|
129 | _d.sent();
|
130 | return [4, common_1.fs.merge(common_1.fs.resolve(tmpDir), common_1.fs.resolve(outDir))];
|
131 | case 10:
|
132 | _d.sent();
|
133 | return [4, common_1.fs.remove(common_1.fs.resolve(tmpDir))];
|
134 | case 11:
|
135 | _d.sent();
|
136 | return [2, res];
|
137 | case 12: return [3, 15];
|
138 | case 13:
|
139 | error_1 = _d.sent();
|
140 | return [4, deleteTempDirs()];
|
141 | case 14:
|
142 | _d.sent();
|
143 | return [2, common_1.result.fail(error_1)];
|
144 | case 15: return [2];
|
145 | }
|
146 | });
|
147 | });
|
148 | }
|
149 | exports.build = build;
|
150 | function deleteTempDirs() {
|
151 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
152 | return tslib_1.__generator(this, function (_a) {
|
153 | switch (_a.label) {
|
154 | case 0: return [4, Promise.all(BUILD_FORMATS.map(function (format) { return toTmpDir(format); }).map(function (dir) { return common_1.fs.remove(common_1.fs.resolve(dir)); }))];
|
155 | case 1:
|
156 | _a.sent();
|
157 | return [2];
|
158 | }
|
159 | });
|
160 | });
|
161 | }
|
162 | function toTmpDir(buildFormat) {
|
163 | if (buildFormat === void 0) { buildFormat = 'COMMON_JS'; }
|
164 | return ".tmp.".concat(buildFormat).toLowerCase();
|
165 | }
|
166 | function processArgs(args) {
|
167 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
168 | var silent, watch, _a, as, cwd, _b, error, tsconfig, error, outDir, error;
|
169 | return tslib_1.__generator(this, function (_c) {
|
170 | switch (_c.label) {
|
171 | case 0:
|
172 | silent = args.silent, watch = args.watch, _a = args.as, as = _a === void 0 ? 'COMMON_JS' : _a;
|
173 | _b = args.dir;
|
174 | if (_b) return [3, 2];
|
175 | return [4, common_1.paths.closestParentOf('node_modules')];
|
176 | case 1:
|
177 | _b = (_c.sent());
|
178 | _c.label = 2;
|
179 | case 2:
|
180 | cwd = _b;
|
181 | if (!cwd) {
|
182 | error = new Error("The root directory containing 'node_modules' was not found.");
|
183 | return [2, { code: 1, error: error }];
|
184 | }
|
185 | return [4, common_1.paths.tsconfig(cwd, args.tsconfig)];
|
186 | case 3:
|
187 | tsconfig = _c.sent();
|
188 | if (!tsconfig.success) {
|
189 | error = new Error("A 'tsconfig.json' file could not be found.");
|
190 | return [2, { code: 1, error: error }];
|
191 | }
|
192 | outDir = args.outDir || tsconfig.outDir;
|
193 | if (!outDir) {
|
194 | error = new Error("An 'outDir' is not specified within 'tsconfig.json'.");
|
195 | return [2, { code: 1, error: error }];
|
196 | }
|
197 | return [2, { code: 0, cwd: cwd, outDir: outDir, silent: silent, watch: watch, as: as, tsconfig: tsconfig.filename }];
|
198 | }
|
199 | });
|
200 | });
|
201 | }
|
202 | exports.processArgs = processArgs;
|
203 | function ensureMainHasNoExtension(dir, options) {
|
204 | if (options === void 0) { options = {}; }
|
205 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
206 | var path, pkg, ext, log, from, to;
|
207 | return tslib_1.__generator(this, function (_a) {
|
208 | switch (_a.label) {
|
209 | case 0:
|
210 | path = common_1.fs.join(dir, 'package.json');
|
211 | return [4, common_1.fs.file.loadAndParse(path)];
|
212 | case 1:
|
213 | pkg = _a.sent();
|
214 | if (!pkg.main) {
|
215 | return [2, false];
|
216 | }
|
217 | ext = common_1.fs.extname(pkg.main);
|
218 | if (!['.js', '.mjs'].includes(ext)) return [3, 3];
|
219 | log = (0, common_1.getLog)(options.silent);
|
220 | from = pkg.main;
|
221 | to = pkg.main.substr(0, pkg.main.length - ext.length);
|
222 | log.info();
|
223 | log.info("Removed extension (".concat(ext, ") from [package.json].main"));
|
224 | log.info("\u2022 from: ".concat(from));
|
225 | log.info("\u2022 to: ".concat(to));
|
226 | log.info();
|
227 | pkg.main = to;
|
228 | return [4, common_1.fs.file.stringifyAndSave(path, pkg)];
|
229 | case 2:
|
230 | _a.sent();
|
231 | return [2, true];
|
232 | case 3: return [2, false];
|
233 | }
|
234 | });
|
235 | });
|
236 | }
|