2023-04-07 15:34:11 +00:00
|
|
|
import { addDependency } from 'nypm'
|
2023-07-30 20:25:00 +00:00
|
|
|
import { resolvePackageJSON } from 'pkg-types'
|
2023-04-07 15:34:11 +00:00
|
|
|
import { logger } from '@nuxt/kit'
|
|
|
|
import prompts from 'prompts'
|
|
|
|
|
|
|
|
export async function ensurePackageInstalled (rootDir: string, name: string, searchPaths?: string[]) {
|
2023-07-30 20:25:00 +00:00
|
|
|
if (await resolvePackageJSON(name, { url: searchPaths }).catch(() => null)) {
|
2023-04-07 15:34:11 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`Package ${name} is missing`)
|
|
|
|
|
|
|
|
const { confirm } = await prompts({
|
|
|
|
type: 'confirm',
|
|
|
|
name: 'confirm',
|
|
|
|
message: `Do you want to install ${name} package?`,
|
|
|
|
initial: true
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!confirm) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`Installing ${name}...`)
|
|
|
|
try {
|
|
|
|
await addDependency(name, {
|
|
|
|
cwd: rootDir,
|
|
|
|
dev: true
|
|
|
|
})
|
|
|
|
logger.success(`Installed ${name}`)
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|