-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
110 lines (103 loc) · 2.6 KB
/
server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import chokidar from 'chokidar';
import express from 'express';
import graphQLHTTP from 'express-graphql';
import path from 'path';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import { clean } from 'require-clean';
import { exec } from 'child_process';
const APP_PORT = 3000;
const GRAPHQL_PORT = 4756;
let graphQLServer;
let appServer;
function startAppServer(callback) {
// Serve the Relay app
const compiler = webpack({
entry: path.resolve(__dirname, 'src', 'index.jsx'),
module: {
loaders: [
{
exclude: /node_modules/,
loader: 'babel-loader',
test: /\.(js|jsx)?$/,
},
],
rules: [
{
test: /\.jsx$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {},
},
},
],
},
plugins: [
new webpack.EnvironmentPlugin({
MapboxAccessToken:
'pk.eyJ1IjoibGltaW5hbDE4IiwiYSI6ImNqYzhiZm95dDA0NDkzM2xndjVwd2o5c20ifQ.ECbY-ZvwX_SAwhSxSN2IHQ',
}),
],
output: { filename: './index.js', path: '/', publicPath: '/src/' },
});
appServer = new WebpackDevServer(compiler, {
contentBase: '/public',
proxy: { '/graphql': `http://localhost:${GRAPHQL_PORT}` },
publicPath: '/src/',
stats: { colors: true },
});
// Serve static resources
appServer.use('/', express.static(path.resolve(__dirname, 'public')));
appServer.listen(APP_PORT, () => {
console.log(`App is now running on http://localhost:${APP_PORT}`);
if (callback) {
callback();
}
});
}
function startGraphQLServer(callback) {
// Expose a GraphQL endpoint
clean('./data/schema');
const { Schema } = require('./data/schema');
const graphQLApp = express();
graphQLApp.use(
'/',
graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
})
);
graphQLServer = graphQLApp.listen(GRAPHQL_PORT, () => {
console.log(
`GraphQL server is now running on http://localhost:${GRAPHQL_PORT}`
);
if (callback) {
callback();
}
});
}
function startServers(callback) {
// Shut down the servers
if (appServer) {
appServer.listeningApp.close();
}
if (graphQLServer) {
graphQLServer.close();
}
// Compile the schema
exec('npm run update-schema', (error, stdout) => {
console.log(stdout);
let doneTasks = 0;
function handleTaskDone() {
doneTasks++;
if (doneTasks === 2 && callback) {
callback();
}
}
startGraphQLServer(handleTaskDone);
startAppServer(handleTaskDone);
});
}
startServers();