forked from tsdjs/tsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
84 lines (74 loc) · 1.98 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
JsxEmit,
ScriptTarget,
ModuleResolutionKind,
parseJsonConfigFileContent,
CompilerOptions,
findConfigFile,
sys,
readJsonConfigFile,
parseJsonSourceFileConfigFileContent,
ModuleKind
} from '@tsd/typescript';
import {Config, PackageJsonWithTsdConfig, RawCompilerOptions} from './interfaces';
/**
* Load the configuration settings.
*
* @param pkg - The package.json object.
* @returns The config object.
*/
export default (pkg: PackageJsonWithTsdConfig, cwd: string): Config => {
const pkgConfig = pkg.tsd ?? {};
const tsConfigCompilerOptions = getOptionsFromTsConfig(cwd);
const packageJsonCompilerOptions = parseCompilerConfigObject(
pkgConfig.compilerOptions ?? {},
cwd
);
const combinedCompilerOptions = {
...tsConfigCompilerOptions,
...packageJsonCompilerOptions,
};
const module = combinedCompilerOptions.module ?? ModuleKind.CommonJS;
return {
directory: 'test-d',
...pkgConfig,
compilerOptions: {
strict: true,
jsx: JsxEmit.React,
lib: parseRawLibs(['es2017', 'dom', 'dom.iterable'], cwd),
module,
target: ScriptTarget.ES2017,
esModuleInterop: true,
...combinedCompilerOptions,
moduleResolution: module === ModuleKind.NodeNext ?
ModuleResolutionKind.NodeNext :
module === ModuleKind.Node16 ?
ModuleResolutionKind.Node16 :
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,
cwd,
undefined,
configPath,
).options;
}
function parseCompilerConfigObject(compilerOptions: RawCompilerOptions, cwd: string): CompilerOptions {
return parseJsonConfigFileContent(
{compilerOptions: compilerOptions || {}},
sys,
cwd
).options;
}
function parseRawLibs(libs: string[], cwd: string): string[] {
return parseCompilerConfigObject({lib: libs}, cwd).lib ?? [];
}