Skip to content

Commit d8e71f5

Browse files
committed
feat(tsfmt): support include, exclude properties @tsconfig.json when using --replace options #48
1 parent cbb561c commit d8e71f5

File tree

11 files changed

+107
-24
lines changed

11 files changed

+107
-24
lines changed

lib/cli.ts

+1-19
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ try {
1313

1414
import * as fs from "fs";
1515
import * as commandpost from "commandpost";
16-
import * as path from "path";
17-
18-
import * as expand from "glob-expand";
1916

2017
import * as lib from "./";
21-
import { getConfigFileName } from "./utils";
18+
import { getConfigFileName, readFilesFromTsconfig } from "./utils";
2219

2320
let packageJson = JSON.parse(fs.readFileSync(__dirname + "/../package.json").toString());
2421

@@ -170,18 +167,3 @@ function errorHandler(err: any): Promise<any> {
170167
return null;
171168
});
172169
}
173-
174-
function readFilesFromTsconfig(configPath: string) {
175-
"use strict";
176-
177-
let tsconfigDir = path.dirname(configPath);
178-
let tsconfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
179-
if (tsconfig.files) {
180-
let files: string[] = tsconfig.files;
181-
return files.map(filePath => path.resolve(tsconfigDir, filePath));
182-
} else if (tsconfig.filesGlob) {
183-
return expand({ filter: "isFile", cwd: tsconfigDir }, tsconfig.filesGlob);
184-
} else {
185-
throw new Error(`No "files" or "filesGlob" section present in tsconfig.json`);
186-
}
187-
}

lib/utils.ts

+58
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import * as ts from "typescript";
44

5+
import * as expand from "glob-expand";
6+
57
import * as fs from "fs";
68
import * as path from "path";
79

@@ -41,3 +43,59 @@ export function getConfigFileName(baseDir: string, configFileName: string): stri
4143

4244
return getConfigFileName(path.resolve(baseDir, "../"), configFileName);
4345
}
46+
47+
export function readFilesFromTsconfig(configPath: string): string[] {
48+
"use strict";
49+
50+
interface TsConfigJSON {
51+
files?: string[];
52+
filesGlob?: string[];
53+
include?: string[];
54+
exclude?: string[];
55+
}
56+
57+
let tsconfigDir = path.dirname(configPath);
58+
let tsconfig: TsConfigJSON = JSON.parse(fs.readFileSync(configPath, "utf-8"));
59+
if (tsconfig.files) {
60+
let files: string[] = tsconfig.files;
61+
return files.map(filePath => path.resolve(tsconfigDir, filePath));
62+
} else if (tsconfig.filesGlob) {
63+
return expand({ filter: "isFile", cwd: tsconfigDir }, tsconfig.filesGlob);
64+
} else if (tsconfig.include || tsconfig.exclude) {
65+
return tsMatchFiles(tsconfig.exclude || [], tsconfig.include || []);
66+
} else {
67+
throw new Error(`No "files" or "filesGlob" section present in tsconfig.json`);
68+
}
69+
70+
function tsMatchFiles(excludes: string[], includes: string[]) {
71+
interface TsMatchFiles {
72+
(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string, getFileSystemEntries: (path: string) => TsFileSystemEntries): string[];
73+
}
74+
interface TsFileSystemEntries {
75+
files: string[];
76+
directories: string[];
77+
}
78+
79+
let f: TsMatchFiles = (ts as any).matchFiles;
80+
if (!f) {
81+
throw new Error("ts.matchFiles is not exists. typescript@^2.0.0 required");
82+
}
83+
return f(tsconfigDir, [".ts", ".tsx"], excludes, includes, true, tsconfigDir, dirPath => {
84+
let stat = fs.statSync(dirPath);
85+
if (stat.isDirectory()) {
86+
let result: TsFileSystemEntries = { files: [], directories: [] };
87+
let dirEntries = fs.readdirSync(dirPath);
88+
dirEntries.forEach(entry => {
89+
let stat = fs.statSync(path.join(dirPath, entry));
90+
if (stat.isDirectory()) {
91+
result.directories.push(entry);
92+
} else if (stat.isFile()) {
93+
result.files.push(entry);
94+
}
95+
});
96+
return result;
97+
}
98+
return { files: [], directories: [] };
99+
});
100+
}
101+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@
5555
"mocha": "^2.5.3",
5656
"power-assert": "^1.2.0",
5757
"tslint": "^3.13.0",
58-
"typescript": "~2.0.0"
58+
"typescript": "^2.0.0"
5959
}
6060
}
File renamed without changes.
File renamed without changes.

test/cli/includeExclude/exclude.ts

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

test/cli/includeExclude/include.ts

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

test/cli/includeExclude/tsconfig.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": false,
6+
"noImplicitAny": true,
7+
"removeComments": false,
8+
"noLib": false
9+
},
10+
"include": [
11+
"./*.ts"
12+
],
13+
"exclude": [
14+
"./exclude.ts",
15+
"./*.d.ts"
16+
]
17+
}
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"compilerOptions": {
33
"newLine": "CRLF"
4-
}
4+
},
5+
"files": [
6+
"main.ts"
7+
]
58
}
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"compilerOptions": {
33
"newLine": "LF"
4-
}
4+
},
5+
"files": [
6+
"main.ts"
7+
]
58
}

test/indexSpec.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,27 @@ describe("tsfmt test", () => {
202202
});
203203

204204
describe("CLI test", () => {
205-
it("should reformat files specified in tsconfig.json", () => {
206-
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve('./test/cli') }).then(result => {
205+
it("should reformat files specified at files in tsconfig.json", () => {
206+
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve('./test/cli/files') }).then(result => {
207207
assert.equal(result.status, 0);
208208
assert.equal(result.stdout.trim(), `
209209
class TestCLI {
210210
method() {
211211
212212
}
213213
}
214+
`.trim().replace(/\n/g, "\r\n"));
215+
});
216+
});
217+
218+
it("should reformat files specified at include, exclude in tsconfig.json", () => {
219+
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve('./test/cli/includeExclude') }).then(result => {
220+
assert.equal(result.status, 0);
221+
assert.equal(result.stdout.trim(), `
222+
export class TestCLI {
223+
method() {
224+
}
225+
}
214226
`.trim().replace(/\n/g, "\r\n"));
215227
});
216228
});

0 commit comments

Comments
 (0)