Skip to content

Commit ddbb56a

Browse files
committed
Update load method to return config w/ path
1 parent 1735d66 commit ddbb56a

File tree

3 files changed

+75
-57
lines changed

3 files changed

+75
-57
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ See the [TypeScript wiki](https://github.com/Microsoft/TypeScript/wiki/tsconfig.
1717
* **resolveSync(cwd: string, path?: string): string | void** Synchronous `resolve`.
1818
* **find(cwd: string): Promise<string | void>** Standalone behaviour of recursively resolving `tsconfig.json` upward.
1919
* **findSync(cwd: string): string | void** Synchronous `find`.
20-
* **load(cwd: string, path?: string): Promise<TSConfig>** Resolve, load and parse `tsconfig.json`.
21-
* **loadSync(cwd: string, path?: string): TSConfig** Synchronous `load`.
22-
* **readFile(filename: string): Promise<TSConfig>** Read a JSON file as `tsconfig.json` (strip BOM, parse JSON and support empty contents).
23-
* **readFileSync(filename: string): TSConfig** Synchronous `readFile`.
24-
* **parse(contents: string, filename: string): TSConfig** Parse file contents as `tsconfig.json` (strip BOM, parse JSON and support empty contents).
20+
* **load(cwd: string, path?: string): Promise<{ path?: string, config?: any}>** Resolve, load and parse `tsconfig.json`.
21+
* **loadSync(cwd: string, path?: string): { path?: string, config?: any}** Synchronous `load`.
22+
* **readFile(filename: string): Promise<any>** Read a JSON file as `tsconfig.json` (strip BOM, parse JSON and support empty contents).
23+
* **readFileSync(filename: string): any** Synchronous `readFile`.
24+
* **parse(contents: string, filename: string): any** Parse file contents as `tsconfig.json` (strip BOM, parse JSON and support empty contents).
2525

2626
## Contributing
2727

src/tsconfig.spec.ts

+46-35
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,48 @@ import * as tsconfig from './tsconfig'
66
const TEST_DIR = join(__dirname, '../tests')
77

88
interface Test {
9-
path: [string] | [string, string]
10-
result?: tsconfig.TSConfig
9+
args: [string] | [string, string]
10+
config?: any
11+
path?: string
1112
error?: string
12-
filename?: string
1313
}
1414

1515
describe('tsconfig', function () {
1616
const tests: Test[] = [
1717
{
18-
path: [TEST_DIR, 'invalidfile'],
18+
args: [TEST_DIR, 'invalidfile'],
1919
error: `Unexpected token 's' at 1:1 in ${join(TEST_DIR, 'invalidfile/tsconfig.json')}\nsome random string\n^`
2020
},
2121
{
22-
path: [TEST_DIR, 'missing'],
22+
args: [TEST_DIR, 'missing'],
2323
error: 'Cannot find a tsconfig.json file at the specified directory: missing'
2424
},
2525
{
26-
path: [TEST_DIR, 'missing/foobar'],
26+
args: [TEST_DIR, 'missing/foobar'],
2727
error: 'The specified path does not exist: missing/foobar'
2828
},
2929
{
30-
path: ['/'],
31-
result: {}
30+
args: ['/'],
31+
config: {}
3232
},
3333
{
34-
path: [TEST_DIR, 'empty'],
35-
result: {}
34+
args: [TEST_DIR, 'empty'],
35+
config: {},
36+
path: join(TEST_DIR, 'empty/tsconfig.json')
3637
},
3738
{
38-
path: [TEST_DIR, 'empty/tsconfig.json'],
39-
result: {}
39+
args: [TEST_DIR, 'empty/tsconfig.json'],
40+
config: {},
41+
path: join(TEST_DIR, 'empty/tsconfig.json')
4042
},
4143
{
42-
path: [join(TEST_DIR, 'find/up/config')],
43-
result: {}
44+
args: [join(TEST_DIR, 'find/up/config')],
45+
config: {},
46+
path: join(TEST_DIR, 'find/tsconfig.json')
4447
},
4548
{
46-
path: [TEST_DIR, 'valid'],
47-
result: {
49+
args: [TEST_DIR, 'valid'],
50+
config: {
4851
compilerOptions: {
4952
module: 'commonjs',
5053
noImplicitAny: true,
@@ -57,11 +60,11 @@ describe('tsconfig', function () {
5760
'./src/foo.ts'
5861
]
5962
},
60-
filename: join(__dirname, '../tests/valid/tsconfig.json')
63+
path: join(TEST_DIR, 'valid/tsconfig.json')
6164
},
6265
{
63-
path: [TEST_DIR, 'bom'],
64-
result: {
66+
args: [TEST_DIR, 'bom'],
67+
config: {
6568
compilerOptions: {
6669
module: 'commonjs',
6770
noImplicitAny: true,
@@ -74,11 +77,11 @@ describe('tsconfig', function () {
7477
'./src/bom.ts'
7578
]
7679
},
77-
filename: join(__dirname, '../tests/bom/tsconfig.json')
80+
path: join(TEST_DIR, 'bom/tsconfig.json')
7881
},
7982
{
80-
path: [join(TEST_DIR, 'cwd')],
81-
result: {
83+
args: [join(TEST_DIR, 'cwd')],
84+
config: {
8285
compilerOptions: {
8386
module: 'commonjs',
8487
noImplicitAny: true,
@@ -88,30 +91,31 @@ describe('tsconfig', function () {
8891
preserveConstEnums: true
8992
}
9093
},
91-
filename: join(__dirname, '../tests/cwd/tsconfig.json')
94+
path: join(TEST_DIR, 'cwd/tsconfig.json')
9295
}
9396
]
9497

9598
describe('sync', function () {
9699
tests.forEach(function (test) {
97-
describe(inspect(test.path), function () {
100+
describe(inspect(test.args), function () {
98101
it('should try to find config', function () {
99102
let result: any
100103

101104
try {
102-
result = tsconfig.loadSync(test.path[0], test.path[1])
105+
result = tsconfig.loadSync(test.args[0], test.args[1])
103106
} catch (err) {
104107
expect(err.message).to.equal(test.error)
105108

106109
return
107110
}
108111

109-
expect(result).to.deep.equal(test.result)
112+
expect(result.path).to.equal(test.path)
113+
expect(result.config).to.deep.equal(test.config)
110114
})
111115

112-
if (test.filename) {
116+
if (test.path) {
113117
it('should resolve filename', function () {
114-
expect(tsconfig.resolveSync(test.path[0], test.path[1])).to.equal(test.filename)
118+
expect(tsconfig.resolveSync(test.args[0], test.args[1])).to.equal(test.path)
115119
})
116120
}
117121
})
@@ -120,19 +124,26 @@ describe('tsconfig', function () {
120124

121125
describe('async', function () {
122126
tests.forEach(function (test) {
123-
describe(inspect(test.path), function () {
127+
describe(inspect(test.args), function () {
124128
it('should try to find config', function () {
125-
return tsconfig.load(test.path[0], test.path[1])
129+
return tsconfig.load(test.args[0], test.args[1])
126130
.then(
127-
config => expect(config).to.deep.equal(test.result),
128-
error => expect(error.message).to.equal(test.error)
131+
result => {
132+
expect(result.path).to.equal(test.path)
133+
expect(result.config).to.deep.equal(test.config)
134+
},
135+
error => {
136+
expect(error.message).to.equal(test.error)
137+
}
129138
)
130139
})
131140

132-
if (test.filename) {
141+
if (test.path) {
133142
it('should resolve filename', function () {
134-
return tsconfig.resolve(test.path[0], test.path[1])
135-
.then(filename => expect(filename).to.equal(test.filename))
143+
return tsconfig.resolve(test.args[0], test.args[1])
144+
.then(filename => {
145+
expect(filename).to.equal(test.path)
146+
})
136147
})
137148
}
138149
})

src/tsconfig.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@ import stripBom = require('strip-bom')
55
import parseJson = require('parse-json')
66
import stripComments = require('strip-json-comments')
77

8-
export interface CompilerOptions {
9-
[key: string]: any
10-
}
11-
12-
export interface TSConfig {
13-
compilerOptions?: CompilerOptions
14-
files?: string[]
15-
include?: string[]
16-
exclude?: string[]
17-
[key: string]: any
8+
export interface LoadResult {
9+
path?: string
10+
config?: any
1811
}
1912

2013
const CONFIG_FILENAME = 'tsconfig.json'
@@ -126,26 +119,40 @@ export function findSync (dir: string): string | void {
126119
/**
127120
* Resolve and load configuration file.
128121
*/
129-
export function load (cwd: string, filename?: string): Promise<TSConfig> {
122+
export function load (cwd: string, filename?: string): Promise<LoadResult> {
130123
return resolve(cwd, filename)
131-
.then(path => {
132-
return path == null ? Promise.resolve({}) : readFile(path as string)
124+
.then<LoadResult>(path => {
125+
if (path == null) {
126+
return Promise.resolve({
127+
config: {}
128+
})
129+
}
130+
131+
return readFile(path as string).then(config => ({ config, path }))
133132
})
134133
}
135134

136135
/**
137136
* Synchronous `load`.
138137
*/
139-
export function loadSync (cwd: string, filename?: string): TSConfig {
138+
export function loadSync (cwd: string, filename?: string): LoadResult {
140139
const path = resolveSync(cwd, filename)
141140

142-
return path == null ? {} : readFileSync(path as string)
141+
if (path == null) {
142+
return {
143+
config: {}
144+
}
145+
}
146+
147+
const config = readFileSync(path as string)
148+
149+
return { path: path as string, config }
143150
}
144151

145152
/**
146153
* Read `tsconfig.json` and parse/sanitize contents.
147154
*/
148-
export function readFile (filename: string): Promise<TSConfig> {
155+
export function readFile (filename: string): Promise<any> {
149156
return new Promise((resolve, reject) => {
150157
fs.readFile(filename, 'utf8', (err, contents) => {
151158
if (err) {
@@ -164,7 +171,7 @@ export function readFile (filename: string): Promise<TSConfig> {
164171
/**
165172
* Synchonrous `readFile`.
166173
*/
167-
export function readFileSync (filename: string): TSConfig {
174+
export function readFileSync (filename: string): any {
168175
const contents = fs.readFileSync(filename, 'utf8')
169176

170177
return parse(contents, filename)

0 commit comments

Comments
 (0)