-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathnpm.ts
186 lines (169 loc) · 4.65 KB
/
npm.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { chalk } from './terminal'
import * as Util from './utils'
import type TResolve from 'resolve'
const PEERS = /UNMET PEER DEPENDENCY ([a-z\-0-9.]+)@(.+)/gm
const npmCached = {}
const erroneous: string[] = []
type pluginFunction = (
pluginName: string,
content: string | null,
file: string,
config: Record<string, any>,
root: string
) => any
export interface IInstallOptions {
dev: boolean
peerDependencies?: boolean
}
const defaultInstallOptions: IInstallOptions = {
dev: false,
peerDependencies: true,
}
export const taroPluginPrefix = '@tarojs/plugin-'
export function resolveNpm(pluginName: string, root?: string): Promise<string> {
const resolvePath = require('resolve') as typeof TResolve
if (!npmCached[pluginName]) {
return new Promise((resolve, reject) => {
resolvePath(`${pluginName}`, { basedir: root }, (err, res) => {
if (err) {
return reject(err)
}
npmCached[pluginName] = res
resolve(res || '')
})
})
}
return Promise.resolve(npmCached[pluginName])
}
export function resolveNpmSync(pluginName: string, root?: string): string {
const resolvePath = require('resolve') as typeof TResolve
try {
if (!npmCached[pluginName]) {
const res = resolvePath.sync(pluginName, { basedir: root })
return res
}
return npmCached[pluginName]
} catch (err: any) {
if (err.code === 'MODULE_NOT_FOUND') {
console.log(chalk.cyan(`缺少npm包${pluginName},开始安装...`))
const installOptions: IInstallOptions = {
dev: false,
}
if (pluginName.indexOf(taroPluginPrefix) >= 0) {
installOptions.dev = true
}
installNpmPkg(pluginName, installOptions)
return resolveNpmSync(pluginName, root)
}
return ''
}
}
export function installNpmPkg(pkgList: string[] | string, options: IInstallOptions) {
if (!pkgList) {
return
}
if (!Array.isArray(pkgList)) {
pkgList = [pkgList]
}
pkgList = pkgList.filter((dep) => {
return erroneous.indexOf(dep) === -1
})
if (!pkgList.length) {
return
}
options = Object.assign({}, defaultInstallOptions, options)
let installer = ''
let args: string[] = []
if (Util.shouldUseYarn()) {
installer = 'yarn'
} else if (Util.shouldUseCnpm()) {
installer = 'cnpm'
} else {
installer = 'npm'
}
if (Util.shouldUseYarn()) {
args = ['add'].concat(pkgList).filter(Boolean)
args.push('--silent', '--no-progress')
if (options.dev) {
args.push('-D')
}
} else {
args = ['install'].concat(pkgList).filter(Boolean)
args.push('--silent', '--no-progress')
if (options.dev) {
args.push('--save-dev')
} else {
args.push('--save')
}
}
const spawn = require('cross-spawn')
const output = spawn.sync(installer, args, {
stdio: ['ignore', 'pipe', 'inherit'],
})
if (output.status) {
pkgList.forEach((dep) => {
erroneous.push(dep)
})
}
let matches: RegExpExecArray | null = null
const peers: string[] = []
while ((matches = PEERS.exec(output.stdout))) {
const pkg = matches[1]
const version = matches[2]
if (version.match(' ')) {
peers.push(pkg)
} else {
peers.push(`${pkg}@${version}`)
}
}
if (options.peerDependencies && peers.length) {
console.info('正在安装 peerDependencies...')
installNpmPkg(peers, options)
}
return output
}
export const callPlugin: pluginFunction = async (
pluginName: string,
content: string | null,
file: string,
config: Record<string, any>,
root: string
) => {
const pluginFn = await getNpmPkg(`${taroPluginPrefix}${pluginName}`, root)
return pluginFn(content, file, config)
}
export const callPluginSync: pluginFunction = (
pluginName: string,
content: string | null,
file: string,
config: Record<string, any>,
root: string
) => {
const pluginFn = getNpmPkgSync(`${taroPluginPrefix}${pluginName}`, root)
return pluginFn(content, file, config)
}
export function getNpmPkgSync(npmName: string, root: string) {
const npmPath = resolveNpmSync(npmName, root)
const npmFn = require(npmPath)
return npmFn
}
export async function getNpmPkg(npmName: string, root: string) {
let npmPath
try {
npmPath = resolveNpmSync(npmName, root)
} catch (err: any) {
if (err.code === 'MODULE_NOT_FOUND') {
console.log(chalk.cyan(`缺少npm包${npmName},开始安装...`))
const installOptions: IInstallOptions = {
dev: false,
}
if (npmName.indexOf(taroPluginPrefix) >= 0) {
installOptions.dev = true
}
installNpmPkg(npmName, installOptions)
npmPath = await resolveNpm(npmName, root)
}
}
const npmFn = require(npmPath)
return npmFn
}