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

feat(nuxi): support mode flags for add command #3921

Merged
merged 31 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e2108c3
fix(nuxi): prevent double extension
Diizzayy Mar 27, 2022
5d3db5a
feat(nuxi): support mode flags for `add` command
Diizzayy Mar 27, 2022
46c8674
remove global flag
Diizzayy Mar 27, 2022
6bdc666
fix(docs): clarify behavior
Diizzayy Mar 27, 2022
d119291
Merge remote-tracking branch 'upstream/main' into feat/cli-mode-flags
Diizzayy Mar 28, 2022
818fa12
provide `mode` types
Diizzayy Mar 28, 2022
6ee5b1d
Merge branch 'feat/cli-mode-flags' of github.com:Diizzayy/framework i…
Diizzayy Mar 28, 2022
a5bd655
improve example
Diizzayy Mar 28, 2022
030119d
apply component template mode in `#1919`
Diizzayy Mar 28, 2022
95110a0
improve mode handling
Diizzayy Mar 28, 2022
0fb7502
Merge branch 'main' into feat/cli-mode-flags
Diizzayy Mar 28, 2022
bbd17f1
Update docs/content/3.docs/1.usage/8.cli.md
pi0 Mar 29, 2022
1da4d41
retain naming
Diizzayy Mar 29, 2022
7d75954
Merge branch 'feat/cli-mode-flags' of github.com:Diizzayy/framework i…
Diizzayy Mar 29, 2022
7aa236b
Merge remote-tracking branch 'upstream/main' into feat/cli-mode-flags
Diizzayy Apr 7, 2022
6331149
Merge remote-tracking branch 'upstream/main' into feat/cli-mode-flags
Diizzayy Apr 7, 2022
74bfbff
Merge branch 'main' into feat/cli-mode-flags
Diizzayy Apr 21, 2022
2c9c444
Merge branch 'main' into feat/cli-mode-flags
Diizzayy May 15, 2022
6f395f1
Merge branch 'main' into feat/cli-mode-flags
Diizzayy Jun 1, 2022
5f712cc
chore(deps): update all non-major dependencies (#6880)
renovate[bot] Aug 23, 2022
ff58487
Merge branch 'main' into feat/cli-mode-flags
pi0 Aug 23, 2022
d5a5d37
feat: support more modifiers via generic args
pi0 Aug 23, 2022
4b16167
update docs
pi0 Aug 23, 2022
3fe8218
docs: add note about modifier
pi0 Aug 23, 2022
48fe8e7
use or
pi0 Aug 23, 2022
93632ce
improve docs
pi0 Aug 23, 2022
031e984
fix lint
pi0 Aug 23, 2022
c2dca8a
chore: update hookable
pi0 Aug 23, 2022
2288dc8
Merge branch 'main' into feat/cli-mode-flags
pi0 Aug 23, 2022
64402e2
manually revert rebase issues
pi0 Aug 23, 2022
f25a094
also revert yarn.lock
pi0 Aug 23, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/content/3.api/5.commands/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Option | Default | Description
`--cwd` | `.` | The directory of the target application.
`--force` | `false` | Force override file if it already exists.

Some templates support additional modifer flags to add a suffix (like `.client` or `.get`) to their name.

**Example:**

```{bash}
Expand All @@ -22,9 +24,13 @@ npx nuxi add component TheHeader
The `add` command generates new elements:

* **component**: `npx nuxi add component TheHeader`
* Modifier flags: `--mode client|server` or `--client` or `--server`
* **composable**: `npx nuxi add composable foo`
* **layout**: `npx nuxi add layout custom`
* **plugin**: `npx nuxi add plugin analytics`
* **plugin**: `npx nuxi add plugin analytics` or `npx nuxi add plugin sockets --client` (generates `/plugins/sockets.client.ts`)
* Modifier flags: `--mode client|server` or `--client`or `--server`
* **page**: `npx nuxi add page about` or `npx nuxi add page "category/[id]"`
* **middleware**: `npx nuxi add middleware auth`
* Modifier flags: `--global`
* **api**: `npx nuxi add api hello` (CLI will generate file under `/server/api`)
* Modifier flags: `--method=connect|delete|get|head|options|patch|post|put|trace` or `--get`, `--post`, etc.
2 changes: 1 addition & 1 deletion packages/nuxi/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default defineNuxtCommand({
const config = await kit.loadNuxtConfig({ cwd })

// Resolve template
const res = templates[template]({ name })
const res = templates[template]({ name, args })

// Resolve full path to generated file
const path = resolve(config.srcDir, res.path)
Expand Down
41 changes: 32 additions & 9 deletions packages/nuxi/src/utils/templates.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { upperFirst } from 'scule'

interface TemplateOptions {
name: string,
args: Record<string, any>
}

interface Template {
(options: { name: string }): { path: string, contents: string }
(options: TemplateOptions): { path: string, contents: string }
}

const api: Template = ({ name }) => ({
path: `server/api/${name}.ts`,
const httpMethods = ['connect', 'delete', 'get', 'head', 'options', 'post', 'put', 'trace', 'patch']
const api: Template = ({ name, args }) => ({
path: `server/api/${name}${applySuffix(args, httpMethods, 'method')}.ts`,
contents: `
export default defineEventHandler((event) => {
return 'Hello ${name}'
})
`
})

const plugin: Template = ({ name }) => ({
path: `plugins/${name}.ts`,
const plugin: Template = ({ name, args }) => ({
path: `plugins/${name}${applySuffix(args, ['client', 'server'], 'mode')}.ts`,
contents: `
export default defineNuxtPlugin((nuxtApp) => {})
`
})

const component: Template = ({ name }) => ({
path: `components/${name}.vue`,
const component: Template = ({ name, args }) => ({
path: `components/${name}${applySuffix(args, ['client', 'server'], 'mode')}.vue`,
contents: `
<script lang="ts" setup></script>

Expand All @@ -47,8 +53,8 @@ export const ${nameWithUsePrefix} = () => {
}
}

