Skip to content

Commit

Permalink
Add nicer debug message. Remove experiment form duplicate script.
Browse files Browse the repository at this point in the history
  • Loading branch information
danmindru committed Sep 15, 2019
1 parent 4ff3c07 commit b22704d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cncnet-map-downloader.js",
"version": "2.0.0",
"version": "2.0.1",
"description": "Downloads all maps from cncnet",
"main": "index.js",
"scripts": {
Expand Down
73 changes: 41 additions & 32 deletions scripts/remove-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ const { debug } = require('./constants');
*
* @param { string } targetDir
*/
const removeFile = (targetDir) => (filePath) => new Promise((resolve, reject) => fs.unlink(path.resolve(targetDir, filePath), (error) => {
if (error) {
console.error(`Failed to remove ${filePath}`, error);
reject(null);
}
const removeFile = (targetDir) => (filePath) =>
new Promise((resolve, reject) =>
fs.unlink(path.resolve(targetDir, filePath), (error) => {
if (error) {
console.error(`Failed to remove ${filePath}`, error);
reject(null);
}

resolve(path.resolve(targetDir, filePath));
}));
resolve(path.resolve(targetDir, filePath));
})
);

/**
* Get the size of a file.
Expand All @@ -27,14 +30,17 @@ const removeFile = (targetDir) => (filePath) => new Promise((resolve, reject) =>
*
* @return { number }
*/
const getFileSize = (targetDir) => (filePath) => new Promise((resolve, reject) => fs.stat(path.resolve(targetDir, filePath), (error, stats) => {
if (error) {
console.error(`Failed to get file size for ${filePath}`, error);
reject(null);
}
const getFileSize = (targetDir) => (filePath) =>
new Promise((resolve, reject) =>
fs.stat(path.resolve(targetDir, filePath), (error, stats) => {
if (error) {
console.error(`Failed to get file size for ${filePath}`, error);
reject(null);
}

resolve({ size: stats.size, filePath });
}));
resolve({ size: stats.size, filePath });
})
);

/**
* Given a path, removes duplicate files by first checking size, then hash.
Expand All @@ -44,37 +50,42 @@ const getFileSize = (targetDir) => (filePath) => new Promise((resolve, reject) =
const removeDuplicates = async (targetDir) => {
const targetDirFilelist = fs.readdirSync(targetDir);

console.log(`\nComparing file sizes...`);

// Get file sizes, filtering out failed fs.stats
const fileSizes = await Promise.all(targetDirFilelist.map(getFileSize(targetDir)));
const filesBySize = fileSizes
.filter((size) => size)
.reduce((acc, { size, filePath }, ) => {
return { ...acc, [size]: acc[size] ? [...acc[size], filePath] : [filePath] }
.reduce((acc, { size, filePath }) => {
return { ...acc, [size]: acc[size] ? [...acc[size], filePath] : [filePath] };
}, {});

console.log(`Checking for duplicates...`);

// Filter by items with duplicates
const fileIndexesWithSameSize = Object.keys(filesBySize)
.filter((key) => {
const values = filesBySize[key];
return values.length > 1
});
const fileIndexesWithSameSize = Object.keys(filesBySize).filter((key) => {
const values = filesBySize[key];
return values.length > 1;
});

// Filter by items with same checksum
const fileIndexesWithSameChecksum = fileIndexesWithSameSize
.filter(async (key) => {
const values = filesBySize[key];
const fileIndexesWithSameChecksum = fileIndexesWithSameSize.filter(async (key) => {
const values = filesBySize[key];

const hashArray = await Promise.all(values.map((filePath) => hasha.fromFile(path.resolve(targetDir, filePath), { algorithm: 'md5' })))
const hashArray = await Promise.all(
values.map((filePath) => hasha.fromFile(path.resolve(targetDir, filePath), { algorithm: 'md5' }))
);

return hashArray.every(v => v === hashArray[0])
});
return hashArray.every((v) => v === hashArray[0]);
});

console.log(`Removing duplicates...`);
// Remove duplicates, taking the first item of the duplicate array
const fileRemovalPromises = flatten(
fileIndexesWithSameChecksum.map((key) => {
const filesToRemove = filesBySize[key].slice(1);

if(!debug) {
if (debug) {
console.debug(chalk.green(`\nFound map duplicates for file: ${filesBySize[key][0]}. Will keep only 1 map.`));
console.debug(chalk.yellow(`Removing duplicates: ${filesToRemove.map((f) => `\n - ${chalk.bold(f)}`)}`));
}
Expand All @@ -85,10 +96,8 @@ const removeDuplicates = async (targetDir) => {

const removalResult = await Promise.all(fileRemovalPromises);
return removalResult.filter((resolved) => resolved).length;
}
};

module.exports = {
removeDuplicates
}

removeDuplicates('./experiment');
};

0 comments on commit b22704d

Please sign in to comment.