Skip to content

Commit a8e0396

Browse files
authored
feat: commands & context menu nuxt and nitro utils
feat: commands & context menu nuxt and nitro utils
2 parents 34ced5c + c2d3b7c commit a8e0396

File tree

7 files changed

+152
-10
lines changed

7 files changed

+152
-10
lines changed

package.json

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@
180180
"command": "nuxtr.createPlugin",
181181
"when": "nuxtr.isNuxtProject"
182182
},
183+
{
184+
"command": "nuxtr.createUtil",
185+
"when": "nuxtr.isNuxtProject"
186+
},
183187
{
184188
"command": "nuxtr.createMiddleware",
185189
"when": "nuxtr.isNuxtProject"
@@ -403,6 +407,11 @@
403407
"when": "( nuxtr.isNuxtProject && resourceFilename =~ /(plugins.*)/) || ( nuxtr.isNuxtProject && resourceDirname =~ /(plugins.*)/ && explorerResourceIsFolder)",
404408
"group": "navigation"
405409
},
410+
{
411+
"command": "nuxtr.directCreateUtil",
412+
"when": "( nuxtr.isNuxtProject && resourceFilename =~ /(utils.*)/) || ( nuxtr.isNuxtProject && resourceDirname =~ /(utils.*)/ && explorerResourceIsFolder)",
413+
"group": "navigation"
414+
},
406415
{
407416
"command": "nuxtr.directCreateComposable",
408417
"when": "( nuxtr.isNuxtProject && resourceFilename =~ /(composables.*)/) || ( nuxtr.isNuxtProject && resourceDirname =~ /(composables.*)/ && explorerResourceIsFolder)",
@@ -599,18 +608,36 @@
599608
"category": "Nuxtr",
600609
"when": "nuxtr.isNuxtProject"
601610
},
611+
{
612+
"command": "nuxtr.createUtil",
613+
"title": "Create new Utility",
614+
"category": "Nuxtr",
615+
"when": "nuxtr.isNuxtProject"
616+
},
602617
{
603618
"command": "nuxtr.createNitroPlugin",
604619
"title": "Create new Nitro Plugin",
605620
"category": "Nuxtr",
606621
"when": "nuxtr.isNuxtProject"
607622
},
623+
{
624+
"command": "nuxtr.createNitroUtil",
625+
"title": "Create new Nitro Utility",
626+
"category": "Nuxtr",
627+
"when": "nuxtr.isNuxtProject"
628+
},
608629
{
609630
"command": "nuxtr.directCreatePlugin",
610631
"title": "New Plugin...",
611632
"category": "Nuxtr",
612633
"when": "nuxtr.isNuxtProject"
613634
},
635+
{
636+
"command": "nuxtr.directCreateUtil",
637+
"title": "New Utility...",
638+
"category": "Nuxtr",
639+
"when": "nuxtr.isNuxtProject"
640+
},
614641
{
615642
"command": "nuxtr.createMiddleware",
616643
"title": "Create new Middleware",
@@ -629,12 +656,6 @@
629656
"category": "Nuxtr",
630657
"when": "nuxtr.isNuxtProject"
631658
},
632-
{
633-
"command": "nuxtr.createNitroPlugin",
634-
"title": "Create new Nitro Plugin",
635-
"category": "Nuxtr",
636-
"when": "nuxtr.isNuxtProject"
637-
},
638659
{
639660
"command": "nuxtr.createNitroMiddleware",
640661
"title": "Create new Nitro Middleware",

src/commands/Nitro.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { window } from 'vscode'
22
import { projectSrcDirectory, createSubFolders, showSubFolderQuickPick, createFile, hasServerDir, createDir } from '../utils'
3-
import { nitroDefaultTemplate, nitroPluginTemplate } from '../templates/typeScriptFiles'
3+
import { nitroDefaultTemplate, nitroPluginTemplate, nitroUtilTemplate } from '../templates/typeScriptFiles'
44

55
let serverDir = `${projectSrcDirectory()}/${hasServerDir()}/`
66

@@ -83,6 +83,32 @@ const createNitroPlugin = () => {
8383
})
8484
}
8585

