Skip to content

Commit

Permalink
Implement #1
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsheep committed Oct 28, 2018
1 parent 2a76b7d commit b0c1cfb
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 28 deletions.
29 changes: 18 additions & 11 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const path = require('path');
const fs = require('fs');
const util = require('util');

const walk = async (dir, filelist = []) => {
const eraseline = '\u001b[1G\u001b[2K';

const walk = async (dir, verbose = false, filelist = [], failed = 0) => {
const files = await util.promisify(fs.readdir)(dir);

for (file of files) {
Expand All @@ -12,26 +14,31 @@ const walk = async (dir, filelist = []) => {
const stat = await util.promisify(fs.stat)(filepath);

if (stat.isDirectory()) {
filelist = await walk(filepath, filelist);
const result = await walk(filepath, verbose, filelist, failed);
failed = result.failed;
filelist = result.filelist;
} else {
filelist.push(filepath);

if (filepath.length > 50) {
console.log(`Added in file queue: ${filepath.slice(0, 25)}[...]${filepath.slice(-25)}`);
if (verbose) {
console.log(`Added file ${filepath}`);
} else {
console.log(`Added in file queue: ${filepath}`);
process.stdout.write(`${eraseline}Crawling directories... ${filename}`);
}
}
} catch (e) {
if (filepath.length > 50) {
console.log(`\x1b[31mNot added in file queue: ${filepath.slice(0, 25)}[...]${filepath.slice(-25)}\x1b[0m`);
} else {
console.log(`\x1b[31mNot added in file queue: ${filepath}\x1b[0m`);
if (verbose) {
console.log(`\x1b[31mCan't add file ${filepath} (access denied)\x1b[0m`);
}

failed++;
}
}

return filelist;
return {
filelist,
failed,
};
}

/**
Expand All @@ -44,4 +51,4 @@ function* arrayLoop(arr, action) {
}
}

module.exports = { walk, arrayLoop };
module.exports = { walk, arrayLoop, eraseline };
85 changes: 68 additions & 17 deletions warshield.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const program = require('commander');
const fs = require('fs');
const util = require('util');
const wall = require('./wall');
const { walk, arrayLoop } = require('./helpers');
const { walk, arrayLoop, eraseline } = require('./helpers');

function gate(action, file, verbose = false) {
function gate(mode, file, verbose = false) {
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);

Expand All @@ -15,15 +15,15 @@ function gate(action, file, verbose = false) {

rl._writeToOutput = stringToWrite => {
if (i) {
rl.output.write(`\x1B[2K\x1B[200D${query}`);
rl.output.write(`${eraseline}${query}`);
} else {
rl.output.write(stringToWrite);
i = true;
}
}

rl.question(query, key => {
if (action === 'encrypt') {
if (mode === 'encrypt') {
i = false;
query = 'Confirm password: ';

Expand All @@ -36,35 +36,84 @@ function gate(action, file, verbose = false) {

rl.close();

cipher(action, file, key, verbose);
process.stdout.write(`${eraseline}\b${eraseline}`);
cipher(mode, file, key, verbose);
});
} else {
rl.close();

cipher(action, file, key, verbose);
process.stdout.write(eraseline);
cipher(mode, file, key, verbose);
}
});
}

async function cipher(action, file, key, verbose = false) {
async function cipher(mode, file, key, verbose = false) {
try {
process.stdout.write('\n');
const start = process.hrtime();

const stat = await util.promisify(fs.stat)(file);

const files = stat.isDirectory() ? await walk(file) : [file];
let { filelist: files, failed = 0 } = stat.isDirectory() ? await walk(file, verbose) : { filelist: [file] };

const fileslength = files.length + failed;

if (files[0] !== file) {
if (verbose) {
console.log('Crawling directories... Done!');
} else {
process.stdout.write(`${eraseline}Crawling directories... Done!\n`);
}
}

const isEncrypt = mode === 'encrypt';

const action = mode.slice(0, 1).toUpperCase() + mode.slice(1) + 'ing';

const loop = arrayLoop(files, file => {
return wall.cipherizeFile(file, key, action === 'encrypt')
.then(() => console.log(`\x1b[32mDone ${action}ing\x1b[0m - ${file}`))
.catch(() => console.log(`\x1b[31mFAILED ${action}ing\x1b[0m - ${file}`));
return wall.cipherizeFile(file, key, isEncrypt)
.then(() => {
if (verbose) {
console.log(`\x1b[32mDone ${mode}ing\x1b[0m ${file}`)
} else {
process.stdout.write(`${eraseline}${action}... ${file}`);
}
})
.catch(() => {
failed++;

if (verbose) {
console.log(`\x1b[31mFailed ${mode}ing\x1b[0m ${file}`);
} else {
process.stdout.write(`${eraseline}${action}... ${file}`);
}
});
});

let done = 0;

const next = () => {
repeat();

if (++done >= files.length) {
if (!verbose) {
process.stdout.write(eraseline);
}

const diff = process.hrtime(start);

console.log(`Finished ${action.toLowerCase()} files!`);
console.log(`Elapsed time: ${((diff[0] * 1e9 + diff[1]) / 1e9).toFixed(2)}s!`);
console.log(`Total ${mode}ed files: ${fileslength - failed}`);
console.log(`Failed: ${failed} (access denied or non-encrypted files)`);
}
}

const repeat = () => {
const wait = loop.next();

if (wait.value) {
wait.value.then(repeat);
wait.value.then(next).catch(next);
}
}

Expand All @@ -82,14 +131,16 @@ program
.option('-v, --verbose', 'enable verbosity');

program
.command('encrypt <file> [options]')
.command('encrypt <file>')
.option('-v, --verbose', 'enable verbosity')
.description('encrypt a file or all files in a directory')
.action((file, key, options) => gate('encrypt', file, key, options && options.verbose));
.action((file, cmd) => gate('encrypt', file, cmd.parent.verbose === true));

program
.command('decrypt <file> [options]')
.command('decrypt <file>')
.option('-v, --verbose', 'enable verbosity')
.description('decrypt a file or all files in a directory')
.action((file, key, options) => gate('decrypt', file, key, options && options.verbose));
.action((file, cmd) => gate('decrypt', file, cmd.parent.verbose === true));

program.action(() => program.help());

Expand Down

0 comments on commit b0c1cfb

Please sign in to comment.