UNPKG

@platform/ts

Version:

TypesScript build, prepare and publish toolchain.

237 lines (236 loc) 11.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processArgs = exports.build = exports.buildAs = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); var BUILD_FORMATS = ['COMMON_JS', 'ES_MODULE']; function buildAs(formats, args) { if (args === void 0) { args = {}; } return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, silent, _b, outDir, code, error, _c, cwd, log, errorLog, tasks, res, ok; var _this = this; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: return [4, processArgs(args)]; case 1: _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; log = (0, common_1.getLog)(silent); if (code !== 0) { return [2, common_1.result.formatResult({ code: code, error: error })]; } return [4, deleteTempDirs()]; case 2: _d.sent(); return [4, common_1.fs.remove(outDir)]; case 3: _d.sent(); if (!formats.includes('ES_MODULE')) return [3, 5]; return [4, ensureMainHasNoExtension(cwd, { silent: silent })]; case 4: _d.sent(); _d.label = 5; case 5: tasks = formats.map(function (format) { var title = format === 'ES_MODULE' ? '.mjs ESModule' : '.js CommonJS'; return { title: "build ".concat(title), task: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () { var res; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, build(tslib_1.__assign(tslib_1.__assign({}, args), { as: format, silent: true }))]; case 1: res = _a.sent(); if (res.errorLog && !errorLog) { errorLog = res.errorLog; } if (!res.ok) { throw res.error; } return [2, res]; } }); }); }, }; }); log.info(); return [4, common_1.exec.tasks.run(tasks, { concurrent: true })]; case 6: res = _d.sent(); if (errorLog) { log.info("\n".concat(errorLog)); } log.info(); ok = res.ok; return [2, common_1.result.formatResult({ ok: ok, code: ok ? 0 : 1 })]; } }); }); } exports.buildAs = buildAs; function build(args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _a, _b, cwd, _c, outDir, silent, watch, as, code, error, tsconfig, tmpDir, tsc, cmd, res, response, res, errorLog, error_1; return tslib_1.__generator(this, function (_d) { switch (_d.label) { case 0: return [4, processArgs(args)]; case 1: _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; if (code !== 0) { return [2, common_1.result.formatResult({ code: code, error: error })]; } tmpDir = toTmpDir(as); tsc = 'tsc'; cmd = "cd ".concat(common_1.fs.resolve(cwd), "\n"); cmd += tsc; cmd += " --outDir ".concat(common_1.fs.resolve(watch ? outDir : tmpDir)); cmd = watch ? "".concat(cmd, " --watch") : cmd; cmd = tsconfig ? "".concat(cmd, " --project ").concat(tsconfig) : cmd; switch (as) { case 'COMMON_JS': cmd += " --module commonjs"; cmd += " --target es5"; cmd += " --declaration"; break; case 'ES_MODULE': cmd += " --module es2015"; cmd += " --target ES2017"; cmd += " --declaration false"; break; } cmd += '\n'; _d.label = 2; case 2: _d.trys.push([2, 13, , 15]); if (!watch) return [3, 5]; return [4, deleteTempDirs()]; case 3: _d.sent(); return [4, common_1.exec.cmd.run(cmd, { silent: silent, cwd: cwd })]; case 4: res = _d.sent(); return [2, res]; case 5: response = common_1.exec.cmd.runList(cmd, { silent: silent, cwd: cwd }); return [4, response]; case 6: res = _d.sent(); if (res.code !== 0) { errorLog = res.errors.log({ log: null, header: false }); return [2, tslib_1.__assign(tslib_1.__assign({}, common_1.result.fail("Build failed.", res.code)), { errorLog: errorLog })]; } if (!(as === 'ES_MODULE')) return [3, 8]; return [4, (0, common_1.changeExtensions)({ dir: tmpDir, from: '.js', to: '.mjs' })]; case 7: _d.sent(); _d.label = 8; case 8: return [4, common_1.fs.ensureDir(common_1.fs.resolve(outDir))]; case 9: _d.sent(); return [4, common_1.fs.merge(common_1.fs.resolve(tmpDir), common_1.fs.resolve(outDir))]; case 10: _d.sent(); return [4, common_1.fs.remove(common_1.fs.resolve(tmpDir))]; case 11: _d.sent(); return [2, res]; case 12: return [3, 15]; case 13: error_1 = _d.sent(); return [4, deleteTempDirs()]; case 14: _d.sent(); return [2, common_1.result.fail(error_1)]; case 15: return [2]; } }); }); } exports.build = build; function deleteTempDirs() { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { 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)); }))]; case 1: _a.sent(); return [2]; } }); }); } function toTmpDir(buildFormat) { if (buildFormat === void 0) { buildFormat = 'COMMON_JS'; } return ".tmp.".concat(buildFormat).toLowerCase(); } function processArgs(args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var silent, watch, _a, as, cwd, _b, error, tsconfig, error, outDir, error; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: silent = args.silent, watch = args.watch, _a = args.as, as = _a === void 0 ? 'COMMON_JS' : _a; _b = args.dir; if (_b) return [3, 2]; return [4, common_1.paths.closestParentOf('node_modules')]; case 1: _b = (_c.sent()); _c.label = 2; case 2: cwd = _b; if (!cwd) { error = new Error("The root directory containing 'node_modules' was not found."); return [2, { code: 1, error: error }]; } return [4, common_1.paths.tsconfig(cwd, args.tsconfig)]; case 3: tsconfig = _c.sent(); if (!tsconfig.success) { error = new Error("A 'tsconfig.json' file could not be found."); return [2, { code: 1, error: error }]; } outDir = args.outDir || tsconfig.outDir; if (!outDir) { error = new Error("An 'outDir' is not specified within 'tsconfig.json'."); return [2, { code: 1, error: error }]; } return [2, { code: 0, cwd: cwd, outDir: outDir, silent: silent, watch: watch, as: as, tsconfig: tsconfig.filename }]; } }); }); } exports.processArgs = processArgs; function ensureMainHasNoExtension(dir, options) { if (options === void 0) { options = {}; } return tslib_1.__awaiter(this, void 0, void 0, function () { var path, pkg, ext, log, from, to; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: path = common_1.fs.join(dir, 'package.json'); return [4, common_1.fs.file.loadAndParse(path)]; case 1: pkg = _a.sent(); if (!pkg.main) { return [2, false]; } ext = common_1.fs.extname(pkg.main); if (!['.js', '.mjs'].includes(ext)) return [3, 3]; log = (0, common_1.getLog)(options.silent); from = pkg.main; to = pkg.main.substr(0, pkg.main.length - ext.length); log.info(); log.info("Removed extension (".concat(ext, ") from [package.json].main")); log.info("\u2022 from: ".concat(from)); log.info("\u2022 to: ".concat(to)); log.info(); pkg.main = to; return [4, common_1.fs.file.stringifyAndSave(path, pkg)]; case 2: _a.sent(); return [2, true]; case 3: return [2, false]; } }); }); }