const middleware: Template = ({ name }) => ({
path: `middleware/${name}.ts`,
const middleware: Template = ({ name, args }) => ({
path: `middleware/${name}${applySuffix(args, ['global'])}.ts`,
contents: `
export default defineNuxtRouteMiddleware((to, from) => {})
`
Expand Down Expand Up @@ -94,3 +100,20 @@ export const templates = {
layout,
page
} as Record<string, Template>

// -- internal utils --

function applySuffix (args: TemplateOptions['args'], suffixes: string[], unwrapFrom?: string): string {
let suffix = ''
// --client
for (const s of suffixes) {
if (args[s]) {
suffix += '.' + s
}
}
// --mode=server
if (unwrapFrom && args[unwrapFrom] && suffixes.includes(args[unwrapFrom])) {
suffix += '.' + args[unwrapFrom]
}
return suffix
}
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"globby": "^13.1.2",
"h3": "^0.7.16",
"hash-sum": "^2.0.0",
"hookable": "^5.1.2",
"hookable": "^5.2.0",
"knitwork": "^0.1.2",
"magic-string": "^0.26.2",
"mlly": "^0.5.14",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ohmyfetch": "^0.4.18"
},
"devDependencies": {
"playwright": "^1.25.0",
"playwright": "^1.25.1",
"unbuild": "latest",
"vitest": "~0.19.1"
},
Expand Down
30 changes: 15 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ __metadata:
get-port-please: ^2.6.1
jiti: ^1.14.0
ohmyfetch: ^0.4.18
playwright: ^1.25.0
playwright: ^1.25.1
unbuild: latest
vitest: ~0.19.1
peerDependencies:
Expand Down Expand Up @@ -7870,10 +7870,10 @@ __metadata:
languageName: node
linkType: hard

"hookable@npm:^5.1.2":
version: 5.1.2
resolution: "hookable@npm:5.1.2"
checksum: 1b3fb622a26d34c9befe8991cf9ffcb575334ef66e151291bc4b3161e1a609365983d258020571bce4b52226d9b5880150d27ba9f4e4029734e5cd37ac31aa39
"hookable@npm:^5.2.0":
version: 5.2.0
resolution: "hookable@npm:5.2.0"
checksum: d39c7a22d7a48572bb644b69df8ec9ef72a180ce5118b3f6447d46c41de8c413624162549b356112fdcb7c69c00dec9359ffacba2540f3ccf071806000e38bb1
languageName: node
linkType: hard

Expand Down Expand Up @@ -10294,7 +10294,7 @@ __metadata:
globby: ^13.1.2
h3: ^0.7.16
hash-sum: ^2.0.0
hookable: ^5.1.2
hookable: ^5.2.0
knitwork: ^0.1.2
magic-string: ^0.26.2
mlly: ^0.5.14
Expand Down Expand Up @@ -10972,23 +10972,23 @@ __metadata:
languageName: node
linkType: hard

"playwright-core@npm:1.25.0":
version: 1.25.0
resolution: "playwright-core@npm:1.25.0"
"playwright-core@npm:1.25.1":
version: 1.25.1
resolution: "playwright-core@npm:1.25.1"
bin:
playwright: cli.js
checksum: 78a9c8857428c8e9fe3a2061672a2e9ce0106afec473281d3a6389ec29918fd1d29c9d2792edde90bfd92c4477151fbd2d8f9f5061fbddaf8c6995a91c9cf2de
checksum: 34d5602816f73d68de3c022423ef64f0214f5e9827c584bffa608ecf66a9058f2f578e037bd7cbe7f98be53d5b01602f1f98aec73db5b475d8ab22417c803db5
languageName: node
linkType: hard

"playwright@npm:^1.25.0":
version: 1.25.0
resolution: "playwright@npm:1.25.0"
"playwright@npm:^1.25.1":
version: 1.25.1
resolution: "playwright@npm:1.25.1"
dependencies:
playwright-core: 1.25.0
playwright-core: 1.25.1
bin:
playwright: cli.js
checksum: f2a0c32d463dbc4a36c1c5e56f67a4daa3a9ab7cf4267453b561d677654cc69e6c8e18ae739cd112fd765e51311cf273c8c1d26d90d9880ff21c2b24c1edccae
checksum: 39745f159bac378234aa1ec9a3ebbc474876fc5907256f5365a5c844a990164c2ebf8a721bc0e738d74e83a789e5043af6a07e08f8a5be15a7cf905dd0dbc4f0
languageName: node
linkType: hard

Expand Down