-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.ts
147 lines (124 loc) · 3.74 KB
/
gulpfile.ts
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
/*!
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
import { FSWatcher } from 'fs';
import gulp from 'gulp';
import del from 'del';
import sourcemaps from 'gulp-sourcemaps';
import webpack from 'webpack';
import webpackStream from 'webpack-stream';
import named from 'vinyl-named';
import gulpSass from 'gulp-sass';
import webpackConfig from './webpack.config';
// ============================== //
// Utils //
// ============================== //
/** Array of functions to run on quit. */
const exitHandlers = new Set<() => Promise<void> | void>();
/**
* Registers a function to be run when the proccess is quit.
* Usefull for stopping and cleaning up watch tasks/servers.
* @param handler
*/
function onExit(handler: () => Promise<void> | void): void {
exitHandlers.add(handler);
}
// On Ctrl-C cleanup and exit
process.on('SIGINT', async () => {
// Give them 2 seconds before forcibly quiting
setTimeout(() => process.exit(130), 2000);
// Notify all handlers
for (const h of exitHandlers) await h();
});
/**
* Closes the given watcher when the proccess is quit
* @param watcher The watcher to close
*/
async function closeOnExit(watcher: FSWatcher): Promise<void> {
return new Promise((resolve) => onExit(() => resolve(watcher.close())));
}
// ============================== //
// Tasks //
// ============================== //
/**
* Cleans the build folders
*/
export function clean(): Promise<string[]> {
return del("./dist");
}
clean.description = "Cleans the build folders";
/**
* Compiles TypeScript files using webpack
* @param watch If webpack should watch the files
*/
function compileTypescript(watch = false): NodeJS.ReadWriteStream {
return gulp.src([
"./src/scripts/main.ts"
])
.pipe(named())
.pipe(webpackStream({ watch, ...webpackConfig }, webpack))
.pipe(gulp.dest('dist/scripts/'));
}
/**
* Compiles TypeScript files
*/
export function typescript(): NodeJS.ReadWriteStream {
return compileTypescript(false);
}
typescript.description = "Compiles TypeScript files";
/**
* Compiles sass files
*/
export function sass(): NodeJS.ReadWriteStream {
return gulp.src("./src/styles/**/*.scss")
.pipe(sourcemaps.init())
.pipe(gulpSass().on("error", gulpSass.logError))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("./dist/styles"));
}
sass.description = "Compiles sass files";
/**
* Copies static files
*/
export function copy(): NodeJS.ReadWriteStream {
return gulp.src(["./static/**/*"])
.pipe(gulp.dest("./dist"));
}
copy.description = "Copies static files";
/**
* Watches TypeScript files
*/
export function watchTypescript(): NodeJS.ReadWriteStream {
return compileTypescript(true);
}
watchTypescript.description = "Watches TypeScript files";
/**
* Watches sass files
*/
export async function watchSass(): Promise<void> {
return closeOnExit(gulp.watch("./src/styles/**/*.scss", sass));
}
watchSass.description = "Watches sass files";
/**
* Watches static files
*/
export function watchCopy(): Promise<void> {
return closeOnExit(gulp.watch("./static/**/*", copy));
}
watchCopy.description = "Watches static files";
/**
* Watches all files
*/
export const watch = gulp.parallel(watchSass, watchTypescript, watchCopy);
watch.description = "Watches all files";
/**
* Builds all files
*/
export const build = gulp.parallel(sass, typescript);
build.description = "Builds all files";
const defaultTask = gulp.series(clean, copy, build);
defaultTask.description = "Builds and packages the app";
export default defaultTask;