-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGulpfile.js
338 lines (284 loc) · 10.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* Inspiration for this file taken from https://github.com/babel/babel/blob/master/Gulpfile.js
*/
require('source-map-support/register');
require('colors');
const { join, sep, resolve, relative } = require('path');
const spawn = require('cross-spawn');
const gulp = require('gulp');
const sourcemaps = require('gulp-sourcemaps');
const babel = require('gulp-babel');
const changed = require('gulp-changed');
const filter = require('gulp-filter');
const plumber = require('gulp-plumber');
const gulpTslint = require('gulp-tslint');
const gulpTypescript = require('gulp-typescript');
const tslint = require('tslint');
const tsProject = gulpTypescript.createProject('tsconfig.json');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const historyApiFallback = require('connect-history-api-fallback');
const browserSync = require('browser-sync').create();
const through = require('through2');
const packagesDirName = 'packages';
const frontendPackageName = 'frontend';
let backendNode = undefined;
function swapSrcWith(srcPath, newDirName) {
// Should look like /packages/<package-name>/src/<rest-of-the-path>
srcPath = relative(__dirname, srcPath);
const parts = srcPath.split(sep);
// Swap out src for the new dir name
parts[2] = newDirName;
return join(__dirname, ...parts);
}
/**
* @param srcPath An absolute path
*/
function swapSrcWithLib(srcPath) {
return swapSrcWith(srcPath, 'lib');
}
function rename(fn) {
return through.obj(function(file, enc, callback) {
file.path = fn(file);
callback(null, file);
});
}
function globFolderFromPackagesDirName(dirName, folderName) {
return [
`./${dirName}/*/${folderName}/**/*.{js,jsx,ts,tsx,html,css}`,
`!./${dirName}/*/${folderName}/**/__mocks__/*.{js,ts,tsx,jsx,html,css}`,
];
}
function globSrcFromPackagesDirName(dirName) {
return globFolderFromPackagesDirName(dirName, 'src');
}
function compilationLogger() {
return through.obj(function(file, enc, callback) {
console.log(`Compiling '${file.relative.cyan}'`);
callback(null, file);
});
}
function errorLogger() {
return plumber({
errorHandler(err) {
console.error(err.stack);
},
});
}
const packagesDir = join(__dirname, packagesDirName);
function packagesSrcStream() {
return gulp.src(globSrcFromPackagesDirName(packagesDirName), { base: packagesDir });
}
function runLinter({ fix }) {
const stream = packagesSrcStream();
const tslintProgram = tslint.Linter.createProgram('./tsconfig.json');
return stream
.pipe(
gulpTslint({
program: tslintProgram,
fix,
formatter: 'stylish',
tslint,
}),
)
.pipe(
gulpTslint.report({
summarizeFailureOutput: true,
}),
);
}
function reloadBrowser(done) {
browserSync.reload();
done();
}
function buildBabel(exclude = []) {
let stream = packagesSrcStream();
if (exclude) {
// We need to exclude things that get bundled
const filters = exclude.map(p => `!**/${p}/**`);
filters.unshift('**');
stream = stream.pipe(filter(filters));
}
return stream
.pipe(errorLogger())
.pipe(changed(packagesDir, { extension: '.js', transformPath: swapSrcWithLib }))
.pipe(compilationLogger())
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(rename(file => (file.path = swapSrcWithLib(file.path))))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(packagesDir));
}
function createWebpackStream(packageDir) {
const createWebpackConfig = require(join(packageDir, 'webpack.config'));
const webpackConfig = createWebpackConfig();
return webpackStream(webpackConfig, webpack);
}
function buildBundle(packageName) {
const packageDir = join(packagesDir, packageName);
const stream = gulp.src(packageDir, 'src', 'index.tsx');
return stream
.pipe(errorLogger())
.pipe(createWebpackStream(packageDir))
.pipe(gulp.dest(join(packageDir, 'dist')));
}
function watchPackages(task, options, folderName = 'src') {
return gulp.watch(
globFolderFromPackagesDirName(packagesDirName, folderName),
{ delay: 200, ...options },
task,
);
}
function transpile() {
return buildBabel();
}
transpile.description =
'Transpiles the sources for each package so that they can be run.';
function bundle() {
return buildBundle(frontendPackageName);
}
bundle.description = 'Creates bundles for the frontend packages for use in a browser.';
function serveFrontend(done) {
const { readConfig } = require('@openapi-platform/config');
const openapiPlatformConfig = readConfig();
const uiPort = openapiPlatformConfig.get('ui.port');
browserSync.init({
port: uiPort,
server: {
baseDir: join(__dirname, 'packages/frontend/dist'),
ws: true,
// We need this so that routes work properly
middleware: [historyApiFallback()],
},
ui: {
// Keep in mind that 'uiPort', from the context of OpenAPI Platform, is the frontend web app
port: uiPort + 1,
},
});
done();
}
serveFrontend.description =
'Serves the frontend app on the port specified in the OpenAPI Platform config file in the UI ' +
'section. Note that this is intended for development purposes only, please see the ' +
'start-openapi-platform-frontend script in the frontend package for a production server.';
function restartServer(done) {
if (backendNode) {
backendNode.kill();
}
const backendEnv = Object.create(process.env);
backendNode = spawn(
'node',
[join(__dirname, 'packages/server/bin/start-openapi-platform-server.js')],
{
stdio: 'inherit',
env: backendEnv,
},
);
backendNode.on('close', function(code) {
if (code === 8) {
console.log('Error detected, waiting for changes...'.red);
}
});
done();
}
function watchTranspile() {
return watchPackages(transpile, { ignoreInitial: false });
}
watchTranspile.description =
'Same as transpile, but watches for changes in the src directory of each package and retranspiles ' +
'whenever a change is detected.';
function watchBuild() {
return watchPackages(build, { ignoreInitial: false });
}
watchBuild.description =
'Same as build, but watches for changes in the src directory of each package and retranspiles ' +
'and rebundles whenever a change is detected.';
function watchServer() {
return watchPackages(restartServer, { ignoreInitial: false }, 'lib');
}
watchServer.description =
'Runs the backend server, restarting it whenever the transpiled sources for any package ' +
'change. Note that in order to automatically retranspile sources, you will need to run a ' +
'command like watch:build.';
function formatLint() {
return runLinter({ fix: true });
}
formatLint.description =
'Corrects any automatically fixable linter warnings or errors. Note that this command will ' +
'overwrite files without creating a backup.';
function checkLint() {
return runLinter({ fix: false });
}
checkLint.description =
'Runs the linter on the codebase, displaying the output. This will display any linter warnings ' +
'or errors, as configured for the project.';
function checkTypes() {
const stream = packagesSrcStream();
return stream.pipe(tsProject(gulpTypescript.reporter.fullReporter()));
}
checkTypes.description =
'Runs the TypeScript type checker on the codebase, displaying the output. This will display any ' +
'serious errors in the code, such as invalid syntax or the use of incorrect types.';
function watchChecker() {
return watchPackages(checker, { ignoreInitial: false });
}
watchChecker.description =
'Runs the linter and TypeScript type checker on the entire codebase, watching for any changes ' +
'made to the code. When changes are detected, the linter and TypeScript type checker are ' +
'automatically rerun.';
gulp.task('transpile', transpile);
gulp.task('bundle', bundle);
const build = gulp.series(transpile, bundle);
build.description =
'Transpiles the sources for each package and creates bundles for the frontend packages.';
gulp.task('build', build);
gulp.task('serve:frontend', serveFrontend);
// TODO: Note that if you're not running watch or watch:server, restartServer doesn't actually
// explicitly kill the original node instance.
gulp.task('restart:server', restartServer);
gulp.task('watch:transpile', watchTranspile);
gulp.task('watch:build', watchBuild);
const watchFrontend = gulp.series(serveFrontend, function watchReloadBrowser() {
return watchPackages(gulp.series(reloadBrowser), {}, 'dist');
});
watchFrontend.description =
'Serves the frontend app like serve:frontend, but automatically reloads the app in the browser ' +
'whenever the frontend bundle changes. Note that in order to automatically rebundle the ' +
'frontend, you will need to run a command like watch:build.';
gulp.task('watch:frontend', watchFrontend);
gulp.task('watch:server', watchServer);
gulp.task('format:lint', formatLint);
gulp.task('checker:lint', checkLint);
gulp.task('checker:types', checkTypes);
const checker = gulp.series(checkTypes, checkLint);
checker.description =
'Runs the linter and TypeScript type checker on the codebase, displaying the output. This is the ' +
'same as running the checker:types and checker:lint commands in succession. Use the ' +
'watch:checker command to automatically rerun this command when changes are made.';
gulp.task('checker', checker);
gulp.task('watch:checker', watchChecker);
const watch = gulp.series(
transpile,
restartServer,
bundle,
serveFrontend,
function rebuild() {
return watchPackages(gulp.series(transpile, restartServer, bundle, reloadBrowser));
},
);
watch.description =
'Watches for any changes in the src folder of each package. If a change is detected then the ' +
'code will be transpiled, before rebundling the frontend, restarting the backend, and ' +
'reloading the frontend in the browser.';
gulp.task('watch', watch);
const defaultTask = gulp.series('watch');
defaultTask.description =
'Same as the watch command. Use it to spin up a backend and frontend server instance for testing ' +
'your code in a dev environment.';
gulp.task('default', defaultTask);
process.on('exit', () => {
if (backendNode) {
// Kill off the backend node instance
backendNode.kill();
}
});