-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
features.ts
65 lines (56 loc) · 1.87 KB
/
features.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { addDependency } from 'nypm'
import { resolvePackageJSON } from 'pkg-types'
import { logger, useNuxt } from '@nuxt/kit'
import { isCI, provider } from 'std-env'
const isStackblitz = provider === 'stackblitz'
interface EnsurePackageInstalledOptions {
rootDir: string
searchPaths?: string[]
prompt?: boolean
}
async function promptToInstall (name: string, installCommand: () => Promise<void>, options: EnsurePackageInstalledOptions) {
if (await resolvePackageJSON(name, { url: options.searchPaths }).catch(() => null)) {
return true
}
logger.info(`Package ${name} is missing`)
if (isCI) {
return false
}
// In StackBlitz we install packages automatically by default
if (options.prompt === true || (options.prompt !== false && !isStackblitz)) {
const confirm = await logger.prompt(`Do you want to install ${name} package?`, {
type: 'confirm',
name: 'confirm',
initial: true
})
if (!confirm) {
return false
}
}
logger.info(`Installing ${name}...`)
try {
await installCommand()
logger.success(`Installed ${name}`)
return true
} catch (err) {
logger.error(err)
return false
}
}
// TODO: refactor to Nuxi
const installPrompts = new Set<string>()
export function installNuxtModule (name: string, options?: EnsurePackageInstalledOptions) {
if (installPrompts.has(name)) { return }
installPrompts.add(name)
const nuxt = useNuxt()
return promptToInstall(name, async () => {
const { runCommand } = await import('nuxi')
await runCommand('module', ['add', name, '--cwd', nuxt.options.rootDir])
}, { rootDir: nuxt.options.rootDir, searchPaths: nuxt.options.modulesDir, ...options })
}
export function ensurePackageInstalled (name: string, options: EnsurePackageInstalledOptions) {
return promptToInstall(name, () => addDependency(name, {
cwd: options.rootDir,
dev: true
}), options)
}