Skip to content

Commit 8faac63

Browse files
committed
Read list of files to format from tsconfig.json
If no files to process are specified on the command-line but a tsconfig.json file exists, then read the list of files to format from that. This provides an easy way to reformat all source files in a TypeScript project using `tsfmt -r`.
1 parent ea4401c commit 8faac63

File tree

2 files changed

+87
-55
lines changed

2 files changed

+87
-55
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ $ tsfmt
2424
2525
## Usage
2626
27+
### Format or verify specific TypeScript files
28+
2729
```bash
2830
$ cat sample.ts
2931
class Sample {hello(word="world"){return "Hello, "+word;}}
@@ -61,6 +63,17 @@ $ echo $?
6163
1
6264
```
6365
66+
### Reformat all files in a TypeScript project
67+
68+
If no files are specified on the command line but
69+
a TypeScript project file (tsconfig.json) exists,
70+
the list of files will be read from the project file.
71+
72+
```bash
73+
# reads list of files to format from tsconfig.json
74+
tsfmt -r
75+
```
76+
6477
## Note
6578
6679
now `indentSize` parameter is ignored. it is TypeScript compiler matters.

lib/cli.ts

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require("es6-promise").polyfill();
55

66
import fs = require("fs");
77
import commandpost = require("commandpost");
8+
import path = require("path");
89

910
import lib = require("./index");
1011

@@ -35,60 +36,65 @@ var root = commandpost
3536
.option("--no-tsfmt", "don't read a tsfmt.json")
3637
.option("--verbose", "makes output more verbose")
3738
.action((opts, args) => {
38-
var replace = !!opts.replace;
39-
var verify = !!opts.verify;
40-
var stdin = !!opts.stdin;
41-
var tslint = !!opts.tslint;
42-
var editorconfig = !!opts.editorconfig;
43-
var tsfmt = !!opts.tsfmt;
44-
45-
if (args.files.length === 0 && !opts.stdin) {
46-
process.stdout.write(root.helpText() + '\n');
47-
return;
48-
}
39+
var replace = !!opts.replace;
40+
var verify = !!opts.verify;
41+
var stdin = !!opts.stdin;
42+
var tslint = !!opts.tslint;
43+
var editorconfig = !!opts.editorconfig;
44+
var tsfmt = !!opts.tsfmt;
4945

50-
if (opts.verbose) {
51-
console.log("replace: " + (replace ? "ON" : "OFF"));
52-
console.log("verify: " + (verify ? "ON" : "OFF"));
53-
console.log("stdin: " + (stdin ? "ON" : "OFF"));
54-
console.log("tslint: " + (tslint ? "ON" : "OFF"));
55-
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
56-
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
57-
}
46+
var files = args.files;
47+
if (files.length === 0 && fs.existsSync("tsconfig.json")) {
48+
files = readFilesFromTsconfig("tsconfig.json");
49+
}
5850

59-
if (opts.stdin) {
60-
if (opts.replace) {
61-
errorHandler("--stdin option can not use with --replace option");
62-
return;
63-
}
64-
lib
65-
.processStream(args.files[0] || "temp.ts", process.stdin, {
66-
replace: replace,
67-
verify: verify,
68-
tslint: tslint,
69-
editorconfig: editorconfig,
70-
tsfmt: tsfmt
71-
})
72-
.then(result => {
73-
var resultMap: lib.ResultMap = {};
74-
resultMap[result.fileName] = result;
75-
return resultMap;
76-
})
77-
.then(showResultHandler)
78-
.catch(errorHandler);
79-
} else {
80-
lib
81-
.processFiles(args.files, {
82-
replace: replace,
83-
verify: verify,
84-
tslint: tslint,
85-
editorconfig: editorconfig,
86-
tsfmt: tsfmt
87-
})
88-
.then(showResultHandler)
89-
.catch(errorHandler);
51+
if (files.length === 0 && !opts.stdin) {
52+
process.stdout.write(root.helpText() + '\n');
53+
return;
54+
}
55+
56+
if (opts.verbose) {
57+
console.log("replace: " + (replace ? "ON" : "OFF"));
58+
console.log("verify: " + (verify ? "ON" : "OFF"));
59+
console.log("stdin: " + (stdin ? "ON" : "OFF"));
60+
console.log("tslint: " + (tslint ? "ON" : "OFF"));
61+
console.log("editorconfig: " + (editorconfig ? "ON" : "OFF"));
62+
console.log("tsfmt: " + (tsfmt ? "ON" : "OFF"));
63+
}
64+
65+
if (opts.stdin) {
66+
if (opts.replace) {
67+
errorHandler("--stdin option can not use with --replace option");
68+
return;
9069
}
91-
});
70+
lib
71+
.processStream(files[0] || "temp.ts", process.stdin, {
72+
replace: replace,
73+
verify: verify,
74+
tslint: tslint,
75+
editorconfig: editorconfig,
76+
tsfmt: tsfmt
77+
})
78+
.then(result => {
79+
var resultMap: lib.ResultMap = {};
80+
resultMap[result.fileName] = result;
81+
return resultMap;
82+
})
83+
.then(showResultHandler)
84+
.catch(errorHandler);
85+
} else {
86+
lib
87+
.processFiles(files, {
88+
replace: replace,
89+
verify: verify,
90+
tslint: tslint,
91+
editorconfig: editorconfig,
92+
tsfmt: tsfmt
93+
})
94+
.then(showResultHandler)
95+
.catch(errorHandler);
96+
}
97+
});
9298

9399
commandpost
94100
.exec(root, process.argv)
@@ -108,10 +114,10 @@ function showResultHandler(resultMap: lib.ResultMap): Promise<any> {
108114
Object.keys(resultMap)
109115
.map(fileName => resultMap[fileName])
110116
.forEach(result => {
111-
if (result.message) {
112-
console.log(result.message);
113-
}
114-
});
117+
if (result.message) {
118+
console.log(result.message);
119+
}
120+
});
115121
}
116122
return null;
117123
}
@@ -129,3 +135,16 @@ function errorHandler(err: any): Promise<any> {
129135
return null;
130136
});
131137
}
138+
139+
function readFilesFromTsconfig(configPath: string) {
140+
"use strict";
141+
142+
var tsconfigDir = path.dirname(configPath);
143+
var tsconfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
144+
if (tsconfig.files) {
145+
var files: string[] = tsconfig.files;
146+
return files.map(filePath => path.resolve(tsconfigDir, filePath));
147+
} else {
148+
throw new Error(`No "files" section present in tsconfig.json`);
149+
}
150+
}

0 commit comments

Comments
 (0)