Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) create sveltekit route files (#1620)
Adds commands and a context menu entries for creating SvelteKit route files + some snippets
- Loading branch information
1 parent
678e76d
commit 513f935
Showing
19 changed files
with
666 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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;", | ||
"}" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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;", | ||
"};" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/svelte-vscode/src/sveltekit/generateFiles/commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
38
packages/svelte-vscode/src/sveltekit/generateFiles/generate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
}) | ||
); | ||
} |
Oops, something went wrong.