Skip to content

Added: iron-man-with-gauntlet flag #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ module.exports = {
snapFingers: 'snap-fingers',
withGlove: 'with-infinity-gauntlet-glove',
noMercy: 'no-mercy',
ironManWithGauntlet: 'iron-man-with-gauntlet'
},
};
34 changes: 33 additions & 1 deletion lib/snap-fingers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const constants = require('../constants');
const cwd = process.cwd();

module.exports = () => {

if(!fs.existsSync(`${os.homedir()}/.to-dust`)) {
fs.mkdirSync(`${os.homedir()}/.to-dust`);
}

const gonnaSnapFingers = readArgs(constants.arguments.snapFingers, false);
const gonnaUseGlove = readArgs(constants.arguments.withGlove);
const noMercy = readArgs(constants.arguments.noMercy);
Expand All @@ -25,6 +30,24 @@ module.exports = () => {
logger.log(`😈 Snapping fingers inside '${cwd}' with full power...`);
}

const ironManWithGauntlet = readArgs(constants.arguments.ironManWithGauntlet);

if(ironManWithGauntlet) {
logger.log(`I... am... Iron Man!`);
const correctPaths = JSON.parse(fs.readFileSync(`${os.homedir()}/.to-dust/correct-paths.json`, 'utf8'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, this will throw en error in case the .to-dust is not already existing.. You might want to first create the directory

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry my bad, I put that checking condition below. I'll move it up.


correctPaths.forEach((filePath) => {
let splitFilePath = filePath.file.split('/');
let filename = splitFilename.join('-');
fs.rename(aFile.file, `${os.homedir()}/.to-dust/${filename}`, (err) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this? aFile is undefiend here. :/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the sequence of argument would be reverse. We would want to move files from .to-dust to its original location

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, actually I was having a test branch where I was doing all this and after the fork, I just copied pasted stuff around which caused these issues.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested it and it works now, do let me know if you find any more issues :)

if(err) throw err;
})
});

logger.log(`Yours file have been restored`);
process.exit(0);
}

/**
* Takes the name of the directory and determines whether it is safe to traverse on the directory or not
* For now,
Expand Down Expand Up @@ -62,10 +85,19 @@ module.exports = () => {
? filesWithChancesSorted.slice(0, filesWithChancesSorted.length / 2)
: filesWithChancesSorted.slice(filesWithChancesSorted.length / 2);

fs.writeFile(`${os.homedir()}/.to-dust/correct-paths.json`, JSON.stringify(filesToBeDeleted, null, 4), (err) => {
if(err) throw err;
});

let deletedFilesCount = 0;
filesToBeDeleted.forEach((aFile) => {
try {
fs.unlinkSync(aFile.file);
// fs.unlinkSync(aFile.file);
let splitFilename = aFile.file.split('/');
let filename = splitFilename.join('-');
fs.rename(aFile.file, `${os.homedir()}/.to-dust/${filename}`, (err) => {
if(err) throw err;
})
// In case `unlinkSync` throws an Exception, deletedFilesCount won't be incremented
// This way, we would have the precise deletion count.
deletedFilesCount++;
Expand Down