1+ /* eslint-disable no-console */
12const prompts = require ( 'prompts' ) ;
23const { program } = require ( 'commander' ) ;
34const fs = require ( 'fs' ) ;
45const envfile = require ( 'envfile' ) ;
5- const path = require ( 'path' ) ;
66const { version } = require ( './package.json' ) ;
77const Master = require ( './src/index' ) ;
88
@@ -12,123 +12,134 @@ const sourcePath = '.env';
1212
1313program . version ( version ) ;
1414
15- function getBase64 ( file ) {
16- return fs . readFileSync ( file , { encoding : 'base64' } ) ;
17- }
1815function makeid ( length ) {
19- let result = '' ;
20- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
21- const charactersLength = characters . length ;
22- for ( let i = 0 ; i < length ; i += 1 ) {
23- result += characters . charAt ( Math . floor ( Math . random ( ) * charactersLength ) ) ;
24- }
25- return result ;
16+ let result = '' ;
17+ const characters =
18+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
19+ const charactersLength = characters . length ;
20+ for ( let i = 0 ; i < length ; i += 1 ) {
21+ result += characters . charAt (
22+ Math . floor ( Math . random ( ) * charactersLength )
23+ ) ;
24+ }
25+ return result ;
2626}
2727async function setup ( ) {
28- console . log ( 'Transfer Encrypt Token is used to encrypt data communications with workers.' ) ;
29- console . log ( 'Master and Workers have to pick the same Transfer Encrypt Token ..' ) ;
30- console . log ( 'Dont share it with dont trusty parties.' ) ;
31- const questions = [
32- {
33- type : 'confirm' ,
34- name : 'choice' ,
35- message : 'Generate new random Transfer Encrypt Token ?' ,
36- initial : true ,
37- } ,
38- ] ;
39- const genTransferEncryptToken = await prompts ( questions ) ;
40- if ( genTransferEncryptToken . choice ) {
41- const transferEncryptToken = { transferEncryptToken : makeid ( 32 ) } ;
42- console . log ( `Share this token with your workers ${ FgYellow } transferEncryptToken${ Reset } : ${ transferEncryptToken . transferEncryptToken } ` ) ;
43- fs . appendFileSync ( sourcePath , envfile . stringify ( transferEncryptToken ) ) ;
44- } else {
45- const transferEncryptTokenFromUser = await prompts ( {
46- type : 'text' ,
47- name : 'transferEncryptToken' ,
48- message : 'ENTER Transfer Encrypt Token [32 length string]' ,
49- validate : ( transferEncryptToken ) => ( transferEncryptToken . length < 32 ? 'Minimum length is 32' : true ) ,
50- } ) ;
51- fs . appendFileSync ( sourcePath , envfile . stringify ( transferEncryptTokenFromUser ) ) ;
52- }
28+ console . log (
29+ 'Transfer Encrypt Token is used to encrypt data communications with workers.'
30+ ) ;
31+ console . log (
32+ 'Master and Workers have to pick the same Transfer Encrypt Token ..'
33+ ) ;
34+ console . log ( 'Dont share it with dont trusty parties.' ) ;
35+ const questions = [
36+ {
37+ type : 'confirm' ,
38+ name : 'choice' ,
39+ message : 'Generate new random Transfer Encrypt Token ?' ,
40+ initial : true ,
41+ } ,
42+ ] ;
43+ const genTransferEncryptToken = await prompts ( questions ) ;
44+ if ( genTransferEncryptToken . choice ) {
45+ const transferEncryptToken = { transferEncryptToken : makeid ( 32 ) } ;
46+ console . log (
47+ `Share this token with your workers ${ FgYellow } transferEncryptToken${ Reset } : ${ transferEncryptToken . transferEncryptToken } `
48+ ) ;
49+ fs . appendFileSync ( sourcePath , envfile . stringify ( transferEncryptToken ) ) ;
50+ } else {
51+ const transferEncryptTokenFromUser = await prompts ( {
52+ type : 'text' ,
53+ name : 'transferEncryptToken' ,
54+ message : 'ENTER Transfer Encrypt Token [32 length string]' ,
55+ validate : ( transferEncryptToken ) =>
56+ transferEncryptToken . length < 32
57+ ? 'Minimum length is 32'
58+ : true ,
59+ } ) ;
60+ fs . appendFileSync (
61+ sourcePath ,
62+ envfile . stringify ( transferEncryptTokenFromUser )
63+ ) ;
64+ }
5365
54- const genKeys = await prompts (
55- {
56- type : 'confirm' ,
57- name : 'gen_keys' ,
58- message : 'Create new random token [Will used to find your workers and them you]?' ,
59- initial : true ,
60- } ,
61- ) ;
62- if ( ! genKeys . gen_keys ) {
63- const hashFromUser = await prompts ( {
64- type : 'text' ,
65- name : 'token' ,
66- message : 'Enter your token [atleast 20 length string]' ,
67- validate : ( token ) => ( token . length < 20 ? 'Minimum length is 20' : true ) ,
66+ const genKeys = await prompts ( {
67+ type : 'confirm' ,
68+ name : 'gen_keys' ,
69+ message :
70+ 'Create new random token [Will used to find your workers and them you]?' ,
71+ initial : true ,
6872 } ) ;
69- fs . appendFileSync ( sourcePath , envfile . stringify ( hashFromUser ) ) ;
70- return ;
71- }
72- const key = { token : makeid ( 20 ) } ;
73- console . log ( `Share this token with your workers ${ FgYellow } token${ Reset } : ${ key . token } ` ) ;
74- fs . appendFileSync ( sourcePath , envfile . stringify ( key ) ) ;
73+ if ( ! genKeys . gen_keys ) {
74+ const hashFromUser = await prompts ( {
75+ type : 'text' ,
76+ name : 'token' ,
77+ message : 'Enter your token [atleast 20 length string]' ,
78+ validate : ( token ) =>
79+ token . length < 20 ? 'Minimum length is 20' : true ,
80+ } ) ;
81+ fs . appendFileSync ( sourcePath , envfile . stringify ( hashFromUser ) ) ;
82+ return ;
83+ }
84+ const key = { token : makeid ( 20 ) } ;
85+ console . log (
86+ `Share this token with your workers ${ FgYellow } token${ Reset } : ${ key . token } `
87+ ) ;
88+ fs . appendFileSync ( sourcePath , envfile . stringify ( key ) ) ;
7589
76- console . log ( 'Settings stored in .env' ) ;
90+ console . log ( 'Settings stored in .env' ) ;
7791}
7892
7993function resultCollect ( result ) {
80- console . dir ( result ) ;
94+ console . dir ( result ) ;
8195}
8296async function run ( ) {
83- const mm = new Master ( {
84- onResults : resultCollect ,
85- execAssets : {
86- dependencies : [ ] , // pass Worker dependencies like : ['big.js', 'moment']
87- files : [
88- // { masterPath: '/src/Logger.js', name: 'Logger.js', workerPath: '/workplace/Logger.js' },
89- // { masterPath: '/src/Helper.js', name: 'Helper.js', workerPath: '/workplace/Helper.js' },
90- // { masterPath: '/src/task.js', name: 'task.js', workerPath: '/workplace/task.js' },
91- ] ,
92- } ,
93- } ) ;
94- const dummy = {
95-
96- } ;
97-
98- let cnt = 0 ;
99- for ( let i = 0 ; i < 5 ; i ++ ) {
100- const payload = {
101- id : cnt ,
102- data : JSON . stringify ( dummy ) ,
97+ const mm = new Master ( {
98+ onResults : resultCollect ,
99+ execAssets : {
100+ dependencies : [ ] , // pass Worker dependencies like : ['big.js', 'moment']
101+ files : [
102+ // { masterPath: '/src/Logger.js', name: 'Logger.js', workerPath: '/workplace/Logger.js' },
103+ // { masterPath: '/src/Helper.js', name: 'Helper.js', workerPath: '/workplace/Helper.js' },
104+ // { masterPath: '/src/task.js', name: 'task.js', workerPath: '/workplace/task.js' },
105+ ] ,
106+ } ,
107+ } ) ;
108+ const dummy = { } ;
103109
104- } ;
105- await mm . pushNewJob ( payload ) . catch ( ( e ) => console . log ( e ) ) ;
106- // mm.pushNewJob();
107- cnt += 1 ;
108- }
109- // setInterval(async () => {
110- // // console.dir(mm.getQueueLength());
111- // // const file = getBase64(path.join(process.cwd(), '/task.js'));
110+ let cnt = 0 ;
111+ for ( let i = 0 ; i < 5 ; i += 1 ) {
112+ const payload = {
113+ id : cnt ,
114+ data : JSON . stringify ( dummy ) ,
115+ } ;
116+ // eslint-disable-next-line no-await-in-loop
117+ await mm . pushNewJob ( payload ) . catch ( ( e ) => console . log ( e ) ) ;
118+ // mm.pushNewJob();
119+ cnt += 1 ;
120+ }
121+ // setInterval(async () => {
122+ // // console.dir(mm.getQueueLength());
123+ // // const file = getBase64(path.join(process.cwd(), '/task.js'));
112124
113- // }, 100);
125+ // }, 100);
114126}
115127
116- ( async function ( ) {
117- program
118- . option ( '-s, --setup' , 'Setup/Register master settings' ) ;
128+ ( async function main ( ) {
129+ program . option ( '-s, --setup' , 'Setup/Register master settings' ) ;
119130
120- program . parse ( process . argv ) ;
121- const options = program . opts ( ) ;
122- switch ( true ) {
123- case ( options . setup ) :
124- await setup ( ) ;
125- break ;
126- default :
127- await run ( ) ;
128- break ;
129- }
130- } ( ) ) ;
131+ program . parse ( process . argv ) ;
132+ const options = program . opts ( ) ;
133+ switch ( true ) {
134+ case options . setup :
135+ await setup ( ) ;
136+ break ;
137+ default :
138+ await run ( ) ;
139+ break ;
140+ }
141+ } ) ( ) ;
131142
132143process . on ( 'uncaughtException' , ( error ) => {
133- console . log ( error . message ) ;
144+ console . log ( error . message ) ;
134145} ) ;
0 commit comments