diff --git a/lib/provider/tsconfigjson.ts b/lib/provider/tsconfigjson.ts index 1896412..96d6b9a 100644 --- a/lib/provider/tsconfigjson.ts +++ b/lib/provider/tsconfigjson.ts @@ -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 { @@ -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) { diff --git a/lib/utils.ts b/lib/utils.ts index e219e5b..e17ce47 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -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 {