Skip to content

Commit

Permalink
perf: some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jan 9, 2025
1 parent 127d2e4 commit fbecb8d
Showing 1 changed file with 34 additions and 47 deletions.
81 changes: 34 additions & 47 deletions src/commands/module/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ export default defineCommand({
const maybeResolvedModules = await Promise.all(modules.map(moduleName => resolveModule(moduleName, cwd)))
const resolvedModules = maybeResolvedModules.filter((x: ModuleResolution): x is ResolvedModule => x != null)

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

await addModules(resolvedModules, { ...ctx.args, cwd }, projectPkg)

Expand All @@ -101,59 +97,50 @@ export default defineCommand({
async function addModules(modules: ResolvedModule[], { skipInstall, skipConfig, cwd, dev }: { skipInstall: boolean, skipConfig: boolean, cwd: string, dev: boolean }, projectPkg: PackageJson) {
// Add dependencies
if (!skipInstall) {
const modulesGrouppedByInstallationStatus = modules.reduce<
{ installed: ResolvedModule[], notInstalled: ResolvedModule[] }
>((acc, module) => {
const isInstalled = (projectPkg.dependencies && module.pkgName in projectPkg.dependencies)
|| (projectPkg.devDependencies && module.pkgName in projectPkg.devDependencies)

if (isInstalled) {
acc.installed.push(module)
const installedModules: ResolvedModule[] = []
const notInstalledModules: ResolvedModule[] = []

const dependencies = new Set([
...Object.keys(projectPkg.dependencies || {}),
...Object.keys(projectPkg.devDependencies || {}),
])

for (const module of modules) {
if (dependencies.has(module.pkgName)) {
installedModules.push(module)
}
else {
acc.notInstalled.push(module)
notInstalledModules.push(module)
}
}

return acc
}, { installed: [], notInstalled: [] })

if (modulesGrouppedByInstallationStatus.installed.length > 0) {
logger.info(`\`${
modulesGrouppedByInstallationStatus.installed.map(module => module.pkgName).join('\`, \`')
}\` ${
modulesGrouppedByInstallationStatus.installed.length > 1 ? 'are' : 'is'
} already installed`)
if (installedModules.length > 0) {
const installedModulesList = installedModules.map(module => module.pkgName).join('\`, \`')
const are = installedModules.length > 1 ? 'are' : 'is'
logger.info(`\`${installedModulesList}\` ${are} already installed`)
}

if (modulesGrouppedByInstallationStatus.notInstalled.length > 0) {
if (notInstalledModules.length > 0) {
const isDev = Boolean(projectPkg.devDependencies?.nuxt) || dev

logger.info(`Installing \`${
modulesGrouppedByInstallationStatus.notInstalled.map(module => module.pkg).join('\`, \`')
}\`${
isDev ? ' development' : ''
} ${
modulesGrouppedByInstallationStatus.notInstalled.length > 1 ? 'dependencies' : 'dependency'
}`)

const res = await addDependency(
modulesGrouppedByInstallationStatus.notInstalled.map(module => module.pkg),
{ cwd, dev: isDev, installPeerDependencies: true },
).catch(
const notInstalledModulesList = notInstalledModules.map(module => module.pkg).join('\`, \`')
const dependencies = notInstalledModules.length > 1 ? 'dependencies' : 'a dependency'
logger.info(`Installing \`${notInstalledModulesList} as\`${isDev ? ' development' : ''} ${dependencies}`)

const res = await addDependency(notInstalledModules.map(module => module.pkg), {
cwd,
dev: isDev,
installPeerDependencies: true,
}).catch(
(error) => {
logger.error(error)

return logger.prompt(
`Install failed for \`${
modulesGrouppedByInstallationStatus.notInstalled.map(module => colors.cyan(module.pkg)).join('\`, \`')
}\`. Do you want to continue adding the module${
modulesGrouppedByInstallationStatus.notInstalled.length > 1 ? 's' : ''
} to ${colors.cyan('nuxt.config')}?`,
{
type: 'confirm',
initial: false,
},
)
const failedModulesList = notInstalledModules.map(module => colors.cyan(module.pkg)).join('\`, \`')
const s = notInstalledModules.length > 1 ? 's' : ''
return logger.prompt(`Install failed for \`${failedModulesList}\`. Do you want to continue adding the module${s} to ${colors.cyan('nuxt.config')}?`, {
type: 'confirm',
initial: false,
})
},
)

Expand Down

0 comments on commit fbecb8d

Please sign in to comment.