@wgoo/cli-utils
Version:
shared utilities for cli packages
76 lines (66 loc) • 1.75 kB
JavaScript
const pluginRE = /^(@wgoo\/|wgoo-|@[\w-]+(\.)?[\w-]+\/wgoo-)cli-plugin-/
const scopeRE = /^@[\w-]+(\.)?[\w-]+\//
const officialRE = /^@wgoo\//
const officialPlugins = [
'babel',
'eslint',
'pwa',
'typescript',
'unit-jest',
'unit-mocha',
'webpack-5'
]
exports.isPlugin = id => pluginRE.test(id)
exports.isOfficialPlugin = id => exports.isPlugin(id) && officialRE.test(id)
exports.toShortPluginId = id => id.replace(pluginRE, '')
exports.resolvePluginId = id => {
// already full id
// e.g. wgoo-cli-plugin-foo, @wgoo/cli-plugin-foo, @bar/wgoo-cli-plugin-foo
if (pluginRE.test(id)) {
return id
}
if (id === '@wgoo/cli-command') {
return id
}
if (officialPlugins.includes(id)) {
return `@wgoo/cli-plugin-${id}`
}
// scoped short
// e.g. @wgoo/foo, @bar/foo
if (id.charAt(0) === '@') {
const scopeMatch = id.match(scopeRE)
if (scopeMatch) {
const scope = scopeMatch[0]
const shortId = id.replace(scopeRE, '')
return `${scope}${scope === '@wgoo/' ? `` : `wgoo-`}cli-plugin-${shortId}`
}
}
// default short
// e.g. foo
return `wgoo-cli-plugin-${id}`
}
exports.matchesPluginId = (input, full) => {
const short = full.replace(pluginRE, '')
return (
// input is full
full === input ||
// input is short without scope
short === input ||
// input is short with scope
short === input.replace(scopeRE, '')
)
}
exports.getPluginLink = id => {
if (officialRE.test(id)) {
return exports.toShortPluginId(id)
}
let pkg = {}
try {
pkg = require(`${id}/package.json`)
} catch (e) {}
return (
pkg.homepage ||
(pkg.repository && pkg.repository.url) ||
`https://www.npmjs.com/package/${id.replace(`/`, `%2F`)}`
)
}