Skip to content

Commit

Permalink
fix: Generated types no longer permanently delete themselves (#5063)
Browse files Browse the repository at this point in the history
* fix:  sync no longer permanently deletes  directory

* Changelog

* DRY, you idiot

* fix: Lint
  • Loading branch information
1 parent 5d58703 commit c936eb8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-planets-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] svelte-kit sync no longer permanently deletes the types directory
14 changes: 11 additions & 3 deletions packages/kit/src/core/sync/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ const previous_contents = new Map();
*/
export function write_if_changed(file, code) {
if (code !== previous_contents.get(file)) {
previous_contents.set(file, code);
mkdirp(path.dirname(file));
fs.writeFileSync(file, code);
write(file, code);
}
}

/**
* @param {string} file
* @param {string} code
*/
export function write(file, code) {
previous_contents.set(file, code);
mkdirp(path.dirname(file));
fs.writeFileSync(file, code);
}

/** @param {string} str */
export function trim(str) {
const indentation = /** @type {RegExpExecArray} */ (/\n?(\s*)/.exec(str))[1];
Expand Down
7 changes: 2 additions & 5 deletions packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { rimraf } from '../../utils/filesystem.js';
import { parse_route_id } from '../../utils/routing.js';
import { write_if_changed } from './utils.js';
import { write } from './utils.js';

/** @param {string} imports */
const header = (imports) => `
Expand Down Expand Up @@ -76,9 +76,6 @@ export function write_types(config, manifest_data) {
const parts = (key || 'index').split('/');
parts.push('__types', /** @type {string} */ (parts.pop()));

write_if_changed(
`${config.kit.outDir}/types/${parts.join('/')}.d.ts`,
content.join('\n').trim()
);
write(`${config.kit.outDir}/types/${parts.join('/')}.d.ts`, content.join('\n').trim());
});
}

0 comments on commit c936eb8

Please sign in to comment.