1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.publish = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var common_1 = require("../common");
|
6 | function publish(args) {
|
7 | if (args === void 0) { args = {}; }
|
8 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
9 | var dir, _a, tsconfig, outDir, modules, tmp, error_1;
|
10 | return tslib_1.__generator(this, function (_b) {
|
11 | switch (_b.label) {
|
12 | case 0:
|
13 | _a = args.dir;
|
14 | if (_a) return [3, 2];
|
15 | return [4, common_1.paths.closestParentOf('tsconfig.json')];
|
16 | case 1:
|
17 | _a = (_b.sent());
|
18 | _b.label = 2;
|
19 | case 2:
|
20 | dir = _a;
|
21 | if (!dir) {
|
22 | return [2, common_1.result.fail("A 'tsconfig.json' file could not be found.")];
|
23 | }
|
24 | return [4, common_1.paths.tsconfig(dir)];
|
25 | case 3:
|
26 | tsconfig = _b.sent();
|
27 | if (!tsconfig.success) {
|
28 | return [2, common_1.result.fail("Failed to load the 'tsconig.json' file.")];
|
29 | }
|
30 | outDir = args.outDir || tsconfig.outDir;
|
31 | if (!outDir) {
|
32 | return [2, common_1.result.fail("The 'tsconfig.json' does not contain an 'outDir'.")];
|
33 | }
|
34 | outDir = common_1.fs.resolve(outDir);
|
35 | modules = common_1.fs.join(dir, 'node_modules');
|
36 | console.group('\n\n🐷 TODO publish\n');
|
37 | console.log('modules', modules);
|
38 | console.log('tsconfig', tsconfig);
|
39 | console.log('outDir', outDir);
|
40 | console.log('\n\n');
|
41 | console.groupEnd();
|
42 | _b.label = 4;
|
43 | case 4:
|
44 | _b.trys.push([4, 7, , 8]);
|
45 | tmp = common_1.fs.resolve('.publish');
|
46 | return [4, copyPackageJson({ rootDir: dir, target: tmp })];
|
47 | case 5:
|
48 | _b.sent();
|
49 | return [4, common_1.fs.copy(outDir, tmp)];
|
50 | case 6:
|
51 | _b.sent();
|
52 | return [2, common_1.result.success()];
|
53 | case 7:
|
54 | error_1 = _b.sent();
|
55 | return [2, common_1.result.fail(error_1)];
|
56 | case 8: return [2];
|
57 | }
|
58 | });
|
59 | });
|
60 | }
|
61 | exports.publish = publish;
|
62 | function copyPackageJson(args) {
|
63 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
64 | var packagePath, source, target, pkg, json, error_2;
|
65 | return tslib_1.__generator(this, function (_a) {
|
66 | switch (_a.label) {
|
67 | case 0:
|
68 | _a.trys.push([0, 4, , 5]);
|
69 | return [4, common_1.fs.ensureDir(args.target)];
|
70 | case 1:
|
71 | _a.sent();
|
72 | packagePath = function (dir) { return common_1.fs.resolve(common_1.fs.join(dir, 'package.json')); };
|
73 | source = packagePath(args.rootDir);
|
74 | target = packagePath(args.target);
|
75 | return [4, common_1.fs.file.loadAndParse(source)];
|
76 | case 2:
|
77 | pkg = _a.sent();
|
78 | pkg.types = pkg.types ? toParent(pkg.types) : pkg.types;
|
79 | pkg.main = pkg.main ? toParent(pkg.main) : pkg.main;
|
80 | pkg.main = pkg.main ? removeExtension(pkg.main) : pkg.main;
|
81 | delete pkg.devDependencies;
|
82 | if (pkg.scripts) {
|
83 | delete pkg.scripts.prepare;
|
84 | }
|
85 | delete pkg.files;
|
86 | json = JSON.stringify(pkg, null, ' ') + "\n";
|
87 | return [4, common_1.fs.writeFile(target, json)];
|
88 | case 3:
|
89 | _a.sent();
|
90 | return [2, { success: true, source: source, target: target }];
|
91 | case 4:
|
92 | error_2 = _a.sent();
|
93 | return [2, { success: false, error: error_2 }];
|
94 | case 5: return [2];
|
95 | }
|
96 | });
|
97 | });
|
98 | }
|
99 | var toParent = function (path) { return path.replace(/^\.\//, '').split('/').slice(1).join('/'); };
|
100 | var removeExtension = function (path) { return path.substr(0, path.length - common_1.fs.extname(path).length); };
|