Skip to content

Commit 4b9db7c

Browse files
author
Guillaume Chau
committed
fix(ui): refactoring and bug fixes
1 parent c04f69e commit 4b9db7c

File tree

9 files changed

+101
-59
lines changed

9 files changed

+101
-59
lines changed

packages/@vue/cli-ui/apollo-server/api/PluginApi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const { validateSuggestion } = require('./suggestion')
2121
const { validateProgress } = require('./progress')
2222

2323
class PluginApi {
24-
constructor ({ plugins, file }, context) {
24+
constructor ({ plugins, file, project }, context) {
2525
// Context
2626
this.context = context
2727
this.pluginId = null
28-
this.project = null
28+
this.project = project
2929
this.plugins = plugins
3030
this.cwd = file
3131
// Hooks

packages/@vue/cli-ui/apollo-server/connectors/dependencies.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ function list (file, context) {
4040
const pkg = folders.readPackage(file, context)
4141
dependencies = []
4242
dependencies = dependencies.concat(
43-
findDependencies(pkg.devDependencies || {}, 'devDependencies', context)
43+
findDependencies(pkg.devDependencies || {}, 'devDependencies', file, context)
4444
)
4545
dependencies = dependencies.concat(
46-
findDependencies(pkg.dependencies || {}, 'dependencies', context)
46+
findDependencies(pkg.dependencies || {}, 'dependencies', file, context)
4747
)
4848
return dependencies
4949
}
@@ -54,35 +54,37 @@ function findOne (id, context) {
5454
)
5555
}
5656

57-
function findDependencies (deps, type, context) {
57+
function findDependencies (deps, type, file, context) {
5858
return Object.keys(deps).filter(
5959
id => !isPlugin(id) && id !== CLI_SERVICE
6060
).map(
6161
id => ({
6262
id,
6363
versionRange: deps[id],
64-
installed: fs.existsSync(getPath(id)),
65-
website: getLink(id, context),
66-
type
64+
installed: fs.existsSync(getPath({ id, file })),
65+
website: getLink({ id, file }, context),
66+
type,
67+
baseFir: file
6768
})
6869
)
6970
}
7071

71-
function getPath (id) {
72-
return resolveModuleRoot(resolveModule(path.join(id, 'package.json'), cwd.get()), id)
72+
function getPath ({ id, file = cwd.get() }) {
73+
const filePath = resolveModule(path.join(id, 'package.json'), file)
74+
return resolveModuleRoot(filePath, id)
7375
}
7476

75-
function readPackage (id, context) {
77+
function readPackage ({ id, file }, context) {
7678
try {
77-
return folders.readPackage(getPath(id), context)
79+
return folders.readPackage(getPath({ id, file }), context)
7880
} catch (e) {
7981
console.log(e)
8082
}
8183
return {}
8284
}
8385

84-
function invalidatePackage (id, context) {
85-
return folders.invalidatePackage(getPath(id), context)
86+
function invalidatePackage ({ id, file }, context) {
87+
return folders.invalidatePackage(getPath({ id, file }), context)
8688
}
8789

8890
async function getMetadata (id, context) {
@@ -116,10 +118,10 @@ async function getMetadata (id, context) {
116118
}
117119
}
118120

119-
async function getVersion ({ id, installed, versionRange }, context) {
121+
async function getVersion ({ id, installed, versionRange, baseDir }, context) {
120122
let current
121123
if (installed) {
122-
const pkg = readPackage(id, context)
124+
const pkg = readPackage({ id, file: baseDir }, context)
123125
current = pkg.version
124126
} else {
125127
current = null
@@ -152,8 +154,8 @@ async function getDescription ({ id }, context) {
152154
return null
153155
}
154156

155-
function getLink (id, context) {
156-
const pkg = readPackage(id, context)
157+
function getLink ({ id, file }, context) {
158+
const pkg = readPackage({ id, file }, context)
157159
return pkg.homepage ||
158160
(pkg.repository && pkg.repository.url) ||
159161
`https://www.npmjs.com/package/${id.replace(`/`, `%2F`)}`

packages/@vue/cli-ui/apollo-server/connectors/plugins.js

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const path = require('path')
22
const fs = require('fs')
33
const LRU = require('lru-cache')
44
const chalk = require('chalk')
5+
// Context
6+
const getContext = require('../context')
57
// Subs
68
const channels = require('../channels')
79
// Connectors
@@ -52,11 +54,11 @@ let installationStep
5254
let pluginsStore = new Map()
5355
let pluginApiInstances = new Map()
5456

55-
async function list (file, context, resetApi = true) {
57+
async function list (file, context, { resetApi = true, lightApi = false, autoLoadApi = true } = {}) {
5658
const pkg = folders.readPackage(file, context)
5759
let plugins = []
58-
plugins = plugins.concat(findPlugins(pkg.devDependencies || {}))
59-
plugins = plugins.concat(findPlugins(pkg.dependencies || {}))
60+
plugins = plugins.concat(findPlugins(pkg.devDependencies || {}, file))
61+
plugins = plugins.concat(findPlugins(pkg.dependencies || {}, file))
6062

6163
// Put cli service at the top
6264
const index = plugins.findIndex(p => p.id === CLI_SERVICE)
@@ -68,27 +70,34 @@ async function list (file, context, resetApi = true) {
6870

6971
pluginsStore.set(file, plugins)
7072

71-
if (resetApi || !pluginApiInstances.get(file)) await resetPluginApi({ file }, context)
73+
log('Plugins found:', plugins.length, chalk.grey(file))
74+
75+
if (resetApi || (autoLoadApi && !pluginApiInstances.has(file))) {
76+
await resetPluginApi({ file, lightApi }, context)
77+
}
7278
return plugins
7379
}
7480

7581
function findOne ({ id, file }, context) {
7682
const plugins = getPlugins(file)
77-
return plugins.find(
83+
const plugin = plugins.find(
7884
p => p.id === id
7985
)
86+
if (!plugin) log('Plugin Not found', id, chalk.grey(file))
87+
return plugin
8088
}
8189

82-
function findPlugins (deps) {
90+
function findPlugins (deps, file) {
8391
return Object.keys(deps).filter(
8492
id => isPlugin(id) || id === CLI_SERVICE
8593
).map(
8694
id => ({
8795
id,
8896
versionRange: deps[id],
8997
official: isOfficialPlugin(id) || id === CLI_SERVICE,
90-
installed: fs.existsSync(dependencies.getPath(id)),
91-
website: getPluginLink(id)
98+
installed: fs.existsSync(dependencies.getPath({ id, file })),
99+
website: getPluginLink(id),
100+
baseDir: file
92101
})
93102
)
94103
}
@@ -99,8 +108,10 @@ function getPlugins (file) {
99108
return plugins
100109
}
101110

102-
function resetPluginApi ({ file }, context) {
111+
function resetPluginApi ({ file, lightApi }, context) {
103112
return new Promise((resolve, reject) => {
113+
log('Plugin API reloading...', chalk.grey(file))
114+
104115
let pluginApi = pluginApiInstances.get(file)
105116
let projectId
106117

@@ -110,25 +121,30 @@ function resetPluginApi ({ file }, context) {
110121
pluginApi.views.forEach(r => views.remove(r.id, context))
111122
pluginApi.ipcHandlers.forEach(fn => ipc.off(fn))
112123
}
113-
sharedData.unWatchAll()
114-
115-
clientAddons.clear(context)
116-
suggestions.clear(context)
124+
if (!lightApi) {
125+
sharedData.unWatchAll(context)
126+
clientAddons.clear(context)
127+
suggestions.clear(context)
128+
}
117129

118130
// Cyclic dependency with projects connector
119131
setTimeout(() => {
120132
const projects = require('./projects')
121-
const project = projects.getCurrent(context)
133+
const project = projects.findByPath(file, context)
122134
const plugins = getPlugins(file)
123135

136+
if (project && projects.getType(project, context) !== 'vue') {
137+
resolve(false)
138+
return
139+
}
140+
124141
pluginApi = new PluginApi({
125142
plugins,
126-
file
143+
file,
144+
project
127145
}, context)
128146
pluginApiInstances.set(file, pluginApi)
129147

130-
if (projects.getType(project) !== 'vue') return
131-
132148
// Run Plugin API
133149
runPluginApi(path.resolve(__dirname, '../../'), pluginApi, context, 'ui-defaults')
134150
plugins.forEach(plugin => runPluginApi(plugin.id, pluginApi, context))
@@ -138,8 +154,11 @@ function resetPluginApi ({ file }, context) {
138154
// Add views
139155
pluginApi.views.forEach(view => views.add(view, context))
140156

141-
if (!project) return
142-
pluginApi.project = project
157+
if (!project || lightApi) {
158+
resolve(true)
159+
return
160+
}
161+
143162
if (projectId !== project.id) {
144163
callHook({
145164
id: 'projectOpen',
@@ -166,7 +185,7 @@ function resetPluginApi ({ file }, context) {
166185
function runPluginApi (id, pluginApi, context, fileName = 'ui') {
167186
let module
168187
try {
169-
module = loadModule(`${id}/${fileName}`, cwd.get(), true)
188+
module = loadModule(`${id}/${fileName}`, pluginApi.cwd, true)
170189
} catch (e) {
171190
if (process.env.VUE_CLI_DEBUG) {
172191
console.error(e)
@@ -181,14 +200,13 @@ function runPluginApi (id, pluginApi, context, fileName = 'ui') {
181200

182201
// Locales
183202
try {
184-
const folder = fs.existsSync(id) ? id : dependencies.getPath(id)
203+
const folder = fs.existsSync(id) ? id : dependencies.getPath({ id, file: pluginApi.cwd })
185204
locales.loadFolder(folder, context)
186205
} catch (e) {}
187206
}
188207

189208
function getApi (folder) {
190209
const pluginApi = pluginApiInstances.get(folder)
191-
if (!pluginApi) throw new Error(`No plugin API available for ${folder}`)
192210
return pluginApi
193211
}
194212

@@ -199,12 +217,13 @@ function callHook ({ id, args, file }, context) {
199217
fns.forEach(fn => fn(...args))
200218
}
201219

202-
async function getLogo ({ id }, context) {
220+
async function getLogo (plugin, context) {
221+
const { id, baseDir } = plugin
203222
const cached = logoCache.get(id)
204223
if (cached) {
205224
return cached
206225
}
207-
const folder = dependencies.getPath(id)
226+
const folder = dependencies.getPath({ id, file: baseDir })
208227
const file = path.join(folder, 'logo.png')
209228
if (fs.existsSync(file)) {
210229
const data = `/_plugin-logo/${encodeURIComponent(id)}`
@@ -342,7 +361,7 @@ function finishInstall (context) {
342361
async function initPrompts (id, context) {
343362
await prompts.reset()
344363
try {
345-
let data = require(path.join(dependencies.getPath(id), 'prompts'))
364+
let data = require(path.join(dependencies.getPath({ id, file: cwd.get() }), 'prompts'))
346365
if (typeof data === 'function') {
347366
data = await data()
348367
}
@@ -386,7 +405,7 @@ function update (id, context) {
386405

387406
async function updateAll (context) {
388407
return progress.wrap('plugins-update', context, async setProgress => {
389-
const plugins = list(cwd.get(), context, false)
408+
const plugins = list(cwd.get(), context, { resetApi: false })
390409
let updatedPlugins = []
391410
for (const plugin of plugins) {
392411
const version = await dependencies.getVersion(plugin, context)
@@ -456,25 +475,35 @@ async function callAction ({ id, params, file = cwd.get() }, context) {
456475
return { id, params, results, errors }
457476
}
458477

459-
function serveFile (projectId, file, res) {
460-
const basePath = projectId === '.' ? cwd.get() : dependencies.getPath(decodeURIComponent(projectId))
478+
function serveFile ({ pluginId, projectId = null, file }, res) {
479+
let baseFile = cwd.get()
480+
if (projectId) {
481+
const projects = require('./projects')
482+
const project = projects.findOne(projectId, getContext())
483+
if (project) {
484+
baseFile = project.path
485+
}
486+
}
487+
488+
const basePath = pluginId === '.' ? baseFile : dependencies.getPath({ id: decodeURIComponent(pluginId), file: baseFile })
461489
if (basePath) {
462490
res.sendFile(path.join(basePath, file))
463491
return
464492
}
465493

466494
res.status(404)
467-
res.send(`Addon ${projectId} not found in loaded addons. Try opening a vue-cli project first?`)
495+
res.send(`Addon ${pluginId} not found in loaded addons. Try opening a vue-cli project first?`)
468496
}
469497

470498
function serve (req, res) {
471-
const { id, 0: file } = req.params
472-
serveFile(id, path.join('ui-public', file), res)
499+
const { pluginId, 0: file } = req.params
500+
serveFile({ pluginId, file: path.join('ui-public', file) }, res)
473501
}
474502

475503
function serveLogo (req, res) {
476-
const { id } = req.params
477-
serveFile(id, 'logo.png', res)
504+
const { id: pluginId } = req.params
505+
const { project: projectId } = req.query
506+
serveFile({ pluginId, projectId, file: 'logo.png' }, res)
478507
}
479508

480509
module.exports = {

packages/@vue/cli-ui/apollo-server/connectors/projects.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ function setFavorite ({ id, favorite }, context) {
418418
return findOne(id, context)
419419
}
420420

421-
function getType (project) {
421+
function getType (project, context) {
422+
if (typeof project === 'string') {
423+
project = findByPath(project, context)
424+
}
425+
if (!project) return 'unknown'
422426
return !project.type ? 'vue' : project.type
423427
}
424428

packages/@vue/cli-ui/apollo-server/connectors/shared-data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function unwatch (id, handler) {
5353
}
5454
}
5555

56-
function unWatchAll () {
56+
function unWatchAll (context) {
5757
watchers = new Map()
5858
}
5959

packages/@vue/cli-ui/apollo-server/connectors/tasks.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const logs = require('./logs')
1010
const plugins = require('./plugins')
1111
const prompts = require('./prompts')
1212
const views = require('./views')
13+
const projects = require('./projects')
1314
// Utils
1415
const { log } = require('../util/logger')
1516
const { notify } = require('../util/notification')
@@ -36,15 +37,19 @@ async function list ({ file = null, api = true } = {}, context) {
3637
if (pkg.scripts) {
3738
const existing = new Map()
3839

39-
await plugins.list(file, context, false)
40+
if (projects.getType(file, context) === 'vue') {
41+
await plugins.list(file, context, { resetApi: false, lightApi: true })
42+
}
43+
44+
const pluginApi = api && plugins.getApi(file)
4045

4146
// Get current valid tasks in project `package.json`
4247
let currentTasks = Object.keys(pkg.scripts).map(
4348
name => {
4449
const id = `${file}:${name}`
4550
existing.set(id, true)
4651
const command = pkg.scripts[name]
47-
const moreData = api ? plugins.getApi(file).getDescribedTask(command) : null
52+
const moreData = pluginApi ? pluginApi.getDescribedTask(command) : null
4853
return {
4954
id,
5055
name,
@@ -58,7 +63,7 @@ async function list ({ file = null, api = true } = {}, context) {
5863
}
5964
)
6065

61-
if (api) {
66+
if (api && pluginApi) {
6267
currentTasks = currentTasks.concat(plugins.getApi(file).addedTasks.map(
6368
task => {
6469
const id = `${file}:${task.name}`

0 commit comments

Comments
 (0)