-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcli.ts
131 lines (116 loc) · 4.04 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
119
120
121
122
123
124
125
126
127
128
129
130
131
/// <reference path="../typings/node/node.d.ts" />
/// <reference path="../node_modules/commandpost/commandpost.d.ts" />
require("es6-promise").polyfill();
import fs = require("fs");
import commandpost = require("commandpost");
import lib = require("./index");
var packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toString());
interface RootOptions {
replace: boolean;
verify: boolean;
stdin: boolean;
tslint: boolean;
editorconfig: boolean;
tsfmt: boolean;
verbose: boolean;
}
interface RootArguments {
files: string[];
}
var root = commandpost
.create<RootOptions, RootArguments>("tsfmt [files...]")
.version(packageJson.version, "-v, --version")
.option("-r, --replace", "replace .ts file")
.option("--verify", "checking file format")
.option("--stdin", "get formatting content from stdin")
.option("--no-tslint", "don't read a tslint.json")
.option("--no-editorconfig", "don't read a .editorconfig")
.option("--no-tsfmt", "don't read a tsfmt.json")
.option("--verbose", "makes output more verbose")
.action((opts, args) => {
var replace = !!opts.replace;
var verify = !!opts.verify;
var stdin = !!opts.stdin;
var tslint = !!opts.tslint;
var editorconfig = !!opts.editorconfig;
var tsfmt = !!opts.tsfmt;
if (args.files.length === 0 && !opts.stdin) {
process.stdout.write(root.helpText() + '\n');
return;
}
if (opts.verbose) {
console.log("replace: " + (replace ? "ON" : "OFF"));
console.log("verify: " + (verify ? "ON" : "OFF"));
console.log("stdin: " + (stdin ? "ON" : "OFF"));
console.log("tslint: " + (tslint ? "ON" : "OFF"));
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
}
if (opts.stdin) {
if (opts.replace) {
errorHandler("--stdin option can not use with --replace option");
return;
}
lib
.processStream(args.files[0] || "temp.ts", process.stdin, {
replace: replace,
verify: verify,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt
})
.then(result => {
var resultMap: lib.ResultMap = {};
resultMap[result.fileName] = result;
return resultMap;
})
.then(showResultHandler)
.catch(errorHandler);
} else {
lib
.processFiles(args.files, {
replace: replace,
verify: verify,
tslint: tslint,
editorconfig: editorconfig,
tsfmt: tsfmt
})
.then(showResultHandler)
.catch(errorHandler);
}
});
commandpost
.exec(root, process.argv)
.catch(errorHandler);
function showResultHandler(resultMap: lib.ResultMap): Promise<any> {
"use strict";
var hasError = Object.keys(resultMap).filter(fileName => resultMap[fileName].error).length !== 0;
if (hasError) {
Object.keys(resultMap)
.map(fileName => resultMap[fileName])
.filter(result => result.error)
.forEach(result => console.error(result.message));
process.exit(1);
} else {
Object.keys(resultMap)
.map(fileName => resultMap[fileName])
.forEach(result => {
if (result.message) {
console.log(result.message);
}
});
}
return null;
}
function errorHandler(err: any): Promise<any> {
"use strict";
if (err instanceof Error) {
console.error(err.stack);
} else {
console.error(err);
}
return Promise.resolve(null).then(() => {
process.exit(1);
return null;
});
}