This repository has been archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
106 lines (87 loc) · 2.81 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
/**
* Commands:
* - gulp clean: delete compiled code
* - gulp compile: compile code in preparation for deployment
* - gulp server: compile and run a new application server
* - gulp watch: compile code and start the node process while watching for code changes so it can re-compile as needed
* - gulp test: compile the code, start the server, and run automated tests
* - gulp: compile code in preparation for deployment
*/
const gulp = require('gulp');
const ts = require('gulp-typescript');
const clean = require('gulp-clean');
const mocha = require('gulp-mocha');
const spawn = require('child_process').spawn;
const tsProject = ts.createProject('tsconfig.json');
// Variable to store the node process between gulp server runs
let node;
/**
* Delete all files and folders in the release folder.
*/
function cleanTask() {
return gulp.src('release', { read: false, allowEmpty: true })
.pipe(clean());
}
/**
* Compile TypeScript source code using type declarations.
* Turns it into ES5 JavaScript code and uglifies it.
*/
function compileTask() {
const tsResult = gulp.src([
"./src/**/*.ts", // source TypeScript files
"./types/**/*.d.ts", // custom TypeScript type declarations
"./node_modules/@types/**/*.d.ts" // type declarations from node modules
]).pipe(tsProject());
// Merge output streams if necessary
return tsResult.js.pipe(gulp.dest('./release'));
}
/**
* Try to run the application server.
* @param {function} callback - run when done
*/
function serverTask(callback) {
// Kill the previously running server, if there is one
if (node) {
node.kill();
}
// Spawn a new node ChildProcess running the release server
node = spawn('node', ['main.js'], { stdio: 'inherit' });
// Log errors on close
node.on('close', (statusCode) => {
if (statusCode === 8) {
gulp.log("Error detected, waiting for changes...");
}
});
// Signal that we are done with this task
// This is necessary because we don't return anything
callback();
}
/**
* Run automated mocha tests.
*/
function testTask() {
// Run mocha tests
return gulp
.src("./tests/main.js", { read: false })
.pipe(mocha({ reporter: "spec", exit: true, bail: true }))
.once("error", (_err) => {});
}
/**
* Re-run gulp server when changes in source code are detected.
*/
function watchTask() {
gulp.watch(["./src/**/*.ts", "./configs/**/*", "./main.js"], gulp.series("server"));
}
/* Export functions for gulp to run as tasks. */
// gulp clean
exports.clean = cleanTask;
// gulp compile
exports.compile = gulp.series(exports.clean, compileTask);
// gulp server
exports.server = gulp.series(exports.compile, serverTask);
// gulp test
exports.test = gulp.series(exports.compile, testTask);
// gulp watch
exports.watch = gulp.series(exports.server, watchTask);
// gulp
exports.default = exports.compile;