Skip to content

Commit de8e10c

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 de8e10c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
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: 22 additions & 3 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

@@ -42,7 +43,12 @@ var root = commandpost
4243
var editorconfig = !!opts.editorconfig;
4344
var tsfmt = !!opts.tsfmt;
4445

45-
if (args.files.length === 0 && !opts.stdin) {
46+
var files = args.files;
47+
if (files.length === 0 && fs.existsSync("tsconfig.json")) {
48+
files = readFilesFromTsconfig("tsconfig.json");
49+
}
50+
51+
if (files.length === 0 && !opts.stdin) {
4652
process.stdout.write(root.helpText() + '\n');
4753
return;
4854
}
@@ -62,7 +68,7 @@ var root = commandpost
6268
return;
6369
}
6470
lib
65-
.processStream(args.files[0] || "temp.ts", process.stdin, {
71+
.processStream(files[0] || "temp.ts", process.stdin, {
6672
replace: replace,
6773
verify: verify,
6874
tslint: tslint,
@@ -78,7 +84,7 @@ var root = commandpost
7884
.catch(errorHandler);
7985
} else {
8086
lib
81-
.processFiles(args.files, {
87+
.processFiles(files, {
8288
replace: replace,
8389
verify: verify,
8490
tslint: tslint,
@@ -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)