UNPKG

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