1 | const resolveFrom = require('resolve-from')
|
2 | const logger = require('@poi/logger')
|
3 | const isLocalPath = require('./isLocalPath')
|
4 | const PoiError = require('./PoiError')
|
5 |
|
6 | const normalizePluginName = (name, cwd) => {
|
7 | if (isLocalPath(name)) return name
|
8 |
|
9 |
|
10 |
|
11 | if (/^@[^/]+\//.test(name)) {
|
12 | return name.replace(/^@([^/]+)\/(poi-)?(plugin-)?/, (_, m1) => {
|
13 | return m1 === 'poi' ? `@poi/plugin-` : `@${m1}/poi-plugin-`
|
14 | })
|
15 | }
|
16 |
|
17 | const prefixedName = name.replace(/^(poi-plugin-)?/, 'poi-plugin-')
|
18 |
|
19 |
|
20 | if (resolveFrom.silent(cwd, prefixedName)) {
|
21 | return prefixedName
|
22 | }
|
23 |
|
24 | return name
|
25 | }
|
26 |
|
27 | exports.normalizePlugins = (plugins, cwd) => {
|
28 | return [].concat(plugins || []).map(v => {
|
29 | if (typeof v === 'string') {
|
30 | v = { resolve: v }
|
31 | }
|
32 | if (typeof v.resolve === 'string') {
|
33 | const pluginName = normalizePluginName(v.resolve, cwd)
|
34 | const resolvedPlugin = resolveFrom.silent(cwd, pluginName)
|
35 | if (!resolvedPlugin) {
|
36 | const message = `Cannot find plugin \`${pluginName}\` in your project`
|
37 | logger.error(message)
|
38 | logger.error(`Did you forget to install it?`)
|
39 | throw new PoiError({
|
40 | message,
|
41 | dismiss: true
|
42 | })
|
43 | }
|
44 | v = Object.assign({}, v, {
|
45 | resolve: resolvedPlugin
|
46 | })
|
47 | }
|
48 | return v
|
49 | })
|
50 | }
|
51 |
|
52 | exports.mergePlugins = (configPlugins, cliPlugins) => {
|
53 | return configPlugins.concat(
|
54 | cliPlugins.filter(cliPlugin => {
|
55 | return !configPlugins.find(
|
56 | configPlugin => configPlugin.resolve === cliPlugin.resolve
|
57 | )
|
58 | })
|
59 | )
|
60 | }
|