-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.ts
99 lines (84 loc) · 2.51 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
/// <reference types="node" />
import ts from 'typescript';
import path from 'path';
import parser, { Arguments } from 'yargs-parser';
import { build } from './build';
import { watch } from './watch';
import { help } from './help';
import { jest } from './jest';
interface ArgvArguments extends Arguments {
disableBabel?: boolean;
esm?: boolean | string;
cjs?: string;
entry?: string;
fileNames?: string | string[];
}
const argv: ArgvArguments = parser(process.argv.slice(2), {
alias: {
entry: ['e'],
output: ['o'],
help: ['h'],
version: ['v'],
'file-names': ['f'],
},
});
(() => {
const version = require('../package.json').version;
if (argv.v) {
console.log();
console.log(` Version \x1b[32;1m ${version}\x1b[0m`);
console.log();
return;
}
if (argv.h) {
return help();
}
try {
argv.entry = path.resolve(process.cwd(), argv.entry || 'src/index.tsx');
if (ts.sys.fileExists(argv.entry.replace(/\.tsx$/, '.ts'))) {
argv.entry = argv.entry.replace(/\.tsx$/, '.ts');
}
if (typeof argv.fileNames === 'string') {
argv.fileNames = [argv.fileNames];
}
if (argv.fileNames && Array.isArray(argv.fileNames)) {
argv.fileNames = argv.fileNames.map((filename: string) => path.resolve(process.cwd(), filename));
argv.fileNames = [argv.entry, ...argv.fileNames];
argv.fileNames = Array.from(new Set(argv.fileNames));
}
if (!argv.fileNames) {
argv.fileNames = [argv.entry];
}
if (argv.disableBabel) {
argv.esm = false;
}
const configPath = ts.findConfigFile(path.dirname(argv.entry), ts.sys.fileExists);
let tsConf = { compilerOptions: {} as ts.CompilerOptions };
if (!configPath) {
tsConf.compilerOptions.noEmit = true;
} else {
const data = ts.readConfigFile(configPath, ts.sys.readFile);
const configParseResult = ts.parseJsonConfigFileContent(data.config, ts.sys, path.dirname(configPath));
tsConf.compilerOptions = configParseResult.options;
}
if (argv.disableBabel === undefined) {
argv.disableBabel = false;
}
if (!tsConf.compilerOptions.outDir) {
tsConf.compilerOptions.outDir = 'lib';
}
if (argv._[0] === 'build') {
return build(argv, { ...tsConf.compilerOptions });
}
if (argv._[0] === 'watch') {
return watch(argv, { ...tsConf.compilerOptions });
}
if (argv._[0] === 'test') {
return jest(argv);
}
help();
} catch (error) {
console.log('ERROR:', error);
}
})();