mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-21 21:25:11 +00:00
feat(nuxt): auto-install optional features on StackBlitz (#23607)
This commit is contained in:
parent
914fbd8883
commit
fc7b131bf7
@ -1,10 +1,21 @@
|
||||
import { addDependency } from 'nypm'
|
||||
import { resolvePackageJSON } from 'pkg-types'
|
||||
import { logger } from '@nuxt/kit'
|
||||
import { isCI } from 'std-env'
|
||||
import { isCI, provider } from 'std-env'
|
||||
|
||||
export async function ensurePackageInstalled (rootDir: string, name: string, searchPaths?: string[]) {
|
||||
if (await resolvePackageJSON(name, { url: searchPaths }).catch(() => null)) {
|
||||
const isStackblitz = provider === 'stackblitz'
|
||||
|
||||
export interface EnsurePackageInstalledOptions {
|
||||
rootDir: string
|
||||
searchPaths?: string[]
|
||||
prompt?: boolean
|
||||
}
|
||||
|
||||
export async function ensurePackageInstalled (
|
||||
name: string,
|
||||
options: EnsurePackageInstalledOptions
|
||||
) {
|
||||
if (await resolvePackageJSON(name, { url: options.searchPaths }).catch(() => null)) {
|
||||
return true
|
||||
}
|
||||
|
||||
@ -13,20 +24,23 @@ export async function ensurePackageInstalled (rootDir: string, name: string, sea
|
||||
return false
|
||||
}
|
||||
|
||||
const confirm = await logger.prompt(`Do you want to install ${name} package?`, {
|
||||
type: 'confirm',
|
||||
name: 'confirm',
|
||||
initial: true
|
||||
})
|
||||
// In StackBlitz we install packages automatically by default
|
||||
if (options.prompt === true || (options.prompt !== false && !isStackblitz)) {
|
||||
const confirm = await logger.prompt(`Do you want to install ${name} package?`, {
|
||||
type: 'confirm',
|
||||
name: 'confirm',
|
||||
initial: true
|
||||
})
|
||||
|
||||
if (!confirm) {
|
||||
return false
|
||||
if (!confirm) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`Installing ${name}...`)
|
||||
try {
|
||||
await addDependency(name, {
|
||||
cwd: rootDir,
|
||||
cwd: options.rootDir,
|
||||
dev: true
|
||||
})
|
||||
logger.success(`Installed ${name}`)
|
||||
|
@ -443,7 +443,10 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||
|
||||
// Nuxt DevTools is currently opt-in
|
||||
if (options.devtools === true || (options.devtools && options.devtools.enabled !== false)) {
|
||||
if (await import('./features').then(r => r.ensurePackageInstalled(options.rootDir, '@nuxt/devtools', options.modulesDir))) {
|
||||
if (await import('./features').then(r => r.ensurePackageInstalled('@nuxt/devtools', {
|
||||
rootDir: options.rootDir,
|
||||
searchPaths: options.modulesDir
|
||||
}))) {
|
||||
options._modules.push('@nuxt/devtools')
|
||||
} else {
|
||||
logger.warn('Failed to install `@nuxt/devtools`, please install it manually, or disable `devtools` in `nuxt.config`')
|
||||
@ -452,7 +455,10 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
||||
|
||||
// Nuxt Webpack Builder is currently opt-in
|
||||
if (options.builder === '@nuxt/webpack-builder') {
|
||||
if (!await import('./features').then(r => r.ensurePackageInstalled(options.rootDir, '@nuxt/webpack-builder', options.modulesDir))) {
|
||||
if (!await import('./features').then(r => r.ensurePackageInstalled('@nuxt/webpack-builder', {
|
||||
rootDir: options.rootDir,
|
||||
searchPaths: options.modulesDir
|
||||
}))) {
|
||||
logger.warn('Failed to install `@nuxt/webpack-builder`, please install it manually, or change the `builder` option to vite in `nuxt.config`')
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user