-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.js
586 lines (483 loc) · 21.7 KB
/
pipeline.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/**
* This script automates the release process of a progressive web application via sftp.
*
* To use this script:
*
* 1. Install the necessary dependencies by running `npm install` in the terminal.
* 2. Run the script with `node pipeline.js`. This will start the release process.
* 3. If you want to release to production, add the `-production` or `-p` flag.
* 4. Use `-help` or `-h` to display more command line options.
*
* Please ensure that you have the necessary permissions to read from and write to the specified directories and files,
* and to connect to the SFTP server.
*/
const startTime = new Date();
const fs = require('fs');
const { promises: fsPromises, statSync } = require('fs');
const path = require('path');
const ProgressBar = require('progress');
const chalk = require('chalk');
const SftpClient = require('ssh2-sftp-client');
const UglifyJS = require("uglify-js");
const CleanCSS = require('clean-css');
const ignoredFileTypes = ['.zip', '.rar', '.md', '.txt', '.psd'];
const directoriesToInclude = ['./src', './img', './fonts', './themes'];
let filesToCache = [
'service-worker.js',
'service-worker-registration.js',
'manifest.json',
'robots.txt',
'sitemap.xml',
'index.html',
'style.css',
];
const htaccessFile = '.htaccess';
const cacheFile = './filesToCache.js';
const applicationPaths = {
production: 'housebird_games/calculator',
staging: 'calculator-staging'
};
const statisticsFileName = 'pipeline-log.txt';
const minifiedDirectory = 'minified';
const productionFlag = process.argv.includes('-production') || process.argv.includes('-p');
const stagingFlag = process.argv.includes('-staging') || process.argv.includes('-s');
const cacheFlag = process.argv.includes('-cache') || process.argv.includes('-c');
const rollbackFlag = process.argv.includes('-rollback') || process.argv.includes('-r');
const backupFlag = process.argv.includes('-backup') || process.argv.includes('-b');
const deleteFlag = process.argv.includes('-delete') || process.argv.includes('-d');
const versionFlagIndex = process.argv.findIndex(arg => arg === '-version' || arg === '-v');
const helpFlag = process.argv.includes('-help') || process.argv.includes('-h');
const infoFlag = process.argv.includes('-info') || process.argv.includes('-i');
const minifyFlag = process.argv.includes('-minify') || process.argv.includes('-m');
const disableStatisticsFlag = process.argv.includes('-nolog') || process.argv.includes('-nl');
const fileTypeCounts = {};
let fileTypeSizes = {};
function help() {
if (helpFlag || process.argv.length === 2) {
console.log(` Usage: node pipeline.js [options]
Options:
-v, -version <version> Set the version number. Expected format is x, x.x, x.x.x, or x.x.x.x
-v, -version Increment the last part of the version number by 1
-h, -help or [no flag] Display this help message and exit
-i, -info Display detailed information about the process
-c, -cache (Re-)Generates the filesToCache.js file
-d, -delete <-p|-s> Deletes the application production or staging directory from the server
-b, -backup Creates a backup before deploying the new version that can be rolled back to.
-r, -rollback Rollback to the backup version, if on server (used with -p or -s)
-nl, -nolog No statistics logged and added to the log file
-m, -minify Minifies the files in filesToCache.js (before uploading them to the server)
-p, -production Release to production
-s, -staging Release to staging (is ignored if -p is set)
`);
process.exit(0);
}
}
async function main() {
help();
console.log('');
const applicationPath = getApplicationPath();
if (productionFlag || stagingFlag) {
console.log(chalk.green(`Starting ${productionFlag ? 'production' : 'staging'} ${deleteFlag ? 'deletion' : 'release'} process to ${applicationPath}...`));
}
const currentVersion = await getCurrentVersion();
let version = currentVersion;
console.log(`Current version: ${currentVersion}`);
if (versionFlagIndex !== -1) {
const newVersion = getNewVersion(currentVersion);
version = newVersion;
await updateVersion(newVersion);
}
const filesToCache = await getFilesToCache();
information(filesToCache);
const cacheSize = await writeFilesToCacheFile(filesToCache);
let minifiedSize = '';
if (!deleteFlag) {
minifiedSize = await minifyFiles(filesToCache);
}
let filesUploaded = null;
if (rollbackFlag) {
await rollback(applicationPath);
}
else if (deleteFlag) {
await deleteDirectoryFromServer(applicationPath);
}
else {
filesUploaded = await uploadFilesToServer(filesToCache, applicationPath) | 0;
}
console.log('');
if ((productionFlag || stagingFlag) && !deleteFlag && !rollbackFlag) {
console.log(chalk.green(`Release process of version ${version} completed successfully.`));
}
else {
console.log(chalk.green('Done.'));
}
const endTime = new Date();
const elapsedTime = endTime - startTime;
const minutes = Math.floor(elapsedTime / 60000);
const seconds = ((elapsedTime % 60000) / 1000).toFixed(0);
console.log(chalk.gray(`Elapsed time: ${minutes}:${seconds} minutes`));
await addStatistics(`${minutes}:${seconds} minutes`, version, filesUploaded, filesToCache.length, cacheSize, minifiedSize);
console.log('');
}
function information(filesToCache) {
if (infoFlag) {
console.log('');
console.log(chalk.green(`Information:`));
console.log('');
console.log(` Production path: ${applicationPaths.production}`);
console.log(` Staging path: ${applicationPaths.staging}`);
console.log('');
console.log(chalk.yellow(` Included ${filesToCache.length} files`));
console.log('');
for (const fileType in fileTypeCounts) {
console.log(chalk.gray(` ${fileType ? fileType : 'NO EXT'}: ${fileTypeCounts[fileType]}x${fileTypeSizes[fileType] ? ` > ${(fileTypeSizes[fileType] / 1048576).toFixed(2)} MB` : ''}`));
}
}
}
async function minifyFiles(filesToCache) {
if (!minifyFlag) return null;
console.log('');
console.log(chalk.gray(`Minifying ${filesToCache.length} files...`));
await fsPromises.mkdir(minifiedDirectory, { recursive: true });
let oldTotalSize = 0;
let totalSize = 0;
const bar = new ProgressBar(' Minifying files [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 24,
total: filesToCache.length
});
for (const file of filesToCache) {
const outputPath = path.join(minifiedDirectory, path.basename(file));
const originalStats = await fsPromises.stat(file);
const originalSizeKB = (originalStats.size / 1024).toFixed(2);
oldTotalSize += originalStats.size;
if (file.endsWith('.js')) {
const fileContent = await fsPromises.readFile(file, 'utf8');
const result = UglifyJS.minify(fileContent);
if (result.error) throw result.error;
await fsPromises.writeFile(outputPath, result.code, 'utf8');
} else if (file.endsWith('.css')) {
const fileContent = await fsPromises.readFile(file, 'utf8');
const result = new CleanCSS({}).minify(fileContent);
if (result.errors.length > 0) throw result.errors;
await fsPromises.writeFile(outputPath, result.styles, 'utf8');
}
else {
if (!infoFlag) bar.tick();
continue;
}
const minifiedStats = await fsPromises.stat(outputPath);
totalSize += minifiedStats.size;
if (infoFlag) {
console.log(chalk.gray(` Minified ${path.basename(file)}: ${originalSizeKB} KB > ${(minifiedStats.size / 1024).toFixed(2)} KB`));
}
else {
bar.tick();
}
}
if (infoFlag) {
bar.tick(filesToCache.length);
}
const oldFileSize = (oldTotalSize / 1048576).toFixed(2);
const newFileSize = (totalSize / 1048576).toFixed(2);
console.log('');
console.log(`Total minified size: ${oldFileSize} MB > ${newFileSize} MB`);
return newFileSize;
}
async function addStatistics(time, version, uploadedFiles, cachedFiles, cacheFileSize, minifiedSize) {
if (disableStatisticsFlag) return;
let currentTime = new Date();
currentTime = currentTime.toLocaleString('en-GB').replace(',', '');
const scriptArguments = process.argv
.slice(2)
.map(arg => path.basename(arg))
.join(' ');
const statistics = `Version: ${version}\nFinished: ${currentTime}\nArguments: ${scriptArguments}\nProcess Time: ${time}\nUploaded files: ${uploadedFiles ? uploadedFiles : '0'}\nCached files: ${cachedFiles}\nCache file size: ${(cacheFileSize / 1048576).toFixed(2)} MB${minifiedSize ? `\nMinified size: ${minifiedSize} MB` : ''}\n\n`;
try {
const data = await fsPromises.readFile(statisticsFileName, 'utf8');
await fsPromises.writeFile(statisticsFileName, statistics + (data || ''), 'utf8');
console.log(chalk.gray('Statistics saved'));
} catch (error) {
if (error.code === 'ENOENT') {
// File does not exist, create it with the new data
try {
await fsPromises.writeFile(statisticsFileName, statistics, 'utf8');
console.log(chalk.green(`Statistics file created: ${statisticsFileName} and statistics saved`));
} catch (writeError) {
console.error(`Error creating file: ${writeError}`);
}
} else {
console.error(`Error: ${error}`);
}
}
}
async function getCurrentVersion() {
const data = await fsPromises.readFile(path.join(__dirname, 'service-worker.js'), 'utf8');
const versionMatch = data.match(/self.CACHE_VERSION = '(.*?)';/);
if (!versionMatch) throw new Error('Could not find CACHE_VERSION in service-worker.js');
return versionMatch[1];
}
function getNewVersion(currentVersion) {
if (versionFlagIndex !== -1) {
let newVersion = process.argv[versionFlagIndex + 1];
if (newVersion === undefined || newVersion.startsWith('-')) {
const versionParts = currentVersion.split('.');
versionParts[3] = (parseInt(versionParts[3], 10) + 1).toString();
return versionParts.join('.');
} else if (!/^\d+(\.\d+){0,3}$/.test(newVersion)) {
throw new Error('Invalid version number format. Expected format is x, x.x, x.x.x, or x.x.x.x');
} else {
const versionParts = newVersion.split('.');
while (versionParts.length < 4) {
versionParts.push('0');
}
return versionParts.join('.');
}
}
return currentVersion;
}
async function updateVersion(newVersion) {
const serviceWorkerPath = path.join(__dirname, 'service-worker.js');
let data = await fsPromises.readFile(serviceWorkerPath, 'utf8');
data = data.replace(/self.CACHE_VERSION = '(.*?)';/, `self.CACHE_VERSION = '${newVersion}';`);
await fsPromises.writeFile(serviceWorkerPath, data, 'utf8');
console.log(chalk.blue(`Service worker version updated to: ${newVersion}`));
const indexPath = path.join(__dirname, 'index.html');
data = await fsPromises.readFile(indexPath, 'utf8');
data = data.replace(/<p id="versionText">Version (.*?)<\/p>/, `<p id="versionText">Version ${newVersion}</p>`);
await fsPromises.writeFile(indexPath, data, 'utf8');
console.log(chalk.blue(`Index.html version updated to: ${newVersion}`));
}
async function getFilesToCache() {
if (infoFlag) {
console.log(chalk.gray(`Reading files from root folder...`));
filesToCache.forEach(element => {
console.log(chalk.gray(` ${element}`));
});
}
for (const dir of directoriesToInclude) {
if (dir == './uploads' || dir == './img/screenshots') continue;
infoFlag && console.log(chalk.gray(`Reading files from ${dir}...`));
await readFilesFromDirectory(dir, filesToCache);
}
return [...new Set(filesToCache)].sort();
}
async function readFilesFromDirectory(directory, filesToCache) {
const filesInDirectory = await fsPromises.readdir(directory);
for (const file of filesInDirectory) {
const fullPath = path.join(directory, file);
const stats = await fsPromises.stat(fullPath);
if (stats.isDirectory()) {
await readFilesFromDirectory(fullPath, filesToCache);
} else {
const fileType = path.extname(file);
if (!ignoredFileTypes.includes(fileType)) {
filesToCache.push(fullPath.replace(/\//g, '/'));
fileTypeCounts[fileType] = (fileTypeCounts[fileType] || 0) + 1;
fileTypeSizes[fileType] = (fileTypeSizes[fileType] || 0) + stats.size;
}
}
}
}
async function writeFilesToCacheFile(filesToCache) {
let totalSize = 0;
filesToCache.forEach(file => {
const stats = statSync(path.join(__dirname, file));
totalSize += stats.size;
});
if (!cacheFlag) return totalSize;
console.log('');
console.log(chalk.gray(`Writing ${filesToCache.length} files to ${cacheFile}...`));
let fileContent = 'self.filesToCache = [\n';
fileContent += filesToCache.map(f => `'/calculator/${f}',`.replace(/\\/g, '/')).join('\n');
fileContent += '\n];';
await fsPromises.writeFile(cacheFile, fileContent, 'utf8');
console.log(chalk.yellow(`Wrote ${filesToCache.length} files (total size: ${(totalSize / 1048576).toFixed(2)} MB) to ${cacheFile}.`));
return totalSize;
}
async function uploadFilesToServer(filesToCache, applicationPath) {
if (!productionFlag && !stagingFlag) return;
let filesToUpload = [...filesToCache, 'filesToCache.js'];
filesToUpload.sort((a, b) => {
if (path.basename(a) === 'service-worker.js') return 1;
if (path.basename(b) === 'service-worker.js') return -1;
return 0;
});
console.log('');
console.log(chalk.grey(`Uploading ${filesToUpload.length} files to server:`))
const sftpConfig = require('./sftp-config.js');
const sftp = new SftpClient();
sftp.client.setMaxListeners((filesToUpload.length + 1));
try {
console.log(chalk.blue(' Attempting to connect to SFTP server...'));
await sftp.connect(sftpConfig);
console.log(chalk.green(' Successfully connected to SFTP server.'));
console.log(chalk.blue(' Uploading files to server...'));
await uploadFiles(filesToUpload, sftp, applicationPath);
console.log(chalk.green(' Files successfully uploaded.'));
} catch (error) {
const endTime = new Date();
const elapsedTime = endTime - startTime;
console.error(`Error: ${error.message}. Elapsed time: ${elapsedTime} ms.`);
return 0;
} finally {
console.log(chalk.blue(' Closing SFTP connection...'));
await sftp.end();
console.log(chalk.green(' SFTP connection closed.'));
}
console.log(chalk.gray('Upload complete.'));
return filesToUpload.length;
}
async function createDirectoriesOnServer(filesToUpload, sftp, applicationPath) {
applicationPath = `${applicationPath}`;
const directories = Array.from(new Set(filesToUpload.map(file => path.dirname(file).replace(/\\/g, '/'))));
directories.sort((a, b) => a.split('/').length - b.split('/').length);
const dirBar = new ProgressBar(' Creating directories [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 24,
total: directories.length - 1
});
for (const dir of directories) {
if (dir === '.') continue;
infoFlag && console.log(chalk.gray(` Creating directory: ${dir}`));
const remoteDir = `/${applicationPath}/` + (dir.startsWith('./') ? dir.slice(2) : dir);
await sftp.mkdir(remoteDir, true);
infoFlag || dirBar.tick();
}
infoFlag && dirBar.tick(directories.length - 1);
}
async function uploadFilesToDirectory(filesToUpload, sftp, applicationPath) {
const uploadBar = new ProgressBar(' Uploading files [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 24,
total: filesToUpload.length
});
await Promise.all(filesToUpload.map(async (file) => {
infoFlag && console.log(chalk.gray(` Uploading: ${file}`));
const relativeFilePath = file.replace(/^\.\/|^\//, '');
const remotePath = `/${applicationPath}/${relativeFilePath.replace(/\\/g, '/')}`;
const minifiedFilePath = path.join(minifiedDirectory, path.basename(file));
const localFilePath = fs.existsSync(minifiedFilePath) && minifyFlag ? minifiedFilePath : path.join(__dirname, file);
await sftp.fastPut(localFilePath, remotePath);
uploadBar.tick();
}));
await sftp.fastPut(htaccessFile, `/${applicationPath}/.htaccess`);
}
async function uploadFiles(filesToUpload, sftp, applicationPath) {
const backupApplicationPath = `${applicationPath}_BACKUP`;
if (backupFlag && await sftp.exists(backupApplicationPath)) {
console.log(chalk.blue(' Starting Backup...'))
await deleteDirectory(sftp, backupApplicationPath);
}
if (backupFlag) {
await sftp.mkdir(backupApplicationPath);
await copyDirectory(sftp, applicationPath, backupApplicationPath, 'backup');
}
await createDirectoriesOnServer(filesToUpload, sftp, applicationPath);
await uploadFilesToDirectory(filesToUpload, sftp, applicationPath);
}
async function copyDirectory(sftp, src, dest, tempFolderName = 'temp') {
const projectTempDir = path.join(__dirname, tempFolderName);
console.log(chalk.gray(` Copying ${src} to ${dest}...`));
if (fs.existsSync(projectTempDir)) {
fs.rmSync(projectTempDir, { recursive: true, force: true });
}
console.log(chalk.gray(` Downloading from ${src}...`));
const downloadInterval = spinner();
await sftp.downloadDir(src, projectTempDir);
clearInterval(downloadInterval);
process.stdout.write('\r');
console.log(chalk.gray(` Uploading to ${dest}.`));
const uploadInterval = spinner();
await sftp.uploadDir(projectTempDir, dest);
clearInterval(uploadInterval);
process.stdout.write('\r');
console.log(chalk.gray(` Copy completed.`));
}
async function rollback(applicationPath) {
console.log(chalk.grey('Rolling back to backup...'));
const backupApplicationPath = `${applicationPath}_BACKUP`;
const sftpConfig = require('./sftp-config.js');
const sftp = new SftpClient();
try {
console.log(chalk.blue(' Attempting to connect to SFTP server...'));
await sftp.connect(sftpConfig);
console.log(chalk.green(' Successfully connected to SFTP server.'));
if (await sftp.exists(backupApplicationPath)) {
sftp.client.setMaxListeners(200);
await copyDirectory(sftp, backupApplicationPath, applicationPath);
} else {
console.log(chalk.red('No backup version found for rollback.'));
}
} catch (error) {
console.error(chalk.red(' An error occurred:', error.message));
} finally {
console.log(chalk.blue(' Closing SFTP connection...'));
await sftp.end();
console.log(chalk.green(' SFTP connection closed.'));
}
console.log('');
console.log(chalk.green('Rollback complete.'));
}
async function deleteDirectory(sftp, path) {
try {
console.log(chalk.gray(` Deleting ${path}...`));
const interval = spinner();
await sftp.rmdir(path, true);
clearInterval(interval);
process.stdout.write('\r');
console.log(chalk.gray(` Deleted ${path}.`));
} catch (error) {
clearInterval(interval);
process.stdout.write('\r');
console.error(chalk.red(`Failed to delete ${path}:`, error));
}
}
async function deleteDirectoryFromServer(applicationPath) {
if (!productionFlag && !stagingFlag) {
console.log(chalk.red('No production (-p) or staging flag (-s) set. Aborting deletion.'))
return;
}
else if (!deleteFlag) {
console.log(chalk.red('No delete flag (-d) set. Aborting deletion.'))
return;
}
console.log('');
console.log(chalk.grey('Deleting directory from server:'))
const sftpConfig = require('./sftp-config.js');
const sftp = new SftpClient();
try {
console.log(chalk.blue(' Attempting to connect to SFTP server...'));
await sftp.connect(sftpConfig);
console.log(chalk.green(' Successfully connected to SFTP server.'));
console.log(chalk.blue(' Deleting directory from server...'));
const interval = spinner();
await sftp.rmdir(applicationPath, true);
clearInterval(interval);
process.stdout.write('\r');
console.log(chalk.green(' Directory successfully deleted.'));
} catch (error) {
clearInterval(interval);
process.stdout.write('\r');
console.error(chalk.red(' An error occurred:', error.message));
} finally {
console.log(chalk.blue(' Closing SFTP connection...'));
await sftp.end();
console.log(chalk.green(' SFTP connection closed.'));
}
console.log(chalk.grey('Deletion complete.'));
}
function spinner() {
const spinner = ['/', '-', '\\', '|'];
let i = 0;
return interval = setInterval(() => {
process.stdout.write(`\r ${spinner[i++ % spinner.length]}`);
}, 250);
}
function getApplicationPath() {
return productionFlag ? applicationPaths.production : applicationPaths.staging;
}
main().catch(err => console.error(chalk.red('An error occurred in the pipeline:', err.message)));