-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.js
1444 lines (1216 loc) · 52.9 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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This script automates the release process of a progressive web application via sftp.
To use this script:
1. Install the necessary dependencies by running [i]npm install[/i] in the terminal.
2. Run the script with [i]node pipeline.js -init[/i]. This will initialize the project by copying the necessary files and creating the config files.
3. Run the script with [i]node pipeline.js -h[/i]. This will display the help message.
4. If you want to release to production, add the [i]-production[/i] or [i]-p[/i] flag.
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.
*/
process.chdir('..');
const startTime = new Date();
console.log('');
console.log('Started Pipeline in ' + process.cwd());
const fs = require('fs');
const { promises: fsPromises, statSync } = require('fs');
const path = require('path');
const ProgressBar = require('progress');
let chalk = null;
const SftpClient = require('ssh2-sftp-client');
const UglifyJS = require("uglify-js");
const CleanCSS = require('clean-css');
const sharp = require('sharp');
const vm = require('vm');
const { parse } = require('node-html-parser');
const { exec } = require('child_process');
const util = require('util');
const toIco = require('to-ico');
const { loadConfig } = require('./package.js');
const execAsync = util.promisify(exec);
const pathSegments = path.dirname(__dirname).split(path.sep);
const localhostPath = pathSegments[pathSegments.length - 1];
const lockFilePath = './Birdhouse/pipeline.lock';
const infoFlag = process.argv.includes('-info') || process.argv.includes('-i');
removeLock();
function createLock() {
fs.writeFileSync(lockFilePath, 'locked');
infoFlag && console.log('Pipeline lock created.');
}
function removeLock() {
if (fs.existsSync(lockFilePath)) {
fs.unlinkSync(lockFilePath);
infoFlag && console.log('Pipeline lock removed.');
}
}
function exitPipeline() {
removeLock();
process.exit(0);
}
const defaultConfig = {
version: "1.0.0.0",
pageTitle: "My Web App",
cookieIdentifier: "my_unique_identifier_that_could_be_the_page_title_and_should_never_change_and_should_be_unique_on_the_domain_and_should_be_much_shorter_than_this_one",
foundationYear: 2024,
pageDescription: "",
localhostPath: '/' + localhostPath,
scope: '/',
excludedPaths: [],
openCookiePopupAtPageLoad: true,
showNewUpdateNotes: true,
maintenanceModeWithFailedBackend: false,
enableInputValidation: true,
enableImageComparisonSliders: true,
enableInfoBar: false,
userLoginEnabled: false,
redirect404ToRoot: false,
appIcon: 'img/app-icons/icon',
trustedImageDomains: [],
useMouseDown: false,
useFlexPopups: false,
backNavigationClosesPopups: true,
scrollPositionRecallLimit: 20,
};
const defaultPipelineConfig = {
sftpConfigFile: '../sftp-config.js',
productionPath: 'my_app_production',
stagingPath: 'my_app_staging',
distPath: 'Birdhouse/dist',
htaccessFile: 'UPLOAD-THIS.htaccess',
basePath: '/',
databaseDir: 'database',
uncompressedDir: 'img/uploads-uncompressed',
compressedDir: 'uploads',
faviconPath: 'img/logos-originals/Birdhouse-Logo.jpg',
faviconsOutputDir: 'img/favicons',
faviconsFileName: 'Favicon',
faviconSizes: [],
manifestIconPath: 'img/logos-originals/Birdhouse-Logo.png',
manifestIconOutputDir: 'img/icons',
manifestIconFileName: 'Icon',
manifestIconSizes: [],
statisticsFile: 'pipeline-log.txt',
ignoredFileTypes: ['.zip', '.rar', '.md', '.psd', '.htaccess'],
directoriesToInclude: ['src', 'fonts', 'img/favicons', 'img/icons', 'img/app-icons', 'img/screenshots', 'uploads'],
directoriesToExcludeFromCache: ['img/screenshots', 'uploads'],
preReleaseScripts: [],
postReleaseScripts: [],
appIconSourcePath: 'img/logos-originals/Birdhouse-Logo.jpg',
appIconOutputDir: 'img/app-icons',
};
const initializeFlag = process.argv.includes('-init') || process.argv.includes('-initialize');
const updateFlag = process.argv.includes('-update') || process.argv.includes('-u');
const updateRootFlag = process.argv.includes('-root') || process.argv.includes('-r');
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 forcedUpdateFlag = process.argv.includes('-forced') || process.argv.includes('-force');
const silentUpdateFlag = process.argv.includes('-silent');
const helpFlag = process.argv.includes('-help') || process.argv.includes('-h');
const minifyFlag = process.argv.includes('-minify') || process.argv.includes('-m');
const skipCompressedUploadFlag = process.argv.includes('-skipCompU') || process.argv.includes('-su');
const disableStatisticsFlag = process.argv.includes('-noLog') || process.argv.includes('-nl');
const generateFaviconsFlag = process.argv.includes('-genFavicons') || process.argv.includes('-gf');
const generateIconsFlag = process.argv.includes('-genIcons') || process.argv.includes('-gi');
const generateAppIconsFlag = process.argv.includes('-genAppIcons') || process.argv.includes('-ga');
const localFlag = process.argv.includes('-l') || process.argv.includes('-local');
function help() {
if (helpFlag || process.argv.length === 2) {
console.log(` Usage: node pipeline [options]
Options:
-u, -update Updates or creates the config.js and config-pipeline.js with necessary entries, orders them and exits
-r, -root Copies all files from /Birdhouse/root to the root directory and exits
-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|-l> Deletes the application production or staging directory from the server or clears the local dist directory
-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)
-l, -local Builds the project to the local dist directory and thereby skips the upload to the server (so -p and -s are ignored)
-forced <-p|-s|-l> Forces the update (triggers a page reload after the new version is cached on the user's device), without notifying the user
-silent <-p|-s|-l> Performs a silent update which does not display the update notification and becomes active after the next page reload
-su,-skipCompU Skips image compression and upload of the compressed folder
-gf,-genFavicons Creates favicons of all sizes from the original favicon and exits
-gi,-genIcons Creates icons of all sizes from the original icon and exits
-ga,-genAppIcons Creates .icon from the original icon and exits
Note: The database folder is uploaded, but not added to the cached files.
`);
exitPipeline();
}
}
let config = defaultPipelineConfig;
let sftpConfigFile = null;
if (!initializeFlag) {
help();
try {
config = require('../pipeline-config.js');
} catch (error) {
console.log(`Error loading pipeline - config.js: ${error.message}`);
process.exit(1);
}
if (!localFlag) {
const sftpConfigFilePath = '../' + (config.sftpConfigFile ? config.sftpConfigFile : '../sftp-config.js');
try {
sftpConfigFile = require(sftpConfigFilePath);
} catch (error) {
console.log(`Error loading sftp config file(${sftpConfigFilePath}): ${error.message}`);
process.exit(1);
}
}
}
async function importChalk() {
if (!chalk) {
chalk = (await import('chalk')).default;
}
}
const ignoredFileTypes = config.ignoredFileTypes ? config.ignoredFileTypes : [];
const directoriesToInclude = config.directoriesToInclude;
const cacheFile = './Birdhouse/filesToCache.js';
const applicationPaths = {
production: config.productionPath,
staging: config.stagingPath
};
const sftpConfig = localFlag ? null : {
host: sftpConfigFile ? sftpConfigFile.sftpHost : 'localhost',
port: sftpConfigFile ? sftpConfigFile.sftpPort : 22,
username: sftpConfigFile ? sftpConfigFile.sftpUsername : 'anonymous',
password: sftpConfigFile ? sftpConfigFile.sftpPassword : ''
};
const minifiedDirectory = 'Birdhouse/minified';
const faviconsFileName = config.faviconsFileName;
const faviconPath = config.faviconPath;
const faviconsOutputDir = config.faviconsOutputDir;
const faviconSizes = config.faviconSizes.length > 0 ? config.faviconSizes : [16, 32, 64, 128, 152, 167, 180, 192, 196];
const statisticsFile = config.statisticsFile;
const compressedDir = config.compressedDir;
const uncompressedDir = config.uncompressedDir;
const htaccessFile = config.htaccessFile;
const databaseDir = config.databaseDir;
const manifestIconFileName = config.manifestIconFileName;
const manifestIconPath = config.manifestIconPath;
const manifestIconOutputDir = config.manifestIconOutputDir;
const manifestIconSizes = config.faviconSizes.length > 0 ? config.faviconSizes : [48, 72, 464, 3000];
const appIconSourcePath = config.appIconSourcePath;
const appIconOutputDir = config.appIconOutputDir;
const fileTypeCounts = {};
let fileTypeSizes = {};
async function main() {
await importChalk();
if (localFlag || productionFlag || stagingFlag) {
createLock();
}
console.log('');
if (initializeFlag) {
console.log(chalk.green('Initializing project in ' + process.cwd()));
await initializeProject();
exitPipeline();
}
if ((productionFlag || stagingFlag || localFlag) && !deleteFlag && !rollbackFlag && config.preReleaseScripts.length > 0) {
await runScriptsSequentially(config.preReleaseScripts)
.then(() => {
console.log('Execution of all pre release scripts finished.')
console.log('');
})
.catch(error => console.error('An error occurred during pre release script execution:', error));
}
if (updateFlag) {
await updateConfig();
await updatePipelineConfig();
exitPipeline();
}
if (updateRootFlag) {
await updateRoot();
exitPipeline();
}
if (generateFaviconsFlag) {
await generateImageSizes(faviconPath, faviconsOutputDir, faviconsFileName, faviconSizes);
exitPipeline();
}
if (generateIconsFlag) {
await generateImageSizes(manifestIconPath, manifestIconOutputDir, manifestIconFileName, manifestIconSizes);
exitPipeline();
}
if (generateAppIconsFlag) {
await generateAppIcons(appIconSourcePath, appIconOutputDir);
exitPipeline();
}
let missingConfigs = [];
if (!config.ignoredFileTypes) missingConfigs.push('ignoredFileTypes');
if (!config.directoriesToInclude) missingConfigs.push('directoriesToInclude');
if (!config.productionPath) missingConfigs.push('productionPath');
if (!config.stagingPath) missingConfigs.push('stagingPath');
if (sftpConfigFile) {
if (!sftpConfigFile.sftpHost) missingConfigs.push('sftpHost');
if (!sftpConfigFile.sftpPort) missingConfigs.push('sftpPort');
if (!sftpConfigFile.sftpUsername) missingConfigs.push('sftpUsername');
if (!sftpConfigFile.sftpPassword) missingConfigs.push('sftpPassword');
}
if (missingConfigs.length > 0 && !localFlag) {
console.error(`Error: Missing necessary configuration values in ${config.sftpConfigFile}:`, missingConfigs.join(', '));
console.log('');
process.exit(1);
}
const applicationPath = getApplicationPath();
if (productionFlag || stagingFlag || localFlag) {
await updateRoot();
if (localFlag) {
console.log(chalk.green(`Starting build process to ${config.distPath}...`));
}
else {
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 = await updateVersion(newVersion);
}
else {
version = await updateVersion(currentVersion);
}
await createConfigForServiceWorker();
const filesToCache = await getFilesToCache();
information(filesToCache);
const cacheSize = await writeFilesToCacheFile(filesToCache);
await checkFilesExist(filesToCache);
let minifiedSize = '';
if (!deleteFlag) {
minifiedSize = await minifyFiles(filesToCache);
}
let filesUploaded = null;
if (rollbackFlag && !localFlag) {
await rollback(applicationPath);
}
else if (deleteFlag) {
if (localFlag) {
await clearDirectory(config.distPath);
}
else {
await deleteDirectoryFromServer(applicationPath);
}
}
else {
await compressImages();
if (localFlag) {
console.log('');
let filesToUpload = filesToCache;
if (databaseDir) {
filesToUpload = await readFilesFromDirectory(databaseDir, [...filesToCache]);
}
await copyFilesToLocalDirectory(filesToUpload, config.distPath, true);
if (htaccessFile) {
infoFlag && console.log(chalk.gray(`Copying ${htaccessFile} file as ".htacces" to ${config.distPath}...`));
await fsPromises.copyFile(htaccessFile, path.join(config.distPath, '.htaccess'));
infoFlag && console.log(chalk.gray('Copy successful'));
infoFlag && console.log('');
}
console.log(chalk.green(`Build process to ${config.distPath} finished.`));
} else {
filesUploaded = await uploadFilesToServer(filesToCache, applicationPath) | 0;
}
}
console.log('');
if ((productionFlag || stagingFlag || localFlag) && !deleteFlag && !rollbackFlag && (filesUploaded > 0 || localFlag)) {
console.log(chalk.green(`${localFlag ? 'Build' : 'Release'} process of version ${version} completed successfully.`));
if ((productionFlag || stagingFlag) && !deleteFlag && !rollbackFlag && config.postReleaseScripts.length > 0) {
await runScriptsSequentially(config.postReleaseScripts)
.then(() => {
console.log('Execution of all post release scripts finished.')
console.log('');
})
.catch(error => console.error('An error occurred during post release script execution:', error));
}
if (localFlag) {
loadConfig();
}
}
else {
console.log(chalk.green('Done.'));
}
const endTime = new Date();
const elapsedTime = endTime - startTime;
let minutes = Math.floor(elapsedTime / 60000);
let seconds = ((elapsedTime % 60000) / 1000).toFixed(0);
minutes = String(minutes).padStart(2, '0');
seconds = String(seconds).padStart(2, '0');
console.log(chalk.gray(`Elapsed time: ${minutes}:${seconds} minutes`));
await addStatistics(`${minutes}:${seconds} minutes`, version, filesUploaded, filesToCache.length, cacheSize, minifiedSize);
console.log('');
}
async function runScriptsSequentially(scriptPaths) {
for (const scriptPath of scriptPaths) {
console.log(`Executing script: ${scriptPath}`);
try {
const { stdout, stderr } = await execAsync(`node ${scriptPath}`);
if (stdout) console.log(stdout);
if (stderr) console.error('Error:', stderr);
} catch (error) {
console.error(`Failed to execute script ${scriptPath}:`, error);
}
}
console.log(`${scriptPaths.length} ${scriptPaths.length > 1 ? 'scripts have' : 'script has'} been executed.`);
}
async function initializeProject() {
const sourceDir = './Birdhouse/root_EXAMPLE';
const targetDir = './';
console.log('');
console.log(chalk.gray('If you just want to update your config.js and your pipeline-config.js, use the "-update"-flag.'));
console.log(chalk.gray('If you just want to move your root files to the root directory, use the "-root"-flag.'));
console.log('');
if (!fs.existsSync(sourceDir)) {
console.log(chalk.yellow('The root_EXAMPLE directory does not exist. Please pull/download the framework again. Skipping initialization.'));
console.log('');
} else {
console.log(chalk.yellow('Initializing project...'));
console.log('');
console.log('Copying files from root_EXAMPLE to root...');
await copyDirectory(sourceDir, targetDir);
console.log('');
await updateConfig();
await updatePipelineConfig();
await updateRoot();
console.log('');
await generateImageSizes(faviconPath, faviconsOutputDir, faviconsFileName, faviconSizes);
await generateImageSizes(manifestIconPath, manifestIconOutputDir, manifestIconFileName, manifestIconSizes);
console.log(chalk.green('Project initialized'));
console.log('');
}
}
async function copyDirectory(source, target) {
infoFlag && console.log(chalk.gray(` Copying from ${source} to ${target}...`));
if (!fs.existsSync(target)) {
fs.mkdirSync(target, { recursive: true });
}
const files = fs.readdirSync(source);
for (let file of files) {
const sourcePath = path.join(source, file);
const targetPath = path.join(target, file);
if (fs.existsSync(targetPath)) {
console.log(chalk.grey(` File ${targetPath} already exists. Skipping.`));
continue;
}
if (fs.lstatSync(sourcePath).isDirectory()) {
copyDirectory(sourcePath, targetPath);
} else {
if (file === '.htaccess') {
let content = fs.readFileSync(sourcePath, 'utf8');
content = content.replace(/LOCALHOST_PATH/g, defaultConfig.localhostPath);
fs.writeFileSync(targetPath, content, 'utf8');
} else {
fs.copyFileSync(sourcePath, targetPath);
}
}
}
infoFlag && console.log(chalk.gray(` Copy successful`));
}
async function checkFilesExist(files) {
let filesMissing = false;
await Promise.all(files.map(async (file) => {
try {
await fsPromises.access(file, fs.constants.F_OK);
} catch (err) {
console.log(chalk.red(` File does not exist: ${file}`));
filesMissing = true;
}
}));
if (filesMissing) {
console.log('');
throw new Error('One or more files are missing');
} else {
console.log(chalk.green('All files exist.'));
}
}
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;
if (!minifiedDirectory) {
console.log(chalk.yellow('No minified directory specified. Skipping minification.'));
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');
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 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');
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 {
totalSize += originalStats.size;
if (infoFlag) {
console.log(chalk.gray(` No minification for ${path.basename(file)}: ${originalSizeKB} KB`));
}
}
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;
if (!statisticsFile) {
console.log(chalk.yellow('No statistics file specified. Skipping statistics.'));
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(statisticsFile, 'utf8');
await fsPromises.writeFile(statisticsFile, statistics + (data || ''), 'utf8');
console.log(chalk.gray('Statistics saved'));
} catch (error) {
if (error.code === 'ENOENT') {
try {
await fsPromises.writeFile(statisticsFile, statistics, 'utf8');
console.log(chalk.green(`Statistics file created: ${statisticsFile} and statistics saved`));
} catch (writeError) {
console.error(`Error creating file: ${writeError}`);
}
} else {
console.error(`Error: ${error}`);
}
}
}
async function updateConfig() {
console.log(chalk.gray('Updating config.js...'));
const configPath = './config.js';
if (fs.existsSync(configPath)) {
const fileContent = fs.readFileSync(configPath, 'utf8');
const configMatch = fileContent.match(/export default (\{[\s\S]*\});/);
let newConfig;
if (configMatch) {
const configString = configMatch[1];
const script = new vm.Script(`obj = ${configString}`);
const context = { obj: {} };
script.runInNewContext(context);
const existingConfig = context.obj;
newConfig = { ...defaultConfig, ...existingConfig };
} else {
console.log(chalk.yellow('Could not parse config.js, writing default config'));
newConfig = defaultConfig;
}
fs.writeFileSync(configPath, `export default ${JSON.stringify(newConfig, null, 2)};`);
} else {
console.log(chalk.white('config.js does not exist, creating default config'));
fs.writeFileSync(configPath, `export default ${JSON.stringify(defaultConfig, null, 2)};`);
}
console.log(chalk.green('config.js updated'));
console.log('');
}
async function updatePipelineConfig() {
console.log(chalk.gray('Updating pipeline-config.js...'));
const configPath = './pipeline-config.js';
if (fs.existsSync(configPath)) {
const fileContent = fs.readFileSync(configPath, 'utf8');
const configMatch = fileContent.match(/module\.exports = (\{[\s\S]*\});/);
let newConfig;
if (configMatch) {
const configString = configMatch[1];
const script = new vm.Script(`obj = ${configString}`);
const context = { obj: {} };
script.runInNewContext(context);
const existingConfig = context.obj;
newConfig = { ...defaultPipelineConfig, ...existingConfig };
} else {
console.log(chalk.yellow('Could not parse pipeline-config.js, writing default config'));
newConfig = defaultPipelineConfig;
}
const configString = stringifyObject(newConfig);
fs.writeFileSync(configPath, `module.exports = ${configString};`);
} else {
console.log(chalk.white('pipeline-config.js does not exist, creating default config'));
const configString = stringifyObject(defaultPipelineConfig);
fs.writeFileSync(configPath, `module.exports = ${configString};`);
}
console.log(chalk.green('pipeline-config.js updated'));
console.log('');
function stringifyObject(obj, indent = ' ') {
if (Array.isArray(obj)) {
const entries = obj.map(value => {
if (typeof value === 'object' && value !== null) {
return `${indent}${stringifyObject(value, indent + ' ')}`;
}
return `${indent}${JSON.stringify(value)}`;
});
return `[\n${entries.join(',\n')}\n${indent}]`;
} else {
const entries = Object.entries(obj).map(([key, value]) => {
if (typeof value === 'object' && value !== null) {
return `${indent}${key}: ${stringifyObject(value, indent + ' ')}`;
}
return `${indent}${key}: ${JSON.stringify(value)}`;
});
return `{\n${entries.join(',\n')}\n${indent}}`;
}
}
}
async function updateRoot() {
console.log(chalk.gray('Updating root directory...'));
const sourceDir = './Birdhouse/root';
const destDir = './';
const files = await fsPromises.readdir(sourceDir);
for (const file of files) {
const sourceFile = path.join(sourceDir, file);
const destFile = path.join(destDir, file);
await fsPromises.copyFile(sourceFile, destFile);
}
console.log(chalk.green('Root directory updated'));
}
async function compressImages() {
if (skipCompressedUploadFlag) {
console.log(chalk.grey('Skipping image compression, because compressed uploads are skipped.'));
return;
}
if (!uncompressedDir) {
console.log(chalk.yellow('No uncompressed directory specified. Skipping image compression.'));
return;
}
else if (!compressedDir) {
console.log(chalk.yellow('No compressed directory specified. Skipping image compression.'));
return;
}
console.log('');
console.log(chalk.gray(`Compressing images...`));
const compressedFiles = await compressImagesInDirectory(uncompressedDir, compressedDir);
console.log(chalk.green(`Compressed ${compressedFiles} images`));
}
function isImageFile(filename) {
const imageExtensions = ['.jpg', '.jpeg', '.png'];
return imageExtensions.includes(path.extname(filename).toLowerCase());
}
async function compressImagesInDirectory(sourceDir, targetDir) {
await ensureDirectoryExists(sourceDir);
await ensureDirectoryExists(targetDir);
let compressedFiles = 0;
try {
const entries = await fsPromises.readdir(sourceDir, { withFileTypes: true });
for (const entry of entries) {
const sourcePath = path.join(sourceDir, entry.name);
const targetPath = path.join(targetDir, entry.name);
if (entry.isDirectory()) {
await fsPromises.mkdir(targetPath, { recursive: true });
compressedFiles += await compressImagesInDirectory(sourcePath, targetPath);
} else if (entry.isFile() && isImageFile(entry.name)) {
if (infoFlag) console.log(chalk.gray(`Compressing: ${entry.name}`));
try {
await sharp(sourcePath)
.resize(800)
.jpeg({ quality: 100 })
.toFile(targetPath);
compressedFiles++;
if (infoFlag) console.log(chalk.gray(`Compressed: ${entry.name}`));
} catch (err) {
console.error(`Error compressing ${entry.name}: ${err}`);
}
}
}
} catch (err) {
console.error(`Error reading directory: ${err}`);
}
return compressedFiles;
}
async function ensureDirectoryExists(dir) {
try {
await fsPromises.access(dir);
} catch (error) {
if (error.code === 'ENOENT') {
console.log(chalk.gray(`Creating directory: ${dir}`));
await fsPromises.mkdir(dir, { recursive: true });
} else {
throw error;
}
}
}
async function getCurrentVersion() {
let data;
try {
data = await fsPromises.readFile('./config.js', 'utf8');
} catch (err) {
throw new Error('Could not read config.js: ' + err.message);
}
const versionMatch = data.match(/"version": "(.*?)",/);
if (!versionMatch) throw new Error('Could not find version in config.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 filePath = './config.js';
let data = await fsPromises.readFile(filePath, 'utf8');
newVersion = newVersion.replace(/-f|-s/g, '');
console.log(forcedUpdateFlag ? 'Type: Forced Update' : silentUpdateFlag ? 'Type: Silent Update' : 'Type: Regular Update')
newVersion = `${newVersion}${forcedUpdateFlag ? '-f' : silentUpdateFlag ? '-s' : ''}`;
data = data.replace(/("version": ")(.*?)(",)/, `$1${newVersion}$3`);
await fsPromises.writeFile(filePath, data, 'utf8');
await updateVersionOnServiceWorker(newVersion);
console.log(`Version updated to: ${newVersion}`);
return newVersion;
}
async function updateVersionOnServiceWorker(newVersion) {
const filePath = './service-worker.js';
let data = await fsPromises.readFile(filePath, 'utf8');
data = data.replace(/self.CACHE_VERSION = "(.*?)";/, `self.CACHE_VERSION = "${newVersion}";`);
await fsPromises.writeFile(filePath, data, 'utf8');
console.log(chalk.gray(`Service Worker Version updated to: ${newVersion}`));
}
async function createConfigForServiceWorker() {
const filePath = './config.js';
const swFilePath = './config-sw.js';
try {
await fsPromises.access(filePath, fs.constants.F_OK);
} catch (err) {
const defaultConfig = `export default {
"version": "1.0.0",
};`;
await fsPromises.writeFile(filePath, defaultConfig, 'utf8');
console.log('Created default config.js');
}
try {
await fsPromises.access(swFilePath, fs.constants.F_OK);
} catch (err) {
await fsPromises.writeFile(swFilePath, '', 'utf8');
console.log('Created empty config-sw.js');
}
let data = await fsPromises.readFile(filePath, 'utf8');
data = data.replace('export default {', 'self.config = {');
await fsPromises.writeFile(swFilePath, data, 'utf8');
console.log('Created config for the service worker');
}
async function getFilesToCache() {
let filesToCache = [
'index.html',
'sitemap.xml',
'robots.txt',
'service-worker.js',
'config-sw.js',
'everywhere.js',
'config.js',
'updateNotes.js',
'manifest.json',
'admin-style.css',
'style.css',
'Birdhouse/default-style.css',
'Birdhouse/filesToCache.js',
'Birdhouse/service-worker-registration.js',
];
if (infoFlag) {
console.log(chalk.gray(`Specified files to cache:`));
filesToCache.forEach(element => {
console.log(chalk.gray(` ${element}`));
});
}
directoriesToInclude.push('Birdhouse/src');
directoriesToInclude.push('Birdhouse/fonts');
for (const dir of directoriesToInclude) {
infoFlag && console.log(chalk.gray(`Reading files from ${dir}...`));
await readFilesFromDirectory(dir, filesToCache);
}
return [...new Set(filesToCache)].sort();
}
async function readFilesFromDirectory(directory, files = []) {
await ensureDirectoryExists(directory);
infoFlag && console.log(chalk.gray(`Reading files from ${directory}...`));
const filesInDirectory = await fsPromises.readdir(directory);
for (const file of filesInDirectory) {
const fullPath = path.join(directory, file);
if (file.startsWith('.')) {
infoFlag && console.log(chalk.gray(` Skipping hidden file/folder: ${fullPath}`));
}
else if (fs.existsSync(fullPath)) {
const stats = await fsPromises.stat(fullPath);
if (stats.isDirectory()) {
infoFlag && console.log(chalk.gray(` Reading files from ${fullPath}...`));
await readFilesFromDirectory(fullPath, files);
} else {
const fileType = path.extname(file);
if (!ignoredFileTypes.includes(fileType)) {
infoFlag && console.log(chalk.gray(` Adding ${fullPath} to files to cache...`));
files.push(fullPath.replace(/\//g, '/'));
fileTypeCounts[fileType] = (fileTypeCounts[fileType] || 0) + 1;
fileTypeSizes[fileType] = (fileTypeSizes[fileType] || 0) + stats.size;
}
}
} else {
console.log(`File does not exist: ${fullPath}`);
}
}
return files;
}
async function writeFilesToCacheFile(filesToCache) {
console.log('');
let totalSize = 0;
const filteredFilesToCache = filesToCache.filter(file => {
return !config.directoriesToExcludeFromCache.some(dir => file.startsWith(dir));
});
if (cacheFlag) {
console.log(chalk.gray(`Writing ${filteredFilesToCache.length} files to ${cacheFile}...`));
let fileContent = 'self.filesToCache = [\n';
fileContent += filteredFilesToCache.map(f => `'/${f}',`.replace(/\\/g, '/')).join('\n');
fileContent += '\n];';
await fsPromises.writeFile(cacheFile, fileContent, 'utf8');
const stats = statSync(cacheFile);
totalSize += stats.size;
}
filteredFilesToCache.forEach(file => {
infoFlag && console.log(chalk.gray(` File: ${file}`));
if (file != cacheFile) {
const stats = statSync(file);
totalSize += stats.size;
}
});
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;
if (skipCompressedUploadFlag) {
console.log(chalk.grey('Skipping files that are in the compressed directory.'))
filesToCache = filesToCache.filter(file => !file.startsWith(config.compressedDir));