Skip to content

Commit

Permalink
fix: robustify and fix file writing
Browse files Browse the repository at this point in the history
- throw an error if writing didn't work (ts doesn't throw itself)
- fix html path logic

#2584
  • Loading branch information
dummdidumm committed Nov 15, 2024
1 parent 1b205c2 commit b83b665
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte2tsx",
"version": "0.7.23",
"version": "0.7.25",
"description": "Convert Svelte components to TSX for type checking",
"author": "David Pershouse",
"license": "MIT",
Expand Down
26 changes: 18 additions & 8 deletions packages/svelte2tsx/src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export function get_global_types(
typesPath: string,
hiddenFolderPath?: string
): string[] {
const svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts');
const svelteHtmlPathExists = svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath);
const svelteHtmlFile = svelteHtmlPathExists ? svelteHtmlPath : './svelte-jsx-v4.d.ts';
let svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts');
svelteHtmlPath =
svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath) ? svelteHtmlPath : undefined;

let svelteTsxFiles: string[];
if (isSvelte3) {
svelteTsxFiles = ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts'];
} else {
svelteTsxFiles = ['./svelte-shims-v4.d.ts', './svelte-native-jsx.d.ts'];
if (!svelteHtmlPathExists) {
svelteTsxFiles.push(svelteHtmlPath);
if (!svelteHtmlPath) {
svelteTsxFiles.push('./svelte-jsx-v4.d.ts');
}
}
svelteTsxFiles = svelteTsxFiles.map((f) => tsSystem.resolvePath(resolve(typesPath, f)));
Expand Down Expand Up @@ -53,19 +53,29 @@ export function get_global_types(
for (const f of svelteTsxFiles) {
const hiddenFile = resolve(hiddenPath, basename(f));
const existing = tsSystem.readFile(hiddenFile);
const toWrite = tsSystem.readFile(f) || '';
const toWrite = tsSystem.readFile(f);

if (!toWrite) {
throw new Error(`Could not read file: ${f}`);
}

if (existing !== toWrite) {
tsSystem.writeFile(hiddenFile, toWrite);
// TS doesn't throw an error if the file wasn't written
if (!tsSystem.fileExists(hiddenFile)) {
throw new Error(`Could not write file: ${hiddenFile}`);
}
}

newFiles.push(hiddenFile);
}
svelteTsxFiles = newFiles;
}
} catch (e) {}
}

if (svelteHtmlPathExists) {
svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlFile)));
if (svelteHtmlPath) {
svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlPath)));
}

return svelteTsxFiles;
Expand Down

0 comments on commit b83b665

Please sign in to comment.