Skip to content

Commit 8fae98e

Browse files
committedJun 10, 2018
feat(ui): api.hasPlugin(id)
1 parent be3d538 commit 8fae98e

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed
 

‎docs/dev-guide/ui-api.md

+12
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,18 @@ There are some builtin icons:
10391039
- `'done'`
10401040
- `'error'`
10411041

1042+
## Other plugins
1043+
1044+
### hasPlugin
1045+
1046+
Returns `true` if the project uses the plugin.
1047+
1048+
```js
1049+
api.hasPlugin('eslint')
1050+
api.hasPlugin('apollo')
1051+
api.hasPlugin('vue-cli-plugin-apollo')
1052+
```
1053+
10421054
## Hooks
10431055

10441056
Hooks allows to react to certain cli-ui events.

‎packages/@vue/cli-ui/src/graphql-api/api/PluginApi.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const views = require('../connectors/views')
55
// Utils
66
const ipc = require('../utils/ipc')
77
const { notify } = require('../utils/notification')
8+
const { matchesPluginId } = require('@vue/cli-shared-utils')
89
// Validators
910
const { validateConfiguration } = require('./configuration')
1011
const { validateDescribeTask, validateAddTask } = require('./task')
@@ -13,11 +14,12 @@ const { validateView, validateBadge } = require('./view')
1314
const { validateNotify } = require('./notify')
1415

1516
class PluginApi {
16-
constructor (context) {
17+
constructor ({ plugins }, context) {
1718
// Context
1819
this.context = context
1920
this.pluginId = null
2021
this.project = null
22+
this.plugins = plugins
2123
// Hooks
2224
this.projectOpenHooks = []
2325
this.pluginReloadHooks = []
@@ -284,6 +286,14 @@ class PluginApi {
284286
}
285287
}
286288

289+
/**
290+
* Indicates if a specific plugin is used by the project
291+
* @param {string} id Plugin id or short id
292+
*/
293+
hasPlugin (id) {
294+
return this.plugins.some(p => matchesPluginId(id, p.id))
295+
}
296+
287297
/* Namespaced */
288298

289299
/**

‎packages/@vue/cli-ui/src/graphql-api/connectors/plugins.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ function resetPluginApi (context) {
101101
}
102102
sharedData.unWatchAll()
103103

104-
pluginApi = new PluginApi(context)
104+
pluginApi = new PluginApi({
105+
plugins
106+
}, context)
105107
// Run Plugin API
106108
plugins.forEach(plugin => runPluginApi(plugin.id, context))
107109
runPluginApi(cwd.get(), context, 'vue-cli-ui')

‎packages/@vue/cli-ui/vue-cli-ui.js

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
module.exports = api => {
33
if (!process.env.VUE_CLI_UI_DEV) return
44

5+
console.log('has(eslint)', api.hasPlugin('eslint'))
6+
console.log('has(typescript)', api.hasPlugin('typescript'))
7+
58
// Add dynamic components in dev mode (webpack dashboard & analyzer)
69
api.addClientAddon({
710
id: 'vue-webpack',

0 commit comments

Comments
 (0)