-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgulpfile.js
199 lines (167 loc) · 4.66 KB
/
gulpfile.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const { parallel, series, src, dest, task } = require('gulp');
const del = require('del');
const { readdirSync } = require('fs');
const path = require('path');
const { spawn } = require('child_process');
const nodemon = require('gulp-nodemon');
const inquirer = require('inquirer');
let configFile = undefined;
// **************************
// INSTALL
// **************************
function install(done) {
return runShell('yarn install --frozen-lockfile', undefined, done);
}
// **************************
// CLEANING AND PUBLISHING
// **************************
async function clean() {
await del([
'dist',
'src/server/public',
'src/client/dist',
'src/client/src/shared/*',
]);
}
function copySharedSrc() {
return src('src/server/shared/**/*').pipe(dest('src/client/src/shared'));
}
function publishClientToServer() {
return src('src/client/dist/**/*')
.pipe(dest('src/server/public'))
.pipe(dest('dist/public'));
}
// **************************
// CLIENT/SERVER BUILD
// **************************
function buildClient(done) {
runShell('yarn build', 'src/client', done);
}
function buildServer(done) {
// just run `tsc` rather than use the gulp-typescript plugin.
// output of`tsc` is much nicer, generates consistent source maps,
// and spares an extra dependency.
runShell('yarn tsc', undefined, done);
}
// wrap `buildServer` in a task so nodemon can call it
task('buildServer', buildServer);
/**
* Helper for running a shell command.
* @param cmd Command string (e.g. "yarn install")
* @param cwd Current working directory or undefined for current.
* @param done Done callback.
*/
function runShell(cmd, cwd, done) {
const [command, ...args] = cmd.split(' ');
const proc = spawn(command, args, {
stdio: 'inherit',
cwd: cwd ? path.resolve(cwd) : undefined,
});
return proc.on('close', (status) => {
if (status !== 0) {
done(new Error(`Command "${cmd}" failed with return code: ${status}`));
} else {
done();
}
});
}
function watchClient(done) {
return runShell('yarn serve', './src/client', done);
}
// **************************
// CONFIG FILE MANAGEMENT
// **************************
async function loadConfig(done) {
const availableOverrides = readdirSync('./src/server/config/overrides')
.filter((filename) => filename.endsWith('.ts'))
.map((filename) => filename.split('.')[0]);
if (availableOverrides.length > 1) {
try {
const answers = await inquirer.prompt([
{
name: 'configFile',
type: 'list',
choices: availableOverrides,
message: 'Multiple configuration files found. Please choose one:',
},
]);
configFile = answers.configFile;
done();
} catch (err) {
done(err);
}
} else if (availableOverrides.length === 1) {
configFile = availableOverrides[0];
} else {
done();
}
}
// **************************
// NODEMON
// **************************
function nodeDevServer(done) {
const stream = nodemon({
script: 'dist/index.js',
watch: ['./src/server'],
tasks: 'buildServer',
ext: 'ts',
nodeArgs: ['--inspect', '--max-http-header-size=81920'],
env: {
NODE_ENV: 'development',
// use the config file chosen by the user or use undefined to use base config
DATA_EXPLORER_CONFIG_NAME: configFile,
},
});
stream
.on('start', (msg) => {
console.log(`nodemon started: ${msg}`);
})
.on('restart', (msg) => {
console.log(`restarted! due to ${msg}`);
})
.on('crash', () => {
console.error('Application has crashed!\n');
stream.emit('restart', 10); // restart the server in 10 seconds
})
.on('quit', () => {
console.log('quit');
done();
});
return stream;
}
// **************************
// DOCKER DEV ENV
// **************************
function dockerDevEnv(done) {
runShell('yarn docker:dev', undefined, done);
}
// **************************
// TESTS
// **************************
function testServer(done) {
runShell('yarn test:unit', undefined, done);
}
function testClient(done) {
runShell('yarn test:unit', './src/client', done);
}
// **************************
// EXPORTED BUILD TASKS
// **************************
const build = series(
buildServer,
copySharedSrc,
buildClient,
publishClientToServer,
);
const test = series(copySharedSrc, testServer, testClient);
exports.build = build;
exports.ci = series(clean, install, build, test);
exports.clean = clean;
exports.dev = series(
copySharedSrc,
dockerDevEnv,
loadConfig,
buildServer,
parallel(nodeDevServer, watchClient),
);
exports.test = series(build, test); // tests need some static files in the server directory