1 | const { log, error, openBrowser } = require('@vue/cli-shared-utils')
|
2 | const { portfinder, server } = require('@vue/cli-ui/server')
|
3 | const shortid = require('shortid')
|
4 |
|
5 | function simpleCorsValidation (allowedHost) {
|
6 | return function (req, socket) {
|
7 | const { host, origin } = req.headers
|
8 |
|
9 | const hostRegExp = new RegExp(`^https?://(${host}|${allowedHost}|localhost)(:\\d+)?$`)
|
10 |
|
11 | if (!origin || !hostRegExp.test(origin)) {
|
12 | socket.destroy()
|
13 | }
|
14 | }
|
15 | }
|
16 |
|
17 | async function ui (options = {}, context = process.cwd()) {
|
18 | const host = options.host || 'localhost'
|
19 |
|
20 | let port = options.port
|
21 | if (!port) {
|
22 | port = await portfinder.getPortPromise()
|
23 | }
|
24 |
|
25 |
|
26 | process.env.VUE_APP_CLI_UI_URL = ''
|
27 |
|
28 |
|
29 | const nodeEnv = process.env.NODE_ENV
|
30 | process.env.NODE_ENV = 'production'
|
31 |
|
32 |
|
33 | if (options.dev) {
|
34 | process.env.VUE_APP_CLI_UI_DEBUG = true
|
35 | }
|
36 |
|
37 | if (!process.env.VUE_CLI_IPC) {
|
38 |
|
39 | process.env.VUE_CLI_IPC = `vue-cli-${shortid()}`
|
40 | }
|
41 |
|
42 | if (!options.quiet) log(`🚀 Starting GUI...`)
|
43 |
|
44 | const opts = {
|
45 | host,
|
46 | port,
|
47 | graphqlPath: '/graphql',
|
48 | subscriptionsPath: '/graphql',
|
49 | enableMocks: false,
|
50 | enableEngine: false,
|
51 | cors: {
|
52 | origin: host
|
53 | },
|
54 | timeout: 1000000,
|
55 | quiet: true,
|
56 | paths: {
|
57 | typeDefs: require.resolve('@vue/cli-ui/apollo-server/type-defs.js'),
|
58 | resolvers: require.resolve('@vue/cli-ui/apollo-server/resolvers.js'),
|
59 | context: require.resolve('@vue/cli-ui/apollo-server/context.js'),
|
60 | pubsub: require.resolve('@vue/cli-ui/apollo-server/pubsub.js'),
|
61 | server: require.resolve('@vue/cli-ui/apollo-server/server.js'),
|
62 | directives: require.resolve('@vue/cli-ui/apollo-server/directives.js')
|
63 | }
|
64 | }
|
65 |
|
66 | const { httpServer } = server(opts, () => {
|
67 |
|
68 | if (typeof nodeEnv === 'undefined') {
|
69 | delete process.env.NODE_ENV
|
70 | } else {
|
71 | process.env.NODE_ENV = nodeEnv
|
72 | }
|
73 |
|
74 |
|
75 | const url = `http://${host}:${port}`
|
76 | if (!options.quiet) log(`🌠Ready on ${url}`)
|
77 | if (options.headless) {
|
78 | console.log(port)
|
79 | } else {
|
80 | openBrowser(url)
|
81 | }
|
82 | })
|
83 |
|
84 | httpServer.on('upgrade', simpleCorsValidation(host))
|
85 | }
|
86 |
|
87 | module.exports = (...args) => {
|
88 | return ui(...args).catch(err => {
|
89 | error(err)
|
90 | if (!process.env.VUE_CLI_TEST) {
|
91 | process.exit(1)
|
92 | }
|
93 | })
|
94 | }
|