From 672c53ed76105b53adf85c5f8b2f2dbb31e8e037 Mon Sep 17 00:00:00 2001 From: rohrig Date: Tue, 26 Sep 2023 22:56:59 +0200 Subject: [PATCH 1/6] feat: add multiple modules at once --- src/commands/module/add.ts | 71 ++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index ab74b51e..9eac2386 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -28,46 +28,49 @@ export default defineCommand({ }, async setup(ctx) { const cwd = resolve(ctx.args.cwd || '.') + const modules = ctx.args._ - // TODO: Resolve and validate npm package name first - const npmPackage = ctx.args.moduleName - - // Add npm dependency - if (!ctx.args.skipInstall) { - consola.info(`Installing dev dependency \`${npmPackage}\``) - await addDependency(npmPackage, { cwd, dev: true }).catch((err) => { - consola.error(err) - consola.error( - `Please manually install \`${npmPackage}\` as a dev dependency`, - ) - }) - } - - // Update nuxt.config.ts - if (!ctx.args.skipConfig) { - await updateNuxtConfig(cwd, (config) => { - if (!config.modules) { - config.modules = [] - } - for (let i = 0; i < config.modules.length; i++) { - if (config.modules[i] === npmPackage) { - consola.info(`\`${npmPackage}\` is already in the \`modules\``) - return - } - } - consola.info(`Adding \`${npmPackage}\` to the \`modules\``) - config.modules.push(npmPackage) - }).catch((err) => { - consola.error(err) - consola.error( - `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, - ) - }) + for (const moduleName of modules) { + // TODO: Resolve and validate npm package name first + await addModule(ctx, cwd, moduleName) } }, }) // -- Internal Utils -- +async function addModule(ctx, cwd: string, npmPackage: string) { + // Add npm dependency + if (!ctx.args.skipInstall) { + consola.info(`Installing dev dependency \`${npmPackage}\``) + await addDependency(npmPackage, { cwd, dev: true }).catch((err) => { + consola.error(err) + consola.error( + `Please manually install \`${npmPackage}\` as a dev dependency`, + ) + }) + } + + if (!ctx.args.skipConfig) { + await updateNuxtConfig(cwd, (config) => { + if (!config.modules) { + config.modules = [] + } + for (let i = 0; i < config.modules.length; i++) { + if (config.modules[i] === npmPackage) { + consola.info(`\`${npmPackage}\` is already in the \`modules\``) + return + } + } + consola.info(`Adding \`${npmPackage}\` to the \`modules\``) + config.modules.push(npmPackage) + }).catch((err) => { + consola.error(err) + consola.error( + `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, + ) + }) + } +} async function updateNuxtConfig( rootDir: string, From 256dd49b537b1c2e8c604995bbbf0e3ab0b42f88 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:15:27 +0000 Subject: [PATCH 2/6] [autofix.ci] apply automated fixes --- src/commands/module/add.ts | 348 ++++++++++++++++++------------------- 1 file changed, 174 insertions(+), 174 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index e7fe852c..a9041d3c 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -1,117 +1,117 @@ -import {resolve} from 'pathe' -import {defineCommand} from 'citty' -import {sharedArgs} from '../_shared' -import {existsSync} from 'node:fs' -import {loadFile, writeFile, parseModule, ProxifiedModule} from 'magicast' +import { resolve } from 'pathe' +import { defineCommand } from 'citty' +import { sharedArgs } from '../_shared' +import { existsSync } from 'node:fs' +import { loadFile, writeFile, parseModule, ProxifiedModule } from 'magicast' import consola from 'consola' -import {addDependency} from 'nypm' +import { addDependency } from 'nypm' import { - NuxtModule, - checkNuxtCompatibility, - fetchModules, - getNuxtVersion, + NuxtModule, + checkNuxtCompatibility, + fetchModules, + getNuxtVersion, } from './_utils' -import {satisfies} from 'semver' -import {colors} from 'consola/utils' +import { satisfies } from 'semver' +import { colors } from 'consola/utils' export default defineCommand({ - meta: { - name: 'module', - description: 'Manage Nuxt Modules', + meta: { + name: 'module', + description: 'Manage Nuxt Modules', + }, + args: { + ...sharedArgs, + moduleName: { + type: 'positional', + description: 'Module name', }, - args: { - ...sharedArgs, - moduleName: { - type: 'positional', - description: 'Module name', - }, - skipInstall: { - type: 'boolean', - description: 'Skip npm install', - }, - skipConfig: { - type: 'boolean', - description: 'Skip nuxt.config.ts update', - }, + skipInstall: { + type: 'boolean', + description: 'Skip npm install', }, - async setup(ctx) { - const cwd = resolve(ctx.args.cwd || '.') - const modules = ctx.args._ - - for (const moduleName of modules) { - const r = await resolveModule(ctx.args.moduleName, cwd) - if (r === false) { - return - } - await addModule(ctx, cwd, moduleName) - } + skipConfig: { + type: 'boolean', + description: 'Skip nuxt.config.ts update', + }, + }, + async setup(ctx) { + const cwd = resolve(ctx.args.cwd || '.') + const modules = ctx.args._ + + for (const moduleName of modules) { + const r = await resolveModule(ctx.args.moduleName, cwd) + if (r === false) { + return + } + await addModule(ctx, cwd, moduleName) } + }, }) // -- Internal Utils -- async function addModule(ctx, cwd: string, npmPackage: string) { - // Add npm dependency - if (!ctx.args.skipInstall) { - consola.info(`Installing dev dependency \`${npmPackage}\``) - await addDependency(npmPackage, {cwd, dev: true}).catch((err) => { - consola.error(err) - consola.error( - `Please manually install \`${npmPackage}\` as a dev dependency`, - ) - }) - } - - // Update nuxt.config.ts - if (!ctx.args.skipConfig) { - await updateNuxtConfig(cwd, (config) => { - if (!config.modules) { - config.modules = [] - } - for (let i = 0; i < config.modules.length; i++) { - if (config.modules[i] === npmPackage) { - consola.info(`\`${npmPackage}\` is already in the \`modules\``) - return - } - } - consola.info(`Adding \`${npmPackage}\` to the \`modules\``) - config.modules.push(npmPackage) - }).catch((err) => { - consola.error(err) - consola.error( - `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, - ) - }) - } + // Add npm dependency + if (!ctx.args.skipInstall) { + consola.info(`Installing dev dependency \`${npmPackage}\``) + await addDependency(npmPackage, { cwd, dev: true }).catch((err) => { + consola.error(err) + consola.error( + `Please manually install \`${npmPackage}\` as a dev dependency`, + ) + }) + } + + // Update nuxt.config.ts + if (!ctx.args.skipConfig) { + await updateNuxtConfig(cwd, (config) => { + if (!config.modules) { + config.modules = [] + } + for (let i = 0; i < config.modules.length; i++) { + if (config.modules[i] === npmPackage) { + consola.info(`\`${npmPackage}\` is already in the \`modules\``) + return + } + } + consola.info(`Adding \`${npmPackage}\` to the \`modules\``) + config.modules.push(npmPackage) + }).catch((err) => { + consola.error(err) + consola.error( + `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, + ) + }) + } } async function updateNuxtConfig( - rootDir: string, - update: (config: any) => void, + rootDir: string, + update: (config: any) => void, ) { - let _module: ProxifiedModule - const nuxtConfigFile = resolve(rootDir, 'nuxt.config.ts') - if (existsSync(nuxtConfigFile)) { - consola.info('Updating `nuxt.config.ts`') - _module = await loadFile(nuxtConfigFile) - } else { - consola.info('Creating `nuxt.config.ts`') - _module = parseModule(getDefaultNuxtConfig()) - } - const defaultExport = _module.exports.default - if (!defaultExport) { - throw new Error('`nuxt.config.ts` does not have a default export!') - } - if (defaultExport.$type === 'function-call') { - update(defaultExport.$args[0]) - } else { - update(defaultExport) - } - await writeFile(_module as any, nuxtConfigFile) - consola.success('`nuxt.config.ts` updated') + let _module: ProxifiedModule + const nuxtConfigFile = resolve(rootDir, 'nuxt.config.ts') + if (existsSync(nuxtConfigFile)) { + consola.info('Updating `nuxt.config.ts`') + _module = await loadFile(nuxtConfigFile) + } else { + consola.info('Creating `nuxt.config.ts`') + _module = parseModule(getDefaultNuxtConfig()) + } + const defaultExport = _module.exports.default + if (!defaultExport) { + throw new Error('`nuxt.config.ts` does not have a default export!') + } + if (defaultExport.$type === 'function-call') { + update(defaultExport.$args[0]) + } else { + update(defaultExport) + } + await writeFile(_module as any, nuxtConfigFile) + consola.success('`nuxt.config.ts` updated') } function getDefaultNuxtConfig() { - return ` + return ` // https://nuxt.com/docs/api/configuration/nuxt-config export default defineNuxtConfig({ modules: [] @@ -120,94 +120,94 @@ export default defineNuxtConfig({ // Based on https://github.com/dword-design/package-name-regex const packageRegex = - /^(@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*)(@[^@]+)?$/ + /^(@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*)(@[^@]+)?$/ async function resolveModule( - moduleName: string, - cwd: string, + moduleName: string, + cwd: string, ): Promise< - | false - | { - nuxtModule?: NuxtModule - pkg: string - pkgName: string - pkgVersion: string -} + | false + | { + nuxtModule?: NuxtModule + pkg: string + pkgName: string + pkgVersion: string + } > { - let pkgName = moduleName - let pkgVersion: string | undefined - - const reMatch = moduleName.match(packageRegex) - if (reMatch) { - if (reMatch[3]) { - pkgName = `${reMatch[1] || ''}${reMatch[2] || ''}` - pkgVersion = reMatch[3].slice(1) - } - } else { - consola.error(`Invalid package name \`${pkgName}\`.`) - return false + let pkgName = moduleName + let pkgVersion: string | undefined + + const reMatch = moduleName.match(packageRegex) + if (reMatch) { + if (reMatch[3]) { + pkgName = `${reMatch[1] || ''}${reMatch[2] || ''}` + pkgVersion = reMatch[3].slice(1) } - - const modulesDB = await fetchModules().catch((err) => { - consola.warn('Cannot search in the Nuxt Modules database: ' + err) - return [] - }) - - const matchedModule = modulesDB.find( - (module) => module.name === moduleName || module.npm === pkgName, - ) - - if (matchedModule?.npm) { - pkgName = matchedModule.npm + } else { + consola.error(`Invalid package name \`${pkgName}\`.`) + return false + } + + const modulesDB = await fetchModules().catch((err) => { + consola.warn('Cannot search in the Nuxt Modules database: ' + err) + return [] + }) + + const matchedModule = modulesDB.find( + (module) => module.name === moduleName || module.npm === pkgName, + ) + + if (matchedModule?.npm) { + pkgName = matchedModule.npm + } + + if (matchedModule && matchedModule.compatibility.nuxt) { + // Get local Nuxt version + const nuxtVersion = await getNuxtVersion(cwd) + + // Check for Module Compatibility + if (!checkNuxtCompatibility(matchedModule, nuxtVersion)) { + consola.warn( + `The module \`${pkgName}\` is not compatible with Nuxt \`${nuxtVersion}\` (requires \`${matchedModule.compatibility.nuxt}\`)`, + ) + const shouldContinue = await consola.prompt( + 'Do you want to continue installing incompatible version?', + { + type: 'confirm', + initial: false, + }, + ) + if (shouldContinue !== true) { + return false + } } - if (matchedModule && matchedModule.compatibility.nuxt) { - // Get local Nuxt version - const nuxtVersion = await getNuxtVersion(cwd) - - // Check for Module Compatibility - if (!checkNuxtCompatibility(matchedModule, nuxtVersion)) { + // Match corresponding version of module for local Nuxt version + const versionMap = matchedModule.compatibility.versionMap + if (versionMap) { + for (const [_nuxtVersion, _moduleVersion] of Object.entries(versionMap)) { + if (satisfies(nuxtVersion, _nuxtVersion)) { + if (!pkgVersion) { + pkgVersion = _moduleVersion + } else { consola.warn( - `The module \`${pkgName}\` is not compatible with Nuxt \`${nuxtVersion}\` (requires \`${matchedModule.compatibility.nuxt}\`)`, - ) - const shouldContinue = await consola.prompt( - 'Do you want to continue installing incompatible version?', - { - type: 'confirm', - initial: false, - }, + `Recommended version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_moduleVersion}\` but you have requested \`${pkgVersion}\``, ) - if (shouldContinue !== true) { - return false - } + pkgVersion = await consola.prompt('Choose a version:', { + type: 'select', + options: [_moduleVersion, pkgVersion], + }) + } + break } - - // Match corresponding version of module for local Nuxt version - const versionMap = matchedModule.compatibility.versionMap - if (versionMap) { - for (const [_nuxtVersion, _moduleVersion] of Object.entries(versionMap)) { - if (satisfies(nuxtVersion, _nuxtVersion)) { - if (!pkgVersion) { - pkgVersion = _moduleVersion - } else { - consola.warn( - `Recommended version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_moduleVersion}\` but you have requested \`${pkgVersion}\``, - ) - pkgVersion = await consola.prompt('Choose a version:', { - type: 'select', - options: [_moduleVersion, pkgVersion], - }) - } - break - } - } - } - } - - return { - nuxtModule: matchedModule, - pkg: `${pkgName}@${pkgVersion || 'latest'}`, - pkgName, - pkgVersion: pkgVersion || 'latest', + } } + } + + return { + nuxtModule: matchedModule, + pkg: `${pkgName}@${pkgVersion || 'latest'}`, + pkgName, + pkgVersion: pkgVersion || 'latest', + } } From aee7f8c9a0ce846155206bcd53d89448009b5801 Mon Sep 17 00:00:00 2001 From: rohrig Date: Tue, 26 Sep 2023 23:42:21 +0200 Subject: [PATCH 3/6] feat: add multiple modules at once --- src/commands/module/add.ts | 348 +++++++++++++++++++------------------ 1 file changed, 177 insertions(+), 171 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index e7fe852c..ccb9ce8b 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -1,117 +1,123 @@ -import {resolve} from 'pathe' -import {defineCommand} from 'citty' -import {sharedArgs} from '../_shared' -import {existsSync} from 'node:fs' -import {loadFile, writeFile, parseModule, ProxifiedModule} from 'magicast' +import { resolve } from 'pathe' +import { defineCommand } from 'citty' +import { sharedArgs } from '../_shared' +import { existsSync } from 'node:fs' +import { loadFile, writeFile, parseModule, ProxifiedModule } from 'magicast' import consola from 'consola' -import {addDependency} from 'nypm' +import { addDependency } from 'nypm' import { - NuxtModule, - checkNuxtCompatibility, - fetchModules, - getNuxtVersion, + NuxtModule, + checkNuxtCompatibility, + fetchModules, + getNuxtVersion, } from './_utils' -import {satisfies} from 'semver' -import {colors} from 'consola/utils' +import { satisfies } from 'semver' +import { colors } from 'consola/utils' export default defineCommand({ - meta: { - name: 'module', - description: 'Manage Nuxt Modules', + meta: { + name: 'module', + description: 'Manage Nuxt Modules', + }, + args: { + ...sharedArgs, + moduleName: { + type: 'positional', + description: 'Module name', }, - args: { - ...sharedArgs, - moduleName: { - type: 'positional', - description: 'Module name', - }, - skipInstall: { - type: 'boolean', - description: 'Skip npm install', - }, - skipConfig: { - type: 'boolean', - description: 'Skip nuxt.config.ts update', - }, + skipInstall: { + type: 'boolean', + description: 'Skip npm install', }, - async setup(ctx) { - const cwd = resolve(ctx.args.cwd || '.') - const modules = ctx.args._ - - for (const moduleName of modules) { - const r = await resolveModule(ctx.args.moduleName, cwd) - if (r === false) { - return - } - await addModule(ctx, cwd, moduleName) - } + skipConfig: { + type: 'boolean', + description: 'Skip nuxt.config.ts update', + }, + }, + async setup(ctx) { + const cwd = resolve(ctx.args.cwd || '.') + const modules = ctx.args._ + + for (const moduleName of modules) { + const isResolved = await resolveModule(moduleName, cwd) + if (isResolved === false) { + return + } + consola.info(`${moduleName} has been resolved. Adding module...`) + await addModule(moduleName, ctx.args.skipInstall, ctx.args.skipConfig, cwd) } + } }) // -- Internal Utils -- -async function addModule(ctx, cwd: string, npmPackage: string) { - // Add npm dependency - if (!ctx.args.skipInstall) { - consola.info(`Installing dev dependency \`${npmPackage}\``) - await addDependency(npmPackage, {cwd, dev: true}).catch((err) => { - consola.error(err) - consola.error( - `Please manually install \`${npmPackage}\` as a dev dependency`, - ) - }) - } - - // Update nuxt.config.ts - if (!ctx.args.skipConfig) { - await updateNuxtConfig(cwd, (config) => { - if (!config.modules) { - config.modules = [] - } - for (let i = 0; i < config.modules.length; i++) { - if (config.modules[i] === npmPackage) { - consola.info(`\`${npmPackage}\` is already in the \`modules\``) - return - } - } - consola.info(`Adding \`${npmPackage}\` to the \`modules\``) - config.modules.push(npmPackage) - }).catch((err) => { - consola.error(err) - consola.error( - `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, - ) - }) - } +async function addModule( + npmPackage: string, + skipInstall: boolean, + skipConfig: boolean, + cwd: string +) { + // Add npm dependency + if (!skipInstall) { + consola.info(`Installing dev dependency \`${npmPackage}\``) + await addDependency(npmPackage, { cwd, dev: true }).catch((err) => { + consola.error(err) + consola.error( + `Please manually install \`${npmPackage}\` as a dev dependency`, + ) + }) + } + + // Update nuxt.config.ts + if (!skipConfig) { + await updateNuxtConfig(cwd, (config) => { + if (!config.modules) { + config.modules = [] + } + for (let i = 0; i < config.modules.length; i++) { + if (config.modules[i] === npmPackage) { + consola.info(`\`${npmPackage}\` is already in the \`modules\``) + return + } + } + consola.info(`Adding \`${npmPackage}\` to the \`modules\``) + config.modules.push(npmPackage) + }).catch((err) => { + consola.error(err) + consola.error( + `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, + ) + }) + } } async function updateNuxtConfig( - rootDir: string, - update: (config: any) => void, + rootDir: string, + update: (config: any) => void, ) { - let _module: ProxifiedModule - const nuxtConfigFile = resolve(rootDir, 'nuxt.config.ts') - if (existsSync(nuxtConfigFile)) { - consola.info('Updating `nuxt.config.ts`') - _module = await loadFile(nuxtConfigFile) - } else { - consola.info('Creating `nuxt.config.ts`') - _module = parseModule(getDefaultNuxtConfig()) - } - const defaultExport = _module.exports.default - if (!defaultExport) { - throw new Error('`nuxt.config.ts` does not have a default export!') - } - if (defaultExport.$type === 'function-call') { - update(defaultExport.$args[0]) - } else { - update(defaultExport) - } - await writeFile(_module as any, nuxtConfigFile) - consola.success('`nuxt.config.ts` updated') + let _module: ProxifiedModule + const nuxtConfigFile = resolve(rootDir, 'nuxt.config.ts') + if (existsSync(nuxtConfigFile)) { + consola.info('Updating `nuxt.config.ts`') + _module = await loadFile(nuxtConfigFile) + } else { + consola.info('Creating `nuxt.config.ts`') + _module = parseModule(getDefaultNuxtConfig()) + } + const defaultExport = _module.exports.default + if (!defaultExport) { + throw new Error('`nuxt.config.ts` does not have a default export!') + } + if (defaultExport.$type === 'function-call') { + update(defaultExport.$args[0]) + } else { + update(defaultExport) + } + await writeFile(_module as any, nuxtConfigFile) + consola.success('`nuxt.config.ts` updated') } function getDefaultNuxtConfig() { - return ` + return ` // https://nuxt.com/docs/api/configuration/nuxt-config export default defineNuxtConfig({ modules: [] @@ -120,94 +126,94 @@ export default defineNuxtConfig({ // Based on https://github.com/dword-design/package-name-regex const packageRegex = - /^(@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*)(@[^@]+)?$/ + /^(@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*)(@[^@]+)?$/ async function resolveModule( - moduleName: string, - cwd: string, + moduleName: string, + cwd: string, ): Promise< - | false - | { + | false + | { nuxtModule?: NuxtModule pkg: string pkgName: string pkgVersion: string -} + } > { - let pkgName = moduleName - let pkgVersion: string | undefined - - const reMatch = moduleName.match(packageRegex) - if (reMatch) { - if (reMatch[3]) { - pkgName = `${reMatch[1] || ''}${reMatch[2] || ''}` - pkgVersion = reMatch[3].slice(1) - } - } else { - consola.error(`Invalid package name \`${pkgName}\`.`) - return false + let pkgName = moduleName + let pkgVersion: string | undefined + + const reMatch = moduleName.match(packageRegex) + if (reMatch) { + if (reMatch[3]) { + pkgName = `${reMatch[1] || ''}${reMatch[2] || ''}` + pkgVersion = reMatch[3].slice(1) } - - const modulesDB = await fetchModules().catch((err) => { - consola.warn('Cannot search in the Nuxt Modules database: ' + err) - return [] - }) - - const matchedModule = modulesDB.find( - (module) => module.name === moduleName || module.npm === pkgName, - ) - - if (matchedModule?.npm) { - pkgName = matchedModule.npm + } else { + consola.error(`Invalid package name \`${pkgName}\`.`) + return false + } + + const modulesDB = await fetchModules().catch((err) => { + consola.warn('Cannot search in the Nuxt Modules database: ' + err) + return [] + }) + + const matchedModule = modulesDB.find( + (module) => module.name === moduleName || module.npm === pkgName, + ) + + if (matchedModule?.npm) { + pkgName = matchedModule.npm + } + + if (matchedModule && matchedModule.compatibility.nuxt) { + // Get local Nuxt version + const nuxtVersion = await getNuxtVersion(cwd) + + // Check for Module Compatibility + if (!checkNuxtCompatibility(matchedModule, nuxtVersion)) { + consola.warn( + `The module \`${pkgName}\` is not compatible with Nuxt \`${nuxtVersion}\` (requires \`${matchedModule.compatibility.nuxt}\`)`, + ) + const shouldContinue = await consola.prompt( + 'Do you want to continue installing incompatible version?', + { + type: 'confirm', + initial: false, + }, + ) + if (shouldContinue !== true) { + return false + } } - if (matchedModule && matchedModule.compatibility.nuxt) { - // Get local Nuxt version - const nuxtVersion = await getNuxtVersion(cwd) - - // Check for Module Compatibility - if (!checkNuxtCompatibility(matchedModule, nuxtVersion)) { + // Match corresponding version of module for local Nuxt version + const versionMap = matchedModule.compatibility.versionMap + if (versionMap) { + for (const [_nuxtVersion, _moduleVersion] of Object.entries(versionMap)) { + if (satisfies(nuxtVersion, _nuxtVersion)) { + if (!pkgVersion) { + pkgVersion = _moduleVersion + } else { consola.warn( - `The module \`${pkgName}\` is not compatible with Nuxt \`${nuxtVersion}\` (requires \`${matchedModule.compatibility.nuxt}\`)`, - ) - const shouldContinue = await consola.prompt( - 'Do you want to continue installing incompatible version?', - { - type: 'confirm', - initial: false, - }, + `Recommended version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_moduleVersion}\` but you have requested \`${pkgVersion}\``, ) - if (shouldContinue !== true) { - return false - } + pkgVersion = await consola.prompt('Choose a version:', { + type: 'select', + options: [_moduleVersion, pkgVersion], + }) + } + break } - - // Match corresponding version of module for local Nuxt version - const versionMap = matchedModule.compatibility.versionMap - if (versionMap) { - for (const [_nuxtVersion, _moduleVersion] of Object.entries(versionMap)) { - if (satisfies(nuxtVersion, _nuxtVersion)) { - if (!pkgVersion) { - pkgVersion = _moduleVersion - } else { - consola.warn( - `Recommended version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_moduleVersion}\` but you have requested \`${pkgVersion}\``, - ) - pkgVersion = await consola.prompt('Choose a version:', { - type: 'select', - options: [_moduleVersion, pkgVersion], - }) - } - break - } - } - } - } - - return { - nuxtModule: matchedModule, - pkg: `${pkgName}@${pkgVersion || 'latest'}`, - pkgName, - pkgVersion: pkgVersion || 'latest', + } } + } + + return { + nuxtModule: matchedModule, + pkg: `${pkgName}@${pkgVersion || 'latest'}`, + pkgName, + pkgVersion: pkgVersion || 'latest', + } } From f315d8f883621044eb8e1e8c1954c0dd448cb338 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:44:12 +0000 Subject: [PATCH 4/6] [autofix.ci] apply automated fixes --- src/commands/module/add.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index ee5ce153..05b892b9 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -44,7 +44,12 @@ export default defineCommand({ return } consola.info(`${moduleName} has been resolved. Adding module...`) - await addModule(moduleName, ctx.args.skipInstall, ctx.args.skipConfig, cwd) + await addModule( + moduleName, + ctx.args.skipInstall, + ctx.args.skipConfig, + cwd, + ) } }, }) @@ -54,7 +59,7 @@ async function addModule( npmPackage: string, skipInstall: boolean, skipConfig: boolean, - cwd: string + cwd: string, ) { // Add npm dependency if (!skipInstall) { @@ -134,11 +139,11 @@ async function resolveModule( ): Promise< | false | { - nuxtModule?: NuxtModule - pkg: string - pkgName: string - pkgVersion: string - } + nuxtModule?: NuxtModule + pkg: string + pkgName: string + pkgVersion: string + } > { let pkgName = moduleName let pkgVersion: string | undefined From ae404322bee97eaa9a843bab15f8d6746e74da82 Mon Sep 17 00:00:00 2001 From: rohrig Date: Wed, 27 Sep 2023 00:04:50 +0200 Subject: [PATCH 5/6] feat: add multiple modules at once --- src/commands/module/add.ts | 64 +++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 05b892b9..1909571f 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -14,6 +14,15 @@ import { import { satisfies } from 'semver' import { colors } from 'consola/utils' +type ResolvedModule = { + nuxtModule?: NuxtModule + pkg: string + pkgName: string + pkgVersion: string +} +type UnresolvedModule = false +type ModuleResolution = ResolvedModule | UnresolvedModule + export default defineCommand({ meta: { name: 'module', @@ -39,13 +48,13 @@ export default defineCommand({ const modules = ctx.args._ for (const moduleName of modules) { - const isResolved = await resolveModule(moduleName, cwd) - if (isResolved === false) { + const moduleResolution = await resolveModule(moduleName, cwd) + if (moduleResolution === false) { return } consola.info(`${moduleName} has been resolved. Adding module...`) await addModule( - moduleName, + moduleResolution, ctx.args.skipInstall, ctx.args.skipConfig, cwd, @@ -56,20 +65,33 @@ export default defineCommand({ // -- Internal Utils -- async function addModule( - npmPackage: string, + resolvedModule: ResolvedModule, skipInstall: boolean, skipConfig: boolean, cwd: string, ) { // Add npm dependency if (!skipInstall) { - consola.info(`Installing dev dependency \`${npmPackage}\``) - await addDependency(npmPackage, { cwd, dev: true }).catch((err) => { - consola.error(err) - consola.error( - `Please manually install \`${npmPackage}\` as a dev dependency`, - ) - }) + consola.info(`Installing dev dependency \`${resolvedModule.pkg}\``) + const res = await addDependency(resolvedModule.pkg, { cwd, dev: true }).catch( + (error) => { + consola.error(error) + return consola.prompt( + `Install failed for ${colors.cyan( + resolvedModule.pkg, + )}. Do you want to continue adding the module to ${colors.cyan( + 'nuxt.config', + )}?`, + { + type: 'confirm', + initial: false, + }, + ) + }, + ) + if (res === false) { + return + } } // Update nuxt.config.ts @@ -79,17 +101,17 @@ async function addModule( config.modules = [] } for (let i = 0; i < config.modules.length; i++) { - if (config.modules[i] === npmPackage) { - consola.info(`\`${npmPackage}\` is already in the \`modules\``) + if (config.modules[i] === resolvedModule.pkgName) { + consola.info(`\`${resolvedModule.pkgName}\` is already in the \`modules\``) return } } - consola.info(`Adding \`${npmPackage}\` to the \`modules\``) - config.modules.push(npmPackage) + consola.info(`Adding \`${resolvedModule.pkgName}\` to the \`modules\``) + config.modules.push(resolvedModule.pkgName) }).catch((err) => { consola.error(err) consola.error( - `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, + `Please manually add \`${resolvedModule.pkgName}\` to the \`modules\` in \`nuxt.config.ts\``, ) }) } @@ -136,15 +158,7 @@ const packageRegex = async function resolveModule( moduleName: string, cwd: string, -): Promise< - | false - | { - nuxtModule?: NuxtModule - pkg: string - pkgName: string - pkgVersion: string - } -> { +): Promise { let pkgName = moduleName let pkgVersion: string | undefined From d355d5870b35685e000f9685dcdd77ecc4ee8ba5 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:05:30 +0000 Subject: [PATCH 6/6] [autofix.ci] apply automated fixes --- src/commands/module/add.ts | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 1909571f..e8076f66 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -73,22 +73,23 @@ async function addModule( // Add npm dependency if (!skipInstall) { consola.info(`Installing dev dependency \`${resolvedModule.pkg}\``) - const res = await addDependency(resolvedModule.pkg, { cwd, dev: true }).catch( - (error) => { - consola.error(error) - return consola.prompt( - `Install failed for ${colors.cyan( - resolvedModule.pkg, - )}. Do you want to continue adding the module to ${colors.cyan( - 'nuxt.config', - )}?`, - { - type: 'confirm', - initial: false, - }, - ) + const res = await addDependency(resolvedModule.pkg, { + cwd, + dev: true, + }).catch((error) => { + consola.error(error) + return consola.prompt( + `Install failed for ${colors.cyan( + resolvedModule.pkg, + )}. Do you want to continue adding the module to ${colors.cyan( + 'nuxt.config', + )}?`, + { + type: 'confirm', + initial: false, }, - ) + ) + }) if (res === false) { return } @@ -102,7 +103,9 @@ async function addModule( } for (let i = 0; i < config.modules.length; i++) { if (config.modules[i] === resolvedModule.pkgName) { - consola.info(`\`${resolvedModule.pkgName}\` is already in the \`modules\``) + consola.info( + `\`${resolvedModule.pkgName}\` is already in the \`modules\``, + ) return } } @@ -111,7 +114,7 @@ async function addModule( }).catch((err) => { consola.error(err) consola.error( - `Please manually add \`${resolvedModule.pkgName}\` to the \`modules\` in \`nuxt.config.ts\``, + `Please manually add \`${resolvedModule.pkgName}\` to the \`modules\` in \`nuxt.config.ts\``, ) }) }