This repository has been archived by the owner on May 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
238 lines (205 loc) · 6.82 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*!
* 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 fs, { 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 gulp_sass from 'gulp-sass';
import node_sass from 'node-sass';
import gulp_zip from 'gulp-zip';
import * as jsonc from 'jsonc-parser';
import webpackConfig from './webpack.config';
(gulp_sass as any).compiler = node_sass;
// ============================== //
// Utils //
// ============================== //
/**
* Partial typedef for a package.json file
*/
interface Package {
name: string;
version: string;
}
/**
* Partial typedef for a manifest.json file
*/
interface Manifest {
name: string;
version: string;
permissions: Array<string | { [key: string]: any }>
}
/**
* Reads a json file from disk
* @param path The path to the JSON file
*/
async function readJson<T>(path: string): Promise<T> {
return jsonc.parse(await fs.promises.readFile(path, "utf-8")) as T;
}
/** 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) {
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) {
return new Promise((resolve) => onExit(() => resolve(watcher.close())));
}
async function waitOnStream(stream: NodeJS.ReadWriteStream) {
return new Promise((resolve, reject) => stream.on("end", resolve).on("error", reject));
}
// ============================== //
// Tasks //
// ============================== //
/**
* Cleans the build folders
*/
export function clean() {
return del("./dist");
}
clean.description = "Cleans the build folders";
/**
* Syncs the app's manifest.json version to match package.json
*/
export async function sync_manifest_version() {
const packageJson = await readJson<Package>("./package.json");
const manifestString = await fs.promises.readFile('./public/manifest.json', "utf-8");
const edits = jsonc.modify(manifestString, ["version"], packageJson.version, { formattingOptions: {} });
const newManifestString = jsonc.applyEdits(manifestString, edits);
await fs.promises.writeFile('./public/manifest.json', newManifestString, "utf-8");
}
sync_manifest_version.description = "Syncs the app's manifest.json version to match package.json";
/**
* Syncs known usb devices into manifest.json
*/
export async function sync_usb_ids() {
const usbDevices = await readJson<any[]>("./src/usbDevices.jsonc");
for (const device of usbDevices) {
const props = Object.getOwnPropertyNames(device) as Array<keyof typeof device>;
for (const prop of props) {
if(typeof device[prop] === 'string') device[prop] = Number.parseInt(device[prop], 16);
}
}
const manifestString = await fs.promises.readFile('./public/manifest.json', "utf-8");
const manifestJson = jsonc.parse(manifestString) as Manifest;
let usbDevicesIndex = manifestJson.permissions.findIndex(p => typeof p === 'object' && 'usbDevices' in p);
let edits;
if(usbDevicesIndex === -1) {
edits = jsonc.modify(manifestString, ["permissions", usbDevicesIndex], { usbDevices }, { formattingOptions: {} });
}
else {
edits = jsonc.modify(manifestString, ["permissions", usbDevicesIndex, "usbDevices"], usbDevices, { formattingOptions: {} });
}
const newManifestString = jsonc.applyEdits(manifestString, edits);
await fs.promises.writeFile('./public/manifest.json', newManifestString, "utf-8");
}
sync_manifest_version.description = "Syncs known usb devices into manifest.json";
/**
* Compiles TypeScript files using webpack
* @param watch If webpack should watch the files
*/
function compile_typescript(watch = false) {
return gulp.src([
"./src/scripts/application.ts",
"./src/scripts/background.ts",
"./src/scripts/client-api-injector.ts",
"./src/scripts/client-api.ts"
])
.pipe(named())
.pipe(webpackStream({ watch, ...webpackConfig }, webpack as any))
.pipe(gulp.dest('dist/scripts/'));
}
/**
* Compiles TypeScript files
*/
export function typescript() {
return compile_typescript(false);
}
typescript.description = "Compiles TypeScript files";
/**
* Compiles sass files
*/
export function sass() {
return gulp.src("./src/styles/**/*.scss")
.pipe(sourcemaps.init())
.pipe(gulp_sass().on("error", gulp_sass.logError))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("./dist/styles"));
}
sass.description = "Compiles sass files";
/**
* Copies static files
*/
export function copy() {
return gulp.src(["./public/**/*"])
.pipe(gulp.dest("./dist"));
}
copy.description = "Copies static files";
/**
* Packages built files into a .zip
*/
export async function packageapp() {
const packageJson = await readJson<Package>("./package.json");
const outName = `${packageJson.name}-v${packageJson.version}.zip`;
return waitOnStream(
gulp.src("./dist/**/*")
.pipe(gulp_zip(outName))
.pipe(gulp.dest('./build'))
);
}
packageapp.description = "Packages built files into a .zip";
/**
* Watches TypeScript files
*/
export function watch_typescript() {
return compile_typescript(true);
}
watch_typescript.description = "Watches TypeScript files";
/**
* Watches sass files
*/
export async function watch_sass() {
return closeOnExit(gulp.watch("./src/styles/**/*.scss", sass));
}
watch_sass.description = "Watches sass files";
/**
* Watches static files
*/
export function watch_copy() {
return closeOnExit(gulp.watch("./public/**/*", copy));
}
watch_copy.description = "Watches static files";
/**
* Watches all files
*/
export const watch = gulp.parallel(watch_sass, watch_typescript, watch_copy);
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, sync_manifest_version, copy, build, packageapp);
defaultTask.description = "Builds and packages the app";
export default defaultTask;