From eafb08249494b1dc41b997ec08c135597b5acf5e Mon Sep 17 00:00:00 2001 From: Alexandre Oger Date: Mon, 28 Oct 2024 08:00:55 +0100 Subject: [PATCH 1/3] fix: add svelte-kit snippet for svelte-5 --- .../src/sveltekit/generateFiles/index.ts | 8 +++- .../generateFiles/templates/error.ts | 34 +++++++++----- .../generateFiles/templates/layout-load.ts | 36 +++++++++------ .../generateFiles/templates/layout-server.ts | 36 +++++++++------ .../generateFiles/templates/layout.ts | 40 +++++++++++++---- .../generateFiles/templates/page-load.ts | 36 +++++++++------ .../generateFiles/templates/page-server.ts | 36 +++++++++------ .../sveltekit/generateFiles/templates/page.ts | 40 +++++++++++++---- .../generateFiles/templates/server.ts | 32 +++++++++----- .../src/sveltekit/generateFiles/types.ts | 18 +++++++- packages/svelte-vscode/src/sveltekit/utils.ts | 44 ++++++++++++++++--- 11 files changed, 261 insertions(+), 99 deletions(-) diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/index.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/index.ts index 0caf7725f..10184a85b 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/index.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/index.ts @@ -3,7 +3,7 @@ import { commands, ExtensionContext, ProgressLocation, Uri, window, workspace } import { addResourceCommandMap } from './commands'; import { generateResources } from './generate'; import { resourcesMap } from './resources'; -import { FileType, ResourceType, GenerateConfig, CommandType } from './types'; +import { FileType, ResourceType, GenerateConfig, CommandType, ProjectType } from './types'; import { checkProjectType } from '../utils'; class GenerateError extends Error {} @@ -126,7 +126,7 @@ async function getCommonConfig(uri: Uri | undefined) { } const type = await checkProjectType(rootPath); - const scriptExtension = type === 'js' ? 'js' : 'ts'; + const scriptExtension = getScriptExtension(type); return { type, scriptExtension, @@ -134,6 +134,10 @@ async function getCommonConfig(uri: Uri | undefined) { } as const; } +function getScriptExtension(type: ProjectType) { + return type === ProjectType.JS || type === ProjectType.JS_SV5 ? 'js' : 'ts'; +} + function getRootPath(uri: Uri | undefined) { let rootPath!: string; if (uri) { diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts index ffd0748e2..6d3fae224 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts @@ -1,21 +1,31 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function (config: GenerateConfig) { - const ts = ` - -

{$page.status}: {$page.error?.message}

- `.trim(); +

{$page.status}: {$page.error.message}

+`; - const js = ` - -

{$page.status}: {$page.error.message}

- `.trim(); +

{$page.status}: {$page.error?.message}

+`; + +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.JS_SV5, defaultScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : ts; -} +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); +} \ No newline at end of file diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-load.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-load.ts index 4259a54c5..9cf1b7b6c 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-load.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-load.ts @@ -1,28 +1,38 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function (config: GenerateConfig) { - const ts = ` +const defaultScriptTemplate = ` +/** @type {import('./$types').LayoutLoad} */ +export async function load() { + return {}; +} +`; + + +const tsScriptTemplate = ` import type { LayoutLoad } from './$types'; export const load: LayoutLoad = async () => { return {}; }; - `.trim(); +`; - const tsSatisfies = ` +const tsSatisfiesScriptTemplate = ` import type { LayoutLoad } from './$types'; export const load = (async () => { return {}; }) satisfies LayoutLoad; - `.trim(); +`; - const js = ` -/** @type {import('./$types').LayoutLoad} */ -export async function load() { - return {}; -} - `.trim(); +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsSatisfiesScriptTemplate], + [ProjectType.JS_SV5, defaultScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsSatisfiesScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : config.type === 'ts' ? ts : tsSatisfies; +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); } diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts index e467ba5d8..24bb14fec 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts @@ -1,28 +1,38 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function (config: GenerateConfig) { - const ts = ` +const defaultScriptTemplate = ` +/** @type {import('./$types').LayoutServerLoad} */ +export async function load() { + return {}; +} +`; + + +const tsScriptTemplate = ` import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = async () => { return {}; }; - `.trim(); +`; - const tsSatisfies = ` +const tsSatisfiesScriptTemplate = ` import type { LayoutServerLoad } from './$types'; export const load = (async () => { return {}; }) satisfies LayoutServerLoad; - `.trim(); +`; - const js = ` -/** @type {import('./$types').LayoutServerLoad} */ -export async function load() { - return {}; -} - `.trim(); +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.JS_SV5, defaultScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : config.type === 'ts' ? ts : tsSatisfies; +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); } diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts index 00563d89a..3e1fa4ce1 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts @@ -1,20 +1,44 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function (config: GenerateConfig) { - const ts = ` +const defaultScriptTemplate = ` + +`; + +const tsSv5ScriptTemplate = ` + +`; + +const tsScriptTemplate = ` - `.trim(); +`; - const js = ` +const jsSv5ScriptTemplate = ` - `.trim(); +`; + +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsSv5ScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsSv5ScriptTemplate], + [ProjectType.JS_SV5, jsSv5ScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : ts; +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); } diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts index 6fb2ca84c..5d24854ff 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts @@ -1,28 +1,38 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function (config: GenerateConfig) { - const ts = ` +const defaultScriptTemplate = ` +/** @type {import('./$types').PageLoad} */ +export async function load() { + return {}; +}; +`; + + +const tsScriptTemplate = ` import type { PageLoad } from './$types'; export const load: PageLoad = async () => { return {}; }; - `.trim(); +`; - const tsSatisfies = ` +const tsSatisfiesScriptTemplate = ` import type { PageLoad } from './$types'; export const load = (async () => { return {}; }) satisfies PageLoad; - `.trim(); +`; - const js = ` -/** @type {import('./$types').PageLoad} */ -export async function load() { - return {}; -}; - `.trim(); +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.JS_SV5, defaultScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : config.type === 'ts' ? ts : tsSatisfies; +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); } diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts index bbb2ef7e9..41ae7ad55 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts @@ -1,28 +1,38 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function (config: GenerateConfig) { - const ts = ` +const defaultScriptTemplate = ` +/** @type {import('./$types').PageServerLoad} */ +export async function load() { + return {}; +}; +`; + + +const tsScriptTemplate = ` import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async () => { return {}; }; - `.trim(); +`; - const tsSatisfies = ` +const tsSatisfiesScriptTemplate = ` import type { PageServerLoad } from './$types'; export const load = (async () => { return {}; }) satisfies PageServerLoad; - `.trim(); +`; - const js = ` -/** @type {import('./$types').PageServerLoad} */ -export async function load() { - return {}; -}; - `.trim(); +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.JS_SV5, defaultScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : config.type === 'ts' ? ts : tsSatisfies; +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); } diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts index abaf94924..b67e0df8e 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts @@ -1,20 +1,44 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function (config: GenerateConfig) { - const ts = ` +const defaultScriptTemplate = ` + +`; + +const tsSv5ScriptTemplate = ` + +`; + +const tsScriptTemplate = ` - `.trim(); +`; - const js = ` +const jsSv5ScriptTemplate = ` - `.trim(); +`; + +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsSv5ScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsSv5ScriptTemplate], + [ProjectType.JS_SV5, jsSv5ScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : ts; +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); } diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/server.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/server.ts index 826b0b660..9e293b08d 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/server.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/server.ts @@ -1,20 +1,30 @@ -import { GenerateConfig } from '../types'; +import { GenerateConfig, ProjectType, Resource } from '../types'; -export default async function generate(config: GenerateConfig) { - const ts = ` +const defaultScriptTemplate = ` +/** @type {import('./$types').RequestHandler} */ +export async function GET() { + return new Response(); +}; +`; + + +const tsScriptTemplate = ` import type { RequestHandler } from './$types'; export const GET: RequestHandler = async () => { return new Response(); }; - `.trim(); +`; - const js = ` -/** @type {import('./$types').RequestHandler} */ -export async function GET() { - return new Response(); -}; - `.trim(); +const scriptTemplate: ReadonlyMap = new Map([ + [ProjectType.TS_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.JS_SV5, defaultScriptTemplate], + [ProjectType.TS, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.JS, defaultScriptTemplate] +]) - return config.type === 'js' ? js : ts; +export default async function (config: GenerateConfig): ReturnType { + return (scriptTemplate.get(config.type) ?? defaultScriptTemplate).trim(); } diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/types.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/types.ts index e50f4225c..b71adce2a 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/types.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/types.ts @@ -32,9 +32,25 @@ export type Resource = { generate: (config: GenerateConfig) => Promise; }; +export enum ProjectType { + TS_SV5 = 'ts-sv5', + JS_SV5 = 'js-sv5', + TS_SATISFIES_SV5 = 'ts-satisfies-sv5', + TS = 'ts', + JS = 'js', + TS_SATISFIES = 'ts-satisfies', +} + +export type IsSvelte5Plus = boolean + +export const IsSvelte5Plus: Record = { + yes: true, + no: false +} + export interface GenerateConfig { path: string; - type: 'js' | 'ts' | 'ts-satisfies'; + type: ProjectType; pageExtension: string; scriptExtension: string; resources: Resource[]; diff --git a/packages/svelte-vscode/src/sveltekit/utils.ts b/packages/svelte-vscode/src/sveltekit/utils.ts index 25701c1be..83904f668 100644 --- a/packages/svelte-vscode/src/sveltekit/utils.ts +++ b/packages/svelte-vscode/src/sveltekit/utils.ts @@ -1,5 +1,6 @@ import * as path from 'path'; import { Uri, workspace } from 'vscode'; +import { IsSvelte5Plus, ProjectType } from './generateFiles/types' export async function fileExists(file: string) { try { @@ -24,9 +25,11 @@ export async function findFile(searchPath: string, fileName: string) { } } -export async function checkProjectType(path: string) { +export async function checkProjectType(path: string): Promise { const tsconfig = await findFile(path, 'tsconfig.json'); const jsconfig = await findFile(path, 'jsconfig.json'); + const svelteVersion = await getSvelteVersionFromPackageJson(); + const isSv5Plus = isSvelte5Plus(svelteVersion); const isTs = !!tsconfig && (!jsconfig || tsconfig.length >= jsconfig.length); if (isTs) { try { @@ -36,14 +39,45 @@ export async function checkProjectType(path: string) { const { version } = require(packageJSONPath); const [major, minor] = version.split('.'); if ((Number(major) === 4 && Number(minor) >= 9) || Number(major) > 4) { - return 'ts-satisfies'; + return isSv5Plus ? ProjectType.TS_SATISFIES_SV5 : ProjectType.TS_SATISFIES; } else { - return 'ts'; + return isSv5Plus ? ProjectType.TS_SV5 : ProjectType.TS; } } catch (e) { - return 'ts'; + return isSv5Plus ? ProjectType.TS_SV5 : ProjectType.TS; } } else { - return 'js'; + return isSv5Plus ? ProjectType.JS_SV5 : ProjectType.JS; } } + +export function isSvelte5Plus(version: string | undefined): IsSvelte5Plus { + if (!version) return IsSvelte5Plus.no; + + return version.split('.')[0] >= '5'; +} + +export async function getSvelteVersionFromPackageJson(): Promise { + const packageJsonList = await workspace.findFiles('**/package.json', '**/node_modules/**'); + + if (packageJsonList.length === 0) { + // We assume that the user has not setup their project yet + throw new Error('No package.json found'); + } + + for (const fileUri of packageJsonList) { + try { + const text = new TextDecoder().decode(await workspace.fs.readFile(fileUri)); + const pkg = JSON.parse(text); + const svelteVersion = pkg.devDependencies?.svelte ?? pkg.dependencies?.svelte; + + if (svelteVersion !== undefined) { + return svelteVersion; + } + } catch (error) { + console.error(error); + } + } + + throw new Error('No svelte version found'); +} From ab57c7bdf5335c98923496dac7987ca1c6d54da6 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:23:09 +0100 Subject: [PATCH 2/3] fixes --- .../sveltekit/generateFiles/templates/layout-server.ts | 4 ++-- .../src/sveltekit/generateFiles/templates/layout.ts | 4 ++-- .../src/sveltekit/generateFiles/templates/page-load.ts | 4 ++-- .../src/sveltekit/generateFiles/templates/page-server.ts | 4 ++-- .../src/sveltekit/generateFiles/templates/page.ts | 4 ++-- packages/svelte-vscode/src/sveltekit/utils.ts | 8 ++++---- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts index 24bb14fec..0954b5e65 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout-server.ts @@ -26,10 +26,10 @@ export const load = (async () => { const scriptTemplate: ReadonlyMap = new Map([ [ProjectType.TS_SV5, tsScriptTemplate], - [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsSatisfiesScriptTemplate], [ProjectType.JS_SV5, defaultScriptTemplate], [ProjectType.TS, tsScriptTemplate], - [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsSatisfiesScriptTemplate], [ProjectType.JS, defaultScriptTemplate] ]) diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts index 3e1fa4ce1..11651dbbf 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/layout.ts @@ -11,7 +11,7 @@ const tsSv5ScriptTemplate = ` `; @@ -26,7 +26,7 @@ const tsScriptTemplate = ` const jsSv5ScriptTemplate = ` `; diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts index 5d24854ff..c604e2495 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-load.ts @@ -26,10 +26,10 @@ export const load = (async () => { const scriptTemplate: ReadonlyMap = new Map([ [ProjectType.TS_SV5, tsScriptTemplate], - [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsSatisfiesScriptTemplate], [ProjectType.JS_SV5, defaultScriptTemplate], [ProjectType.TS, tsScriptTemplate], - [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsSatisfiesScriptTemplate], [ProjectType.JS, defaultScriptTemplate] ]) diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts index 41ae7ad55..56d1c5bdf 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page-server.ts @@ -26,10 +26,10 @@ export const load = (async () => { const scriptTemplate: ReadonlyMap = new Map([ [ProjectType.TS_SV5, tsScriptTemplate], - [ProjectType.TS_SATISFIES_SV5, tsScriptTemplate], + [ProjectType.TS_SATISFIES_SV5, tsSatisfiesScriptTemplate], [ProjectType.JS_SV5, defaultScriptTemplate], [ProjectType.TS, tsScriptTemplate], - [ProjectType.TS_SATISFIES, tsScriptTemplate], + [ProjectType.TS_SATISFIES, tsSatisfiesScriptTemplate], [ProjectType.JS, defaultScriptTemplate] ]) diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts index b67e0df8e..910493a35 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/page.ts @@ -11,7 +11,7 @@ const tsSv5ScriptTemplate = ` `; @@ -26,7 +26,7 @@ const tsScriptTemplate = ` const jsSv5ScriptTemplate = ` `; diff --git a/packages/svelte-vscode/src/sveltekit/utils.ts b/packages/svelte-vscode/src/sveltekit/utils.ts index 83904f668..7eeef64bc 100644 --- a/packages/svelte-vscode/src/sveltekit/utils.ts +++ b/packages/svelte-vscode/src/sveltekit/utils.ts @@ -1,3 +1,4 @@ +import { TextDecoder } from 'util'; import * as path from 'path'; import { Uri, workspace } from 'vscode'; import { IsSvelte5Plus, ProjectType } from './generateFiles/types' @@ -57,12 +58,11 @@ export function isSvelte5Plus(version: string | undefined): IsSvelte5Plus { return version.split('.')[0] >= '5'; } -export async function getSvelteVersionFromPackageJson(): Promise { +export async function getSvelteVersionFromPackageJson(): Promise { const packageJsonList = await workspace.findFiles('**/package.json', '**/node_modules/**'); if (packageJsonList.length === 0) { - // We assume that the user has not setup their project yet - throw new Error('No package.json found'); + return undefined; } for (const fileUri of packageJsonList) { @@ -79,5 +79,5 @@ export async function getSvelteVersionFromPackageJson(): Promise { } } - throw new Error('No svelte version found'); + return undefined; } From 00227bc321ffce53f1ca828de8feaa5d03d46b38 Mon Sep 17 00:00:00 2001 From: Alexandre Oger Date: Mon, 28 Oct 2024 11:56:22 +0100 Subject: [PATCH 3/3] format --- .../src/sveltekit/generateFiles/templates/error.ts | 5 ++--- .../src/sveltekit/generateFiles/templates/layout-load.ts | 3 +-- .../src/sveltekit/generateFiles/templates/layout-server.ts | 3 +-- .../src/sveltekit/generateFiles/templates/layout.ts | 2 +- .../src/sveltekit/generateFiles/templates/page-load.ts | 3 +-- .../src/sveltekit/generateFiles/templates/page-server.ts | 3 +-- .../src/sveltekit/generateFiles/templates/page.ts | 2 +- .../src/sveltekit/generateFiles/templates/server.ts | 3 +-- packages/svelte-vscode/src/sveltekit/generateFiles/types.ts | 6 +++--- packages/svelte-vscode/src/sveltekit/utils.ts | 2 +- 10 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts index 6d3fae224..5727bd010 100644 --- a/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts +++ b/packages/svelte-vscode/src/sveltekit/generateFiles/templates/error.ts @@ -8,7 +8,6 @@ const defaultScriptTemplate = `

{$page.status}: {$page.error.message}

`; - const tsScriptTemplate = `