Skip to content

Commit 562abd5

Browse files
danielroeatinux
andauthored
feat(add): move module add -> add and add new template-add command (#1176)
Co-authored-by: Sébastien Chopin <[email protected]>
1 parent 1bccf8b commit 562abd5

File tree

9 files changed

+95
-43
lines changed

9 files changed

+95
-43
lines changed

knip.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"@clack/prompts",
3333
"c12",
3434
"confbox",
35-
"consola",
3635
"copy-paste",
3736
"debug",
3837
"defu",

packages/nuxi/src/commands/add.ts renamed to packages/nuxi/src/commands/add-template.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import { dirname, extname, resolve } from 'pathe'
99
import { loadKit } from '../utils/kit'
1010
import { logger } from '../utils/logger'
1111
import { relativeToProcess } from '../utils/paths'
12-
import { templates } from '../utils/templates/index'
12+
import { templateNames, templates } from '../utils/templates/index'
1313
import { cwdArgs, logLevelArgs } from './_shared'
1414

15-
const templateNames = Object.keys(templates)
16-
1715
export default defineCommand({
1816
meta: {
19-
name: 'add',
17+
name: 'add-template',
2018
description: 'Create a new template file.',
2119
},
2220
args: {

packages/nuxi/src/commands/index.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import type { CommandDef } from 'citty'
33
const _rDefault = (r: any) => (r.default || r) as Promise<CommandDef>
44

55
export const commands = {
6-
add: () => import('./add').then(_rDefault),
7-
analyze: () => import('./analyze').then(_rDefault),
8-
build: () => import('./build').then(_rDefault),
9-
cleanup: () => import('./cleanup').then(_rDefault),
10-
_dev: () => import('./dev-child').then(_rDefault),
11-
dev: () => import('./dev').then(_rDefault),
12-
devtools: () => import('./devtools').then(_rDefault),
13-
generate: () => import('./generate').then(_rDefault),
14-
info: () => import('./info').then(_rDefault),
15-
init: () => import('./init').then(_rDefault),
16-
module: () => import('./module').then(_rDefault),
17-
prepare: () => import('./prepare').then(_rDefault),
18-
preview: () => import('./preview').then(_rDefault),
19-
start: () => import('./preview').then(_rDefault),
20-
test: () => import('./test').then(_rDefault),
21-
typecheck: () => import('./typecheck').then(_rDefault),
22-
upgrade: () => import('./upgrade').then(_rDefault),
6+
'add': () => import('./module/add').then(_rDefault),
7+
'add-template': () => import('./add-template').then(_rDefault),
8+
'analyze': () => import('./analyze').then(_rDefault),
9+
'build': () => import('./build').then(_rDefault),
10+
'cleanup': () => import('./cleanup').then(_rDefault),
11+
'_dev': () => import('./dev-child').then(_rDefault),
12+
'dev': () => import('./dev').then(_rDefault),
13+
'devtools': () => import('./devtools').then(_rDefault),
14+
'generate': () => import('./generate').then(_rDefault),
15+
'info': () => import('./info').then(_rDefault),
16+
'init': () => import('./init').then(_rDefault),
17+
'module': () => import('./module').then(_rDefault),
18+
'prepare': () => import('./prepare').then(_rDefault),
19+
'preview': () => import('./preview').then(_rDefault),
20+
'start': () => import('./preview').then(_rDefault),
21+
'test': () => import('./test').then(_rDefault),
22+
'typecheck': () => import('./typecheck').then(_rDefault),
23+
'upgrade': () => import('./upgrade').then(_rDefault),
2324
} as const

packages/nuxi/src/commands/module/add.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { homedir } from 'node:os'
88
import { join } from 'node:path'
99

1010
import process from 'node:process'
11-
import { confirm, isCancel, select } from '@clack/prompts'
11+
import { cancel, confirm, isCancel, select } from '@clack/prompts'
1212
import { updateConfig } from 'c12/update'
1313
import { defineCommand } from 'citty'
1414
import { colors } from 'consola/utils'
@@ -85,7 +85,12 @@ export default defineCommand({
8585
}
8686

8787
const maybeResolvedModules = await Promise.all(modules.map(moduleName => resolveModule(moduleName, cwd)))
88-
const resolvedModules = maybeResolvedModules.filter((x: ModuleResolution): x is ResolvedModule => x != null)
88+
const resolvedModules = maybeResolvedModules.filter((x: ModuleResolution): x is ResolvedModule => !!x)
89+
90+
if (!resolvedModules.length) {
91+
cancel('No modules to add.')
92+
process.exit(1)
93+
}
8994

9095
logger.info(`Resolved ${resolvedModules.map(x => colors.cyan(x.pkgName)).join(', ')}, adding module${resolvedModules.length > 1 ? 's' : ''}...`)
9196

packages/nuxi/src/commands/module/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async function findModuleByKeywords(query: string, nuxtVersion: string) {
6868
compatibility: `nuxt: ${result.item.compatibility?.nuxt || '*'}`,
6969
repository: result.item.github,
7070
description: result.item.description,
71-
install: `npx nuxt module add ${result.item.name}`,
71+
install: `npx nuxt add ${result.item.name}`,
7272
stars: colors.yellow(formatNumber(result.item.stats.stars)),
7373
monthlyDownloads: colors.yellow(formatNumber(result.item.stats.downloads)),
7474
}

packages/nuxi/src/main.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import { resolve } from 'node:path'
55
import process from 'node:process'
66

77
import { runMain as _runMain, defineCommand } from 'citty'
8+
import { colors } from 'consola/utils'
89
import { provider } from 'std-env'
910

1011
import { description, name, version } from '../package.json'
1112
import { commands } from './commands'
1213
import { cwdArgs } from './commands/_shared'
1314
import { initCompletions } from './completions'
15+
import { runCommand } from './run'
1416
import { setupGlobalConsole } from './utils/console'
1517
import { checkEngines } from './utils/engines'
1618
import { debug, logger } from './utils/logger'
19+
import { templateNames } from './utils/templates'
1720

1821
// globalThis.crypto support for Node.js 18
1922
if (!globalThis.crypto) {
@@ -63,6 +66,17 @@ const _main = defineCommand({
6366
await backgroundTasks
6467
}
6568

69+
if (command === 'add' && ctx.rawArgs[1] && templateNames.includes(ctx.rawArgs[1])) {
70+
logger.warn(`${colors.yellow('Deprecated:')} Using ${colors.cyan('nuxt add <template> <name>')} is deprecated.`)
71+
logger.info(`Please use ${colors.cyan('nuxt add-template <template> <name>')} instead.`)
72+
const addTemplate = await import('./commands/add-template').then(m => m.default || m)
73+
await runCommand(addTemplate, [...ctx.rawArgs.slice(1)]).catch((err) => {
74+
console.error(err.message)
75+
process.exit(1)
76+
})
77+
process.exit(0)
78+
}
79+
6680
// allow running arbitrary commands if there's a locally registered binary with `nuxt-` prefix
6781
if (ctx.args.command && !(ctx.args.command in commands)) {
6882
const cwd = resolve(ctx.args.cwd)

packages/nuxi/src/utils/templates/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ const templates = {
4545
'server-util': serverUtil,
4646
} satisfies Record<string, Template>
4747

48+
const _templateNames: Record<keyof typeof templates, undefined> = {
49+
'api': undefined,
50+
'app': undefined,
51+
'app-config': undefined,
52+
'component': undefined,
53+
'composable': undefined,
54+
'error': undefined,
55+
'layer': undefined,
56+
'layout': undefined,
57+
'middleware': undefined,
58+
'module': undefined,
59+
'page': undefined,
60+
'plugin': undefined,
61+
'server-middleware': undefined,
62+
'server-plugin': undefined,
63+
'server-route': undefined,
64+
'server-util': undefined,
65+
}
66+
67+
export const templateNames = Object.keys(_templateNames)
68+
4869
// -- internal utils --
4970

5071
function applySuffix(

packages/nuxt-cli/src/main.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import { resolve } from 'node:path'
33
import process from 'node:process'
44

55
import { defineCommand } from 'citty'
6+
import { colors } from 'consola/utils'
67
import { provider } from 'std-env'
78

89
import { commands } from '../../nuxi/src/commands'
910
import { cwdArgs } from '../../nuxi/src/commands/_shared'
1011
import { setupGlobalConsole } from '../../nuxi/src/utils/console'
1112
import { checkEngines } from '../../nuxi/src/utils/engines'
12-
1313
import { logger } from '../../nuxi/src/utils/logger'
14+
import { templateNames } from '../../nuxi/src/utils/templates'
15+
1416
import { description, name, version } from '../package.json'
17+
import { runCommand } from './run'
1518

1619
const _main = defineCommand({
1720
meta: {
@@ -44,6 +47,16 @@ const _main = defineCommand({
4447
await backgroundTasks
4548
}
4649

50+
if (command === 'add' && ctx.rawArgs[1] && templateNames.includes(ctx.rawArgs[1])) {
51+
logger.warn(`${colors.yellow('Deprecated:')} Using ${colors.cyan('nuxt add <template> <name>')} is deprecated.`)
52+
logger.info(`Please use ${colors.cyan('nuxt add-template <template> <name>')} instead.`)
53+
await runCommand('add-template', [...ctx.rawArgs.slice(1)]).catch((err) => {
54+
console.error(err.message)
55+
process.exit(1)
56+
})
57+
process.exit(0)
58+
}
59+
4760
// allow running arbitrary commands if there's a locally registered binary with `nuxt-` prefix
4861
if (ctx.args.command && !(ctx.args.command in commands)) {
4962
const cwd = resolve(ctx.args.cwd)

packages/nuxt-cli/test/e2e/commands.spec.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const nuxi = fileURLToPath(new URL('../../bin/nuxi.mjs', import.meta.url))
1818

1919
describe('commands', () => {
2020
const tests: Record<keyof typeof commands, 'todo' | TestFunction<object>> = {
21-
_dev: 'todo',
22-
add: async () => {
21+
'_dev': 'todo',
22+
'add': 'todo',
23+
'add-template': async () => {
2324
const file = join(fixtureDir, 'server/api/test.ts')
2425
await rm(file, { force: true })
2526
await x(nuxi, ['add', 'api', 'test'], {
@@ -29,8 +30,8 @@ describe('commands', () => {
2930
expect(existsSync(file)).toBeTruthy()
3031
await rm(file, { force: true })
3132
},
32-
analyze: 'todo',
33-
build: async () => {
33+
'analyze': 'todo',
34+
'build': async () => {
3435
const res = await x(nuxi, ['build'], {
3536
throwOnError: true,
3637
nodeOptions: { stdio: 'pipe', cwd: fixtureDir },
@@ -40,16 +41,16 @@ describe('commands', () => {
4041
expect(existsSync(join(fixtureDir, '.output/server'))).toBeTruthy()
4142
expect(existsSync(join(fixtureDir, '.output/public'))).toBeTruthy()
4243
},
43-
cleanup: async () => {
44+
'cleanup': async () => {
4445
const res = await x(nuxi, ['cleanup'], {
4546
throwOnError: true,
4647
nodeOptions: { stdio: 'pipe', cwd: fixtureDir },
4748
})
4849
expect(res.exitCode).toBe(0)
4950
},
50-
devtools: 'todo',
51-
module: 'todo',
52-
prepare: async () => {
51+
'devtools': 'todo',
52+
'module': 'todo',
53+
'prepare': async () => {
5354
const res = await x(nuxi, ['prepare'], {
5455
throwOnError: true,
5556
nodeOptions: { stdio: 'pipe', cwd: fixtureDir },
@@ -58,7 +59,7 @@ describe('commands', () => {
5859
expect(existsSync(join(fixtureDir, '.nuxt'))).toBeTruthy()
5960
expect(existsSync(join(fixtureDir, '.nuxt/types'))).toBeTruthy()
6061
},
61-
preview: async () => {
62+
'preview': async () => {
6263
await x(nuxi, ['build'], {
6364
throwOnError: true,
6465
nodeOptions: { stdio: 'pipe', cwd: fixtureDir },
@@ -76,17 +77,17 @@ describe('commands', () => {
7677

7778
previewProcess.kill()
7879
},
79-
start: 'todo',
80-
test: 'todo',
81-
typecheck: async () => {
80+
'start': 'todo',
81+
'test': 'todo',
82+
'typecheck': async () => {
8283
const res = await x(nuxi, ['typecheck'], {
8384
throwOnError: true,
8485
nodeOptions: { stdio: 'pipe', cwd: fixtureDir },
8586
})
8687
expect(res.exitCode).toBe(0)
8788
},
88-
upgrade: 'todo',
89-
dev: async () => {
89+
'upgrade': 'todo',
90+
'dev': async () => {
9091
const controller = new AbortController()
9192
const port = await getPort({ host: '127.0.0.1', port: 3001 })
9293
const devProcess = x(nuxi, ['dev', `--host=127.0.0.1`, `--port=${port}`], {
@@ -104,7 +105,7 @@ describe('commands', () => {
104105
}
105106
catch {}
106107
},
107-
generate: async () => {
108+
'generate': async () => {
108109
const res = await x(nuxi, ['generate'], {
109110
throwOnError: true,
110111
nodeOptions: { stdio: 'pipe', cwd: fixtureDir },
@@ -113,7 +114,7 @@ describe('commands', () => {
113114
expect(existsSync(join(fixtureDir, 'dist'))).toBeTruthy()
114115
expect(existsSync(join(fixtureDir, 'dist/index.html'))).toBeTruthy()
115116
},
116-
init: async () => {
117+
'init': async () => {
117118
const dir = tmpdir()
118119
const pm = 'pnpm'
119120
const installPath = join(dir, pm)
@@ -131,7 +132,7 @@ describe('commands', () => {
131132
await rm(installPath, { recursive: true, force: true })
132133
}
133134
},
134-
info: 'todo',
135+
'info': 'todo',
135136
}
136137

137138
it('throws error if no command is provided', async () => {

0 commit comments

Comments
 (0)