Skip to content

feat: skip already handled modules by @nuxt/ui #879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions packages/nuxi/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,43 @@ const pms: Record<PackageManagerName, undefined> = {
// this is for type safety to prompt updating code in nuxi when nypm adds a new package manager
const packageManagerOptions = Object.keys(pms) as PackageManagerName[]

async function getModuleDependencies(moduleName: string) {
try {
const response = await $fetch(`https://registry.npmjs.org/${moduleName}/latest`)
const dependencies = response.dependencies || {}
return Object.keys(dependencies)
}
catch (err) {
logger.warn(`Could not get dependencies for ${moduleName}: ${err}`)
return []
}
}

function filterModules(modules: string[], allDependencies: Record<string, string[]>) {
const result = {
toInstall: [] as string[],
skipped: [] as string[],
}

for (const module of modules) {
const isDependency = modules.some((otherModule) => {
if (otherModule === module)
return false
const deps = allDependencies[otherModule] || []
return deps.includes(module)
})

if (isDependency) {
result.skipped.push(module)
}
else {
result.toInstall.push(module)
}
}

return result
}

export default defineCommand({
meta: {
name: 'init',
Expand Down Expand Up @@ -283,8 +320,20 @@ export default defineCommand({
process.exit(1)
}

if (selectedOfficialModules.length > 0) {
modulesToAdd.push(...(selectedOfficialModules as unknown as string[]))
if (selectedOfficialModules.length) {
const modules = selectedOfficialModules as unknown as string[]

const allDependencies: Record<string, string[]> = {}
for (const module of modules) {
allDependencies[module] = await getModuleDependencies(module)
}

const { toInstall, skipped } = filterModules(modules, allDependencies)

if (skipped.length) {
logger.info(`The following modules are already included as dependencies and will not be installed: ${skipped.join(', ')}`)
}
modulesToAdd.push(...toInstall)
}
}

Expand Down
Loading