Skip to content

Commit

Permalink
fix: safer prompts to install
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Apr 3, 2024
1 parent 26563c5 commit b01e155
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/kit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addTemplate, createResolver, logger, useNuxt } from '@nuxt/kit'
import {addTemplate, createResolver, logger, tryUseNuxt, useNuxt} from '@nuxt/kit'

Check failure on line 1 in src/kit.ts

View workflow job for this annotation

GitHub Actions / ci

A space is required after '{'

Check failure on line 1 in src/kit.ts

View workflow job for this annotation

GitHub Actions / ci

A space is required before '}'
import { relative } from 'pathe'
import { resolvePackageJSON } from 'pkg-types'
import { isCI, provider } from 'std-env'
Expand Down Expand Up @@ -70,7 +70,9 @@ export function installNuxtModule(name: string, options?: EnsurePackageInstalled
if (installPrompts.has(name))
return
installPrompts.add(name)
const nuxt = useNuxt()
const nuxt = tryUseNuxt()
if (!nuxt)
return
return promptToInstall(name, async () => {
const { runCommand } = await import(String('nuxi'))
await runCommand('module', ['add', name, '--cwd', nuxt.options.rootDir])
Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ ${(config.globals || []).map(g => !Array.isArray(g)
registry,
defaultBundle: config.defaultScriptOptions?.assetStrategy === 'bundle',
moduleDetected(module) {
if (module !== '@nuxt/scripts' && !moduleInstallPromises.has(module) && !hasNuxtModule(module))
if (nuxt.options.dev && module !== '@nuxt/scripts' && !moduleInstallPromises.has(module) && !hasNuxtModule(module))
moduleInstallPromises.set(module, () => installNuxtModule(module))
},
resolveScript(src) {
Expand Down

0 comments on commit b01e155

Please sign in to comment.