Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ nuxt add nuxt-shiki
nuxt add nuxt-seo-kit
```

## Custom templates

To override a built-in `add-template` template, add a matching file to the project `stubs` directory. For example, `stubs/component.vue` is used instead of the default component contents when running `nuxt add-template component <name>`.

## Update Notifications

Once a day, `nuxt/cli` checks in the background whether a newer version of Nuxt has been released and, if so, prints a short nudge to run `nuxt upgrade` after the command finishes. The `latest` dist-tag is read from the registry configured in your `.npmrc` and cached in your user-level `.nuxtrc`.
Expand Down
11 changes: 10 additions & 1 deletion packages/nuxt-cli/src/commands/add-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { styleText } from 'node:util'

import { intro, outro } from '@clack/prompts'
import { defineCommand } from 'citty'
import { dirname, resolve } from 'pathe'
import { dirname, extname, resolve } from 'pathe'

import { loadKit } from '../utils/kit'
import { logger } from '../utils/logger'
Expand Down Expand Up @@ -99,6 +99,15 @@ export default defineCommand({
const kit = await loadKit(cwd)
const config = await kit.loadNuxtConfig({ cwd })
const res = templates[templateName]({ name, args: ctx.args, nuxtOptions: config })
const stubPath = resolve(config.rootDir, 'stubs', `${templateName}${extname(res.path)}`)
try {
res.contents = await fsp.readFile(stubPath, 'utf8')
}
catch (error) {
if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) {
throw error
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const parentDir = dirname(res.path)
const createdDir = await fsp.mkdir(parentDir, { recursive: true })
if (createdDir) {
Expand Down
11 changes: 10 additions & 1 deletion packages/nuxt-cli/test/unit/commands/add-template.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
Expand Down Expand Up @@ -33,6 +33,15 @@ describe('add-template command', () => {
expect(await readFile(path, 'utf8')).toMatch(/\n$/)
})

it('uses a project stub when one exists for the template', async () => {
await mkdir(join(cwd, 'stubs'))
await writeFile(join(cwd, 'stubs/component.vue'), '<template>custom component</template>\n')

await run('component', 'user-card')

await expect(readFile(join(cwd, 'components/user-card.vue'), 'utf8')).resolves.toBe('<template>custom component</template>\n')
})

it('exposes template-specific options', async () => {
await run('api', 'users', '--method', 'get')
await run('component', 'island', '--mode', 'client')
Expand Down
Loading