UNPKG

1.67 kBJavaScriptView Raw
1const resolveFrom = require('resolve-from')
2const logger = require('@poi/logger')
3const isLocalPath = require('./isLocalPath')
4const PoiError = require('./PoiError')
5
6const normalizePluginName = (name, cwd) => {
7 if (isLocalPath(name)) return name
8
9 // @poi/foo => @poi/plugin-foo
10 // @my-org/hehe => @my-org/poi-plugin-hehe
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 // if a prefixed name exists, use it directly
20 if (resolveFrom.silent(cwd, prefixedName)) {
21 return prefixedName
22 }
23
24 return name
25}
26
27exports.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
52exports.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}