-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
client-addons.js
78 lines (67 loc) · 1.69 KB
/
client-addons.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const path = require('path')
// Subs
const channels = require('../channels')
// Utils
const { resolveModuleRoot } = require('../util/resolve-path')
const addons = []
let baseUrl = process.env.VUE_APP_CLI_UI_URL
if (typeof baseUrl === 'undefined') {
baseUrl = `http://localhost:${process.env.VUE_APP_GRAPHQL_PORT}`
} else {
baseUrl = baseUrl.replace(/ws:\/\/([a-z0-9_-]+:\d+).*/i, 'http://$1')
}
function list (context) {
return addons
}
function add (options, context) {
if (findOne(options.id)) remove(options.id, context)
addons.push(options)
context.pubsub.publish(channels.CLIENT_ADDON_ADDED, {
clientAddonAdded: options
})
}
function findOne (id, context = null) {
return addons.find(
addon => addon.id === id
)
}
function remove (id, context) {
const index = addons.findIndex(
addon => addon.id === id
)
if (index !== -1) addons.splice(index, 1)
}
function clear (context) {
for (const addon of addons) {
remove(addon.id, context)
}
}
function getUrl (addon, context) {
return addon.url || `${baseUrl}/_addon/${addon.id}/index.js`
}
function serve (req, res) {
const { id, 0: file } = req.params
const addon = findOne(decodeURIComponent(id))
if (addon && addon.path) {
const resolvedPath = require.resolve(addon.path)
const basePath = resolveModuleRoot(resolvedPath)
if (basePath) {
res.sendFile(path.join(basePath, file), { maxAge: 0 })
} else {
res.status(404)
res.send(`File not found (resolved: ${resolvedPath}`)
}
} else {
res.status(404)
res.send('Addon not found in loaded addons. Try opening a vue-cli project first?')
}
}
module.exports = {
list,
add,
remove,
findOne,
getUrl,
serve,
clear
}