@@ -23,70 +23,71 @@ const readModuleFile = (path, callback) => {
2323 }
2424}
2525
26- // Start script
27- console . log ( '\n\x1b[36m%s\x1b[0m' , 'Creating news verification hashes...\n' ) ;
26+ ( ( ) => {
27+ // Start script
28+ console . log ( '\n\x1b[36m%s\x1b[0m' , 'Creating news verification hashes...\n' ) ;
2829
29- const newsEnvironment = process . env . NEWS_ENV ;
30- const allowedFiles = [
31- { name : 'newsfeed_development.json' , env : 'development' } ,
32- { name : 'newsfeed_mainnet.json' , env : 'mainnet' } ,
33- { name : 'newsfeed_staging.json' , env : 'staging' } ,
34- { name : 'newsfeed_testnet.json' , env : 'testnet' } ,
35- { name : 'news.dummy.json' , env : 'dummy_development' } , // Faked test file for development purposes
36- ] ;
30+ const newsEnvironment = process . env . NEWS_ENV ;
31+ const allowedFiles = [
32+ { name : 'newsfeed_development.json' , env : 'development' } ,
33+ { name : 'newsfeed_mainnet.json' , env : 'mainnet' } ,
34+ { name : 'newsfeed_staging.json' , env : 'staging' } ,
35+ { name : 'newsfeed_testnet.json' , env : 'testnet' } ,
36+ { name : 'news.dummy.json' , env : 'dummy_development' } , // Faked test file for development purposes
37+ ] ;
3738
38- let filesToHash = [ ] ;
39- if ( newsEnvironment ) {
40- const fileName = `newsfeed_${ newsEnvironment . toLowerCase ( ) } .json` ;
41- const fileAllowed = lodash . find ( allowedFiles , ( allowedFile => allowedFile . name === fileName ) ) ;
42- if ( fileAllowed ) {
43- filesToHash . push ( fileAllowed ) ;
39+ let filesToHash = [ ] ;
40+ if ( newsEnvironment ) {
41+ const fileName = `newsfeed_${ newsEnvironment . toLowerCase ( ) } .json` ;
42+ const fileAllowed = lodash . find ( allowedFiles , ( allowedFile => allowedFile . name === fileName ) ) ;
43+ if ( fileAllowed ) {
44+ filesToHash . push ( fileAllowed ) ;
45+ } else {
46+ console . log ( `FILE: \x1b[31m ${ fileName } not allowed. Use one of available environments \x1b[0m development | staging | testnet | mainnet \n` , '\x1b[0m' ) ;
47+ return ;
48+ }
49+ }
50+
51+ if ( filesToHash . length === 0 ) {
52+ filesToHash = allowedFiles ;
53+ console . log ( `\x1b[36m I am hashing ALL available files: [ \x1b[32m ${ lodash . map ( filesToHash , file => file . name ) } \x1b[36m ]` , '\x1b[0m' )
4454 } else {
45- console . log ( `FILE: \x1b[31m ${ fileName } not allowed. Use one of available environments \x1b[0m development | staging | testnet | mainnet \n` , '\x1b[0m' ) ;
46- return ;
55+ console . log ( `\x1b[36m I am hashing files: [ \x1b[32m ${ lodash . map ( filesToHash , file => file . name ) } \x1b[36m ]` , '\x1b[0m' )
4756 }
48- }
4957
50- if ( filesToHash . length === 0 ) {
51- filesToHash = allowedFiles ;
52- console . log ( `\x1b[36m I am hashing ALL available files: [ \x1b[32m ${ lodash . map ( filesToHash , file => file . name ) } \x1b[36m ]` , '\x1b[0m' )
53- } else {
54- console . log ( `\x1b[36m I am hashing files: [ \x1b[32m ${ lodash . map ( filesToHash , file => file . name ) } \x1b[36m ]` , '\x1b[0m' )
55- }
58+ console . log ( '\n \x1b[33m' , 'NOTE: create file with NAME and put HASH as content! \n' , '\x1b[0m' ) ;
59+ lodash . map ( filesToHash , file => {
60+ readModuleFile ( `../../source/renderer/app/config/newsfeed-files/${ file . name } ` , function ( error , fileContent ) {
61+ // Log Environment
62+ console . log ( '\n \x1b[32m' , `${ lodash . capitalize ( file . env ) } ` , '\x1b[0m' ) ;
63+ if ( error ) { // e.g.File not found
64+ console . log ( '\x1b[31m' , error . message , '\x1b[36m' ) ;
65+ } else {
66+ // Check if file is valid JSON file
67+ let parsedFile ;
68+ try {
69+ parsedFile = JSON . parse ( fileContent ) ;
70+ } catch ( err ) {
71+ console . log ( `\x1b[31m File: ${ file . name } is not VALID json file. Please check file and try again!` , '\x1b[0m' )
72+ return ;
73+ }
5674
57- console . log ( '\n \x1b[33m' , 'NOTE: create file with NAME and put HASH as content! \n' , '\x1b[0m' ) ;
58- lodash . map ( filesToHash , file => {
59- readModuleFile ( `../../source/renderer/app/config/newsfeed-files/${ file . name } ` , function ( error , fileContent ) {
60- // Log Environment
61- console . log ( '\n \x1b[32m' , `${ lodash . capitalize ( file . env ) } ` , '\x1b[0m' ) ;
62- if ( error ) { // e.g.File not found
63- console . log ( '\x1b[31m' , error . message , '\x1b[36m' ) ;
64- } else {
65- // Check if file is valid JSON file
66- let parsedFile ;
67- try {
68- parsedFile = JSON . parse ( fileContent ) ;
69- } catch ( err ) {
70- console . log ( `\x1b[31m File: ${ file . name } is not VALID json file. Please check file and try again!` , '\x1b[0m' )
71- return ;
72- }
73-
74- // Check all timestamps in file and throw error if there are duplicates
75- const timestamps = lodash . map ( parsedFile . items , ( item => ( item . date ) ) )
76- const hasDuplicatedTimestamps = lodash . uniq ( timestamps ) . length !== timestamps . length ;
77- if ( hasDuplicatedTimestamps ) {
78- console . log ( `\x1b[31m File: ${ file . name } has duplicated TIMESTAMPS. Please check file and try again!` , '\x1b[0m' ) ;
79- return ;
80- }
75+ // Check all timestamps in file and throw error if there are duplicates
76+ const timestamps = lodash . map ( parsedFile . items , ( item => ( item . date ) ) )
77+ const hasDuplicatedTimestamps = lodash . uniq ( timestamps ) . length !== timestamps . length ;
78+ if ( hasDuplicatedTimestamps ) {
79+ console . log ( `\x1b[31m File: ${ file . name } has duplicated TIMESTAMPS. Please check file and try again!` , '\x1b[0m' ) ;
80+ return ;
81+ }
8182
82- // Create verification hash
83- const hash = crypto . createHash ( 'sha256' ) ;
84- const hashBuffer = hash . digest ( hash . update ( fileContent , 'utf8' ) ) ;
85- const hashArray = Array . from ( new Uint8Array ( hashBuffer ) )
86- const verificationHash = hashArray . map ( b => b . toString ( 16 ) . padStart ( 2 , '0' ) ) . join ( '' ) ;
83+ // Create verification hash
84+ const hash = crypto . createHash ( 'sha256' ) ;
85+ const hashBuffer = hash . digest ( hash . update ( fileContent , 'utf8' ) ) ;
86+ const hashArray = Array . from ( new Uint8Array ( hashBuffer ) )
87+ const verificationHash = hashArray . map ( b => b . toString ( 16 ) . padStart ( 2 , '0' ) ) . join ( '' ) ;
8788
88- console . log ( `New verification FILE NAME: \x1b[36m ${ parsedFile . updatedAt } .txt \x1b[0m | HASH: \x1b[36m ${ verificationHash } ` , '\x1b[0m' ) ;
89- }
89+ console . log ( `New verification FILE NAME: \x1b[36m ${ parsedFile . updatedAt } .txt \x1b[0m | HASH: \x1b[36m ${ verificationHash } ` , '\x1b[0m' ) ;
90+ }
91+ } ) ;
9092 } ) ;
91- } ) ;
92-
93+ } ) ( )
0 commit comments