-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.ts
277 lines (246 loc) · 7.02 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as gulp from 'gulp';
import { spawn } from 'child_process';
import { generateMergedCoverageReports } from './lib/istabul.utils';
import * as net from 'net';
import * as path from 'path';
import { filePathExists } from './lib/fs.utils';
import fs from 'fs';
const DIR_BIN = path.resolve('./node_modules/.bin/');
const DIR_BUILD = path.resolve('build');
const DIR_DIST = path.resolve('dist');
const TARGET_DIRS = [DIR_BUILD, DIR_DIST];
const SERVE_PORT = 4200;
const BUILD_BASE_HREF = process.env.BUILD_BASE_HREF || '.';
const SERVE_BASE_HREF = process.env.SERVE_BASE_HREF || '.';
function bin(command: string): string {
return path.resolve(DIR_BIN, command);
}
function cleanDist() {
return fs.promises.rm(DIR_DIST, { recursive: true, force: true });
}
export async function clean() {
return Promise.all(
TARGET_DIRS.map((filePath) =>
fs.promises.rm(filePath, { recursive: true, force: true })
)
);
}
clean.description = `Remove directories: ` + TARGET_DIRS.join(',');
const checkGlobs = [
'*.ts',
'src/**/*.ts',
'playwright/**/*.ts',
'build_plugins/**.js',
];
export function formatCodeApply() {
return spawn(bin('prettier'), ['--write', ...checkGlobs], {
stdio: 'inherit',
});
}
formatCodeApply.description = 'Format source code.';
export function formatCodeCheck() {
return spawn(bin('prettier'), ['--list-different', ...checkGlobs], {
stdio: 'inherit',
});
}
formatCodeCheck.description = 'Check if source code is formatted.';
export function generateIcons() {
return spawn(
bin('ngx-pwa-icons'),
['--output', path.resolve('./build/generated_assets/icons')],
{ stdio: 'inherit' }
);
}
generateIcons.description = 'Generate icons.';
export function lintApply() {
return spawn(bin('ng'), ['lint', '--fix'], { stdio: 'inherit' });
}
lintApply.description = 'Lint code and apply changes (if possible).';
export function lintCheck() {
return spawn(bin('ng'), ['lint'], { stdio: 'inherit' });
}
lintCheck.description = 'Check if code passes lint checks.';
export function build() {
return spawn(
bin('ng'),
[
'build',
'--plugin',
'~/build_plugins/prod_build_plugin.js',
'--configuration',
'production',
'--base-href',
BUILD_BASE_HREF,
],
{
stdio: 'inherit',
}
);
}
build.description = 'Compile the code.';
export function deploy() {
return spawn(
bin('ng'),
['deploy'], // Additional options are configured inside angular.json.
{ stdio: 'inherit' }
);
}
deploy.description = 'Build and deploy the application to github pages.';
const isPortAvailable = (port: number) =>
new Promise<void>((res, rej) => {
const server = net.createServer();
server.once('error', () => {
server.close(() => rej(new Error(`Port '${port}' is not available.`)));
});
server.once('listening', () => {
server.close(() => res());
});
server.listen(port);
});
export async function assertServePortUnused() {
try {
await isPortAvailable(SERVE_PORT);
} catch (e) {
let messageExtra = '';
if (await filePathExists('/.dockerenv')) {
messageExtra =
'One probably has serve running from another terminal in the devenv.';
} else {
messageExtra =
'The devenv is probably running (run ./devenv.sh status to check) or another process has this port open.';
}
throw new Error(
`Serve port ${SERVE_PORT} is not available. ${messageExtra}`
);
}
}
/**
* gulp.watch can only watch files in directories that exist. So we wrote our own.
* @param filePath
* @param pollDuration
*/
function pollForFileExists(
filePath: string,
pollDuration = 500
): Promise<void> {
return new Promise<void>((res) => {
const interval = setInterval(async () => {
if (await filePathExists(filePath)) {
clearInterval(interval);
res();
}
}, pollDuration);
});
}
function startServe() {
// Build icons and listen for changes.
generateIcons();
const iconWatcher = gulp.watch('./icon.png', () => generateIcons());
// Start build and watch for changes.
const buildWatchProcess = spawn(
bin('ng'),
[
'build',
'--progress',
'--watch',
'--plugin',
'~/build_plugins/dev_build_plugin.js',
'--configuration',
'production',
'--base-href',
SERVE_BASE_HREF,
],
{ stdio: 'inherit' }
);
// Wait for initial build to finish.
pollForFileExists('./dist/app/index.html')
// Wait a moment for things to settle.
.then(() => new Promise((res) => setTimeout(res, 3000)))
// Start the server.
.then(() => {
const localWebServerProcess = spawn(
bin('browser-sync'),
[
'start',
'--single',
'--watch',
'--serveStatic',
'./dist/app',
'--no-open',
'--port',
SERVE_PORT + '',
'--index',
'index.html',
'--ui-port',
'3000',
'--logLevel',
'silent',
],
{ stdio: 'inherit' }
);
// Ensure both build and serve processes are killed when the other exits.
buildWatchProcess.on('exit', () => localWebServerProcess.kill('SIGINT'));
localWebServerProcess.on('exit', () => buildWatchProcess.kill('SIGINT'));
console.log('');
console.log('');
console.log('======================');
console.log(
'Initial build finished. Muggins app is accessible at the following URL..'
);
console.log(`http://localhost:${SERVE_PORT}`);
console.log('======================');
console.log('');
});
// Ensure the icon watch process is stopped when the build process exits.
buildWatchProcess.on('exit', () => {
iconWatcher.close();
});
return buildWatchProcess;
}
export const serve = gulp.series(cleanDist, assertServePortUnused, startServe);
serve.description = 'Serve the application on a local port.';
export function unitTest() {
return spawn(bin('ng'), ['test', '--watch', 'false'], { stdio: 'inherit' });
}
unitTest.description = `Run unit tests`;
export function endToEndTest() {
return spawn(bin('playwright'), ['test'], { stdio: 'inherit' });
}
endToEndTest.description = `Run end-to-end tests.`;
export function unifyCoverage() {
return generateMergedCoverageReports(
[
'./build/coverage/jest/coverage-final.json',
'./build/coverage/playwright/coverage-final.json',
],
'./build/coverage/unified/',
['html', 'json', 'lcovonly']
);
}
unifyCoverage.description =
'Combine the unit and end-to-end code coverage reports.';
export const buildFullCi = gulp.series(
clean,
generateIcons,
formatCodeCheck,
lintCheck,
unitTest,
endToEndTest,
unifyCoverage,
build
);
buildFullCi.description =
'Generate icons, check code format, check lint, test, and compile.';
export const buildFull = gulp.series(
clean,
generateIcons,
formatCodeApply,
lintApply,
unitTest,
endToEndTest,
unifyCoverage,
build
);
buildFull.description =
'Generate icons, format code, apply lint, compile, test, and compile.';
export default buildFull;