Skip to content

Commit a8c441c

Browse files
author
Guillaume Chau
committed
feat(ui): hooks
1 parent c29669b commit a8c441c

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

docs/plugin-dev-ui.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This guide will walk you through the development of cli-ui specific features for
1010
- [Shared data](#shared-data)
1111
- [Plugin actions](#plugin-actions)
1212
- [IPC](#ipc)
13+
- [Hooks](#hooks)
1314
- [Public static files](#public-static-files)
1415

1516
## Plugin Info
@@ -630,6 +631,26 @@ api.ipcSend({
630631
})
631632
```
632633

634+
### Hooks
635+
636+
Hooks allows to react to certain cli-ui events.
637+
638+
`onProjectOpen`: Called when the plugin is loaded for the first time for the current project.
639+
640+
```js
641+
api.onProjectOpen((project, previousProject) => {
642+
// Reset data
643+
})
644+
```
645+
646+
`onPluginReload`: Called when the plugin is reloaded.
647+
648+
```js
649+
api.onPluginReload((project) => {
650+
console.log('plugin reloaded')
651+
})
652+
```
653+
633654
### Public static files
634655

635656
You may need to expose some static files over the cli-ui builtin HTTP server (typically if you want to specify an icon to a custom view).

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ module.exports = api => {
2323
}
2424

2525
// Init data
26-
if (typeof getSharedData('serve-status') === 'undefined') {
26+
api.onProjectOpen(() => {
2727
for (const key of ['serve', 'build']) {
2828
resetSharedData(key)
2929
}
30-
}
30+
})
3131

3232
// Tasks
3333
const views = {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class PluginApi {
1515
// Context
1616
this.context = context
1717
this.pluginId = null
18+
this.project = null
19+
// Hooks
20+
this.projectOpenHooks = []
21+
this.pluginReloadHooks = []
1822
// Data
1923
this.configurations = []
2024
this.tasks = []
@@ -23,6 +27,28 @@ class PluginApi {
2327
this.actions = new Map()
2428
}
2529

30+
/**
31+
* Register an handler called when the project is open (only if this plugin is loaded).
32+
*
33+
* @param {function} cb Handler
34+
*/
35+
onProjectOpen (cb) {
36+
if (this.project) {
37+
cb(this.project)
38+
return
39+
}
40+
this.projectOpenHooks.push(cb)
41+
}
42+
43+
/**
44+
* Register an handler called when the plugin is reloaded.
45+
*
46+
* @param {function} cb Handler
47+
*/
48+
onPluginReload (cb) {
49+
this.pluginReloadHooks.push(cb)
50+
}
51+
2652
/**
2753
* Describe a project configuration (usually for config file like `.eslintrc.json`).
2854
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const cwd = require('./cwd')
2424
const folders = require('./folders')
2525
const prompts = require('./prompts')
2626
const progress = require('./progress')
27+
const projects = require('./projects')
2728
const logs = require('./logs')
2829
const clientAddons = require('./client-addons')
2930
const views = require('./views')
@@ -50,6 +51,7 @@ let eventsInstalled = false
5051
let plugins = []
5152
let pluginApi
5253
let installationStep
54+
let projectId
5355

5456
function getPath (id) {
5557
return path.dirname(resolveModule(id, cwd.get()))
@@ -93,6 +95,16 @@ function resetPluginApi (context) {
9395
pluginApi.clientAddons.forEach(options => clientAddons.add(options, context))
9496
// Add views
9597
pluginApi.views.forEach(view => views.add(view, context))
98+
99+
const project = projects.getCurrent(context)
100+
if (!project) return
101+
if (projectId !== project.id) {
102+
projectId = project.id
103+
pluginApi.projectOpenHooks.forEach(fn => fn(project, projects.getLast(context)))
104+
pluginApi.project = project
105+
} else {
106+
pluginApi.pluginReloadHooks.forEach(fn => fn(project))
107+
}
96108
}
97109

98110
function runPluginApi (id, context, fileName = 'ui') {
@@ -306,6 +318,7 @@ function update (id, context) {
306318
const plugin = findOne(id, context)
307319
const { current, wanted } = await getVersion(plugin, context)
308320
await updatePackage(cwd.get(), getCommand(), null, id)
321+
resetPluginApi(context)
309322
logs.add({
310323
message: `Plugin ${id} updated from ${current} to ${wanted}`,
311324
type: 'info'

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const getContext = require('../context')
1919

2020
const PROGRESS_ID = 'project-create'
2121

22+
let lastProject = null
2223
let currentProject = null
2324
let creator = null
2425
let presets = []
@@ -35,6 +36,10 @@ function getCurrent (context) {
3536
return currentProject
3637
}
3738

39+
function getLast (context) {
40+
return lastProject
41+
}
42+
3843
function generatePresetDescription (preset) {
3944
let description = `Features: ${preset.features.join(', ')}`
4045
if (preset.raw.useConfigFiles) {
@@ -308,6 +313,7 @@ async function open (id, context) {
308313
return null
309314
}
310315

316+
lastProject = currentProject
311317
currentProject = project
312318
cwd.set(project.path, context)
313319
// Load plugins
@@ -354,6 +360,7 @@ function setFavorite ({ id, favorite }, context) {
354360
module.exports = {
355361
list,
356362
getCurrent,
363+
getLast,
357364
getCreation,
358365
applyPreset,
359366
setFeatureEnabled,

0 commit comments

Comments
 (0)