Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ typings/
package-lock.json
_CONFIRMATION_
emoji.json
._RESTORATION_
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'
},
};
44 changes: 43 additions & 1 deletion lib/snap-fingers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fs = require('fs');
const os = require('os');
const path = require('path');

const readArgs = require('./utils/read-args');
const logger = require('./utils/logger');
Expand All @@ -8,9 +10,40 @@ 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);
const ironManWithGauntlet = readArgs(constants.arguments.ironManWithGauntlet);
const pathToRestorationFile = path.resolve(__dirname, '..', '._RESTORATION_');

if(ironManWithGauntlet) {

if(fs.existsSync(pathToRestorationFile)) {
logger.log(`😔 Oops! Our saviour already died and the stones are gone as well`);
process.exit(0);
}

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 = splitFilePath.join('-');
fs.rename(`${os.homedir()}/.to-dust/${filename}`, filePath.file, (err) => {
if(err) throw err;
});
});

fs.closeSync(fs.openSync(pathToRestorationFile, 'w'));

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

if (!gonnaSnapFingers) {
logger.log(`👿 To have me snap my finger, pass '${constants.arguments.snapFingers}' as command`);
Expand Down Expand Up @@ -62,10 +95,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