-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.js
145 lines (133 loc) · 4.51 KB
/
cli.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
/* eslint-disable no-console */
const prompts = require('prompts');
const { program } = require('commander');
const fs = require('fs');
const envfile = require('envfile');
const { version } = require('./package.json');
const Master = require('./src/index');
const { FgYellow, Reset } = require('./src/Logger');
const sourcePath = '.env';
program.version(version);
function makeid(length) {
let result = '';
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i += 1) {
result += characters.charAt(
Math.floor(Math.random() * charactersLength)
);
}
return result;
}
async function setup() {
console.log(
'Transfer Encrypt Token is used to encrypt data communications with workers.'
);
console.log(
'Master and Workers have to pick the same Transfer Encrypt Token ..'
);
console.log('Dont share it with dont trusty parties.');
const questions = [
{
type: 'confirm',
name: 'choice',
message: 'Generate new random Transfer Encrypt Token ?',
initial: true,
},
];
const genTransferEncryptToken = await prompts(questions);
if (genTransferEncryptToken.choice) {
const transferEncryptToken = { transferEncryptToken: makeid(32) };
console.log(
`Share this token with your workers ${FgYellow}transferEncryptToken${Reset}: ${transferEncryptToken.transferEncryptToken}`
);
fs.appendFileSync(sourcePath, envfile.stringify(transferEncryptToken));
} else {
const transferEncryptTokenFromUser = await prompts({
type: 'text',
name: 'transferEncryptToken',
message: 'ENTER Transfer Encrypt Token [32 length string]',
validate: (transferEncryptToken) =>
transferEncryptToken.length < 32
? 'Minimum length is 32'
: true,
});
fs.appendFileSync(
sourcePath,
envfile.stringify(transferEncryptTokenFromUser)
);
}
const genKeys = await prompts({
type: 'confirm',
name: 'gen_keys',
message:
'Create new random token [Will used to find your workers and them you]?',
initial: true,
});
if (!genKeys.gen_keys) {
const hashFromUser = await prompts({
type: 'text',
name: 'token',
message: 'Enter your token [atleast 20 length string]',
validate: (token) =>
token.length < 20 ? 'Minimum length is 20' : true,
});
fs.appendFileSync(sourcePath, envfile.stringify(hashFromUser));
return;
}
const key = { token: makeid(20) };
console.log(
`Share this token with your workers ${FgYellow}token${Reset}: ${key.token}`
);
fs.appendFileSync(sourcePath, envfile.stringify(key));
console.log('Settings stored in .env');
}
function resultCollect(result) {
console.dir(result);
}
async function run() {
const mm = new Master({
onResults: resultCollect,
execAssets: {
dependencies: [], // pass Worker dependencies like : ['big.js', 'moment']
files: [
// { masterPath: '/src/Logger.js', name: 'Logger.js', workerPath: '/workplace/Logger.js' },
// { masterPath: '/src/Helper.js', name: 'Helper.js', workerPath: '/workplace/Helper.js' },
// { masterPath: '/src/task.js', name: 'task.js', workerPath: '/workplace/task.js' },
],
},
});
const dummy = {};
let cnt = 0;
for (let i = 0; i < 5; i += 1) {
const payload = {
id: cnt,
data: JSON.stringify(dummy),
};
// eslint-disable-next-line no-await-in-loop
await mm.pushNewJob(payload).catch((e) => console.log(e));
// mm.pushNewJob();
cnt += 1;
}
// setInterval(async () => {
// // console.dir(mm.getQueueLength());
// // const file = getBase64(path.join(process.cwd(), '/task.js'));
// }, 100);
}
(async function main() {
program.option('-s, --setup', 'Setup/Register master settings');
program.parse(process.argv);
const options = program.opts();
switch (true) {
case options.setup:
await setup();
break;
default:
await run();
break;
}
})();
process.on('uncaughtException', (error) => {
console.log(error.message);
});