Skip to content

Commit b83b665

Browse files
committed
fix: robustify and fix file writing
- throw an error if writing didn't work (ts doesn't throw itself) - fix html path logic #2584
1 parent 1b205c2 commit b83b665

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/svelte2tsx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte2tsx",
3-
"version": "0.7.23",
3+
"version": "0.7.25",
44
"description": "Convert Svelte components to TSX for type checking",
55
"author": "David Pershouse",
66
"license": "MIT",

packages/svelte2tsx/src/helpers/files.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ export function get_global_types(
1212
typesPath: string,
1313
hiddenFolderPath?: string
1414
): string[] {
15-
const svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts');
16-
const svelteHtmlPathExists = svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath);
17-
const svelteHtmlFile = svelteHtmlPathExists ? svelteHtmlPath : './svelte-jsx-v4.d.ts';
15+
let svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts');
16+
svelteHtmlPath =
17+
svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath) ? svelteHtmlPath : undefined;
1818

1919
let svelteTsxFiles: string[];
2020
if (isSvelte3) {
2121
svelteTsxFiles = ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts'];
2222
} else {
2323
svelteTsxFiles = ['./svelte-shims-v4.d.ts', './svelte-native-jsx.d.ts'];
24-
if (!svelteHtmlPathExists) {
25-
svelteTsxFiles.push(svelteHtmlPath);
24+
if (!svelteHtmlPath) {
25+
svelteTsxFiles.push('./svelte-jsx-v4.d.ts');
2626
}
2727
}
2828
svelteTsxFiles = svelteTsxFiles.map((f) => tsSystem.resolvePath(resolve(typesPath, f)));
@@ -53,19 +53,29 @@ export function get_global_types(
5353
for (const f of svelteTsxFiles) {
5454
const hiddenFile = resolve(hiddenPath, basename(f));
5555
const existing = tsSystem.readFile(hiddenFile);
56-
const toWrite = tsSystem.readFile(f) || '';
56+
const toWrite = tsSystem.readFile(f);
57+
58+
if (!toWrite) {
59+
throw new Error(`Could not read file: ${f}`);
60+
}
61+
5762
if (existing !== toWrite) {
5863
tsSystem.writeFile(hiddenFile, toWrite);
64+
// TS doesn't throw an error if the file wasn't written
65+
if (!tsSystem.fileExists(hiddenFile)) {
66+
throw new Error(`Could not write file: ${hiddenFile}`);
67+
}
5968
}
69+
6070
newFiles.push(hiddenFile);
6171
}
6272
svelteTsxFiles = newFiles;
6373
}
6474
} catch (e) {}
6575
}
6676

67-
if (svelteHtmlPathExists) {
68-
svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlFile)));
77+
if (svelteHtmlPath) {
78+
svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlPath)));
6979
}
7080

7181
return svelteTsxFiles;

0 commit comments

Comments
 (0)