Skip to content

Commit

Permalink
(feat) create sveltekit route files (#1620)
Browse files Browse the repository at this point in the history
Adds commands and a context menu entries for creating SvelteKit route files + some snippets
  • Loading branch information
david-plugge committed Sep 15, 2022
1 parent 678e76d commit 513f935
Show file tree
Hide file tree
Showing 19 changed files with 666 additions and 0 deletions.
93 changes: 93 additions & 0 deletions packages/svelte-vscode/package.json
Expand Up @@ -538,6 +538,42 @@
{
"command": "svelte.typescript.findComponentReferences",
"title": "Svelte: Find Component References"
},
{
"command": "svelte.kit.generateMultipleFiles",
"title": "SvelteKit: Generate files"
},
{
"command": "svelte.kit.generatePage",
"title": "SvelteKit: Generate Page"
},
{
"command": "svelte.kit.generatePageLoad",
"title": "SvelteKit: Generate Page load"
},
{
"command": "svelte.kit.generatePageServerLoad",
"title": "SvelteKit: Generate Page server load"
},
{
"command": "svelte.kit.generateLayout",
"title": "SvelteKit: Generate Layout"
},
{
"command": "svelte.kit.generateLayoutLoad",
"title": "SvelteKit: Generate Layout load"
},
{
"command": "svelte.kit.generateLayoutServerLoad",
"title": "SvelteKit: Generate Layout server load"
},
{
"command": "svelte.kit.generateServer",
"title": "SvelteKit: Generate Endpoint"
},
{
"command": "svelte.kit.generateError",
"title": "SvelteKit: Generate Error"
}
],
"menus": {
Expand Down Expand Up @@ -599,9 +635,58 @@
"command": "svelte.typescript.findComponentReferences",
"when": "resourceLangId == svelte",
"group": "4_search"
},
{
"when": "explorerResourceIsFolder",
"submenu": "sveltekit2files",
"group": "1_SvelteKit2files"
}
],
"sveltekit2files": [
{
"command": "svelte.kit.generateMultipleFiles",
"group": "0_SvelteKit_Multiple"
},
{
"command": "svelte.kit.generatePage",
"group": "1_SvelteKit_Page"
},
{
"command": "svelte.kit.generatePageLoad",
"group": "1_SvelteKit_Page"
},
{
"command": "svelte.kit.generatePageServerLoad",
"group": "1_SvelteKit_Page"
},
{
"command": "svelte.kit.generateLayout",
"group": "3_SvelteKit_Layout"
},
{
"command": "svelte.kit.generateLayoutLoad",
"group": "3_SvelteKit_Layout"
},
{
"command": "svelte.kit.generateLayoutServerLoad",
"group": "3_SvelteKit_Layout"
},
{
"command": "svelte.kit.generateServer",
"group": "2_SvelteKit_Server"
},
{
"command": "svelte.kit.generateError",
"group": "4_SvelteKit_Error"
}
]
},
"submenus": [
{
"id": "sveltekit2files",
"label": "SvelteKit Files"
}
],
"breakpoints": [
{
"language": "svelte"
Expand All @@ -611,6 +696,14 @@
{
"language": "svelte",
"path": "./snippets/svelte.json"
},
{
"language": "javascript",
"path": "./snippets/javascript.json"
},
{
"language": "typescript",
"path": "./snippets/typescript.json"
}
]
},
Expand Down
43 changes: 43 additions & 0 deletions packages/svelte-vscode/snippets/javascript.json
@@ -0,0 +1,43 @@
{
"SvelteKit Endpoint": {
"prefix": "kitEndpoint",
"description": "SvelteKit Endpoint",
"body": [
"/** @type {import('./\\$types').RequestHandler} */",
"export async function ${1|GET,POST,PUT,PATCH,DELETE|}($2) {",
"\t$3",
"\treturn new Response();",
"}"
]
},
"SvelteKit Actions": {
"prefix": "kitActions",
"description": "SvelteKit Actions",
"body": [
"/** @type {import('./\\$types').Actions} */",
"export const actions = {",
"\t$1",
"};"
]
},
"SvelteKit Load": {
"prefix": "kitLoad",
"description": "SvelteKit Load",
"body": [
"/** @type {import('./\\$types').${1|PageLoad,PageServerLoad,LayoutLoad,LayoutServerLoad|}} */",
"export async function load($2) {",
"\t$3",
"}"
]
},
"SvelteKit Param Matcher": {
"prefix": "kitParamMatcher",
"description": "SvelteKit Param Matcher",
"body": [
"/** @type {import('./\\$types').ParamMatcher */",
"export function match(param) {",
"\treturn $1;",
"}"
]
}
}
37 changes: 37 additions & 0 deletions packages/svelte-vscode/snippets/typescript.json
@@ -0,0 +1,37 @@
{
"SvelteKit Endpoint": {
"prefix": "kitEndpoint",
"description": "SvelteKit Endpoint",
"body": [
"export const ${1|GET,POST,PUT,PATCH,DELETE|}: RequestHandler = async ($2) => {",
"\t$3",
"\treturn new Response();",
"};"
]
},
"SvelteKit Actions": {
"prefix": "kitActions",
"description": "SvelteKit Actions",
"body": ["export const actions: Actions = {", "\t$1", "};"]
},
"SvelteKit Load": {
"prefix": "kitLoad",
"description": "SvelteKit Load",
"body": [
"export const load: ${1|PageLoad,PageServerLoad,LayoutLoad,LayoutServerLoad|} = async ($2) => {",
"\t$3",
"};"
]
},
"SvelteKit Param Matcher": {
"prefix": "kitParamMatcher",
"description": "SvelteKit Param Matcher",
"body": [
"import type { ParamMatcher } from '@sveltejs/kit';",
"",
"export const match: ParamMatcher = (param) => {",
"\treturn $1;",
"};"
]
}
}
4 changes: 4 additions & 0 deletions packages/svelte-vscode/src/extension.ts
Expand Up @@ -31,7 +31,9 @@ import { EMPTY_ELEMENTS } from './html/htmlEmptyTagsShared';
import { TsPlugin } from './tsplugin';
import { addFindComponentReferencesListener } from './typescript/findComponentReferences';
import { addFindFileReferencesListener } from './typescript/findFileReferences';
import { setupSvelteKit } from './sveltekit';

