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 4 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
4 changes: 3 additions & 1 deletion docs/content/3.docs/1.usage/8.cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ Option | Default | Description
`NAME` | - | Specify a name of the file that will be created.
`--cwd` | `.` | The directory of the target application.
`--force` | `false` | Force override file if already exsits.
`--client` | - | When applicable, this flag indicates that the entity is client only.
`--server` | - | When applicable, this flag indicates that the entity is server only.

**Example:**

Expand All @@ -127,7 +129,7 @@ The `add` command generates new elements:
* **component**: `npx nuxi add component TheHeader`
* **composable**: `npx nuxi add composable foo`
* **layout**: `npx nuxi add layout custom`
* **plugin**: `npx nuxi add plugin analytics`
* **plugin**: `npx nuxi add plugin firebase --client` (generates `/plugins/firebase.client.ts`)
* **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`)
18 changes: 16 additions & 2 deletions packages/nuxi/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default defineNuxtCommand({
const cwd = resolve(args.cwd || '.')

const template = args._[0]
const name = args._[1]
let name = args._[1]
let mode = (args.client && 'client') || (args.server && 'server')

// Validate template name
if (!templates[template]) {
Expand All @@ -33,8 +34,21 @@ export default defineNuxtCommand({
const kit = await loadKit(cwd)
const config = await kit.loadNuxtConfig({ cwd })

const extRegex = /\.[a-z]+$/
const extProvided = name.match(extRegex)?.[0]
const modeProvided = name.match(/\.(client|server)/)?.[0]

if (extProvided && extProvided !== modeProvided) {
name = name.replace(extRegex, '')
}

if (modeProvided) {
name = name.replace(modeProvided, '')
if (!mode) { mode = modeProvided.replace(/\./, '') }
}

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

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

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

const api: Template = ({ name }) => ({
Expand All @@ -15,27 +15,34 @@ export default defineHandle((req, res) => {
`
})

const plugin: Template = ({ name }) => ({
path: `plugins/${name}.ts`,
contents: `
const plugin: Template = (opts) => {
opts.name = opts.mode ? `${opts.name}.${opts.mode}` : opts.name
return {
path: `plugins/${opts.name}.ts`,
contents: `
export default defineNuxtPlugin((nuxtApp) => {})
`
})
}
}

const component: Template = ({ name }) => ({
path: `components/${name}.vue`,
contents: `
const component: Template = (opts) => {
// Enable when https://github.com/nuxt/framework/pull/1919 is merged
// opts.name = opts.mode ? `${opts.name}.${opts.mode}` : opts.name
return {
path: `components/${opts.name}.vue`,
contents: `
<script lang="ts" setup></script>

<template>
<div>
Component: ${name}
Component: ${opts.name}
</div>
</template>

<style scoped></style>
`
})
}
}

const composable: Template = ({ name }) => {
const nameWithUsePrefix = name.startsWith('use') ? name : `use${upperFirst(name)}`
Expand Down