From 7a6dcc1938e58672d728d3dba2c9a19406fd46a3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 00:36:25 +0200 Subject: [PATCH 01/13] feat: search in `nuxt/modules` for `nuxt module add ` --- src/commands/module/add.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index ab74b51ee..305424f78 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -5,6 +5,7 @@ import { existsSync } from 'node:fs' import { loadFile, writeFile, parseModule, ProxifiedModule } from 'magicast' import consola from 'consola' import { addDependency } from 'nypm' +import { fetchModules } from './_utils' export default defineCommand({ meta: { @@ -25,12 +26,30 @@ export default defineCommand({ type: 'boolean', description: 'Skip nuxt.config.ts update', }, + exact: { + type: 'string', + description: 'use exact module name as npm package name to install', + }, }, async setup(ctx) { const cwd = resolve(ctx.args.cwd || '.') - // TODO: Resolve and validate npm package name first - const npmPackage = ctx.args.moduleName + let npmPackage = ctx.args.moduleName + + // Try to find as slug in nuxt/modules database + try { + const modulesDB = await fetchModules() + const matchedModule = modulesDB.find( + (module) => + module.name === ctx.args.moduleName || + module.npm === ctx.args.moduleName, + ) + if (matchedModule?.npm) { + npmPackage = matchedModule.npm + } + } catch (err) { + consola.warn('Cannot search in the modules database:', err) + } // Add npm dependency if (!ctx.args.skipInstall) { From 7a8fb712a9a85f7c59975a1f80d42c7529dda5f4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 00:39:11 +0200 Subject: [PATCH 02/13] remove arg --- src/commands/module/add.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 305424f78..19457418b 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -26,10 +26,6 @@ export default defineCommand({ type: 'boolean', description: 'Skip nuxt.config.ts update', }, - exact: { - type: 'string', - description: 'use exact module name as npm package name to install', - }, }, async setup(ctx) { const cwd = resolve(ctx.args.cwd || '.') From 933d9efaa781b95be5b87cff43407ab79757c504 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 11:29:29 +0200 Subject: [PATCH 03/13] add compatibility check --- src/commands/module/add.ts | 42 +++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 19457418b..74f35e62e 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -5,7 +5,7 @@ import { existsSync } from 'node:fs' import { loadFile, writeFile, parseModule, ProxifiedModule } from 'magicast' import consola from 'consola' import { addDependency } from 'nypm' -import { fetchModules } from './_utils' +import { checkNuxtCompatibility, fetchModules, getNuxtVersion } from './_utils' export default defineCommand({ meta: { @@ -33,18 +33,36 @@ export default defineCommand({ let npmPackage = ctx.args.moduleName // Try to find as slug in nuxt/modules database - try { - const modulesDB = await fetchModules() - const matchedModule = modulesDB.find( - (module) => - module.name === ctx.args.moduleName || - module.npm === ctx.args.moduleName, - ) - if (matchedModule?.npm) { - npmPackage = matchedModule.npm + const modulesDB = await fetchModules().catch((err) => { + consola.warn('Cannot search in the Nuxt Modules database: ' + err) + return [] + }) + const matchedModule = modulesDB.find( + (module) => + module.name === ctx.args.moduleName || + module.npm === ctx.args.moduleName, + ) + if (matchedModule?.npm) { + npmPackage = matchedModule.npm + } + + if (matchedModule && matchedModule.compatibility.nuxt) { + const nuxtVersion = await getNuxtVersion(cwd) + if (!checkNuxtCompatibility(matchedModule, nuxtVersion)) { + consola.warn( + `The module \`${npmPackage}\` 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 + } } - } catch (err) { - consola.warn('Cannot search in the modules database:', err) } // Add npm dependency From cec6c264a0f4b6f727c4d29738b82bf4437ef072 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 11:44:59 +0200 Subject: [PATCH 04/13] add poc for versionMap support --- src/commands/module/_utils.ts | 3 +++ src/commands/module/add.ts | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/commands/module/_utils.ts b/src/commands/module/_utils.ts index 2073078f2..f1903afee 100644 --- a/src/commands/module/_utils.ts +++ b/src/commands/module/_utils.ts @@ -27,6 +27,9 @@ export const categories = [ export interface ModuleCompatibility { nuxt: string requires: { bridge?: boolean | 'optional' } + versionMap: { + [nuxtVersion: string]: string + } } export interface MaintainerInfo { diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 74f35e62e..88d91b0b7 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -6,6 +6,7 @@ import { loadFile, writeFile, parseModule, ProxifiedModule } from 'magicast' import consola from 'consola' import { addDependency } from 'nypm' import { checkNuxtCompatibility, fetchModules, getNuxtVersion } from './_utils' +import { satisfies } from 'semver' export default defineCommand({ meta: { @@ -47,7 +48,10 @@ export default defineCommand({ } 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 \`${npmPackage}\` is not compatible with Nuxt ${nuxtVersion} (requires ${matchedModule.compatibility.nuxt})`, @@ -63,6 +67,27 @@ export default defineCommand({ return } } + + // TODO: Preview for https://github.com/nuxt/modules/pull/770 + if ( + matchedModule.name === 'image' && + !matchedModule.compatibility.versionMap + ) { + matchedModule.compatibility.versionMap = { + '^2.x': '^0', + '^3.x': 'rc', + } + // Match corresponding version of module for local Nuxt version + const versionMap = matchedModule.compatibility.versionMap + if (versionMap) { + for (const _nuxtVersion in versionMap) { + if (satisfies(nuxtVersion, _nuxtVersion)) { + npmPackage = `${npmPackage}@${versionMap[_nuxtVersion]}` + break + } + } + } + } } // Add npm dependency From 459d7953fe0e9f99560664062f4837f92c5fccbf Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 11:57:10 +0200 Subject: [PATCH 05/13] refactor to util --- src/commands/module/add.ts | 149 +++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 66 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 88d91b0b7..eaa7770c2 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -5,7 +5,12 @@ import { existsSync } from 'node:fs' import { loadFile, writeFile, parseModule, ProxifiedModule } from 'magicast' import consola from 'consola' import { addDependency } from 'nypm' -import { checkNuxtCompatibility, fetchModules, getNuxtVersion } from './_utils' +import { + NuxtModule, + checkNuxtCompatibility, + fetchModules, + getNuxtVersion, +} from './_utils' import { satisfies } from 'semver' export default defineCommand({ @@ -31,72 +36,18 @@ export default defineCommand({ async setup(ctx) { const cwd = resolve(ctx.args.cwd || '.') - let npmPackage = ctx.args.moduleName - - // Try to find as slug in nuxt/modules database - const modulesDB = await fetchModules().catch((err) => { - consola.warn('Cannot search in the Nuxt Modules database: ' + err) - return [] - }) - const matchedModule = modulesDB.find( - (module) => - module.name === ctx.args.moduleName || - module.npm === ctx.args.moduleName, - ) - if (matchedModule?.npm) { - npmPackage = 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 \`${npmPackage}\` 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 - } - } - - // TODO: Preview for https://github.com/nuxt/modules/pull/770 - if ( - matchedModule.name === 'image' && - !matchedModule.compatibility.versionMap - ) { - matchedModule.compatibility.versionMap = { - '^2.x': '^0', - '^3.x': 'rc', - } - // Match corresponding version of module for local Nuxt version - const versionMap = matchedModule.compatibility.versionMap - if (versionMap) { - for (const _nuxtVersion in versionMap) { - if (satisfies(nuxtVersion, _nuxtVersion)) { - npmPackage = `${npmPackage}@${versionMap[_nuxtVersion]}` - break - } - } - } - } + const r = await resolveModule(ctx.args.moduleName, cwd) + if (r === false) { + return } // Add npm dependency if (!ctx.args.skipInstall) { - consola.info(`Installing dev dependency \`${npmPackage}\``) - await addDependency(npmPackage, { cwd, dev: true }).catch((err) => { + consola.info(`Installing dev dependency \`${r.npm}\``) + await addDependency(r.npm, { cwd, dev: true }).catch((err) => { consola.error(err) consola.error( - `Please manually install \`${npmPackage}\` as a dev dependency`, + `Please manually install \`${r.npm}\` as a dev dependency`, ) }) } @@ -108,17 +59,17 @@ export default defineCommand({ 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] === r.npm) { + consola.info(`\`${r.npm}\` is already in the \`modules\``) return } } - consola.info(`Adding \`${npmPackage}\` to the \`modules\``) - config.modules.push(npmPackage) + consola.info(`Adding \`${r.npm}\` to the \`modules\``) + config.modules.push(r.npm) }).catch((err) => { consola.error(err) consola.error( - `Please manually add \`${npmPackage}\` to the \`modules\` in \`nuxt.config.ts\``, + `Please manually add \`${r.npm}\` to the \`modules\` in \`nuxt.config.ts\``, ) }) } @@ -160,3 +111,69 @@ export default defineNuxtConfig({ modules: [] })` } + +async function resolveModule( + moduleName: string, + cwd: string, +): Promise { + let npmName = moduleName + + 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 === moduleName, + ) + if (matchedModule?.npm) { + npmName = 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 \`${npmName}\` 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 + } + } + + // TODO: Preview for https://github.com/nuxt/modules/pull/770 + if ( + matchedModule.name === 'image' && + !matchedModule.compatibility.versionMap + ) { + matchedModule.compatibility.versionMap = { + '^2.x': '^0', + '^3.x': 'rc', + } + // Match corresponding version of module for local Nuxt version + const versionMap = matchedModule.compatibility.versionMap + if (versionMap) { + for (const _nuxtVersion in versionMap) { + if (satisfies(nuxtVersion, _nuxtVersion)) { + npmName = `${npmName}@${versionMap[_nuxtVersion]}` + break + } + } + } + } + } + + return { + module: matchedModule, + npm: npmName, + } +} From dc3716fce8cb615dfd2e1b17bee85995de88ab3f Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 12:00:30 +0200 Subject: [PATCH 06/13] typo --- src/commands/module/add.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index eaa7770c2..e468bcd4d 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -159,14 +159,15 @@ async function resolveModule( '^2.x': '^0', '^3.x': 'rc', } - // Match corresponding version of module for local Nuxt version - const versionMap = matchedModule.compatibility.versionMap - if (versionMap) { - for (const _nuxtVersion in versionMap) { - if (satisfies(nuxtVersion, _nuxtVersion)) { - npmName = `${npmName}@${versionMap[_nuxtVersion]}` - break - } + } + + // Match corresponding version of module for local Nuxt version + const versionMap = matchedModule.compatibility.versionMap + if (versionMap) { + for (const _nuxtVersion in versionMap) { + if (satisfies(nuxtVersion, _nuxtVersion)) { + npmName = `${npmName}@${versionMap[_nuxtVersion]}` + break } } } From fd945caecbd3bfe30b053209605c584a0acaf7bd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 12:18:24 +0200 Subject: [PATCH 07/13] validate npm and fastpath when contraint provided --- src/commands/module/add.ts | 107 ++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 44 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index e468bcd4d..198106637 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -112,62 +112,81 @@ export default defineNuxtConfig({ })` } +// Extended from https://github.com/dword-design/package-name-regex +const packageNameRegex = + /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*(@[^@]+)?$/ + async function resolveModule( moduleName: string, cwd: string, ): Promise { let npmName = moduleName - 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 === moduleName, - ) - if (matchedModule?.npm) { - npmName = matchedModule.npm + if (!packageNameRegex.test(moduleName)) { + consola.error(`Invalid module name \`${moduleName}\`.`) + return false } - 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 \`${npmName}\` 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 - } + let checkModules = true + + if (moduleName.includes('@', 1)) { + checkModules = false + } + + let matchedModule: NuxtModule | undefined + + if (checkModules) { + const modulesDB = await fetchModules().catch((err) => { + consola.warn('Cannot search in the Nuxt Modules database: ' + err) + return [] + }) + matchedModule = modulesDB.find( + (module) => module.name === moduleName || module.npm === moduleName, + ) + if (matchedModule?.npm) { + npmName = matchedModule.npm } - // TODO: Preview for https://github.com/nuxt/modules/pull/770 - if ( - matchedModule.name === 'image' && - !matchedModule.compatibility.versionMap - ) { - matchedModule.compatibility.versionMap = { - '^2.x': '^0', - '^3.x': 'rc', + 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 \`${npmName}\` 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 + } } - } - // Match corresponding version of module for local Nuxt version - const versionMap = matchedModule.compatibility.versionMap - if (versionMap) { - for (const _nuxtVersion in versionMap) { - if (satisfies(nuxtVersion, _nuxtVersion)) { - npmName = `${npmName}@${versionMap[_nuxtVersion]}` - break + // TODO: Preview for https://github.com/nuxt/modules/pull/770 + if ( + matchedModule.name === 'image' && + !matchedModule.compatibility.versionMap + ) { + matchedModule.compatibility.versionMap = { + '^2.x': '^0', + '^3.x': 'rc', + } + } + + // Match corresponding version of module for local Nuxt version + const versionMap = matchedModule.compatibility.versionMap + if (versionMap) { + for (const _nuxtVersion in versionMap) { + if (satisfies(nuxtVersion, _nuxtVersion)) { + npmName = `${npmName}@${versionMap[_nuxtVersion]}` + break + } } } } From 1a8f6c4dd2a18488133cab26c2ff078096b1fdcf Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 12:27:35 +0200 Subject: [PATCH 08/13] fix: add to the modules without contraint --- src/commands/module/add.ts | 57 +++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 198106637..ed89eabd1 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -43,11 +43,11 @@ export default defineCommand({ // Add npm dependency if (!ctx.args.skipInstall) { - consola.info(`Installing dev dependency \`${r.npm}\``) - await addDependency(r.npm, { cwd, dev: true }).catch((err) => { + consola.info(`Installing dev dependency \`${r.pkg}\``) + await addDependency(r.pkg, { cwd, dev: true }).catch((err) => { consola.error(err) consola.error( - `Please manually install \`${r.npm}\` as a dev dependency`, + `Please manually install \`${r.pkg}\` as a dev dependency`, ) }) } @@ -59,17 +59,17 @@ export default defineCommand({ config.modules = [] } for (let i = 0; i < config.modules.length; i++) { - if (config.modules[i] === r.npm) { - consola.info(`\`${r.npm}\` is already in the \`modules\``) + if (config.modules[i] === r.pkgName) { + consola.info(`\`${r.pkgName}\` is already in the \`modules\``) return } } - consola.info(`Adding \`${r.npm}\` to the \`modules\``) - config.modules.push(r.npm) + consola.info(`Adding \`${r.pkgName}\` to the \`modules\``) + config.modules.push(r.pkgName) }).catch((err) => { consola.error(err) consola.error( - `Please manually add \`${r.npm}\` to the \`modules\` in \`nuxt.config.ts\``, + `Please manually add \`${r.pkgName}\` to the \`modules\` in \`nuxt.config.ts\``, ) }) } @@ -119,17 +119,32 @@ const packageNameRegex = async function resolveModule( moduleName: string, cwd: string, -): Promise { - let npmName = moduleName +): Promise< + | false + | { + nuxtModule?: NuxtModule + pkg: string + pkgName: string + pkgVersion: string + } +> { + let pkgName = moduleName + let pkgVersion = 'latest' + + if (pkgName.includes('@', 1)) { + const s = pkgName.split('@') + pkgName = s[0] + pkgVersion = s[1] + } - if (!packageNameRegex.test(moduleName)) { - consola.error(`Invalid module name \`${moduleName}\`.`) + if (!packageNameRegex.test(pkgName)) { + consola.error(`Invalid package name \`${pkgName}\`.`) return false } let checkModules = true - if (moduleName.includes('@', 1)) { + if (pkgName.includes('@', 1)) { checkModules = false } @@ -144,7 +159,7 @@ async function resolveModule( (module) => module.name === moduleName || module.npm === moduleName, ) if (matchedModule?.npm) { - npmName = matchedModule.npm + pkgName = matchedModule.npm } if (matchedModule && matchedModule.compatibility.nuxt) { @@ -154,7 +169,7 @@ async function resolveModule( // Check for Module Compatibility if (!checkNuxtCompatibility(matchedModule, nuxtVersion)) { consola.warn( - `The module \`${npmName}\` is not compatible with Nuxt ${nuxtVersion} (requires ${matchedModule.compatibility.nuxt})`, + `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?', @@ -182,9 +197,9 @@ async function resolveModule( // Match corresponding version of module for local Nuxt version const versionMap = matchedModule.compatibility.versionMap if (versionMap) { - for (const _nuxtVersion in versionMap) { - if (satisfies(nuxtVersion, _nuxtVersion)) { - npmName = `${npmName}@${versionMap[_nuxtVersion]}` + for (const _version in versionMap) { + if (satisfies(nuxtVersion, _version)) { + pkgVersion = versionMap[_version] break } } @@ -193,7 +208,9 @@ async function resolveModule( } return { - module: matchedModule, - npm: npmName, + nuxtModule: matchedModule, + pkg: `${pkgName}@${pkgVersion}`, + pkgName, + pkgVersion, } } From b9c545be9081f7e499765cd19b6e60da88fc0849 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 12:45:38 +0200 Subject: [PATCH 09/13] validate npm inputs against db too --- src/commands/module/add.ts | 130 +++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 64 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index ed89eabd1..c6784bafb 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -112,9 +112,9 @@ export default defineNuxtConfig({ })` } -// Extended from https://github.com/dword-design/package-name-regex -const packageNameRegex = - /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*(@[^@]+)?$/ +// Based on https://github.com/dword-design/package-name-regex +const packageRegex = + /^(@[a-z0-9-~][a-z0-9-._~]*\/)?([a-z0-9-~][a-z0-9-._~]*)(@[^@]+)?$/ async function resolveModule( moduleName: string, @@ -129,79 +129,81 @@ async function resolveModule( } > { let pkgName = moduleName - let pkgVersion = 'latest' + let pkgVersion: string | undefined - if (pkgName.includes('@', 1)) { - const s = pkgName.split('@') - pkgName = s[0] - pkgVersion = s[1] - } - - if (!packageNameRegex.test(pkgName)) { + 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 checkModules = true + const modulesDB = await fetchModules().catch((err) => { + consola.warn('Cannot search in the Nuxt Modules database: ' + err) + return [] + }) - if (pkgName.includes('@', 1)) { - checkModules = false - } - - let matchedModule: NuxtModule | undefined - - if (checkModules) { - const modulesDB = await fetchModules().catch((err) => { - consola.warn('Cannot search in the Nuxt Modules database: ' + err) - return [] - }) - matchedModule = modulesDB.find( - (module) => module.name === moduleName || module.npm === moduleName, - ) - if (matchedModule?.npm) { - pkgName = matchedModule.npm - } + const matchedModule = modulesDB.find( + (module) => module.name === moduleName || module.npm === pkgName, + ) - if (matchedModule && matchedModule.compatibility.nuxt) { - // Get local Nuxt version - const nuxtVersion = await getNuxtVersion(cwd) + if (matchedModule?.npm) { + pkgName = matchedModule.npm + } - // 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)) { + 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 } + } - // TODO: Preview for https://github.com/nuxt/modules/pull/770 - if ( - matchedModule.name === 'image' && - !matchedModule.compatibility.versionMap - ) { - matchedModule.compatibility.versionMap = { - '^2.x': '^0', - '^3.x': 'rc', - } + // TODO: Preview for https://github.com/nuxt/modules/pull/770 + if ( + matchedModule.name === 'image' && + !matchedModule.compatibility.versionMap + ) { + matchedModule.compatibility.versionMap = { + '^2.x': '^0', + '^3.x': 'rc', } + } - // Match corresponding version of module for local Nuxt version - const versionMap = matchedModule.compatibility.versionMap - if (versionMap) { - for (const _version in versionMap) { - if (satisfies(nuxtVersion, _version)) { + // Match corresponding version of module for local Nuxt version + const versionMap = matchedModule.compatibility.versionMap + if (versionMap) { + for (const _version in versionMap) { + if (satisfies(nuxtVersion, _version)) { + if (!pkgVersion) { pkgVersion = versionMap[_version] - break + } else { + consola.warn( + `Recommanded version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_version}\` but you have requested \`${pkgVersion}\``, + ) + pkgVersion = await consola.prompt('Choose a version', { + type: 'select', + options: [versionMap[_version], pkgVersion], + }) } + break } } } @@ -209,8 +211,8 @@ async function resolveModule( return { nuxtModule: matchedModule, - pkg: `${pkgName}@${pkgVersion}`, + pkg: `${pkgName}@${pkgVersion || 'latest'}`, pkgName, - pkgVersion, + pkgVersion: pkgVersion || 'latest', } } From d70f943fa0d68ae61830751723b6b28263c2b9dc Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 12:47:29 +0200 Subject: [PATCH 10/13] remove version map patch --- src/commands/module/add.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index c6784bafb..e85339234 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -176,17 +176,6 @@ async function resolveModule( } } - // TODO: Preview for https://github.com/nuxt/modules/pull/770 - if ( - matchedModule.name === 'image' && - !matchedModule.compatibility.versionMap - ) { - matchedModule.compatibility.versionMap = { - '^2.x': '^0', - '^3.x': 'rc', - } - } - // Match corresponding version of module for local Nuxt version const versionMap = matchedModule.compatibility.versionMap if (versionMap) { From 0613561ff21dd7c8bcf83ace8186a25da255aabb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 12:49:07 +0200 Subject: [PATCH 11/13] fix version chooser --- src/commands/module/add.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index e85339234..c18ddc17b 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -179,17 +179,17 @@ async function resolveModule( // Match corresponding version of module for local Nuxt version const versionMap = matchedModule.compatibility.versionMap if (versionMap) { - for (const _version in versionMap) { - if (satisfies(nuxtVersion, _version)) { + for (const [_nuxtVersion, _moduleVersion] of Object.entries(versionMap)) { + if (satisfies(nuxtVersion, _nuxtVersion)) { if (!pkgVersion) { - pkgVersion = versionMap[_version] + pkgVersion = _moduleVersion } else { consola.warn( - `Recommanded version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_version}\` but you have requested \`${pkgVersion}\``, + `Recommanded version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_moduleVersion}\` but you have requested \`${pkgVersion}\``, ) - pkgVersion = await consola.prompt('Choose a version', { + pkgVersion = await consola.prompt('Choose a version:', { type: 'select', - options: [versionMap[_version], pkgVersion], + options: [_moduleVersion, pkgVersion], }) } break From fa98b8ee53550829fd68c24071d639392ded4544 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 12:56:55 +0200 Subject: [PATCH 12/13] fix highlight --- src/commands/module/add.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index c18ddc17b..593d3955d 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -162,7 +162,7 @@ async function resolveModule( // Check for Module Compatibility if (!checkNuxtCompatibility(matchedModule, nuxtVersion)) { consola.warn( - `The module \`${pkgName}\` is not compatible with Nuxt ${nuxtVersion} (requires ${matchedModule.compatibility.nuxt})`, + `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?', From 45b64738fd99f3b46469e761dad881973f2b0d19 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 19 Sep 2023 13:32:18 +0200 Subject: [PATCH 13/13] typo --- src/commands/module/add.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/module/add.ts b/src/commands/module/add.ts index 593d3955d..18f413feb 100644 --- a/src/commands/module/add.ts +++ b/src/commands/module/add.ts @@ -185,7 +185,7 @@ async function resolveModule( pkgVersion = _moduleVersion } else { consola.warn( - `Recommanded version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_moduleVersion}\` but you have requested \`${pkgVersion}\``, + `Recommended version of \`${pkgName}\` for Nuxt \`${nuxtVersion}\` is \`${_moduleVersion}\` but you have requested \`${pkgVersion}\``, ) pkgVersion = await consola.prompt('Choose a version:', { type: 'select',