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'
|
2023-08-14 21:04:47 +00:00
|
|
|
import { isCI } from 'std-env'
|
2023-04-07 15:34:11 +00:00
|
|
|
|
|
|
|
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`)
|
2023-08-14 21:04:47 +00:00
|
|
|
if (isCI) {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-07 15:34:11 +00:00
|
|
|
|
2023-09-14 09:17:55 +00:00
|
|
|
const confirm = await logger.prompt(`Do you want to install ${name} package?`, {
|
2023-04-07 15:34:11 +00:00
|
|
|
type: 'confirm',
|
|
|
|
name: 'confirm',
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|