-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
config.ts
68 lines (61 loc) · 1.54 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as path from 'path';
import {
JsxEmit,
ScriptTarget,
ModuleResolutionKind,
parseJsonConfigFileContent,
CompilerOptions,
findConfigFile,
sys,
readJsonConfigFile,
parseJsonSourceFileConfigFileContent,
ModuleKind
} from 'typescript';
import {Config, RawConfig, RawCompilerOptions} from './interfaces';
/**
* Load the configuration settings.
*
* @param pkg - The package.json object.
* @returns The config object.
*/
export default (pkg: {tsd?: RawConfig}, cwd: string): Config => {
const pkgConfig = pkg.tsd || {};
const tsConfigCompilerOptions = getOptionsFromTsConfig(cwd);
const packageJsonCompilerOptions = parseCompilerConfigObject(
pkgConfig.compilerOptions || {},
cwd
);
return {
directory: 'test-d',
...pkgConfig,
compilerOptions: {
strict: true,
jsx: JsxEmit.React,
lib: ['lib.es2017.d.ts'],
module: ModuleKind.CommonJS,
target: ScriptTarget.ES2017,
...tsConfigCompilerOptions,
...packageJsonCompilerOptions,
moduleResolution: ModuleResolutionKind.NodeJs,
skipLibCheck: false
}
};
};
function getOptionsFromTsConfig(cwd: string): CompilerOptions {
const configPath = findConfigFile(cwd, sys.fileExists);
if (!configPath) {
return {};
}
return parseJsonSourceFileConfigFileContent(
readJsonConfigFile(configPath, sys.readFile),
sys,
path.basename(configPath)
).options;
}
function parseCompilerConfigObject(compilerOptions: RawCompilerOptions, cwd: string): CompilerOptions {
return parseJsonConfigFileContent(
{compilerOptions: compilerOptions || {}},
sys,
cwd
).options;
}