Skip to content

Commit

Permalink
fix(tsfmt): use ts.sys.readDirectory and ts.parseJsonConfigFileConten…
Browse files Browse the repository at this point in the history
…t completely closes #77 #84
  • Loading branch information
vvakame committed Feb 28, 2017
1 parent ccc119b commit 4dd3f55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 71 deletions.
25 changes: 2 additions & 23 deletions lib/provider/tsconfigjson.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as ts from "typescript";

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

import { Options } from "../";
import { getConfigFileName, parseJSON } from "../utils";
import { getConfigFileName, readTsconfig } from "../utils";

export default function makeFormatCodeOptions(fileName: string, opts: Options, formatSettings: ts.FormatCodeSettings): ts.FormatCodeSettings {

Expand All @@ -22,27 +21,7 @@ export default function makeFormatCodeOptions(fileName: string, opts: Options, f
console.log(`read ${configFileName} for ${fileName}`);
}

// for `extends` support. It supported from TypeScript 2.1.1.
// `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support.
const host: ts.ParseConfigHost & { readFile(path: string): string; } = {
useCaseSensitiveFileNames: true,
readDirectory: (rootDir, _extensions, excludes, _includes) => {
// _extensions -> [ '.ts', '.tsx', '.d.ts' ]
// _includes -> [ '**/*' ]

const files = fs.readdirSync(rootDir);
return files
.filter(file => excludes.every(exclude => file !== exclude));
},
fileExists: path => fs.existsSync(path),
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
};
let rootConfig = parseJSON(fs.readFileSync(configFileName, "utf-8"));
let parsed = ts.parseJsonConfigFileContent(rootConfig, host, baseDir);
if (parsed.errors && parsed.errors.length !== 0) {
throw new Error(parsed.errors.map(e => e.messageText).join("\n"));
}

let parsed = readTsconfig(configFileName);
if (parsed.options.newLine === ts.NewLineKind.CarriageReturnLineFeed) {
formatSettings.newLineCharacter = "\r\n";
} else if (parsed.options.newLine === ts.NewLineKind.LineFeed) {
Expand Down
64 changes: 16 additions & 48 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,25 @@ export function getConfigFileName(baseDir: string, configFileName: string): stri
}

export function readFilesFromTsconfig(configPath: string): string[] {
return readTsconfig(configPath).fileNames;
}

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

let tsconfigDir = path.dirname(configPath);
let tsconfig: TsConfigJSON = parseJSON(fs.readFileSync(configPath, "utf-8"));
if (tsconfig.files && (tsconfig.include || tsconfig.exclude)) {
return tsconfig.files.concat(tsMatchFiles(tsconfig.exclude || [], tsconfig.include || []));
} else if (tsconfig.files) {
let files: string[] = tsconfig.files;
return files.map(filePath => path.resolve(tsconfigDir, filePath));
} else if (tsconfig.include || tsconfig.exclude) {
return tsMatchFiles(tsconfig.exclude || [], tsconfig.include || []);
} else {
return tsMatchFiles([], []);
export function readTsconfig(configPath: string): ts.ParsedCommandLine {
// for `extends` support. It supported from TypeScript 2.1.1.
// `& { readFile(path: string): string; }` is backword compat for TypeScript compiler 2.0.3 support.
const host: ts.ParseConfigHost & { readFile(path: string): string; } = {
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
readDirectory: ts.sys.readDirectory,
fileExists: path => fs.existsSync(path),
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
};
let rootConfig = parseJSON(fs.readFileSync(configPath, "utf-8"));
let parsed = ts.parseJsonConfigFileContent(rootConfig, host, path.dirname(configPath));
if (parsed.errors && parsed.errors.length !== 0) {
throw new Error(parsed.errors.map(e => e.messageText).join("\n"));
}

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: [] };
});
}
return parsed;
}

export function parseJSON(jsonText: string): any {
Expand Down

0 comments on commit 4dd3f55

Please sign in to comment.