forked from common-voice/common-voice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
166 lines (138 loc) · 4.16 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
'use strict';
const APP_NAME = 'common-voice';
const TS_CONFIG = 'tsconfig.json';
const TS_GLOB = 'src/**/*';
const DIR_CLIENT = './web/';
const DIR_SERVER = './server/';
const DIR_UPLOAD = DIR_SERVER + 'upload/';
const DIR_SERVER_JS = DIR_SERVER + 'js/';
const DIR_DIST = DIR_CLIENT + 'dist/';
const PATH_CSS = DIR_CLIENT + 'css/*.css';
const PATH_TS = DIR_CLIENT + TS_GLOB;
const PATH_TS_SERVER = DIR_SERVER + TS_GLOB;
const PATH_VENDOR = DIR_CLIENT + 'vendor/';
const RELOAD_DELAY = 2500;
const SERVER_SCRIPT = 'server/js/server.js'
// Add gulp help functionality.
let gulp = require('gulp-help')(require('gulp'));
let shell = require('gulp-shell');
let path = require('path');
let ts = require('gulp-typescript');
let insert = require('gulp-insert');
function compile(project) {
return project.src().pipe(project()).js;
}
function listen() {
require('gulp-nodemon')({
script: SERVER_SCRIPT,
// Use [c] here to workaround nodemon bug #951
watch: ['server/js', '[c]onfig.json'],
delay: RELOAD_DELAY,
});
}
function watch() {
gulp.watch('package.json', ['npm-install']);
gulp.watch(PATH_TS, ['ts']);
gulp.watch(PATH_VENDOR, ['ts']);
gulp.watch(PATH_TS_SERVER, ['ts-server']);
gulp.watch(PATH_CSS, ['css']);
}
function watchAndListen() {
watch();
listen();
}
function doEverything() {
return Promise.all([compileClient, compileServer, compileCSS])
.then(watchAndListen);
}
function getVendorJS() {
let fs = require('fs');
let files = fs.readdirSync(PATH_VENDOR);
return files.reduce((acc, file) => {
return acc + fs.readFileSync(PATH_VENDOR + file) + '\n';
}, '');
}
function compileCSS() {
var cleanCSS = require('gulp-clean-css');
return gulp.src(PATH_CSS)
.pipe(cleanCSS())
.pipe(gulp.dest(DIR_DIST));
}
function compileClient() {
let project = ts.createProject(DIR_CLIENT + TS_CONFIG);
let insert = require('gulp-insert');
let uglify = require('gulp-uglify');
let uglifyOptions = {
mangle: false,
compress: false,
output: {
beautify: true,
indent_level: 2,
semicolons: false
}
};
return compile(project)
.pipe(uglify(uglifyOptions))
.pipe(insert.prepend(getVendorJS()))
.pipe(gulp.dest(DIR_DIST));
}
function compileServer() {
let project = ts.createProject(DIR_SERVER + TS_CONFIG);
return compile(project)
.pipe(gulp.dest(DIR_SERVER_JS));
}
gulp.task('css', 'Minify CSS files', compileCSS);
gulp.task('ts', 'Compile typescript files into bundle.js', compileClient);
gulp.task('ts-server', 'Compile typescript server files.', compileServer);
gulp.task('build', 'Build both server and client js', ['ts', 'ts-server', 'css']);
gulp.task('npm-install', 'Install npm dependencies.',
shell.task(['npm install']));
gulp.task('clean', 'Remove uploaded clips.',
shell.task([`git clean -idx ${DIR_UPLOAD}`]));
gulp.task('listen', 'Run development server.', listen);
gulp.task('run', 'Just run the server', shell.task(['node ' + SERVER_SCRIPT]));
gulp.task('watch', 'Rebuild, rebundle, re-install on file changes.', watch);
gulp.task('create', 'Create the database.', ['ts-server'], (done) => {
let create = require('./tools/createDb');
create.run(err => {
if (!err) {
console.log('Db created.');
}
done();
});
});
gulp.task('drop', 'Detroy the database.', ['ts-server'], (done) => {
let drop = require('./tools/dropDb');
drop.run(done);
});
gulp.task('deploy', 'deploy production',
['npm-install', 'build'], (done) => {
let config = require('./config.json');
let pm2 = require('pm2');
let ff = require('ff');
let f = ff(() => {
pm2.connect(f.wait());
}, () => {
pm2.stop(APP_NAME, f.waitPlain());
}, () => {
pm2.start({
name: APP_NAME,
script: "./gulpfile.js",
output: config.logfile || "log.txt",
error: config.logfile || "log.txt",
}, f());
}).onComplete((err) => {
if (err) {
console.log('prod error', err);
}
pm2.disconnect();
done();
});
});
gulp.task('default', 'Running just `gulp`.', ['build'], () => {
watchAndListen();
});
// Deploy script also runs this file, so exec the default task.
if (require.main === module) {
doEverything();
}