Skip to content

Commit dd83df1

Browse files
committed
move validation into separate function
1 parent 03e8ff0 commit dd83df1

File tree

1 file changed

+54
-47
lines changed

1 file changed

+54
-47
lines changed

Diff for: packages/kit/src/core/tsconfig.js

+54-47
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,7 @@ export function generate_tsconfig(config) {
1212
const out = path.resolve(SVELTE_KIT, 'tsconfig.json');
1313
const user_file = exists('tsconfig.json') || exists('jsconfig.json');
1414

15-
if (user_file) {
16-
// we have to eval the file, since it's not parseable as JSON (contains comments)
17-
const user_tsconfig_json = fs.readFileSync(user_file, 'utf-8');
18-
const user_tsconfig = (0, eval)(`(${user_tsconfig_json})`);
19-
20-
// we need to check that the user's tsconfig extends the framework config
21-
const extend = user_tsconfig.extends;
22-
const extends_framework_config = extend && path.resolve('.', extend) === out;
23-
24-
if (extends_framework_config) {
25-
const { paths: user_paths } = user_tsconfig.compilerOptions || {};
26-
27-
if (user_paths) {
28-
const lib = user_paths['$lib'] || [];
29-
const lib_ = user_paths['$lib/*'] || [];
30-
31-
const missing_lib_paths =
32-
!lib.some(
33-
(/** @type {string} */ relative) => path.resolve('.', relative) === config.kit.files.lib
34-
) ||
35-
!lib_.some(
36-
(/** @type {string} */ relative) =>
37-
path.resolve('.', relative) === config.kit.files.lib + '/*'
38-
);
39-
40-
if (missing_lib_paths) {
41-
console.warn(
42-
colors
43-
.bold()
44-
.yellow(`Your compilerOptions.paths in ${user_file} should include the following:`)
45-
);
46-
const relative = path.relative('.', config.kit.files.lib);
47-
console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`);
48-
}
49-
}
50-
} else {
51-
let relative = path.relative('.', out);
52-
if (relative.startsWith(SVELTE_KIT)) relative = './' + relative;
53-
54-
console.warn(
55-
colors
56-
.bold()
57-
.yellow(`Your ${user_file} should extend the configuration generated by SvelteKit:`)
58-
);
59-
console.warn(`{\n "extends": "${relative}"\n}`);
60-
}
61-
}
15+
if (user_file) validate(config, out, user_file);
6216

6317
mkdirp(SVELTE_KIT);
6418

@@ -107,3 +61,56 @@ export function generate_tsconfig(config) {
10761
)
10862
);
10963
}
64+
65+
/**
66+
* @param {import('types').ValidatedConfig} config
67+
* @param {string} out
68+
* @param {string} user_file
69+
*/
70+
function validate(config, out, user_file) {
71+
// we have to eval the file, since it's not parseable as JSON (contains comments)
72+
const user_tsconfig_json = fs.readFileSync(user_file, 'utf-8');
73+
const user_tsconfig = (0, eval)(`(${user_tsconfig_json})`);
74+
75+
// we need to check that the user's tsconfig extends the framework config
76+
const extend = user_tsconfig.extends;
77+
const extends_framework_config = extend && path.resolve('.', extend) === out;
78+
79+
if (extends_framework_config) {
80+
const { paths: user_paths } = user_tsconfig.compilerOptions || {};
81+
82+
if (user_paths) {
83+
const lib = user_paths['$lib'] || [];
84+
const lib_ = user_paths['$lib/*'] || [];
85+
86+
const missing_lib_paths =
87+
!lib.some(
88+
(/** @type {string} */ relative) => path.resolve('.', relative) === config.kit.files.lib
89+
) ||
90+
!lib_.some(
91+
(/** @type {string} */ relative) =>
92+
path.resolve('.', relative) === config.kit.files.lib + '/*'
93+
);
94+
95+
if (missing_lib_paths) {
96+
console.warn(
97+
colors
98+
.bold()
99+
.yellow(`Your compilerOptions.paths in ${user_file} should include the following:`)
100+
);
101+
const relative = path.relative('.', config.kit.files.lib);
102+
console.warn(`{\n "$lib":["${relative}"],\n "$lib/*":["${relative}/*"]\n}`);
103+
}
104+
}
105+
} else {
106+
let relative = path.relative('.', out);
107+
if (relative.startsWith(SVELTE_KIT)) relative = './' + relative;
108+
109+
console.warn(
110+
colors
111+
.bold()
112+
.yellow(`Your ${user_file} should extend the configuration generated by SvelteKit:`)
113+
);
114+
console.warn(`{\n "extends": "${relative}"\n}`);
115+
}
116+
}

0 commit comments

Comments
 (0)