UNPKG

8.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var yargs = require("yargs");
5var common_1 = require("../common");
6var cmds = require("./cmds");
7process.on('unhandledRejection', function (err) {
8 throw err;
9});
10var CMD = {
11 BUILD: 'build',
12 BUILD_B: 'b',
13 LINT: 'lint',
14 LINT_L: 'l',
15 TEST: 'test',
16 TEST_T: 't',
17 PREPARE: 'prepare',
18 PREPARE_P: 'p',
19 PUBLISH: 'publish',
20 PUBLISH_PUB: 'pub',
21 CHMOD: 'chmod',
22};
23var CMDS = Object.keys(CMD)
24 .map(function (key) { return CMD[key]; })
25 .map(function (cmd) { return cmd.split(' ')[0]; });
26var DESCRIPTION = {
27 SILENT: 'Suppress console output',
28};
29var program = yargs
30 .scriptName('')
31 .usage('Usage: ts <command> [options]')
32 .command([CMD.BUILD, CMD.BUILD_B], 'Transpile typescript', function (e) {
33 return e
34 .option('silent', {
35 alias: 's',
36 describe: DESCRIPTION.SILENT,
37 boolean: true,
38 })
39 .option('watch', {
40 alias: 'w',
41 describe: 'Watch for changes',
42 boolean: true,
43 })
44 .option('dir', {
45 describe: 'The directory of the module',
46 string: true,
47 })
48 .option('outDir', {
49 describe: 'Redirect output structure to the directory',
50 string: true,
51 })
52 .option('tsconfig', {
53 describe: 'Name of the `tsconfig` file',
54 string: true,
55 })
56 .option('no-esm', {
57 describe: 'Do not include ESModule output (.jsm)',
58 boolean: true,
59 });
60}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
61 var silent, watch, dir, outDir, esm, tsconfig, formats, res;
62 return tslib_1.__generator(this, function (_a) {
63 switch (_a.label) {
64 case 0:
65 silent = e.silent, watch = e.watch, dir = e.dir, outDir = e.outDir, esm = e.esm, tsconfig = e.tsconfig;
66 if (!watch) return [3, 2];
67 return [4, cmds.build({ silent: silent, tsconfig: tsconfig, dir: dir, outDir: outDir, watch: true, as: 'COMMON_JS' })];
68 case 1:
69 _a.sent();
70 return [3, 4];
71 case 2:
72 formats = [];
73 formats = esm !== false ? tslib_1.__spreadArray(tslib_1.__spreadArray([], formats), ['ES_MODULE']) : formats;
74 formats = tslib_1.__spreadArray(tslib_1.__spreadArray([], formats), ['COMMON_JS']);
75 return [4, cmds.buildAs(formats, {
76 silent: silent,
77 watch: watch,
78 tsconfig: tsconfig,
79 dir: dir,
80 outDir: outDir,
81 })];
82 case 3:
83 res = _a.sent();
84 if (res.error) {
85 fail(1, res.error);
86 }
87 _a.label = 4;
88 case 4: return [2];
89 }
90 });
91}); })
92 .command([CMD.LINT, CMD.LINT_L], 'Run linter', function (e) {
93 return e
94 .option('silent', {
95 alias: 's',
96 describe: DESCRIPTION.SILENT,
97 boolean: true,
98 })
99 .option('dir', {
100 describe: 'The directory of the module',
101 string: true,
102 });
103}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
104 var silent, dir, res;
105 return tslib_1.__generator(this, function (_a) {
106 switch (_a.label) {
107 case 0:
108 silent = e.silent, dir = e.dir;
109 return [4, cmds.lint({ silent: silent, dir: dir })];
110 case 1:
111 res = _a.sent();
112 if (res.error) {
113 fail(1, res.error);
114 }
115 return [2];
116 }
117 });
118}); })
119 .command([CMD.TEST, CMD.TEST_T], "Run tests", function (e) {
120 return e
121 .option('silent', {
122 alias: 's',
123 describe: DESCRIPTION.SILENT,
124 boolean: true,
125 })
126 .option('dir', {
127 describe: 'The directory of the module',
128 string: true,
129 })
130 .option('watch', {
131 alias: 'w',
132 describe: 'Watch for changes',
133 boolean: true,
134 })
135 .option('suffix', {
136 describe: 'The test file suffix to match.',
137 default: 'test,TEST',
138 string: true,
139 });
140}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
141 var silent, watch, dir, suffix, res;
142 return tslib_1.__generator(this, function (_a) {
143 switch (_a.label) {
144 case 0:
145 silent = e.silent, watch = e.watch, dir = e.dir, suffix = e.suffix;
146 return [4, cmds.test({ silent: silent, watch: watch, dir: dir, suffix: suffix })];
147 case 1:
148 res = _a.sent();
149 if (res.error) {
150 fail(1, res.error);
151 }
152 return [2];
153 }
154 });
155}); })
156 .command([CMD.PREPARE, CMD.PREPARE_P], "Prepare for publish", function (e) {
157 return e
158 .option('silent', {
159 alias: 's',
160 describe: DESCRIPTION.SILENT,
161 boolean: true,
162 })
163 .option('dir', {
164 describe: 'The directory of the module',
165 string: true,
166 });
167}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
168 var silent, dir, res;
169 return tslib_1.__generator(this, function (_a) {
170 switch (_a.label) {
171 case 0:
172 silent = e.silent, dir = e.dir;
173 return [4, cmds.prepare({ silent: silent, dir: dir })];
174 case 1:
175 res = _a.sent();
176 if (res.error) {
177 fail(1, res.error);
178 }
179 return [2];
180 }
181 });
182}); })
183 .command([CMD.PUBLISH, CMD.PUBLISH_PUB], "Publish to NPM", function (e) {
184 return e
185 .option('silent', {
186 alias: 's',
187 describe: DESCRIPTION.SILENT,
188 boolean: true,
189 })
190 .option('dir', {
191 describe: 'The directory of the module',
192 string: true,
193 })
194 .option('outDir', {
195 describe: 'The dir typescript has been transpiled to',
196 string: true,
197 });
198}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
199 var silent, dir, outDir, res;
200 return tslib_1.__generator(this, function (_a) {
201 switch (_a.label) {
202 case 0:
203 silent = e.silent, dir = e.dir, outDir = e.outDir;
204 return [4, cmds.publish({ silent: silent, dir: dir, outDir: outDir })];
205 case 1:
206 res = _a.sent();
207 if (res.error) {
208 fail(1, res.error);
209 }
210 return [2];
211 }
212 });
213}); })
214 .command([CMD.CHMOD], "Change permissions on [node_modules/.bin] files.", function (e) {
215 return e
216 .positional('permissions', {
217 type: 'string',
218 default: '777',
219 describe: 'The permissions to apply',
220 })
221 .option('silent', {
222 alias: 's',
223 describe: DESCRIPTION.SILENT,
224 boolean: true,
225 });
226}, function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
227 var silent, _a, permissions, res;
228 return tslib_1.__generator(this, function (_b) {
229 switch (_b.label) {
230 case 0:
231 silent = e.silent, _a = e.permissions, permissions = _a === void 0 ? '777' : _a;
232 return [4, cmds.chmod({ silent: silent, permissions: permissions })];
233 case 1:
234 res = _b.sent();
235 if (res.error) {
236 fail(1, res.error);
237 }
238 return [2];
239 }
240 });
241}); })
242 .help('h')
243 .alias('h', 'help')
244 .alias('v', 'version')
245 .recommendCommands();
246var command = program.argv._[0];
247if (!CMDS.includes(command)) {
248 program.showHelp('log');
249 common_1.log.info();
250 process.exit(0);
251}
252function fail(code, error) {
253 var message = typeof error === 'string' ? error : error.message;
254 common_1.log.info("\n\uD83D\uDE1E " + message);
255 process.exit(code);
256}