86+
const createNitroUtil = () => {
87+
const utilsDir = `${serverDir}/plugins`
88+
89+
window
90+
.showInputBox({
91+
prompt: 'What is your utility name?',
92+
placeHolder: 'Utility name',
93+
})
94+
.then((name) => {
95+
if (!name) { return }
96+
97+
98+
createDir('server')
99+
createDir('server/plugins')
100+
101+
let subFolders = createSubFolders(utilsDir, 'nitroUtil')
102+
103+
showSubFolderQuickPick({
104+
name,
105+
subFolders: subFolders,
106+
commandType: 'nitroUtil',
107+
content: nitroUtilTemplate
108+
})
109+
})
110+
}
111+
86112
const createNitroMiddleware = () => {
87113
const middlewareDir = `${serverDir}/middleware`
88114

@@ -159,5 +185,6 @@ export {
159185
createNitroRoute,
160186
directCreateNitroRoute,
161187
createNitroPlugin,
162-
createNitroMiddleware
188+
createNitroMiddleware,
189+
createNitroUtil
163190
}

src/commands/Util.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { window } from 'vscode'
2+
import { projectSrcDirectory, createSubFolders, showSubFolderQuickPick, createFile, createDir, hasServerDir } from '../utils'
3+
import { nuxtUtilTemplate, nitroUtilTemplate } from '../templates/typeScriptFiles'
4+
5+
const createUtil = () => {
6+
window
7+
.showInputBox({
8+
prompt: 'What is your utility name?',
9+
placeHolder: 'utility name',
10+
})
11+
.then((name) => {
12+
if (!name) {return}
13+
14+
let utilsDir = `${projectSrcDirectory()}/utils`
15+
16+
createDir('utils')
17+
18+
let subFolders = createSubFolders(utilsDir, 'nuxtUtil')
19+
20+
showSubFolderQuickPick({
21+
name,
22+
subFolders: subFolders,
23+
commandType: 'nuxtUtil',
24+
content: nuxtUtilTemplate(name)
25+
})
26+
27+
})
28+
}
29+
30+
31+
const directCreateUtil = (path: string) => {
32+
const serverDir = hasServerDir()
33+
34+
window
35+
.showInputBox({
36+
prompt: 'What is your utility name?',
37+
placeHolder: 'utility name',
38+
})
39+
.then((name) => {
40+
if (!name) {return}
41+
42+
let filePath = `${path}/${name}.ts`
43+
44+
45+
createFile({
46+
fileName: `${name}.ts`,
47+
content: filePath.includes(`${serverDir}`) ? nitroUtilTemplate : nuxtUtilTemplate(name),
48+
fullPath: filePath,
49+
})
50+
})
51+
}
52+
53+
export { createUtil, directCreateUtil }

src/commands/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createComposable, directCreateComposable } from './Composable'
44
import { createLayout, directCreateLayout } from './Layout'
55
import { createPlugin, directCreatePlugin } from './Plugin'
66
import { createMiddleware, directCreateMiddleware } from './Middleware'
7-
import { createNitroAPI, directCreateNitroAPI, createNitroRoute, directCreateNitroRoute, createNitroPlugin, createNitroMiddleware } from './Nitro'
7+
import { createNitroAPI, directCreateNitroAPI, createNitroRoute, directCreateNitroRoute, createNitroPlugin, createNitroMiddleware, createNitroUtil } from './Nitro'
88
import { projectStructure, appConfig, nuxtIgnore, nuxtRC } from './Structure'
99
import { openDocumentation, openModules } from './externalLinks'
1010
import { nuxtDev, nuxtBuild, nuxtGenerate, nuxtCleanUp, nuxtBuildModule, nuxtAnalyze, nuxtInfo, } from './TerminalCommands'
@@ -16,6 +16,7 @@ import { configureCSS } from './CSS'
1616
import { configureLinters } from './Linters'
1717
import { createPageTemplate, createLayoutTemplate, createFileFromTemplate, createEmptyFileTemplate } from './FileTemplates'
1818
import { nuxtConfigWatcher, directToggleDevTools } from './Devtools'
19+
import { createUtil, directCreateUtil } from './Util'
1920

