1 | import { isWithin, normalizePath, toAbsolutePath } from "./fs/path-utils";
|
2 | import {
|
3 | CompilerOptionsConfig,
|
4 | NormalizedOptions,
|
5 | TypescriptCompilerOptions,
|
6 | } from "./interfaces";
|
7 |
|
8 | export default function normalizeOptions(
|
9 | options: TypescriptCompilerOptions
|
10 | ): NormalizedOptions {
|
11 | const workingPath = toAbsolutePath(
|
12 | options.workingPath === undefined ? process.cwd() : options.workingPath
|
13 | );
|
14 | const rootPath =
|
15 | options.rootPath === undefined
|
16 | ? workingPath
|
17 | : toAbsolutePath(options.rootPath, workingPath);
|
18 | const projectPath =
|
19 | options.projectPath === undefined
|
20 | ? rootPath
|
21 | : toAbsolutePath(options.projectPath, workingPath);
|
22 | const buildPath =
|
23 | options.buildPath === undefined
|
24 | ? undefined
|
25 | : toAbsolutePath(options.buildPath, workingPath);
|
26 | const tsconfig = options.tsconfig;
|
27 |
|
28 | if (
|
29 | buildPath !== undefined &&
|
30 | !(rootPath === buildPath || isWithin(rootPath, buildPath))
|
31 | ) {
|
32 | throw new Error(
|
33 | `buildPath "${buildPath}" must be at or within rootPath "${rootPath}"`
|
34 | );
|
35 | }
|
36 |
|
37 | let configFileName: string | undefined;
|
38 | let rawConfig: CompilerOptionsConfig | undefined;
|
39 | if (typeof tsconfig === "object") {
|
40 | configFileName = undefined;
|
41 | rawConfig = tsconfig;
|
42 | } else if (tsconfig) {
|
43 | configFileName = normalizePath(tsconfig);
|
44 | rawConfig = undefined;
|
45 | }
|
46 |
|
47 | let throwOnError = options.throwOnError;
|
48 | if (throwOnError === undefined) {
|
49 | throwOnError = process.env.NODE_ENV === "production";
|
50 | }
|
51 |
|
52 | return {
|
53 | buildPath,
|
54 | compilerOptions: options.compilerOptions,
|
55 | configFileName,
|
56 | projectPath,
|
57 | rawConfig,
|
58 | rootPath,
|
59 | throwOnError,
|
60 | workingPath,
|
61 | };
|
62 | }
|