From c936eb858d2b5d300fea9838fc3c3f55af283f3a Mon Sep 17 00:00:00 2001 From: "S. Elliott Johnson" Date: Tue, 24 May 2022 15:33:43 -0600 Subject: [PATCH] fix: Generated types no longer permanently delete themselves (#5063) * fix: sync no longer permanently deletes directory * Changelog * DRY, you idiot * fix: Lint --- .changeset/lemon-planets-sit.md | 5 +++++ packages/kit/src/core/sync/utils.js | 14 +++++++++++--- packages/kit/src/core/sync/write_types.js | 7 ++----- 3 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 .changeset/lemon-planets-sit.md diff --git a/.changeset/lemon-planets-sit.md b/.changeset/lemon-planets-sit.md new file mode 100644 index 000000000000..ef79aab28828 --- /dev/null +++ b/.changeset/lemon-planets-sit.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +[fix] svelte-kit sync no longer permanently deletes the types directory diff --git a/packages/kit/src/core/sync/utils.js b/packages/kit/src/core/sync/utils.js index aaf27a3ef74c..be1862cd1927 100644 --- a/packages/kit/src/core/sync/utils.js +++ b/packages/kit/src/core/sync/utils.js @@ -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]; diff --git a/packages/kit/src/core/sync/write_types.js b/packages/kit/src/core/sync/write_types.js index 01f129aa8e37..9327020add85 100644 --- a/packages/kit/src/core/sync/write_types.js +++ b/packages/kit/src/core/sync/write_types.js @@ -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) => ` @@ -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()); }); }