UNPKG

@platform/ts

Version:

TypesScript build, prepare and publish toolchain.

101 lines (100 loc) 4.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.publish = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); function publish(args) { if (args === void 0) { args = {}; } return tslib_1.__awaiter(this, void 0, void 0, function () { var dir, _a, tsconfig, outDir, modules, tmp, error_1; return tslib_1.__generator(this, function (_b) { switch (_b.label) { case 0: _a = args.dir; if (_a) return [3, 2]; return [4, common_1.paths.closestParentOf('tsconfig.json')]; case 1: _a = (_b.sent()); _b.label = 2; case 2: dir = _a; if (!dir) { return [2, common_1.result.fail("A 'tsconfig.json' file could not be found.")]; } return [4, common_1.paths.tsconfig(dir)]; case 3: tsconfig = _b.sent(); if (!tsconfig.success) { return [2, common_1.result.fail("Failed to load the 'tsconig.json' file.")]; } outDir = args.outDir || tsconfig.outDir; if (!outDir) { return [2, common_1.result.fail("The 'tsconfig.json' does not contain an 'outDir'.")]; } outDir = common_1.fs.resolve(outDir); modules = common_1.fs.join(dir, 'node_modules'); console.group('\n\n🐷 TODO publish\n'); console.log('modules', modules); console.log('tsconfig', tsconfig); console.log('outDir', outDir); console.log('\n\n'); console.groupEnd(); _b.label = 4; case 4: _b.trys.push([4, 7, , 8]); tmp = common_1.fs.resolve('.publish'); return [4, copyPackageJson({ rootDir: dir, target: tmp })]; case 5: _b.sent(); return [4, common_1.fs.copy(outDir, tmp)]; case 6: _b.sent(); return [2, common_1.result.success()]; case 7: error_1 = _b.sent(); return [2, common_1.result.fail(error_1)]; case 8: return [2]; } }); }); } exports.publish = publish; function copyPackageJson(args) { return tslib_1.__awaiter(this, void 0, void 0, function () { var packagePath, source, target, pkg, json, error_2; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 4, , 5]); return [4, common_1.fs.ensureDir(args.target)]; case 1: _a.sent(); packagePath = function (dir) { return common_1.fs.resolve(common_1.fs.join(dir, 'package.json')); }; source = packagePath(args.rootDir); target = packagePath(args.target); return [4, common_1.fs.file.loadAndParse(source)]; case 2: pkg = _a.sent(); pkg.types = pkg.types ? toParent(pkg.types) : pkg.types; pkg.main = pkg.main ? toParent(pkg.main) : pkg.main; pkg.main = pkg.main ? removeExtension(pkg.main) : pkg.main; delete pkg.devDependencies; if (pkg.scripts) { delete pkg.scripts.prepare; } delete pkg.files; json = "".concat(JSON.stringify(pkg, null, ' '), "\n"); return [4, common_1.fs.writeFile(target, json)]; case 3: _a.sent(); return [2, { success: true, source: source, target: target }]; case 4: error_2 = _a.sent(); return [2, { success: false, error: error_2 }]; case 5: return [2]; } }); }); } var toParent = function (path) { return path.replace(/^\.\//, '').split('/').slice(1).join('/'); }; var removeExtension = function (path) { return path.substr(0, path.length - common_1.fs.extname(path).length); };