forked from Khan/flow-to-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.ts
118 lines (107 loc) · 3.12 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Command } from "commander";
import fs from "fs";
import glob from "glob";
import prettier from "prettier";
import { convert } from "./convert";
import { detectJsx } from "./detect-jsx";
import { version } from "../package.json";
export const cli = (argv) => {
const program = new Command();
program
.version(version)
.option(
"--inline-utility-types",
"inline utility types when possible, defaults to 'false'"
)
.option("--prettier", "use prettier for formatting")
.option(
"--semi",
"add semi-colons, defaults to 'false' (depends on --prettier)"
)
.option(
"--single-quote",
"use single quotes instead of double quotes, defaults to 'false' (depends on --prettier)"
)
.option(
"--tab-width [width]",
"size of tabs (depends on --prettier)",
/2|4/,
4
)
.option(
"--trailing-comma [all|es5|none]",
"where to put trailing commas (depends on --prettier)",
/all|es5|none/,
"all"
)
.option(
"--bracket-spacing",
"put spaces between braces and contents, defaults to 'false' (depends on --prettier)"
)
.option(
"--arrow-parens [avoid|always]",
"arrow function param list parens (depends on --prettier)",
/avoid|always/,
"avoid"
)
.option("--print-width [width]", "line width (depends on --prettier)", 80)
.option("--write", "write output to disk instead of STDOUT")
.option("--delete-source", "delete the source file");
program.parse(argv);
if (program.args.length === 0) {
program.outputHelp();
process.exit(1);
}
const options = {
inlineUtilityTypes: Boolean(program.inlineUtilityTypes),
prettier: program.prettier,
prettierOptions: {
semi: Boolean(program.semi),
singleQuote: Boolean(program.singleQuote),
tabWidth: parseInt(program.tabWidth),
trailingComma: program.trailingComma,
bracketSpacing: Boolean(program.bracketSpacing),
arrowParens: program.arrowParens,
printWidth: parseInt(program.printWidth),
},
};
if (options.prettier) {
try {
const prettierConfig = prettier.resolveConfig.sync(process.cwd());
if (prettierConfig) {
// @ts-ignore
options.prettierOptions = prettierConfig;
}
} catch (e) {
console.error("error parsing prettier config file");
console.error(e);
process.exit(1);
}
}
const files = new Set<string>();
for (const arg of program.args) {
for (const file of glob.sync(arg)) {
files.add(file);
}
}
for (const file of files) {
const inFile = file;
const inCode = fs.readFileSync(inFile, "utf-8");
try {
const outCode = convert(inCode, options);
if (program.write) {
const extension = detectJsx(inCode) ? ".tsx" : ".ts";
const outFile = file.replace(/\.jsx?$/, extension);
fs.writeFileSync(outFile, outCode);
} else {
console.log(outCode);
}
if (program.deleteSource) {
fs.unlinkSync(inFile);
}
} catch (e) {
console.error(`error processing ${inFile}`);
console.error(e);
}
}
};