Skip to content

Commit ca1db2d

Browse files
committed
Merge branch 'robertknight-read-files-from-tsconfig'
2 parents 0ed8347 + 8b3551f commit ca1db2d

File tree

6 files changed

+252
-160
lines changed

6 files changed

+252
-160
lines changed

README.md

+13
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

+22-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require("es6-promise").polyfill();
22

33
import fs = require("fs");
44
import commandpost = require("commandpost");
5+
import path = require("path");
56

67
import lib = require("./index");
78

@@ -39,7 +40,12 @@ var root = commandpost
3940
var editorconfig = !!opts.editorconfig;
4041
var tsfmt = !!opts.tsfmt;
4142

42-
if (args.files.length === 0 && !opts.stdin) {
43+
var files = args.files;
44+
if (files.length === 0 && fs.existsSync("tsconfig.json")) {
45+
files = readFilesFromTsconfig("tsconfig.json");
46+
}
47+
48+
if (files.length === 0 && !opts.stdin) {
4349
process.stdout.write(root.helpText() + '\n');
4450
return;
4551
}
@@ -59,7 +65,7 @@ var root = commandpost
5965
return;
6066
}
6167
lib
62-
.processStream(args.files[0] || "temp.ts", process.stdin, {
68+
.processStream(files[0] || "temp.ts", process.stdin, {
6369
replace: replace,
6470
verify: verify,
6571
tslint: tslint,
@@ -75,7 +81,7 @@ var root = commandpost
7581
.catch(errorHandler);
7682
} else {
7783
lib
78-
.processFiles(args.files, {
84+
.processFiles(files, {
7985
replace: replace,
8086
verify: verify,
8187
tslint: tslint,
@@ -126,3 +132,16 @@ function errorHandler(err: any): Promise<any> {
126132
return null;
127133
});
128134
}
135+
136+
function readFilesFromTsconfig(configPath: string) {
137+
"use strict";
138+
139+
var tsconfigDir = path.dirname(configPath);
140+
var tsconfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
141+
if (tsconfig.files) {
142+
var files: string[] = tsconfig.files;
143+
return files.map(filePath => path.resolve(tsconfigDir, filePath));
144+
} else {
145+
throw new Error(`No "files" section present in tsconfig.json`);
146+
}
147+
}

test/cli/main.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class TestCLI {
2+
method() {
3+
4+
}
5+
}

test/cli/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": false,
6+
"noImplicitAny": true,
7+
"removeComments": false,
8+
"noLib": false
9+
},
10+
"files": [
11+
"./main.ts"
12+
]
13+
}

0 commit comments

Comments
 (0)