Files
vue-cli/packages/@vue/cli-ui/apollo-server/connectors/client-addons.js
T
Haoqun Jiang 0f377bd31d feat: upgrade to eslint 6 (#4933)
* feat: scaffold projects with eslint 6

* style: eslint fix

* refactor: do not use hard-coded ecmaVersion, use babel-eslint for now

* fix: upgrade to @vue/eslint-config-standard

* style: continue fix lint errors

* chore: upgrade to eslint-plugin-vue@^6.1.2

* refactor: use `ecmaVersion: 2020` for dynamic import syntax support

* test: fix baseESLintConfig

* chore: also update yarn.lock to fix CI caches

* chore: update lockfile again, fix babel regressions

* test: nightwatch tests should fail if lint errors occur

* chore: update the lockfile (again), fixing a bug in airbnb config
2020-01-14 10:13:54 +08:00

79 lines
1.7 KiB
JavaScript

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 ${id} not found in loaded addons. Try opening a vue-cli project first?`)
}
}
module.exports = {
list,
add,
remove,
findOne,
getUrl,
serve,
clear
}