Skip to content

Commit

Permalink
fix: path resolve error
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Oct 30, 2018
1 parent 9edd750 commit 5b0d6dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { getTsConfigFilePath, getTsConfig, getRootNames } from './tsconfig'

// tslint:disable-next-line:no-big-function
export async function lint(project: string, detail: boolean, debug: boolean) {
const { configFilePath, basename } = getTsConfigFilePath(project)
const config = getTsConfig(configFilePath, basename)
const { configFilePath, dirname } = getTsConfigFilePath(project)
const config = getTsConfig(configFilePath, dirname)

const { options: compilerOptions, errors } = ts.convertCompilerOptionsFromJson(config.compilerOptions, basename)
const { options: compilerOptions, errors } = ts.convertCompilerOptionsFromJson(config.compilerOptions, dirname)
if (errors && errors.length > 0) {
throw errors
}

const rootNames = await getRootNames(config, basename)
const rootNames = await getRootNames(config, dirname)

const program = ts.createProgram(rootNames, compilerOptions)
const checker = program.getTypeChecker()
Expand Down
22 changes: 11 additions & 11 deletions src/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import glob from 'glob'

export function getTsConfigFilePath(project: string) {
let configFilePath: string
let basename: string
let dirname: string
const projectStats = fs.statSync(project)
if (projectStats.isDirectory()) {
configFilePath = path.resolve(project, 'tsconfig.json')
basename = project
dirname = project
} else if (projectStats.isFile()) {
configFilePath = project
basename = path.basename(project)
dirname = path.dirname(project)
} else {
throw new Error("paramter '-p' should be a file or directory.")
}
return { configFilePath, basename }
return { configFilePath, dirname }
}

type JsonConfig = {
Expand All @@ -27,33 +27,33 @@ type JsonConfig = {
files?: string[]
}

export function getTsConfig(configFilePath: string, basename: string): JsonConfig {
export function getTsConfig(configFilePath: string, dirname: string): JsonConfig {
const configResult = ts.readConfigFile(configFilePath, p => fs.readFileSync(p).toString())
if (configResult.error) {
throw configResult.error
}
const config = configResult.config as JsonConfig
if (config.extends) {
const project = path.resolve(basename, config.extends)
const { configFilePath, basename: extendsBasename } = getTsConfigFilePath(project)
const project = path.resolve(dirname, config.extends)
const { configFilePath, dirname: extendsBasename } = getTsConfigFilePath(project)
const extendsConfig = getTsConfig(configFilePath, extendsBasename)
config.compilerOptions = { ...extendsConfig.compilerOptions, ...config.compilerOptions }
}
return config
}

// tslint:disable-next-line:cognitive-complexity
export async function getRootNames(config: JsonConfig, basename: string) {
export async function getRootNames(config: JsonConfig, dirname: string) {
const include: string[] | undefined = config.include
const exclude: string[] | undefined = config.exclude || ['./node_modules/**']

if (config.files) {
return config.files.map(f => path.relative(process.cwd(), path.resolve(basename, f)))
return config.files.map(f => path.relative(process.cwd(), path.resolve(dirname, f)))
}
if (include && Array.isArray(include) && include.length > 0) {
const rules: string[] = []
for (const file of include) {
const currentPath = path.resolve(basename, file)
const currentPath = path.resolve(dirname, file)
const stats = await statAsync(currentPath)
if (stats === undefined) {
rules.push(currentPath)
Expand All @@ -65,7 +65,7 @@ export async function getRootNames(config: JsonConfig, basename: string) {
}
return globAsync(rules.length === 1 ? rules[0] : `{${rules.join(',')}}`, exclude)
}
return globAsync(`${basename}/**/*.{ts,tsx}`, exclude)
return globAsync(`${dirname}/**/*.{ts,tsx}`, exclude)
}

function statAsync(file: string) {
Expand Down

0 comments on commit 5b0d6dc

Please sign in to comment.