Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 5ef1356

Browse files
Diizzayypi0
andauthored
feat(nuxi): support mode flags for add command (#3921)
Co-authored-by: Pooya Parsa <[email protected]>
1 parent f3499d7 commit 5ef1356

File tree

3 files changed

+109
-20
lines changed

3 files changed

+109
-20
lines changed

docs/content/3.api/5.commands/add.md

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,84 @@ Option | Default | Description
1313
`--cwd` | `.` | The directory of the target application.
1414
`--force` | `false` | Force override file if it already exists.
1515

16-
**Example:**
16+
**Modifiers:**
1717

18-
```{bash}
18+
Some templates support additional modifer flags to add a suffix (like `.client` or `.get`) to their name.
19+
20+
**Example:** `npx nuxi add plugin sockets --client` generates `/plugins/sockets.client.ts`.
21+
22+
## `nuxi add component`
23+
24+
* Modifier flags: `--mode client|server` or `--client` or `--server`
25+
26+
Example:
27+
28+
```bash
29+
# Generates `components/TheHeader.vue`
1930
npx nuxi add component TheHeader
2031
```
2132

22-
The `add` command generates new elements:
33+
## `nuxi add composable`
34+
35+
Example:
36+
37+
```bash
38+
# Generates `composables/foo.ts`
39+
npx nuxi add composable foo
40+
```
2341

24-
* **component**: `npx nuxi add component TheHeader`
25-
* **composable**: `npx nuxi add composable foo`
26-
* **layout**: `npx nuxi add layout custom`
27-
* **plugin**: `npx nuxi add plugin analytics`
28-
* **page**: `npx nuxi add page about` or `npx nuxi add page "category/[id]"`
29-
* **middleware**: `npx nuxi add middleware auth`
30-
* **api**: `npx nuxi add api hello` (CLI will generate file under `/server/api`)
42+
## `nuxi add layout`
43+
44+
Example:
45+
46+
```bash
47+
# Generates `layouts/custom.ts`
48+
npx nuxi add layout custom
49+
```
50+
51+
## `nuxi add plugin`
52+
53+
* Modifier flags: `--mode client|server` or `--client`or `--server`
54+
55+
Example:
56+
57+
```bash
58+
# Generates `plugins/analytics.ts`
59+
npx nuxi add plugin analytics
60+
```
61+
62+
## `nuxi add page`
63+
64+
Example:
65+
66+
```bash
67+
# Generates `pages/about.vue`
68+
npx nuxi add page about
69+
```
70+
71+
```bash
72+
# Generates `pages/category/[id].vue`
73+
npx nuxi add page "category/[id]"
74+
```
75+
76+
## `nuxi add middleware`
77+
78+
* Modifier flags: `--global`
79+
80+
Example:
81+
82+
```bash
83+
# Generates `middleware/auth.ts`
84+
npx nuxi add middleware auth
85+
```
86+
87+
## `nuxi add api`
88+
89+
* Modifier flags: `--method=connect|delete|get|head|options|patch|post|put|trace` or `--get`, `--post`, etc.
90+
91+
Example:
92+
93+
```bash
94+
# Generates `server/api/hello.ts`
95+
npx nuxi add api hello
96+
```

packages/nuxi/src/commands/add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default defineNuxtCommand({
3434
const config = await kit.loadNuxtConfig({ cwd })
3535

3636
// Resolve template
37-
const res = templates[template]({ name })
37+
const res = templates[template]({ name, args })
3838

3939
// Resolve full path to generated file
4040
const path = resolve(config.srcDir, res.path)

packages/nuxi/src/utils/templates.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import { upperFirst } from 'scule'
22

3+
interface TemplateOptions {
4+
name: string,
5+
args: Record<string, any>
6+
}
7+
38
interface Template {
4-
(options: { name: string }): { path: string, contents: string }
9+
(options: TemplateOptions): { path: string, contents: string }
510
}
611

7-
const api: Template = ({ name }) => ({
8-
path: `server/api/${name}.ts`,
12+
const httpMethods = ['connect', 'delete', 'get', 'head', 'options', 'post', 'put', 'trace', 'patch']
13+
const api: Template = ({ name, args }) => ({
14+
path: `server/api/${name}${applySuffix(args, httpMethods, 'method')}.ts`,
915
contents: `
1016
export default defineEventHandler((event) => {
1117
return 'Hello ${name}'
1218
})
1319
`
1420
})
1521

16-
const plugin: Template = ({ name }) => ({
17-
path: `plugins/${name}.ts`,
22+
const plugin: Template = ({ name, args }) => ({
23+
path: `plugins/${name}${applySuffix(args, ['client', 'server'], 'mode')}.ts`,
1824
contents: `
1925
export default defineNuxtPlugin((nuxtApp) => {})
2026
`
2127
})
2228

23-
const component: Template = ({ name }) => ({
24-
path: `components/${name}.vue`,
29+
const component: Template = ({ name, args }) => ({
30+
path: `components/${name}${applySuffix(args, ['client', 'server'], 'mode')}.vue`,
2531
contents: `
2632
<script lang="ts" setup></script>
2733
@@ -47,8 +53,8 @@ export const ${nameWithUsePrefix} = () => {
4753
}
4854
}
4955

50-
const middleware: Template = ({ name }) => ({
51-
path: `middleware/${name}.ts`,
56+
const middleware: Template = ({ name, args }) => ({
57+
path: `middleware/${name}${applySuffix(args, ['global'])}.ts`,
5258
contents: `
5359
export default defineNuxtRouteMiddleware((to, from) => {})
5460
`
@@ -94,3 +100,20 @@ export const templates = {
94100
layout,
95101
page
96102
} as Record<string, Template>
103+
104+
// -- internal utils --
105+
106+
function applySuffix (args: TemplateOptions['args'], suffixes: string[], unwrapFrom?: string): string {
107+
let suffix = ''
108+
// --client
109+
for (const s of suffixes) {
110+
if (args[s]) {
111+
suffix += '.' + s
112+
}
113+
}
114+
// --mode=server
115+
if (unwrapFrom && args[unwrapFrom] && suffixes.includes(args[unwrapFrom])) {
116+
suffix += '.' + args[unwrapFrom]
117+
}
118+
return suffix
119+
}

0 commit comments

Comments
 (0)