2021

2122
const commands = {
@@ -37,6 +38,9 @@ const commands = {
3738
directCreateNitroRoute,
3839
createNitroPlugin,
3940
createNitroMiddleware,
41+
createUtil,
42+
createNitroUtil,
43+
directCreateUtil,
4044
projectStructure,
4145
openDocumentation,
4246
openModules,

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ const commandList = [
1616
{ command: 'nuxtr.createMiddleware', function: nuxtrCommands.createMiddleware },
1717
{ command: 'nuxtr.createNitroAPI', function: nuxtrCommands.createNitroAPI },
1818
{ command: 'nuxtr.createNitroPlugin', function: nuxtrCommands.createNitroPlugin },
19+
{ command: 'nuxtr.createNitroUtil', function: nuxtrCommands.createNitroUtil },
1920
{ command: 'nuxtr.createNitroMiddleware', function: nuxtrCommands.createNitroMiddleware },
2021
{ command: 'nuxtr.createNitroRoute', function: nuxtrCommands.createNitroRoute },
2122
{ command: 'nuxtr.createStore', function: nuxtrCommands.createStore },
23+
{ command: 'nuxtr.createUtil', function: nuxtrCommands.createUtil },
2224
{ command: 'nuxtr.projectStructure', function: nuxtrCommands.projectStructure },
2325
{ command: 'nuxtr.openDocumentation', function: nuxtrCommands.openDocumentation },
2426
{ command: 'nuxtr.openModules', function: nuxtrCommands.openModules },
@@ -52,6 +54,7 @@ const commandList = [
5254
{ command: 'nuxtr.directCreateNitroAPI', function: (filePath: Uri) => nuxtrCommands.directCreateNitroAPI(filePath.path) },
5355
{ command: 'nuxtr.directCreateNitroRoute', function: (filePath: Uri) => nuxtrCommands.directCreateNitroRoute(filePath.path) },
5456
{ command: 'nuxtr.directCreateStore', function: (filePath: Uri) => nuxtrCommands.directCreateStore(filePath.path) },
57+
{ command: 'nuxtr.directCreateUtil', function: (filePath: Uri) => nuxtrCommands.directCreateUtil(filePath.path) },
5558
];
5659

5760
// categorize commands and functions

src/templates/typeScriptFiles.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,24 @@ export const nuxtPluginTemplate = `export default defineNuxtPlugin((nuxtApp) =>
2828
export const nitroPluginTemplate = `export default defineNitroPlugin((nitroApp) => {
2929
3030
})
31-
`
31+
`
32+
33+
export const nuxtUtilTemplate = (name: string) => `export default () => {
34+
return 'Hello Util'
35+
}
36+
`
37+
38+
export const nitroUtilTemplate =`import type { EventHandler, EventHandlerRequest } from 'h3'
39+
40+
export const defineWrappedResponseHandler = <T extends EventHandlerRequest, D> (
41+
handler: EventHandler<T, D>
42+
): EventHandler<T, D> =>
43+
defineEventHandler<T>(async (event) => {
44+
try {
45+
return { response }
46+
} catch (err) {
47+
// Error handling
48+
return { err }
49+
}
50+
})
51+
`

src/utils/commands.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ const getCommandType = (commandType: string) => {
8484
extension: '.ts',
8585
};
8686
break;
87+
case 'nuxtUtil':
88+
type = {
89+
name: 'Utilities',
90+
path: `utils`,
91+
extension: '.ts',
92+
};
93+
break;
94+
case 'nitroUtil':
95+
type = {
96+
name: 'Utility',
97+
path: `${hasServerDir()}/utils`,
98+
extension: '.ts',
99+
};
100+
break;
87101
default:
88102
// show error message
89103
type = {

0 commit comments

Comments
 (0)