Skip to content

Commit

Permalink
feat(ui): api.hasPlugin(id)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jun 10, 2018
1 parent be3d538 commit 8fae98e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/dev-guide/ui-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,18 @@ There are some builtin icons:
- `'done'`
- `'error'`

## Other plugins

### hasPlugin

Returns `true` if the project uses the plugin.

```js
api.hasPlugin('eslint')
api.hasPlugin('apollo')
api.hasPlugin('vue-cli-plugin-apollo')
```

## Hooks

Hooks allows to react to certain cli-ui events.
Expand Down
12 changes: 11 additions & 1 deletion packages/@vue/cli-ui/src/graphql-api/api/PluginApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const views = require('../connectors/views')
// Utils
const ipc = require('../utils/ipc')
const { notify } = require('../utils/notification')
const { matchesPluginId } = require('@vue/cli-shared-utils')
// Validators
const { validateConfiguration } = require('./configuration')
const { validateDescribeTask, validateAddTask } = require('./task')
Expand All @@ -13,11 +14,12 @@ const { validateView, validateBadge } = require('./view')
const { validateNotify } = require('./notify')

class PluginApi {
constructor (context) {
constructor ({ plugins }, context) {
// Context
this.context = context
this.pluginId = null
this.project = null
this.plugins = plugins
// Hooks
this.projectOpenHooks = []
this.pluginReloadHooks = []
Expand Down Expand Up @@ -284,6 +286,14 @@ class PluginApi {
}
}

/**
* Indicates if a specific plugin is used by the project
* @param {string} id Plugin id or short id
*/
hasPlugin (id) {
return this.plugins.some(p => matchesPluginId(id, p.id))
}

/* Namespaced */

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/@vue/cli-ui/src/graphql-api/connectors/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ function resetPluginApi (context) {
}
sharedData.unWatchAll()

pluginApi = new PluginApi(context)
pluginApi = new PluginApi({
plugins
}, context)
// Run Plugin API
plugins.forEach(plugin => runPluginApi(plugin.id, context))
runPluginApi(cwd.get(), context, 'vue-cli-ui')
Expand Down
3 changes: 3 additions & 0 deletions packages/@vue/cli-ui/vue-cli-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
module.exports = api => {
if (!process.env.VUE_CLI_UI_DEV) return

console.log('has(eslint)', api.hasPlugin('eslint'))
console.log('has(typescript)', api.hasPlugin('typescript'))

// Add dynamic components in dev mode (webpack dashboard & analyzer)
api.addClientAddon({
id: 'vue-webpack',
Expand Down

0 comments on commit 8fae98e

Please sign in to comment.