Skip to content

Commit

Permalink
feat(tsfmt): support include, exclude properties @tsconfig.json when …
Browse files Browse the repository at this point in the history
…using --replace options #48
  • Loading branch information
vvakame committed Jul 25, 2016
1 parent cbb561c commit d8e71f5
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 24 deletions.
20 changes: 1 addition & 19 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ try {

import * as fs from "fs";
import * as commandpost from "commandpost";
import * as path from "path";

import * as expand from "glob-expand";

import * as lib from "./";
import { getConfigFileName } from "./utils";
import { getConfigFileName, readFilesFromTsconfig } from "./utils";

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

Expand Down Expand Up @@ -170,18 +167,3 @@ function errorHandler(err: any): Promise<any> {
return null;
});
}

function readFilesFromTsconfig(configPath: string) {
"use strict";

let tsconfigDir = path.dirname(configPath);
let tsconfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
if (tsconfig.files) {
let files: string[] = tsconfig.files;
return files.map(filePath => path.resolve(tsconfigDir, filePath));
} else if (tsconfig.filesGlob) {
return expand({ filter: "isFile", cwd: tsconfigDir }, tsconfig.filesGlob);
} else {
throw new Error(`No "files" or "filesGlob" section present in tsconfig.json`);
}
}
58 changes: 58 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import * as ts from "typescript";

import * as expand from "glob-expand";

import * as fs from "fs";
import * as path from "path";

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

return getConfigFileName(path.resolve(baseDir, "../"), configFileName);
}

export function readFilesFromTsconfig(configPath: string): string[] {
"use strict";

interface TsConfigJSON {
files?: string[];
filesGlob?: string[];
include?: string[];
exclude?: string[];
}

let tsconfigDir = path.dirname(configPath);
let tsconfig: TsConfigJSON = JSON.parse(fs.readFileSync(configPath, "utf-8"));
if (tsconfig.files) {
let files: string[] = tsconfig.files;
return files.map(filePath => path.resolve(tsconfigDir, filePath));
} else if (tsconfig.filesGlob) {
return expand({ filter: "isFile", cwd: tsconfigDir }, tsconfig.filesGlob);
} else if (tsconfig.include || tsconfig.exclude) {
return tsMatchFiles(tsconfig.exclude || [], tsconfig.include || []);
} else {
throw new Error(`No "files" or "filesGlob" section present in tsconfig.json`);
}

function tsMatchFiles(excludes: string[], includes: string[]) {
interface TsMatchFiles {
(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string, getFileSystemEntries: (path: string) => TsFileSystemEntries): string[];
}
interface TsFileSystemEntries {
files: string[];
directories: string[];
}

let f: TsMatchFiles = (ts as any).matchFiles;
if (!f) {
throw new Error("ts.matchFiles is not exists. typescript@^2.0.0 required");
}
return f(tsconfigDir, [".ts", ".tsx"], excludes, includes, true, tsconfigDir, dirPath => {
let stat = fs.statSync(dirPath);
if (stat.isDirectory()) {
let result: TsFileSystemEntries = { files: [], directories: [] };
let dirEntries = fs.readdirSync(dirPath);
dirEntries.forEach(entry => {
let stat = fs.statSync(path.join(dirPath, entry));
if (stat.isDirectory()) {
result.directories.push(entry);
} else if (stat.isFile()) {
result.files.push(entry);
}
});
return result;
}
return { files: [], directories: [] };
});
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"mocha": "^2.5.3",
"power-assert": "^1.2.0",
"tslint": "^3.13.0",
"typescript": "~2.0.0"
"typescript": "^2.0.0"
}
}
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions test/cli/includeExclude/exclude.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class TestCLI {
method() {
}
}
4 changes: 4 additions & 0 deletions test/cli/includeExclude/include.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class TestCLI {
method() {
}
}
17 changes: 17 additions & 0 deletions test/cli/includeExclude/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": false,
"noImplicitAny": true,
"removeComments": false,
"noLib": false
},
"include": [
"./*.ts"
],
"exclude": [
"./exclude.ts",
"./*.d.ts"
]
}
5 changes: 4 additions & 1 deletion test/fixture/tsconfig/crlf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"compilerOptions": {
"newLine": "CRLF"
}
},
"files": [
"main.ts"
]
}
5 changes: 4 additions & 1 deletion test/fixture/tsconfig/lf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"compilerOptions": {
"newLine": "LF"
}
},
"files": [
"main.ts"
]
}
16 changes: 14 additions & 2 deletions test/indexSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,27 @@ describe("tsfmt test", () => {
});

describe("CLI test", () => {
it("should reformat files specified in tsconfig.json", () => {
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve('./test/cli') }).then(result => {
it("should reformat files specified at files in tsconfig.json", () => {
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve('./test/cli/files') }).then(result => {
assert.equal(result.status, 0);
assert.equal(result.stdout.trim(), `
class TestCLI {
method() {
}
}
`.trim().replace(/\n/g, "\r\n"));
});
});

it("should reformat files specified at include, exclude in tsconfig.json", () => {
return exec(path.resolve("./bin/tsfmt"), [], { cwd: path.resolve('./test/cli/includeExclude') }).then(result => {
assert.equal(result.status, 0);
assert.equal(result.stdout.trim(), `
export class TestCLI {
method() {
}
}
`.trim().replace(/\n/g, "\r\n"));
});
});
Expand Down

0 comments on commit d8e71f5

Please sign in to comment.