Skip to content
This repository has been 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 all 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
86 changes: 76 additions & 10 deletions docs/content/3.api/5.commands/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,84 @@ Option | Default | Description
`--cwd` | `.` | The directory of the target application.
`--force` | `false` | Force override file if it already exists.

**Example:**
**Modifiers:**

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

**Example:** `npx nuxi add plugin sockets --client` generates `/plugins/sockets.client.ts`.

## `nuxi add component`

* Modifier flags: `--mode client|server` or `--client` or `--server`

Example:

```bash
# Generates `components/TheHeader.vue`
npx nuxi add component TheHeader
```

The `add` command generates new elements:
## `nuxi add composable`

Example:

```bash
# Generates `composables/foo.ts`
npx nuxi add composable foo
```

* **component**: `npx nuxi add component TheHeader`
* **composable**: `npx nuxi add composable foo`
* **layout**: `npx nuxi add layout custom`
* **plugin**: `npx nuxi add plugin analytics`
* **page**: `npx nuxi add page about` or `npx nuxi add page "category/[id]"`
* **middleware**: `npx nuxi add middleware auth`
* **api**: `npx nuxi add api hello` (CLI will generate file under `/server/api`)
## `nuxi add layout`

Example:

```bash
# Generates `layouts/custom.ts`
npx nuxi add layout custom
```

## `nuxi add plugin`

* Modifier flags: `--mode client|server` or `--client`or `--server`

Example:

```bash
# Generates `plugins/analytics.ts`
npx nuxi add plugin analytics
```

## `nuxi add page`

Example:

```bash
# Generates `pages/about.vue`
npx nuxi add page about
```

```bash
# Generates `pages/category/[id].vue`
npx nuxi add page "category/[id]"
```

## `nuxi add middleware`

* Modifier flags: `--global`

Example:

```bash
# Generates `middleware/auth.ts`
npx nuxi add middleware auth
```

## `nuxi add api`

* Modifier flags: `--method=connect|delete|get|head|options|patch|post|put|trace` or `--get`, `--post`, etc.

Example:

```bash
# Generates `server/api/hello.ts`
npx nuxi add api hello
```
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
}