-
Notifications
You must be signed in to change notification settings - Fork 21
/
gulpfile.babel.js
116 lines (111 loc) · 3.34 KB
/
gulpfile.babel.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
import fs from "fs";
import chalk from "chalk";
import gulp from "gulp";
import babel from "gulp-babel";
import rename from "gulp-rename";
import clean from "gulp-rimraf";
import minify from "gulp-minifier";
import jeditor from "gulp-json-editor";
import runSequence from "run-sequence";
import config from "./src/config";
const dist = "./dist";
const dist_server = `${dist}/server`;
const dist_swagger = `${dist_server}/lib/swagger/ui`;
const dist_client = `${dist}/client`;
const pm2_simple = `pm2.simple.config.js`;
const pm2_cluster = `pm2.cluster.config.js`;
gulp.task("build", () => {
// Sequence
runSequence('build-clean', 'build-babel', 'build-replace');
});
gulp.task("build-clean", () => {
// Remove files dist, but ignore assets
return gulp
.src([`${dist_server}/*`, `!${dist_server}/assets`, `${dist_client}`], {
read: false,
})
.pipe(clean({ force: true }));
});
gulp.task("build-babel", () => {
// Babel transform, ignore config and swagger /swagger/ui
return gulp
.src(["src/**/*.js", "!src/config/*.js", "!src/lib/swagger/ui/*"])
.pipe(babel())
.pipe(gulp.dest(dist_server));
});
gulp.task("build-replace", () => {
// Copy file config dev or production
const conf = process.argv[3] ? "index" : "production";
// Copy config production
gulp
.src([`src/config/${conf}.js`])
.pipe(babel())
.pipe(rename("index.js"))
.pipe(gulp.dest(`${dist_server}/config`));
// Copy views
gulp
.src(["src/views/**/*.*", "!src/views/**/*.js"])
.pipe(
minify({
minify: true,
collapseWhitespace: true,
conservativeCollapse: true,
minifyCSS: true,
})
)
.pipe(gulp.dest(`${dist_server}/views`));
// Copy assets
gulp.src(["src/assets/**/*"]).pipe(gulp.dest(`${dist_server}/assets`));
// Copy *.yaml
gulp.src(["src/**/*.yaml"]).pipe(gulp.dest(dist_server));
// Copy swagger/ui
gulp.src([`src/lib/swagger/ui/*`]).pipe(gulp.dest(dist_swagger));
// package.json
gulp
.src("package.json")
.pipe(
jeditor((json) => {
delete json.devDependencies;
json.scripts = {
start: `node server/app.js`,
simple: `npm stop && pm2 start ${pm2_simple} --env production`,
cluster: `npm stop && pm2 start ${pm2_cluster} --env production`,
stop: `pm2 delete ${pm2_simple} ${pm2_cluster}`,
};
return json;
})
)
.pipe(gulp.dest(dist));
// Copy pm2 files
gulp.src([pm2_simple, pm2_cluster]).pipe(gulp.dest(dist));
// If exits client folder, then copy current client
if (fs.existsSync(`${dist}/client`)) {
gulp.src(["client/**/*"]).pipe(gulp.dest(dist_client));
} else {
// If not exists client folder, then copy default client
gulp.src(["src/views/default/favicon.ico"]).pipe(gulp.dest(dist_client));
gulp
.src(["src/views/default/production.html"])
.pipe(
minify({
minify: true,
collapseWhitespace: true,
conservativeCollapse: true,
})
)
.pipe(rename("index.html"))
.pipe(gulp.dest(dist_client));
}
// Copy assets if not exists
if (!fs.existsSync(`${dist_server}/assets`)) {
gulp.src("src/assets").pipe(gulp.dest(`${dist_server}`));
}
// Success
setTimeout(
() =>
console.log(
chalk.greenBright("\n---------\nBuild success!\n---------\n")
),
500
);
});