Skip to content

Commit f79063b

Browse files
committed
Clean up & refine
1 parent 2d0f3f5 commit f79063b

File tree

12 files changed

+235
-331
lines changed

12 files changed

+235
-331
lines changed

assets/icon.ico

345 KB
Binary file not shown.

build.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
const { compile } = require('nexe'),
1+
const nexe = require('nexe'),
22
fs = require('fs'),
33
version = JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
44

5-
compile({
5+
nexe.compile({
66
input: './index.js',
7-
target: 'win32-x86-10.13.0',
87
name: 'safe-backup',
98
build: true,
10-
ico: './icon.ico',
9+
ico: './assets/icon.ico',
1110
rc: {
1211
CompanyName: 'scrwdrv.tech',
1312
ProductName: 'Safe Backup',
@@ -16,8 +15,6 @@ compile({
1615
FileVersion: version,
1716
OriginalFilename: 'safe-backup.exe',
1817
InternalName: 'safe-backup',
19-
LegalCopyright: 'Copyright scrwdrv contributors. MIT license.'
18+
LegalCopyright: 'Copyright scrwdrv and contributors. MIT license.'
2019
}
21-
}).then(() => {
22-
console.log('success');
2320
});

icon.ico

-264 KB
Binary file not shown.

index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
#!/usr/bin/env node
12
"use strict";
2-
if (process.env.isWorker)
3-
Promise.resolve().then(() => require('./worker'));
4-
else
5-
Promise.resolve().then(() => require('./master'));
6-
process.on('SIGINT', () => { });
3+
if (process.env.isWorker) require('./lib/worker');
4+
else require('./lib/master');
5+
process.on('SIGINT', () => { });

master.js renamed to lib/master.js

Lines changed: 17 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,13 @@ const cluster_ipc_logger_1 = require("cluster-ipc-logger");
44
const physical_cores_1 = require("physical-cores");
55
const worker_communication_1 = require("worker-communication");
66
const cli_params_1 = require("cli-params");
7-
const readline = require("readline");
7+
const prompt_1 = require("./prompt");
88
const cluster = require("cluster");
99
const crypto = require("crypto");
1010
const dir = require("recurdir");
1111
const PATH = require("path");
1212
const fs = require("fs");
13-
class Prompt {
14-
constructor() {
15-
this.asking = false;
16-
}
17-
getRl() {
18-
this.rl = readline.createInterface({
19-
input: process.stdin,
20-
output: process.stdout,
21-
terminal: false
22-
}).on('SIGINT', () => {
23-
if (this.asking)
24-
process.exit();
25-
});
26-
}
27-
ask(question) {
28-
return new Promise(resolve => {
29-
if (!this.rl)
30-
this.getRl();
31-
this.asking = true;
32-
this.rl.question(question, resolve);
33-
});
34-
}
35-
end() {
36-
this.rl.removeAllListeners();
37-
this.rl.close();
38-
this.rl = null;
39-
this.asking = false;
40-
}
41-
}
42-
const cpc = new worker_communication_1.default(), prompt = new Prompt(), logServer = new cluster_ipc_logger_1.loggerServer({
13+
const cpc = new worker_communication_1.default(), prompt = new prompt_1.default(), logServer = new cluster_ipc_logger_1.loggerServer({
4314
debug: false,
4415
directory: PATH.join(__dirname, 'logs'),
4516
saveInterval: 60000
@@ -154,7 +125,7 @@ process.on('SIGINT', () => exit());
154125
if (process.argv.length === 2)
155126
log.info('No parameters were found, restoring configurations...');
156127
else
157-
return log.error(err), exit();
128+
return log.error(err), console.log(helpText), exit();
158129
else
159130
switch (id) {
160131
case 'regular':
@@ -163,32 +134,10 @@ process.on('SIGINT', () => exit());
163134
config.watch = args.watch;
164135
if (args['save-password'] === false)
165136
nosave = true;
166-
if (args.password)
167-
config.passwordHash = hashPassword(args.password);
168-
else
169-
await (function setPassword() {
170-
return new Promise(resolve => {
171-
prompt.ask('Set your password for encryption: ').then(password => prompt.ask(`Please confirm your password is \x1b[33m\x1b[1m${password}\x1b[0m [Y/N]? `).then(confirm => {
172-
if (confirm.toLowerCase() === 'y') {
173-
config.passwordHash = hashPassword(password);
174-
prompt.end();
175-
resolve();
176-
}
177-
else
178-
setPassword().then(resolve);
179-
}));
180-
});
181-
})();
137+
config.passwordHash = hashPassword(args.password ? args.password : await prompt.questions.getPassword());
182138
break;
183139
case 'decrypt':
184-
if (args.password)
185-
config.passwordHash = hashPassword(args.password);
186-
else
187-
await new Promise(resolve => prompt.ask('Enter your password: ').then(password => {
188-
config.passwordHash = hashPassword(password);
189-
prompt.end();
190-
resolve();
191-
}));
140+
config.passwordHash = hashPassword(args.password ? args.password : await prompt.questions.getPassword());
192141
if (!PATH.isAbsolute(args.decrypt))
193142
return log.error(`Path must be absolute [${formatPath(args.decrypt)}]`), exit();
194143
const t = Date.now();
@@ -233,7 +182,7 @@ process.on('SIGINT', () => exit());
233182
}
234183
catch (err) {
235184
log.debug(err);
236-
log.error(`Failed to initialize, see the log file for details`);
185+
log.error(`Failed to initialize, see log file for details`);
237186
return exit();
238187
}
239188
for (let i = physical_cores_1.default < 1 ? 1 : physical_cores_1.default; i--;)
@@ -296,7 +245,7 @@ process.on('SIGINT', () => exit());
296245
log.info(`Forking worker[${id}]`);
297246
const worker = cpc.tunnel(cluster.fork({ workerId: id, isWorker: true }));
298247
worker.on('exit', () => {
299-
worker.removeAllListeners();
248+
worker.removeAllListeners('message');
300249
const index = workers.indexOf(worker);
301250
if (index > -1)
302251
workers.splice(index, 1);
@@ -317,7 +266,7 @@ process.on('SIGINT', () => exit());
317266
}).on('error', (err) => {
318267
log.debug(err);
319268
log.error(`Error occurred while monitoring [${formatPath(path)}], retry in 10 secs...`);
320-
watcher.removeAllListeners();
269+
watcher.removeAllListeners('close');
321270
watcher.close();
322271
clearTimeout(timeout);
323272
setTimeout(watchMod, 10000, path, isFile, retry + 1);
@@ -364,7 +313,7 @@ async function exit(retry = 0) {
364313
}
365314
for (let i = workers.length; i--;)
366315
workers.pop()
367-
.removeAllListeners()
316+
.removeAllListeners('exit')
368317
.kill();
369318
paused = null;
370319
if (config.watch)
@@ -390,121 +339,33 @@ function handleConfig(c) {
390339
else
391340
fs.readFile(PATH.join(__dirname, 'config.json'), 'utf8', async (err, data) => {
392341
if (err)
393-
return resolve(await askQuestions().catch(reject)); //return reject(err);
342+
return resolve(await askQuestions().catch(reject));
394343
config = JSON.parse(data);
395344
if (c === undefined)
396345
return resolve();
397346
if (!config.passwordHash)
398-
await (function setPassword() {
399-
return new Promise(resolve => {
400-
prompt.ask('Enter your password for encryption: ').then(password => prompt.ask(`Please confirm your password is \x1b[33m\x1b[1m${password}\x1b[0m [Y/N]? `).then(confirm => {
401-
if (confirm.toLowerCase() === 'y') {
402-
config.passwordHash = hashPassword(password);
403-
prompt.end();
404-
resolve();
405-
}
406-
else
407-
setPassword().then(resolve);
408-
}));
409-
});
410-
})();
347+
config.passwordHash = hashPassword(await prompt.questions.getPassword());
411348
resolve();
412349
});
413350
});
414351
}
415352
function askQuestions() {
416353
return new Promise(async (resolve, reject) => {
417354
log.info(`Start building configurations...`);
418-
let ans = {
419-
input: [],
420-
output: [],
421-
watch: null,
422-
passwordHash: null
423-
};
424355
try {
425356
await halt(500);
426-
await getInput();
427-
await getOutput();
428-
await getPassword();
429-
await getWatch();
430-
await getSavePassword();
431-
config = ans;
357+
config.input = await prompt.questions.getInput();
358+
config.output = await prompt.questions.getOutput();
359+
config.watch = await prompt.questions.getWatch();
360+
config.passwordHash = hashPassword(await prompt.questions.getPassword());
361+
if (!await prompt.questions.getSavePassword())
362+
nosave = true;
432363
await handleConfig(config);
433364
resolve();
434365
}
435366
catch (err) {
436367
reject(err);
437368
}
438-
function getInput() {
439-
return new Promise(resolve => {
440-
prompt.ask(`Enter absolute path of folder/file to backup: `).then((path) => {
441-
ans.input.push(path);
442-
(function yn() {
443-
prompt.ask(`More file/folder to backup [Y/N]? `).then(confirm => {
444-
if (confirm.toLowerCase() === 'y')
445-
return getInput().then(resolve).catch(reject);
446-
else if (confirm.toLowerCase() === 'n')
447-
return resolve();
448-
yn();
449-
});
450-
})();
451-
});
452-
});
453-
}
454-
function getOutput() {
455-
return new Promise(resolve => {
456-
prompt.ask(`Enter absolute path of folder to store encrypted file: `).then((path) => {
457-
ans.output.push(path);
458-
(function yn() {
459-
prompt.ask(`More output destination [Y/N]? `).then(confirm => {
460-
if (confirm.toLowerCase() === 'y')
461-
return getOutput().then(resolve).catch(reject);
462-
else if (confirm.toLowerCase() === 'n')
463-
return resolve();
464-
yn();
465-
});
466-
})();
467-
});
468-
});
469-
}
470-
function getWatch() {
471-
return new Promise(resolve => {
472-
(function yn() {
473-
prompt.ask(`Enable watch mode [Y/N]? `).then(confirm => {
474-
if (confirm.toLowerCase() === 'y')
475-
return ans.watch = 60, resolve();
476-
else if (confirm.toLowerCase() === 'n')
477-
return resolve();
478-
yn();
479-
});
480-
})();
481-
});
482-
}
483-
function getPassword() {
484-
return new Promise(resolve => {
485-
prompt.ask('Enter your password for encryption: ').then(password => prompt.ask(`Please confirm your password is \x1b[33m\x1b[1m${password}\x1b[0m [Y/N]? `).then(confirm => {
486-
if (confirm.toLowerCase() === 'y') {
487-
ans.passwordHash = hashPassword(password);
488-
resolve();
489-
}
490-
else
491-
getPassword().then(resolve);
492-
}));
493-
});
494-
}
495-
function getSavePassword() {
496-
return new Promise(resolve => {
497-
(function yn() {
498-
prompt.ask(`Save your password [Y/N]? `).then(confirm => {
499-
if (confirm.toLowerCase() === 'y')
500-
return resolve();
501-
else if (confirm.toLowerCase() === 'n')
502-
return nosave = true, resolve();
503-
yn();
504-
});
505-
})();
506-
});
507-
}
508369
});
509370
}
510371
function halt(ms) {

lib/prompt.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const readline = require("readline");
4+
class Prompt {
5+
constructor() {
6+
this.asking = false;
7+
this.questions = {
8+
getInput: (inputs = []) => {
9+
return new Promise(resolve => this.ask(`Enter absolute path of folder/file to backup: `).then(path => {
10+
inputs.push(path);
11+
this.questions.getYn(`More file/folder to backup [Y/N]? `).then(boo => {
12+
if (boo)
13+
this.questions.getInput(inputs).then(resolve);
14+
else
15+
resolve(inputs);
16+
});
17+
}));
18+
},
19+
getOutput: (outputs = []) => {
20+
return new Promise(resolve => this.ask(`Enter absolute path of folder to store encrypted file: `).then(path => {
21+
outputs.push(path);
22+
this.questions.getYn(`More output destination [Y/N]? `).then(boo => {
23+
if (boo)
24+
this.questions.getOutput(outputs).then(resolve);
25+
else
26+
resolve(outputs);
27+
});
28+
}));
29+
},
30+
getWatch: () => {
31+
return new Promise(resolve => this.questions.getYn(`Enable watch mode [Y/N]? `).then(boo => {
32+
if (boo)
33+
resolve(60);
34+
else
35+
resolve(null);
36+
}));
37+
},
38+
getPassword: () => {
39+
return new Promise(resolve => this.ask('Enter your password for encryption: ').then(password => this.questions.getYn(`Please confirm your password is \x1b[33m\x1b[1m${password}\x1b[0m [Y/N]? `).then(boo => {
40+
if (boo)
41+
resolve(password);
42+
else
43+
this.questions.getPassword().then(resolve);
44+
})));
45+
},
46+
getSavePassword: () => {
47+
return new Promise(resolve => this.questions.getYn(`Save your password (DO NOT use it on public computers!) [Y/N]? `).then(boo => {
48+
if (boo)
49+
resolve(true);
50+
else
51+
resolve(false);
52+
}));
53+
},
54+
getYn: (question) => {
55+
return new Promise(resolve => this.ask(question).then(confirm => {
56+
if (confirm.toLowerCase() === 'y')
57+
resolve(true);
58+
else if (confirm.toLowerCase() === 'n')
59+
resolve(false);
60+
else
61+
this.questions.getYn(question).then(resolve);
62+
}));
63+
}
64+
};
65+
}
66+
getRl() {
67+
this.rl = readline.createInterface({
68+
input: process.stdin,
69+
output: process.stdout,
70+
terminal: false
71+
}).on('SIGINT', () => {
72+
if (this.asking)
73+
process.exit();
74+
});
75+
}
76+
ask(question) {
77+
return new Promise(resolve => {
78+
if (!this.rl)
79+
this.getRl();
80+
this.asking = true;
81+
this.rl.question('\n' + question + '\n \x1b[36m\x1b[1m>\x1b[0m ', (val) => {
82+
this.end();
83+
resolve(val);
84+
});
85+
});
86+
}
87+
end() {
88+
this.rl.removeAllListeners('SIGINT');
89+
this.rl.close();
90+
this.rl = null;
91+
this.asking = false;
92+
}
93+
}
94+
exports.default = Prompt;
File renamed without changes.

0 commit comments

Comments
 (0)