// eslint-disable-next-line @typescript-eslint/no-namespace
namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, string, any> = new RequestType(
'html/tag'
Expand Down Expand Up @@ -60,6 +62,8 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(onTextDocumentListener);
}

setupSvelteKit(context);

// This API is considered private and only exposed for experimenting.
// Interface may change at any time. Use at your own risk!
return {
Expand Down
12 changes: 12 additions & 0 deletions packages/svelte-vscode/src/sveltekit/generateFiles/commands.ts
@@ -0,0 +1,12 @@
import { CommandType, ResourceType } from './types';

export const addResourceCommandMap = new Map<CommandType, ResourceType>([
[CommandType.PAGE, ResourceType.PAGE],
[CommandType.PAGE_LOAD, ResourceType.PAGE_LOAD],
[CommandType.PAGE_SERVER, ResourceType.PAGE_SERVER],
[CommandType.LAYOUT, ResourceType.LAYOUT],
[CommandType.LAYOUT_LOAD, ResourceType.LAYOUT_LOAD],
[CommandType.LAYOUT_SERVER, ResourceType.LAYOUT_SERVER],
[CommandType.SERVER, ResourceType.SERVER],
[CommandType.ERROR, ResourceType.ERROR]
]);
38 changes: 38 additions & 0 deletions packages/svelte-vscode/src/sveltekit/generateFiles/generate.ts
@@ -0,0 +1,38 @@
import { FileType, GenerateConfig } from './types';
import { join } from 'path';
import { Position, Uri, window, workspace, WorkspaceEdit } from 'vscode';

export async function generateResources(config: GenerateConfig) {
workspace.fs.createDirectory(Uri.file(config.path));
const edit = new WorkspaceEdit();

for (const resource of config.resources) {
const ext = resource.type === FileType.PAGE ? config.pageExtension : config.scriptExtension;
const filepath = join(config.path, `${resource.filename}.${ext}`);

const uri = Uri.file(filepath);
edit.createFile(uri, {
overwrite: false,
ignoreIfExists: true
});

const data = await resource.generate(config);
edit.insert(uri, new Position(0, 0), data);
}

await workspace.applyEdit(edit);

// save documents and open the first
await Promise.all(
edit.entries().map(async ([uri], i) => {
const doc = workspace.textDocuments.find((t) => t.uri.path === uri.path);
if (doc) {
await doc?.save();
if (i === 0) {
await workspace.openTextDocument(uri);
await window.showTextDocument(doc);
}
}
})
);
}

0 comments on commit 513f935

Please sign in to comment.