-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
153 lines (130 loc) · 4.33 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
const gulp = require('gulp');
const del = require('del');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const webpackConfig = require('./webpack.config.js');
const livereload = require('gulp-livereload');
const fs = require('fs');
const path = require('path');
const util = require('gulp-util'),
notifier = require('node-notifier'),
child = require('child_process'),
os = require('os');
let server = null;
gulp.task('clean', function clean() {
return del(['./out/**/*', './dist/**/*', './backend/debug'])
});
gulp.task('build:front', function buildFront(done) {
return gulp.src('./frontend/main.js')
.pipe(webpackStream(webpackConfig, webpack))
.on('error', function handleError() {
this.emit('end'); // Recover from errors
})
.on('end', function endPipe() {
done();
})
.pipe(gulp.dest('./dist/'))
.pipe(livereload({
key: fs.readFileSync(path.join(__dirname, './tls/server.key'), 'utf-8'),
cert: fs.readFileSync(path.join(__dirname, './tls/server.pem'), 'utf-8')
}));
});
gulp.task('build:back', function buildBack(done) {
child.spawnSync('make', [], {cwd: './backend/'});
// Build application
const env = Object.create(process.env);
const build = child.spawnSync('go', ['build', '-o', '../out/backend'], {cwd: './backend/', env: env});
if (build.stderr && build.stderr.length) {
util.log(util.colors.red('Something wrong :'));
const lines = build.stderr.toString()
.split('\n').filter(function (line) {
return line.length
});
for (let l in lines)
util.log(util.colors.red(
'Error (go build): ' + lines[l]
));
notifier.notify({
title: 'Error (go build)',
message: lines
});
}
done();
return build;
});
gulp.task('watch:front', function watchFront() {
livereload.listen();
return gulp.watch('./frontend/**/*', gulp.series('build:front'));
});
gulp.task('watch:back', function () {
livereload.listen();
return gulp.watch([
'backend/*.go',
'backend/**/*.go',
'!backend/tests/**/*.go',
'!backend/**/*_test.go',
'conf/**/*'
], gulp.series(
'build:back',
'spawn:gin'
));
});
gulp.task('watch:test', function watchTest() {
child.spawnSync('npm', ['run', 'test'], {stdio: 'inherit'});
return gulp.watch(['backend/*.go', 'backend/**/*.go'], function () {
return child.spawnSync('npm', ['run', 'test'], {stdio: 'inherit'})
})
});
// start backend
gulp.task('spawn:gin', function spawnGin(done) {
// Stop the server
if (server && server !== 'null') {
server.kill();
}
// Run the server
if (os.platform() === 'win32') {
server = child.spawn('out/backend.exe')
.on('error', function (err) {
process.stderr.write(err.toString())
})
.on('end', function () {
done()
});
} else {
server = child.spawn('out/backend')
.on('error', function (err) {
process.stderr.write(err.toString())
})
.on('end', function () {
done()
});
}
// Display terminal informations
server.stderr.on('data', function (data) {
process.stdout.write(data.toString());
});
server.stdout.on('data', function (data) {
process.stdout.write(data.toString());
});
done();
});
function onSigTerm(signal) {
if (server != null) {
server.kill();
server.on('exit', function () {
child.spawnSync('kill', ['-s', 'SIGKILL', process.pid]);
})
} else {
child.spawnSync('kill', ['-s', 'SIGKILL', process.pid]);
}
}
process.on('SIGINT', onSigTerm); // CTRL+C
process.on('SIGTERM', onSigTerm);
process.on('SIGABRT', onSigTerm);
// SIGKILL non catcheable by gulp(see doc : https://nodejs.org/api/process.html)
// Main tasks
gulp.task('build', gulp.parallel('build:back', 'build:front'));
gulp.task('spawn', gulp.parallel('spawn:gin'));
gulp.task('watch', gulp.parallel('watch:back', 'watch:front'));
gulp.task('default', gulp.series('build', 'spawn', 'watch'));
gulp.task('debug', gulp.series('build:front', 'watch:front'));