Skip to content

Commit

Permalink
Split to findConfig and readConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Feb 12, 2017
1 parent 2eda001 commit 6644f0d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 52 deletions.
5 changes: 3 additions & 2 deletions src/bin/vuetype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert = require('assert')
import path = require('path')
import program = require('commander')
import { globSync, deepestSharedRoot } from '../lib/file-util'
import { findAndReadConfig } from '../lib/config'
import { findConfig, readConfig } from '../lib/config'
import { generate } from '../lib/generate'
import { watch } from '../lib/watch'

Expand All @@ -21,7 +21,8 @@ if (program.args.length === 0) {
program.help()
} else {
const root = path.resolve(deepestSharedRoot(program.args))
const config = findAndReadConfig(root)
const configPath = findConfig(root)
const config = configPath && readConfig(configPath)
const options = config ? config.options : {}

if (program['watch']) {
Expand Down
40 changes: 25 additions & 15 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,44 @@ import path = require('path')
import ts = require('typescript')
import { exists } from './file-util'

export function findAndReadConfig (
export function readConfig (
configPath: string,
_parseConfigHost: ts.ParseConfigHost = ts.sys // for test
): ts.ParsedCommandLine | undefined {
const result = ts.readConfigFile(configPath, _parseConfigHost.readFile)

if (result.error) {
return undefined
}

return ts.parseJsonConfigFileContent(
result.config,
_parseConfigHost,
path.dirname(configPath),
undefined,
configPath
)
}

export function findConfig (
baseDir: string,
_parseConfigHost: ts.ParseConfigHost = ts.sys, // for test
_exists: (filePath: string) => boolean = exists // for test
): ts.ParsedCommandLine | undefined {
): string | undefined {
const configFileName = 'tsconfig.json'

function loop (dir: string): ts.ParsedCommandLine | undefined {
function loop (dir: string): string | undefined {
const parentPath = path.dirname(dir)
// It is root directory if parent and current dirname are the same
if (dir === parentPath) {
return undefined
}

const configPath = path.join(dir, configFileName)
if (!_exists(configPath)) {
return loop(parentPath)
if (_exists(configPath)) {
return configPath
}

const result = ts.readConfigFile(configPath, _parseConfigHost.readFile)

return ts.parseJsonConfigFileContent(
result.config,
_parseConfigHost,
dir,
undefined,
configPath
)
return loop(parentPath)
}
return loop(baseDir)
}
70 changes: 35 additions & 35 deletions test/specs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path = require('path')
import glob = require('glob')
import MemoryFs = require('memory-fs')
import ts = require('typescript')
import { findAndReadConfig } from '../../src/lib/config'
import { findConfig, readConfig } from '../../src/lib/config'

const fs = new MemoryFs()
const ScriptTarget = ts.ScriptTarget
Expand All @@ -30,53 +30,53 @@ const exists = (fileName: string) => fs.existsSync(fileName)
describe('tsconfig detection', () => {
beforeEach(clear)

it('should read tsconfig.json on a directory', () => {
const config = {
compilerOptions: {
experimentalDecorators: true
}
}
mock('/path/to/tsconfig.json', config)

const data = findAndReadConfig('/path/to', host, exists)
assert.ok(data)
it('should find tsconfig.json on a directory', () => {
mock('/path/to/tsconfig.json', {})

const options = data!.options
assert(options.experimentalDecorators === true)
const pathname = findConfig('/path/to', exists)
assert(pathname === '/path/to/tsconfig.json')
})

it('should read tsconfig.json on the closest ancestor', () => {
const configA = {
compilerOptions: { target: 'es5' }
}
const configB = {
compilerOptions: { target: 'es2015' }
}
const configC = {
compilerOptions: { target: 'es2016' }
}
mock('/path/to/tsconfig.json', configA)
mock('/path/to/b/tsconfig.json', configB)
mock('/path/to/c/tsconfig.json', configC)
it('should find tsconfig.json on the closest ancestor', () => {
mock('/path/to/tsconfig.json', {})
mock('/path/to/b/tsconfig.json', {})
mock('/path/to/c/tsconfig.json', {})
mock('/path/to/b/src/test.ts', {})

const data = findAndReadConfig('/path/to/b/src', host, exists)
const pathname = findConfig('/path/to/b/src', exists)
assert(pathname === '/path/to/b/tsconfig.json')
})

it('returns undefined if config is not found', () => {
const pathname = findConfig('/path/to/src', exists)
assert(pathname === undefined)
})

it('read tsconfig.json', () => {
mock('/path/to/tsconfig.json', {
compilerOptions: {
target: 'es5',
module: 'es2015',
moduleResolution: 'node',
experimentalDecorators: true
}
})
const data = readConfig('/path/to/tsconfig.json', host)
assert.ok(data)

const options = data!.options
assert(options.target === ScriptTarget.ES2015)
assert(options.target === ts.ScriptTarget.ES5)
assert(options.module === ts.ModuleKind.ES2015)
assert(options.moduleResolution === ts.ModuleResolutionKind.NodeJs)
assert(options.experimentalDecorators === true)
})

it('returns undefined if config is not found', () => {
const data = findAndReadConfig('/path/to/src', host, exists)
assert(data === undefined)
it('returns undefined if the config file is not found', () => {
const data = readConfig('/path/to/tsconfig.json', host)
assert.ifError(data)
})
})

function readFile (fileName: string): string {
return fs.readFileSync(fileName, 'utf8')
}

function mock (fileName: string, data: any): void {
fs.mkdirpSync(fileName.split('/').slice(0, -1).join('/'))
fs.writeFileSync(fileName, JSON.stringify(data))
Expand Down

0 comments on commit 6644f0d

Please sign in to comment.