|
1 |
| -import { existsSync } from 'node:fs' |
2 | 1 | import { defineCommand } from 'citty'
|
3 | 2 | import { resolve } from 'pathe'
|
4 |
| -import { filename } from 'pathe/utils' |
5 |
| -import type { ProxifiedModule } from 'magicast' |
6 |
| -import { loadFile, writeFile, parseModule } from 'magicast' |
7 | 3 | import consola from 'consola'
|
8 | 4 | import { addDependency } from 'nypm'
|
9 | 5 | import { $fetch } from 'ofetch'
|
10 | 6 | import { satisfies } from 'semver'
|
| 7 | +import { updateConfig } from 'c12/update' |
11 | 8 | import { colors } from 'consola/utils'
|
12 | 9 | import { sharedArgs } from '../_shared'
|
13 |
| -import { loadKit } from '../../utils/kit' |
14 | 10 | import {
|
15 | 11 | checkNuxtCompatibility,
|
16 | 12 | fetchModules,
|
@@ -91,59 +87,34 @@ export default defineCommand({
|
91 | 87 |
|
92 | 88 | // Update nuxt.config.ts
|
93 | 89 | if (!ctx.args.skipConfig) {
|
94 |
| - await updateNuxtConfig(cwd, (config) => { |
95 |
| - if (!config.modules) { |
96 |
| - config.modules = [] |
97 |
| - } |
98 |
| - |
99 |
| - if (config.modules.includes(r.pkgName)) { |
100 |
| - consola.info(`\`${r.pkgName}\` is already in the \`modules\``) |
101 |
| - return |
102 |
| - } |
103 |
| - consola.info(`Adding \`${r.pkgName}\` to the \`modules\``) |
104 |
| - config.modules.push(r.pkgName) |
105 |
| - }).catch((err) => { |
106 |
| - consola.error(err) |
107 |
| - consola.error( |
108 |
| - `Please manually add \`${r.pkgName}\` to the \`modules\` in \`nuxt.config.ts\``, |
109 |
| - ) |
| 90 | + await updateConfig({ |
| 91 | + cwd, |
| 92 | + configFile: 'nuxt.config', |
| 93 | + async onCreate() { |
| 94 | + consola.info(`Creating \`nuxt.config.ts\``) |
| 95 | + return getDefaultNuxtConfig() |
| 96 | + }, |
| 97 | + async onUpdate(config) { |
| 98 | + if (!config.modules) { |
| 99 | + config.modules = [] |
| 100 | + } |
| 101 | + if (config.modules.includes(r.pkgName)) { |
| 102 | + consola.info(`\`${r.pkgName}\` is already in the \`modules\``) |
| 103 | + return |
| 104 | + } |
| 105 | + consola.info(`Adding \`${r.pkgName}\` to the \`modules\``) |
| 106 | + config.modules.push(r.pkgName) |
| 107 | + }, |
| 108 | + }).catch((error) => { |
| 109 | + consola.error(`Failed to update \`nuxt.config\`: ${error.message}`) |
| 110 | + consola.error(`Please manually add \`${r.pkgName}\` to the \`modules\` in \`nuxt.config.ts\``) |
| 111 | + return null |
110 | 112 | })
|
111 | 113 | }
|
112 | 114 | },
|
113 | 115 | })
|
114 | 116 |
|
115 | 117 | // -- Internal Utils --
|
116 |
| - |
117 |
| -async function updateNuxtConfig( |
118 |
| - rootDir: string, |
119 |
| - update: (config: any) => void, |
120 |
| -) { |
121 |
| - let _module: ProxifiedModule |
122 |
| - const { resolvePath } = await loadKit(rootDir) |
123 |
| - const nuxtConfigFile = await resolvePath(resolve(rootDir, 'nuxt.config')) |
124 |
| - const nuxtConfigFileName = filename(nuxtConfigFile) |
125 |
| - if (existsSync(nuxtConfigFile)) { |
126 |
| - consola.info(`Updating \`${nuxtConfigFileName}\``) |
127 |
| - _module = await loadFile(nuxtConfigFile) |
128 |
| - } |
129 |
| - else { |
130 |
| - consola.info(`Creating \`nuxt.config\`.ts`) |
131 |
| - _module = parseModule(getDefaultNuxtConfig()) |
132 |
| - } |
133 |
| - const defaultExport = _module.exports.default |
134 |
| - if (!defaultExport) { |
135 |
| - throw new Error(`\`${nuxtConfigFileName}\` does not have a default export!`) |
136 |
| - } |
137 |
| - if (defaultExport.$type === 'function-call') { |
138 |
| - update(defaultExport.$args[0]) |
139 |
| - } |
140 |
| - else { |
141 |
| - update(defaultExport) |
142 |
| - } |
143 |
| - await writeFile(_module as any, nuxtConfigFile.replace(/nuxt\.config$/, 'nuxt.config.ts')) |
144 |
| - consola.success(`\`${nuxtConfigFileName}\` updated`) |
145 |
| -} |
146 |
| - |
147 | 118 | function getDefaultNuxtConfig() {
|
148 | 119 | return `
|
149 | 120 | // https://nuxt.com/docs/api/configuration/nuxt-config
|
|
0 commit comments