From b01e1557f937220c2c1f3ba93859bf25f85303b0 Mon Sep 17 00:00:00 2001 From: harlan Date: Wed, 3 Apr 2024 13:31:54 +1100 Subject: [PATCH] fix: safer prompts to install --- src/kit.ts | 6 ++++-- src/module.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/kit.ts b/src/kit.ts index 4cadb3f9..43bf83d9 100644 --- a/src/kit.ts +++ b/src/kit.ts @@ -1,4 +1,4 @@ -import { addTemplate, createResolver, logger, useNuxt } from '@nuxt/kit' +import {addTemplate, createResolver, logger, tryUseNuxt, useNuxt} from '@nuxt/kit' import { relative } from 'pathe' import { resolvePackageJSON } from 'pkg-types' import { isCI, provider } from 'std-env' @@ -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]) diff --git a/src/module.ts b/src/module.ts index 694920f8..c48b75ad 100644 --- a/src/module.ts +++ b/src/module.ts @@ -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) {