This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- support --plugin, --plugins flags - better error handling for missing plugins
- Loading branch information
Showing
8 changed files
with
144 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { mergePlugins } = require('../plugins') | ||
|
||
test('merge', () => { | ||
expect( | ||
mergePlugins( | ||
[{ resolve: 'foo' }, { resolve: 'bar', options: { foo: 'bar' } }], | ||
[ | ||
{ | ||
resolve: 'foo' | ||
}, | ||
{ | ||
resolve: 'baz' | ||
} | ||
] | ||
) | ||
).toEqual([ | ||
{ resolve: 'foo' }, | ||
{ resolve: 'bar', options: { foo: 'bar' } }, | ||
{ | ||
resolve: 'baz' | ||
} | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,16 @@ | ||
const cac = require('cac') | ||
|
||
module.exports = _args => { | ||
const args = _args.reduce((res, arg) => { | ||
return res.concat( | ||
arg.startsWith('-') && arg.includes('=') ? arg.split('=') : arg | ||
) | ||
}, []) | ||
const cli = cac() | ||
const { options } = cli.parse(_args) | ||
|
||
return { | ||
getValue(name) { | ||
if (!this.has(name)) return | ||
|
||
const index = args.indexOf(name.length === 1 ? `-${name}` : `--${name}`) | ||
return args[index + 1] | ||
get(name) { | ||
return options[name] | ||
}, | ||
|
||
has(name) { | ||
if (name.length > 1) { | ||
return args.includes(`--${name}`) | ||
} | ||
|
||
const RE = new RegExp(`^-([a-zA-Z]+)`) | ||
return args.find(arg => { | ||
return RE.test(arg) && RE.exec(arg)[1].includes(name) | ||
}) | ||
return this.get(name) !== undefined | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const resolveFrom = require('resolve-from') | ||
const logger = require('@poi/logger') | ||
const isLocalPath = require('./isLocalPath') | ||
const PoiError = require('./PoiError') | ||
|
||
const normalizePluginName = name => { | ||
if (isLocalPath(name)) return name | ||
|
||
// @poi/foo => @poi/plugin-foo | ||
// @my-org/hehe => @my-org/poi-plugin-hehe | ||
if (/^@[^/]+\//.test(name)) { | ||
return name.replace(/^@([^/]+)\/(plugin-)?/, (_, m1) => { | ||
return m1 === 'poi' ? `@poi/plugin-` : `@${m1}/poi-plugin-` | ||
}) | ||
} | ||
|
||
return name.replace(/^(poi-plugin-)?/, 'poi-plugin-') | ||
} | ||
|
||
exports.normalizePlugins = (plugins, cwd) => { | ||
return [].concat(plugins || []).map(v => { | ||
if (typeof v === 'string') { | ||
v = { resolve: v } | ||
} | ||
if (typeof v.resolve === 'string') { | ||
const pluginName = normalizePluginName(v.resolve) | ||
const resolvedPlugin = resolveFrom.silent(cwd, pluginName) | ||
if (!resolvedPlugin) { | ||
const message = `Cannot find plugin \`${pluginName}\` in your project` | ||
logger.error(message) | ||
logger.error(`Did you forget to install it?`) | ||
throw new PoiError({ | ||
message, | ||
dismiss: true | ||
}) | ||
} | ||
v = Object.assign({}, v, { | ||
resolve: resolvedPlugin | ||
}) | ||
} | ||
return v | ||
}) | ||
} | ||
|
||
exports.mergePlugins = (configPlugins, cliPlugins) => { | ||
return configPlugins.concat( | ||
cliPlugins.filter(cliPlugin => { | ||
return !configPlugins.find( | ||
configPlugin => configPlugin.resolve === cliPlugin.resolve | ||
) | ||
}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
yarn lint && \ | ||
yarn jest && \ | ||
yarn lerna run test && \ | ||
cd test/kitchensink && \ | ||
yarn && \ | ||
yarn test |