-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommons.js
1121 lines (990 loc) · 33.6 KB
/
commons.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
/* Common information that will be used by other scripts and code */
// Default Country when any possible method to get country isn't available
const defaultCountry = 'UK' // when no other method finds the country of user, use this by default
const defaultPortProd = 3028 // default HTTP Port where the app listens - prod version
const defaultPortDev = 3027 // default HTTP Port where the app listens - dev version
const defaultPortTest = 3026 // default HTTP Port where the app listens - test version
module.exports = {
init: function (eventEmitter) {
if (eventEmitter) {
EVENTEMITTER = eventEmitter
}
_init()
},
getROOT_DIR: function () {
if (!ROOT_DIR || typeof ROOT_DIR === 'undefined') {
setROOT_DIR()
}
return ROOT_DIR
},
getSettings: function () {
if (isObjectEmptyOrInvalid(SETTINGS)) {
_init()
}
return SETTINGS
},
getRelease: function () {
if (!RELEASE || typeof RELEASE === 'undefined') {
_init()
}
return RELEASE
},
setRelease: function (release) {
// check that release was correctly chosen
if (release !== 'dev' && release !== 'prod' && release !== 'test') {
throw Error("Error on function setRelease(release) in commons.js; 'dev', 'test' or 'prod' must be selected")
}
RELEASE = release
},
getDirectories: function () {
if (isObjectEmptyOrInvalid(DIRECTORIES)) {
setDIRECTORIES()
}
return DIRECTORIES
},
getFileNames: function () {
if (isObjectEmptyOrInvalid(FILENAMES)) {
setFILENAMES()
}
return FILENAMES
},
getUrlsObject: getUrlsObject,
getUniqueArray: getUniqueArray,
getKeyByValue: getKeyByValue,
getCClistOnStr: getCClistOnStr,
getDataBaseErrMsg: getDataBaseErrMsg,
getConsoleColors: getConsoleColors,
checkForInternet: checkForInternet,
extractHostname: extractHostname,
runNodeScriptSync: runNodeScriptSync,
getCountriesObj: getCountriesObj,
getNumberOfCountries: getNumberOfCountries,
getProgressBar: getProgressBar,
parseJsonProperty: parseJsonProperty,
isEmptyObject: isEmptyObject
}
/***************************************************************************************************/
/***************************************************************************************************/
/***************************************************************************************************/
let RELEASE // release, "dev", "test" or "prod"
let ROOT_DIR // root directory of the project
let SWITCHES, DIRECTORIES, SETTINGS, FILENAMES, EVENTEMITTER
let optionDefinitions // for the commandLineArgs
const path = require('path')
const fs = require('fs')
const commandLineArgs = require('command-line-args')
const colors = require('colors')
const isOnline = require('is-online')
const flat = require('flat')
const debug = require('debug')('app:commons')
colors.setTheme(getConsoleColors())
// initialization
function _init () {
/* GLOBAL switches, false by default */
/* these values are defined by the command line arguments */
SWITCHES = {
cdn: false, /* Content Delivery Network */
uber: false, /* uses UBER API to give car user comparisions with UBER costs */
social: false, /* Social media pulgins */
disableCharts: false, /* Disable Charts on result */
googleCaptcha: false, /* Google Captcha to avoid spam-bots */
googleAnalytics: false, /* Google Analytics */
database: false, /* Inserts user input data into a DataBase */
print: false, /* Print result option, at the end */
pdf: false /* Download PDF report option */
}
// basic command line settings
optionDefinitions = [
{ name: 'help', alias: 'h', type: Boolean },
{ name: 'release', alias: 'r', type: String },
{ name: 'port', alias: 'p', type: Number }
]
// populates optionDefinitions of commandLineArgs according to SWITCHES
for (const service in SWITCHES) {
optionDefinitions.push({ name: service, type: Boolean })
}
if (RELEASE === 'test') {
optionDefinitions.push({ name: 'frontendTest', type: String })
}
// get set commandLineArgsObject from command line arguments
let commandLineArgsObject
try {
commandLineArgsObject = commandLineArgs(optionDefinitions)
// this "commandLineArgsObject" is just filled with the options that were inserted in the command line
// console.log(commandLineArgsObject);
} catch (err) {
console.log('Unknown option: ' + err.optionName, '\n')
commandLineArgsObject = { help: true }
}
RELEASE = RELEASE || commandLineArgsObject.release // set Global variable
// check that release was correctly chosen
if (RELEASE !== 'dev' && RELEASE !== 'test' && RELEASE !== 'prod') {
RELEASE = 'dev'
}
console.log("Release: '" + RELEASE + "'")
// shows NODE_ENV
if (process.env.NODE_ENV) {
console.log('NODE_ENV: ', process.env.NODE_ENV)
}
// after the RELEASE is known, the directories and files can be obtained and set
setDIRECTORIES()
setFILENAMES()
// check if --help was selected
if (commandLineArgsObject.help) {
console.log(getArgvHelpMsg())
process.exit()
}
// set HTTP port
let HTTPport
if (commandLineArgsObject.port) {
HTTPport = commandLineArgsObject.port
} else {
if (RELEASE === 'prod') {
HTTPport = defaultPortProd
} else if (RELEASE === 'dev') {
HTTPport = defaultPortDev
} else if (RELEASE === 'test') {
HTTPport = defaultPortTest
} else {
throw Error('Error setting port')
}
}
// set SWITCHES according to commandLineArgs input options
if (commandLineArgsObject.All) {
for (const opt in SWITCHES) {
SWITCHES[opt] = true
}
} else {
for (const opt in commandLineArgsObject) {
if (opt !== 'release') {
SWITCHES[opt] = commandLineArgsObject[opt]
}
}
}
SETTINGS = {
release: RELEASE,
switches: SWITCHES,
HTTPport: HTTPport,
cdn: { // a CDN provider might be: https://app.keycdn.com/zones
enabled: SWITCHES.cdn,
name: 'cdn',
propName: 'url',
propType: 'string',
url: ''
},
uber: {
enabled: SWITCHES.uber,
name: 'uber',
propName: 'token',
propType: 'string',
token: ''
},
googleCaptcha: {
enabled: SWITCHES.googleCaptcha,
name: 'googleCaptcha',
propName: 'secretKey',
propType: 'string',
secretKey: ''
},
googleAnalytics: {
enabled: SWITCHES.googleAnalytics,
name: 'googleAnalytics',
propName: 'trackingId',
propType: 'string',
trackingId: ''
},
database: {
enabled: SWITCHES.database,
name: 'database',
propName: 'credentials',
propType: 'object',
credentials: {}
},
money: {
// in test version we don't use the money API because test version credentials are public
// and money API credentials must be private
enabled: RELEASE === 'test' ? false : SWITCHES.database,
name: 'money',
propName: 'ApiId',
propType: 'string',
ApiId: ''
},
defaultCountry: defaultCountry,
commandLineArgsObject: commandLineArgsObject
}
checkForInternet()
// reads data from JSON file with credentials for each service (in directory credentials/)
let credentialsFileName
switch (RELEASE) {
case 'prod':
case 'dev':
case 'test':
credentialsFileName = FILENAMES.server.credentialsFullPath[RELEASE]
break
default:
throw Error('Unkown Release ' + RELEASE)
}
debug(credentialsFileName)
// fills missing information, for each service corresponding property: "url", "token", "secretKey", etc.
// gets the information from the credentials JSON file
for (const service in SETTINGS) {
const serviceObj = SETTINGS[service]
if (serviceObj.enabled) {
if (!fs.existsSync(credentialsFileName)) {
throw getNoServiceErrMsg(serviceObj, credentialsFileName)
}
const credentialsData = JSON.parse(fs.readFileSync(credentialsFileName))
if (serviceObj.propType === 'string') {
const dataStr = credentialsData[serviceObj.name][serviceObj.propName]
// check if string is valid (no just whitespaces or asterisks)
if (!isValidCredentialString(dataStr)) {
throw getNoServiceErrMsg(serviceObj, credentialsFileName)
}
serviceObj[serviceObj.propName] = dataStr
} else if (serviceObj.propType === 'object') { // if service data is an object (normally applies to database)
const dataObj = credentialsData[serviceObj.name]
if (!isValidCredentialString(dataObj)) {
throw getNoServiceErrMsg(serviceObj, credentialsFileName)
}
serviceObj[serviceObj.propName] = Object.assign({}, dataObj) // clone object
} else {
throw Error('Error getting service information from ' + serviceObj.name)
}
}
}
// downloads/copy google analytics file from Google Server
if (SWITCHES.googleAnalytics) {
downloadGoogleAnalyticsJSFIle()
}
// set FILENAMES URIs for JS known files according to Local files or CDN
// if cdn option is enabled, select CDN version, otherwise Local files
setCdnOrLocalFiles(SWITCHES.cdn)
debug('SETTINGS', SETTINGS)
debug('DIRECTORIES', DIRECTORIES)
debug('FILENAMES', FILENAMES)
}
function setDIRECTORIES () {
if (typeof ROOT_DIR === 'undefined') {
setROOT_DIR()
}
/* Always leave the traling slash at the end on each directory */
/* this directory structure was based on Node/JS most common practises,
namely see: docs/nodeJS-directory-structure.md */
// Source directory - the directory where the source code is stored
const srcDir = path.join(ROOT_DIR, 'src')
// Bin directory - the directory to where the source code is deployed after running the bash script ./build.sh
const binDir = path.join(ROOT_DIR, 'bin')
// Build directory - the directory to where the building scripts are stored
const buildDir = path.join(ROOT_DIR, 'build')
// directory where test scripts and files are stored
const testDir = path.join(ROOT_DIR, 'test')
// credentials directory where json credential files are stored for each service
const credentialsDir = path.join(ROOT_DIR, 'credentials')
const serverDirs = {
root: ROOT_DIR,
src: srcDir,
bin: binDir,
build: buildDir,
test: testDir,
credentials: credentialsDir
}
/* ################################# */
// these paths are relative, and they refer to the paths which are seen by the browser
const clientDirs = {
client: 'client', // directory with respect to src/ dir, where the client JS browser files will be stored
css: 'css', // directory with respect to src/ dir, where the CSS files will be stored
tables: 'tables' // where the JPG tables with car costs for each country
}
/* ################################# */
// these paths are relative and refer to the project's code parent folder,
// i.e., the parent directory of these paths is either src/ or bin/
const projectDirs = {
countries: 'countries',
css: 'css',
tables: 'tables',
images: 'images',
public: 'public',
views: 'views',
client: 'client',
server: 'server'
}
const srcProjectDirs = {}
const binProjectDirs = {}
for (const prop in projectDirs) {
srcProjectDirs[prop] = path.join(srcDir, projectDirs[prop])
binProjectDirs[prop] = path.join(binDir, projectDirs[prop])
}
DIRECTORIES = {
// these paths are absolute
server: serverDirs, // these paths are absolute
src: srcProjectDirs, // these paths are absolute
bin: binProjectDirs, // these paths are absolute
client: clientDirs, // these paths are relative (as seen by the browser)
project: projectDirs // these paths are relative (as seen by either src/ or bin/)
}
}
function setFILENAMES () {
if (!RELEASE) {
_init()
}
if (isObjectEmptyOrInvalid(DIRECTORIES)) {
setDIRECTORIES()
}
// for release "prod" and "test" use /bin directories
const countriesDir = RELEASE === 'dev' ? DIRECTORIES.src.countries : DIRECTORIES.bin.countries
const clientDir = RELEASE === 'dev' ? DIRECTORIES.src.client : DIRECTORIES.bin.client
// Default file names for JSON files with credentials for each external service
FILENAMES = {
// these paths are ABSOLUTE
build: {
compressImages: path.join(DIRECTORIES.server.build, 'compressImages.js'),
generateTables: path.join(DIRECTORIES.server.build, 'generateTables.js'),
getAvgFromDB: path.join(DIRECTORIES.server.build, 'getAvgFromDB.js'),
minifyFiles: path.join(DIRECTORIES.server.build, 'minifyFiles.js'),
rasterTables: path.join(DIRECTORIES.server.build, 'rasterTables.js'),
setCountrySpecsDB: path.join(DIRECTORIES.server.build, 'setCountrySpecsDB.js'),
statsFunctions: path.join(DIRECTORIES.server.build, 'statsFunctions.js'),
gAnalytics: path.join(DIRECTORIES.server.build, 'gAnalytics.js')
},
project: {
countriesInfoFile: path.join(countriesDir, 'info.json'),
'conversions.js': path.join(clientDir, 'core', 'conversions.js'),
'calculator.js': path.join(clientDir, 'core', 'calculator.js'),
'convertData.js': path.join(clientDir, 'convertData.js'),
'validateData.js': path.join(clientDir, 'validateData.js')
},
server: {
credentials: {
prod: 'prodCredentials.json',
dev: 'devCredentials.json',
test: 'testCredentials.json'
},
credentialsFullPath: {
prod: '', // it will be filled by this script right under
dev: '',
test: ''
}
},
// the LOCAL paths are RELATIVE to the main host as seen by the BROWSER,
// thus don't use node 'fs' nor 'path' functions, i.e., these are URI or part of URI
client: {
jquery: {
local: DIRECTORIES.client.client + '/jquery/jquery.min.js',
cdn: 'https://code.jquery.com/jquery-latest.min.js',
uri: '' // it will be one of the above
},
chartjs: {
local: DIRECTORIES.client.client + '/chart/chartjs.min.js',
cdn: 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js',
uri: '' // it will be one of the above
},
GrecaptchaAPI: 'https://www.google.com/recaptcha/api.js',
Ganalytics: 'https://www.google-analytics.com/analytics.js'
}
}
// fills credentialsFullPath subObject
for (const file in FILENAMES.server.credentials) {
FILENAMES.server.credentialsFullPath[file] =
path.join(DIRECTORIES.server.credentials, FILENAMES.server.credentials[file])
}
}
// get parent directory of project directory tree
// tries to get according to engine, NodeJS or PhantomJS
function setROOT_DIR () { // eslint-disable-line camelcase
let rootDir
if ((typeof process !== 'undefined') &&
(process.release.name.search(/node|io.js/) !== -1)) { // node
// the root directory of the project is where this file is stored
rootDir = path.resolve(__dirname, '.')
debug('Node is running. ROOT_DIR: ' + rootDir)
} else { // PhantomJS?
try {
// considering the phantom is called from build/
// it needs to go back to the parent directory to get the root directory of the project
rootDir = fs.absolute('../')
debug('Phantom is running. ROOT_DIR: ' + rootDir)
} catch (err) {
throw Error('Engine not recognized, nor NodeJS nor PhantomJS')
}
}
ROOT_DIR = rootDir
}
// set FILENAMES URI for JS known files according to Local files or CDN
// if cdn option is enabled, select CDN version, otherwise local files
// true for CDN; false for Local files
function setCdnOrLocalFiles (isCDN) {
for (const key in FILENAMES.client) {
const obj = FILENAMES.client[key]
// it is a client filename with different values for cdn and local
if (obj.local && obj.cdn) {
obj.uri = isCDN ? obj.cdn : obj.local
}
}
// ensures that CDN URL to be passed to client is blank in case cdn option is not enabled
if (!isCDN) {
SETTINGS.cdn.url = ''
}
}
// checks for internet conn. in case of either options "uber", "cdn", "social", "googleCaptcha" or "googleAnalytics"
// are selected. These options require Internet and thus enables/disables them according to Internet connection
function checkForInternet () {
// bin/server.js services demanding Internet
const servicesDemandingInternet = ['uber', 'cdn', 'social', 'database', 'googleCaptcha', 'googleAnalytics']
const activatedServiceDemandingInternet = []
for (let i = 0; i < servicesDemandingInternet.length; i++) {
if (SWITCHES[servicesDemandingInternet[i]]) {
activatedServiceDemandingInternet.push(servicesDemandingInternet[i])
}
}
const debugInternet = require('debug')('app:checkForInternet')
// if there are no services requiring internet, there is no need to check for internet
if (activatedServiceDemandingInternet.length === 0) {
debugInternet('No activated services requiring Internet, thus no need to check for Internet')
return
}
debugInternet('Activated services requiring Internet:', activatedServiceDemandingInternet)
// check for Internet connection every 10 seconds and updates if status changes
let internetStatus = 'offline'
const checkInternetConnection = () => {
isOnline({ timeout: 10000 }).then(function (online) {
debugInternet(online ? 'online' : 'offline')
if (!online && internetStatus === 'online') {
console.log('There is no Internet connection'.warn)
internetStatus = 'offline'
if (SWITCHES.cdn) {
setCdnOrLocalFiles(false) // set Local files, with paramter "false"
}
// disable activated services that require internet
const len = activatedServiceDemandingInternet.length
for (let i = 0; i < len; i++) {
SWITCHES[activatedServiceDemandingInternet[i]] = false
}
process.stdout.write('Services disabled: ')
for (let i = 0; i < len; i++) {
process.stdout.write(activatedServiceDemandingInternet[i] + (i !== len - 1 ? ', ' : '.\n'))
}
if (EVENTEMITTER) {
EVENTEMITTER.emit('settingsChanged')
EVENTEMITTER.emit('onlineStatus', false)
}
} else if (online && internetStatus === 'offline') {
console.log('The server is online'.green)
internetStatus = 'online'
// enabling services that were initially activated on startup and that require internet
const len = activatedServiceDemandingInternet.length
for (let i = 0; i < len; i++) {
SWITCHES[activatedServiceDemandingInternet[i]] = true
}
process.stdout.write('Services enabled: ')
for (let i = 0; i < len; i++) {
process.stdout.write(activatedServiceDemandingInternet[i] + (i !== len - 1 ? ', ' : '.\n'))
}
if (EVENTEMITTER) {
EVENTEMITTER.emit('onlineStatus', true)
EVENTEMITTER.emit('settingsChanged')
}
}
setTimeout(checkInternetConnection, 10000)
})
}
checkInternetConnection()
}
function getCountriesObj () {
if (isObjectEmptyOrInvalid(FILENAMES)) {
setFILENAMES()
}
const countriesInfo = JSON.parse(fs.readFileSync(FILENAMES.project.countriesInfoFile, 'utf8'))
const availableCountries = countriesInfo.availableCountries
return availableCountries
}
function getNumberOfCountries () {
if (isObjectEmptyOrInvalid(FILENAMES)) {
setFILENAMES()
}
const countriesInfo = JSON.parse(fs.readFileSync(FILENAMES.project.countriesInfoFile, 'utf8'))
const numberOfCountries = Object.keys(countriesInfo.availableCountries).length
return numberOfCountries
}
function downloadGoogleAnalyticsJSFIle () {
if (isObjectEmptyOrInvalid(FILENAMES)) {
setFILENAMES()
}
const https = require('https')
const gAnalyticsJSFile = path.join(DIRECTORIES.bin.client, 'analytics.js')
// download and save file
const file = fs.createWriteStream(gAnalyticsJSFile)
https.get(FILENAMES.client.Ganalytics, function (response) {
response.pipe(file)
console.log(`Downloaded Google Analytics Javascript file from ${FILENAMES.client.Ganalytics} to ${gAnalyticsJSFile}`)
fs.chmod(gAnalyticsJSFile, 0o777, (err) => {
if (err) {
console.error(`Can't change the permissions of file ${file}`)
} else {
debug(`The permissions for file ${gAnalyticsJSFile} have been changed!`)
}
})
}).on('error', (e) => {
SWITCHES.googleAnalytics = false
console.error("Can't download Google Analytics JS file at " + FILENAMES.client.Ganalytics, e)
})
}
// gets Array with unique non-repeated values
// ex: [2,2,3,4,4] returns [2,3,4]
function getUniqueArray (Arr) {
const newArr = (Object.values(Arr))
.filter(function (x, i, a) {
return a.indexOf(x) === i
})
return newArr
}
// get Key by Value, ex: var hash = {foo: 1, bar: 2}; getKeyByValue(hash, 2); => 'bar'
function getKeyByValue (object, value) {
const key = Object.keys(object)
.find(function (key) {
return object[key] === value
})
return key
}
// from the countries Object "available_CT" get a string of CC separated by commas
// for example: "PT, BR, US, UK, etc."
function getCClistOnStr (availableCT) {
let str = ''
for (const CC in availableCT) {
str += CC + ', '
}
// strip the last two character of the string, the last ", "
str = str.slice(0, -2)
return str
}
function getArgvHelpMsg () {
const filename = path.basename(process.mainModule.filename)
// credentials Directory seen from Root directory
const credDirRelativePath = path.relative(DIRECTORIES.server.root, DIRECTORIES.server.credentials)
const messg = '\n\n' +
'Usage: node ' + filename + ' [options]\n' +
'Ex: node ' + filename + ' -r prod --uber --database\n' +
'\n' +
'Options: \n' +
'-r, --release [dev]elopment, [test] or [prod]uction\n' +
'-p, --port HTTP port on which the application is listening. ' +
'Default (test|dev|prod)=(' + defaultPortTest + '|' + defaultPortDev + '|' + defaultPortProd + ')\n' +
' --print Enables the standard printing of final report\n' +
' --pdf Enables the downloading of a pdf final report (using pdfmake)\n' +
' --social Enables social media plugin\n' +
' --disableCharts Disables Charts on final report\n' +
'\n' +
' External API services, disabled by default\n' +
' API credentials must be in either ' + credDirRelativePath + '/(prod|dev|test)Credentials.json according to release\n' +
' --cdn Enables Content Delivery Network\n' +
' --uber Enables UBER API\n' +
' --googleCaptcha Enables Google Captcha V2 anti-bot for calculation button\n' +
' --googleAnalytics Enables Google Analytics\n' +
' --database Enables a mysql Database\n' +
'\n'
return messg
}
function getNoServiceErrMsg (serviceObj, fileName) {
const messg = '\nConsidering you enabled the ' + serviceObj.name +
" services and you're using the release '" + RELEASE + "', " +
'you have to either:\n' +
' - insert within the file ' + colors.green.bold(path.relative(ROOT_DIR, fileName)) + ' a valid ' +
colors.green.bold(serviceObj.name + ' ' + serviceObj.propName) + ' (see readme.md), or\n' +
' - disable the ' + serviceObj.name + ' service.\n'
return messg
}
function getDataBaseErrMsg (scriptFullPath, serviceObj) {
const messg = '\nThis building script ' + path.relative(ROOT_DIR, scriptFullPath) +
' needs the Database credentials to run, therefore:\n' +
'- enable the Database option (--database) and provide also its credentials on ' +
path.relative(ROOT_DIR, FILENAMES.server.credentialsFullPath[RELEASE]) + ', or\n' +
'- do not run this particular building script file while building.\n'
return messg
}
function getConsoleColors () {
const colorsTheme = {
mainOption: ['yellow', 'bold'],
mainOptionStep: ['blue', 'bold'],
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
server: 'magenta',
client: 'blue',
error: ['red', 'bold']
}
return colorsTheme
}
// returns an object with several different information about the domains
function getUrlsObject (countriesInfo) {
if (!countriesInfo) {
countriesInfo = JSON.parse(fs.readFileSync(FILENAMES.project.countriesInfoFile, 'utf8'))
}
const domainsCountries = countriesInfo.domainsCountries
const domainsObj = {}
domainsObj.__url_selector = 'https://github.com/jfoclpf/autocosts/wiki/URL-selector'
domainsObj.__domainName_policy = 'https://github.com/jfoclpf/autocosts/wiki/Domain-name-policy'
domainsObj.canonicalHostname = domainsCountries // Object that associates a Country Code (CC) with a domain
domainsObj.uniqueArrayOfCanonicalHostname = getUniqueArray(domainsCountries) // Array with unique domain names
domainsObj.devDomain = countriesInfo.devDomain
domainsObj.legacyDomains = countriesInfo.legacyDomains
// for every domain, count how many domain names
// ex: 'autocustos.pt': 1, 'autocosts.info': 19
const countsOfCanonicalHostname = {}
const arr = Object.values(domainsCountries)
for (let i = 0; i < arr.length; i++) {
countsOfCanonicalHostname[arr[i]] = 1 + (countsOfCanonicalHostname[arr[i]] || 0)
}
domainsObj.countsOfCanonicalHostname = countsOfCanonicalHostname
// object with the url path after host/domain '/ar' for AR or '' for PT
// because the url for PT is merely autocustos.pt without path
const canonicalPathname = {}
for (const CC in domainsCountries) {
// check if domain is a ccTLD, ex: autocustos.pt is and autocustos.info is not
const uppperExtension = domainsCountries[CC].split('.').pop().toUpperCase() // 'PT' in autocustos.pt
if (isoCountries.hasOwnProperty(uppperExtension)) { // eslint-disable-line no-prototype-builtins
canonicalPathname[CC] = ''
} else {
canonicalPathname[CC] = '/' + CC.toLowerCase()
}
}
domainsObj.canonicalPathname = canonicalPathname
const canonicalStatsUrl = {}
for (const CC in domainsCountries) { // PT: autocustos.pt, CA: autocosts.info, etc.
canonicalStatsUrl[CC] = domainsCountries[CC] + canonicalPathname[CC] + '/stats'
}
domainsObj.canonicalStatsUrl = canonicalStatsUrl
return domainsObj
}
// returns true when an object is empty = {} or invalid
function isObjectEmptyOrInvalid (obj) {
return typeof obj === 'undefined' || !obj || isEmptyObject(obj)
}
// detects whether an Object is empty
function isEmptyObject (obj) {
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) { // eslint-disable-line no-prototype-builtins
return false
}
}
return JSON.stringify(obj) === JSON.stringify({})
}
// checks if a credential is valid
function isValidCredentialString (data) {
if (typeof data === 'string') {
// check if string is valid (no just whitespaces or asterisks)
return data && data.replace(/(\s|\*)/g, '').length
} else if (typeof data === 'object') {
const flattenedObj = flat.flatten(data)
for (const key in flattenedObj) {
const str = flattenedObj[key]
// first character. When property of obj starts with '_' it's comment, thus ignore
if (key.charAt(0) !== '_') {
if (!str || !str.replace(/(\s|\*)/g, '').length) {
return false
}
}
}
return true
} else {
return false
}
}
// extract hostname/domain from url
function extractHostname (url) {
let hostname
// find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf('://') > -1) {
hostname = url.split('/')[2]
} else {
hostname = url.split('/')[0]
}
// find & remove port number
hostname = hostname.split(':')[0]
// find & remove "?"
hostname = hostname.split('?')[0]
return hostname
}
function runNodeScriptSync (scriptPath, argvParam, stdio = 'inherit') {
console.log('\nRunning: ' + colors.cyan(path.relative(__dirname, scriptPath)))
const { spawnSync } = require('child_process')
const argv = argvParam || []
argv.unshift(scriptPath) // adds element at beginning
argv.push('-r', RELEASE)
debug('Running: node ' + argv.join(' '))
try {
spawnSync('node', argv, { stdio: [stdio, stdio, 'inherit'] })
} catch (err) {
const errMsg = 'Error executing script: ' + path.relative(__dirname, scriptPath).error
console.error(Error(errMsg + '\n' + err))
process.exit(1)
}
}
function getProgressBar (totalNumberOfTicks, muted) {
let Bar
if (!muted) {
const ProgressBar = require('progress')
Bar = new ProgressBar('[:bar] :percent :info', { total: totalNumberOfTicks, width: 80 })
} else {
Bar = { tick: function () {}, terminate: function () {} }
}
return Bar
}
// to be used by JSON.parse
// if json property is a number within a String (!isNaN) convert its type to Number
function parseJsonProperty (key, value) {
return !isNaN(value) ? parseFloat(value) : value
}
// 2-letter ISO Country Codes
const isoCountries = {
AF: 'Afghanistan',
AX: 'Aland Islands',
AL: 'Albania',
DZ: 'Algeria',
AS: 'American Samoa',
AD: 'Andorra',
AO: 'Angola',
AI: 'Anguilla',
AQ: 'Antarctica',
AG: 'Antigua And Barbuda',
AR: 'Argentina',
AM: 'Armenia',
AW: 'Aruba',
AU: 'Australia',
AT: 'Austria',
AZ: 'Azerbaijan',
BS: 'Bahamas',
BH: 'Bahrain',
BD: 'Bangladesh',
BB: 'Barbados',
BY: 'Belarus',
BE: 'Belgium',
BZ: 'Belize',
BJ: 'Benin',
BM: 'Bermuda',
BT: 'Bhutan',
BO: 'Bolivia',
BA: 'Bosnia And Herzegovina',
BW: 'Botswana',
BV: 'Bouvet Island',
BR: 'Brazil',
IO: 'British Indian Ocean Territory',
BN: 'Brunei Darussalam',
BG: 'Bulgaria',
BF: 'Burkina Faso',
BI: 'Burundi',
KH: 'Cambodia',
CM: 'Cameroon',
CA: 'Canada',
CV: 'Cape Verde',
KY: 'Cayman Islands',
CF: 'Central African Republic',
TD: 'Chad',
CL: 'Chile',
CN: 'China',
CX: 'Christmas Island',
CC: 'Cocos (Keeling) Islands',
CO: 'Colombia',
KM: 'Comoros',
CG: 'Congo',
CD: 'Congo, Democratic Republic',
CK: 'Cook Islands',
CR: 'Costa Rica',
CI: 'Cote D\'Ivoire',
HR: 'Croatia',
CU: 'Cuba',
CY: 'Cyprus',
CZ: 'Czech Republic',
DK: 'Denmark',
DJ: 'Djibouti',
DM: 'Dominica',
DO: 'Dominican Republic',
EC: 'Ecuador',
EG: 'Egypt',
SV: 'El Salvador',
GQ: 'Equatorial Guinea',
ER: 'Eritrea',
EE: 'Estonia',
ET: 'Ethiopia',
FK: 'Falkland Islands (Malvinas)',
FO: 'Faroe Islands',
FJ: 'Fiji',
FI: 'Finland',
FR: 'France',
GF: 'French Guiana',
PF: 'French Polynesia',
TF: 'French Southern Territories',
GA: 'Gabon',
GM: 'Gambia',
GE: 'Georgia',
DE: 'Germany',
GH: 'Ghana',
GI: 'Gibraltar',
GR: 'Greece',
GL: 'Greenland',
GD: 'Grenada',
GP: 'Guadeloupe',
GU: 'Guam',
GT: 'Guatemala',
GG: 'Guernsey',
GN: 'Guinea',
GW: 'Guinea-Bissau',
GY: 'Guyana',
HT: 'Haiti',
HM: 'Heard Island & Mcdonald Islands',
VA: 'Holy See (Vatican City State)',
HN: 'Honduras',
HK: 'Hong Kong',
HU: 'Hungary',
IS: 'Iceland',
IN: 'India',
ID: 'Indonesia',
IR: 'Iran, Islamic Republic Of',
IQ: 'Iraq',
IE: 'Ireland',
IM: 'Isle Of Man',
IL: 'Israel',
IT: 'Italy',
JM: 'Jamaica',
JP: 'Japan',
JE: 'Jersey',
JO: 'Jordan',
KZ: 'Kazakhstan',
KE: 'Kenya',
KI: 'Kiribati',
KR: 'Korea',
KW: 'Kuwait',
KG: 'Kyrgyzstan',
LA: 'Lao People\'s Democratic Republic',
LV: 'Latvia',
LB: 'Lebanon',
LS: 'Lesotho',
LR: 'Liberia',
LY: 'Libyan Arab Jamahiriya',
LI: 'Liechtenstein',
LT: 'Lithuania',