Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"@electric-sql/pglite": "*",
"@libsql/client": "*",
"@valibot/to-json-schema": "^1.5.0",
"better-sqlite3": "^12.5.0",
"better-sqlite3": "^12.5.0 || ^13.0.0",
"sqlite3": "*",
"valibot": "^1.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/utils/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addDependency } from 'nypm'
import { resolvePackageJSON } from 'pkg-types'
import { hasTTY, isCI } from 'std-env'
import { logger } from './dev'
import nuxtContentContext from './context'
import { tryUseNuxt } from '@nuxt/kit'
Expand All @@ -22,11 +23,13 @@ export async function ensurePackageInstalled(pkg: string) {
if (!await isPackageInstalled(pkg)) {
logger.error(`Nuxt Content requires \`${pkg}\` module to operate.`)

const confirm = await logger.prompt(`Do you want to install \`${pkg}\` package?`, {
type: 'confirm',
name: 'confirm',
initial: true,
})
const confirm = hasTTY && !isCI
? await logger.prompt(`Do you want to install \`${pkg}\` package?`, {
type: 'confirm',
name: 'confirm',
initial: true,
})
: false

if (!confirm) {
logger.error(`Nuxt Content requires \`${pkg}\` module to operate. Please install \`${pkg}\` package manually and try again. \`npm install ${pkg}\``)
Expand Down
60 changes: 60 additions & 0 deletions test/unit/ensurePackageInstalled.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { afterEach, describe, expect, test, vi } from 'vitest'

const logger = { error: vi.fn(), prompt: vi.fn() }
const addDependency = vi.fn()

vi.mock('../../src/utils/dev', () => ({ logger }))
vi.mock('nypm', () => ({ addDependency }))

describe('ensurePackageInstalled', () => {
afterEach(() => {
vi.restoreAllMocks()
vi.resetModules()
})

test('reports how to install the package instead of prompting when there is no TTY', async () => {
vi.doMock('std-env', async importOriginal => ({
...(await importOriginal<typeof import('std-env')>()),
hasTTY: false,
isCI: false,
}))
const exit = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never)
const { ensurePackageInstalled } = await import('../../src/utils/dependencies')

await ensurePackageInstalled('@nuxt/content-not-installed')

expect(logger.prompt).not.toHaveBeenCalled()
expect(logger.error).toHaveBeenLastCalledWith(expect.stringContaining('npm install @nuxt/content-not-installed'))
expect(exit).toHaveBeenCalledWith(1)
})

test('reports how to install the package instead of prompting in CI', async () => {
vi.doMock('std-env', async importOriginal => ({
...(await importOriginal<typeof import('std-env')>()),
hasTTY: true,
isCI: true,
}))
const exit = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never)
const { ensurePackageInstalled } = await import('../../src/utils/dependencies')

await ensurePackageInstalled('@nuxt/content-not-installed')

expect(logger.prompt).not.toHaveBeenCalled()
expect(logger.error).toHaveBeenLastCalledWith(expect.stringContaining('npm install @nuxt/content-not-installed'))
expect(exit).toHaveBeenCalledWith(1)
})

test('prompts to install the package when interactive and not in CI', async () => {
vi.doMock('std-env', async importOriginal => ({
...(await importOriginal<typeof import('std-env')>()),
hasTTY: true,
isCI: false,
}))
vi.spyOn(process, 'exit').mockImplementation(() => undefined as never)
const { ensurePackageInstalled } = await import('../../src/utils/dependencies')

await ensurePackageInstalled('@nuxt/content-not-installed')

expect(logger.prompt).toHaveBeenCalled()
})
})
Loading