Skip to content

fix(init): skip modules that are dependencies of the selected template #910

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 2 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
91 changes: 65 additions & 26 deletions packages/nuxi/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ function filterModules(modules: string[], allDependencies: Record<string, string
return result
}

async function getTemplateDependencies(templateDir: string) {
try {
const packageJsonPath = join(templateDir, 'package.json')
if (!existsSync(packageJsonPath)) {
return []
}
const packageJson = await import(packageJsonPath)
const directDeps = {
...packageJson.dependencies,
...packageJson.devDependencies,
}
const directDepNames = Object.keys(directDeps)
const allDeps = new Set(directDepNames)

const transitiveDepsResults = await Promise.all(
directDepNames.map(dep => getModuleDependencies(dep)),
)

transitiveDepsResults.forEach((deps) => {
deps.forEach(dep => allDeps.add(dep))
})

return Array.from(allDeps)
}
catch (err) {
logger.warn(`Could not read template dependencies: ${err}`)
return []
}
}

export default defineCommand({
meta: {
name: 'init',
Expand Down Expand Up @@ -312,42 +342,51 @@ export default defineCommand({
).catch(() => process.exit(1))

if (wantsUserModules) {
const response = await modulesPromise
const [response, templateDeps] = await Promise.all([
modulesPromise,
getTemplateDependencies(template.dir),
])

const officialModules = response.modules
.filter(module => module.type === 'official' && module.npm !== '@nuxt/devtools')
.filter(module => !templateDeps.includes(module.npm))

const selectedOfficialModules = await logger.prompt(
'Pick the modules to install:',
{
type: 'multiselect',
options: officialModules.map(module => ({
label: `${colors.bold(colors.greenBright(module.npm))} – ${module.description.replace(/\.$/, '')}`,
value: module.npm,
})),
required: false,
},
)

if (selectedOfficialModules === undefined) {
process.exit(1)
if (officialModules.length === 0) {
logger.info('All official modules are already included in this template.')
}
else {
const selectedOfficialModules = await logger.prompt(
'Pick the modules to install:',
{
type: 'multiselect',
options: officialModules.map(module => ({
label: `${colors.bold(colors.greenBright(module.npm))} – ${module.description.replace(/\.$/, '')}`,
value: module.npm,
})),
required: false,
},
)

if (selectedOfficialModules.length > 0) {
const modules = selectedOfficialModules as unknown as string[]
if (selectedOfficialModules === undefined) {
process.exit(1)
}

const allDependencies = Object.fromEntries(
await Promise.all(modules.map(async module =>
[module, await getModuleDependencies(module)] as const,
)),
)
if (selectedOfficialModules.length > 0) {
const modules = selectedOfficialModules as unknown as string[]

const allDependencies = Object.fromEntries(
await Promise.all(modules.map(async module =>
[module, await getModuleDependencies(module)] as const,
)),
)

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

if (skipped.length) {
logger.info(`The following modules are already included as dependencies of another module and will not be installed: ${skipped.map(m => colors.cyan(m)).join(', ')}`)
if (skipped.length) {
logger.info(`The following modules are already included as dependencies of another module and will not be installed: ${skipped.map(m => colors.cyan(m)).join(', ')}`)
}
modulesToAdd.push(...toInstall)
}
modulesToAdd.push(...toInstall)
}
}
}
Expand Down
Loading