Skip to content

Commit

Permalink
Merge pull request #37 from danmindru/feature/#34-unsort-maps
Browse files Browse the repository at this point in the history
Feature/#34 unsort maps
  • Loading branch information
danmindru authored Jul 29, 2020
2 parents 71c441e + ea8c333 commit 0e2de52
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ yarn-debug.log*
yarn-error.log*

# downloads
cncnet-yuri-maps*
cncnet-*
experiment
packages
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"download": "MAP_AGE=99999 RUN_UNPACKAGED=true GAME_TYPE=yr DESTINATION_DIR=./cncnet-yuri-maps node ./scripts/main.js",
"dev": "MAX_NUMBER_OF_MAPS=5 REQUEST_DELAY=5000 MAP_AGE=1 RUN_UNPACKAGED=true GAME_TYPE=yr DESTINATION_DIR=./cncnet-yuri-maps node ./scripts/main.js",
"dev": "MAX_NUMBER_OF_MAPS=5 REQUEST_DELAY=5000 MAP_AGE=1 RUN_UNPACKAGED=true GAME_TYPE=yr DESTINATION_DIR=./cncnet-maps node ./scripts/main.js",
"lint": "eslint ./**/*.js ",
"publish:all": "node package.js"
},
Expand Down
47 changes: 47 additions & 0 deletions scripts/delete-empty-directories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs = require('fs');
const path = require('path');

const { getRecursiveFileList, removeDirectory } = require('./util');
const { getConfig } = require('./configuration');

/**
* Given a path, removes all empty directoies in that path
*
* @param {string} targetDir Target directory to remove empty directories in.
*/
const deleteEmptyDirectories = async (targetDir) => {
const targetDirFilelist = fs.readdirSync(targetDir).filter((filePath) => {
try {
return fs.statSync(path.resolve(targetDir, filePath)).isDirectory();
} catch (error) {
if (getConfig().debug) {
console.error('Failed to stat file', error);
}
return false;
}
});

const dirFilelists = await Promise.all(
targetDirFilelist.map(async (dirPath) => {
const absolutePath = path.resolve(targetDir, dirPath);
const filelist = await getRecursiveFileList(absolutePath);

return {
filelist,
dirPath,
};
})
);
const emptyDirs = dirFilelists
.filter(({ filelist }) => {
return filelist.length === 0;
})
.map(({ dirPath }) => dirPath);

const dirRemovalPromises = emptyDirs.map(removeDirectory(targetDir));
return Promise.all(dirRemovalPromises);
};

module.exports = {
deleteEmptyDirectories,
};
11 changes: 10 additions & 1 deletion scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const { getConfig, setConfigProperty } = require('./configuration');
const { getMaps } = require('./get-maps');
const { removeDuplicates } = require('./remove-duplicates');
const { sortMaps } = require('./sort-maps');
const { unsortMaps } = require('./unsort-maps');
const { deleteEmptyDirectories } = require('./delete-empty-directories');

/**
* Main entry point.
Expand Down Expand Up @@ -64,7 +66,9 @@ const main = async () => {
setConfigProperty('shouldSortInDirectories', shouldSortInDirectories);
setConfigProperty('gameType', gameType);
setConfigProperty('destinationDir', destinationDir);
setConfigProperty('maxNumberOfMaps', maxNumberOfMaps);
if (maxNumberOfMaps) {
setConfigProperty('maxNumberOfMaps', maxNumberOfMaps);
}
}

const { destinationDirAbsolutePath, destinationDir, shouldSortInDirectories, debug } = getConfig();
Expand All @@ -87,8 +91,13 @@ const main = async () => {
if (shouldSortInDirectories) {
spinner.text = 'Sorting maps';
await sortMaps(destinationDirAbsolutePath);
} else {
spinner.text = 'Un-sorting maps';
await unsortMaps(destinationDirAbsolutePath);
}

await deleteEmptyDirectories(destinationDirAbsolutePath);

spinner.stop();

if (!debug) {
Expand Down
22 changes: 1 addition & 21 deletions scripts/remove-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,7 @@ const chalk = require('chalk');
const { runPromisesWithProgress, replaceLine, getRecursiveFileList } = require('./util');
const { flatten } = require('lodash');
const { getConfig } = require('./configuration');

/**
* Remove a file.
*
* @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);
}

resolve(path.resolve(targetDir, filePath));
})
).catch((error) => {
if (getConfig().debug) {
console.error(`Failed to remove ${filePath}`, error);
}
});
const { removeFile } = require('./util');

/**
* Get the size of a file.
Expand Down
39 changes: 11 additions & 28 deletions scripts/sort-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path');
const fs = require('fs');

const { getConfig } = require('./configuration');
const { moveFile } = require('./util');

/**
* Given a path, sorts files in directories by their first letter.
Expand All @@ -12,10 +13,16 @@ const { getConfig } = require('./configuration');
const sortMaps = async (targetDir) => {
console.log(`\nSorting maps...`);

const targetDirFilelist = fs
.readdirSync(targetDir)
.filter((filePath) => fs.statSync(path.resolve(targetDir, filePath)).isFile());

const targetDirFilelist = fs.readdirSync(targetDir).filter((filePath) => {
try {
return fs.statSync(path.resolve(targetDir, filePath)).isFile();
} catch (error) {
if (getConfig().debug) {
console.error('Failed to stat file', error);
}
return false;
}
});
targetDirFilelist.map(async (filePath) => {
const firstChar = filePath.slice(0, 1).toLowerCase();
const isAlphaNumeric = firstChar.match(/^[a-zA-Z0-9]/gm);
Expand All @@ -37,30 +44,6 @@ const sortMaps = async (targetDir) => {
});
};

/**
* Moves a file from a path to another
*
* @param {string} fromPath
* @param {string} toPath
*/
const moveFile = (fromPath, toPath) =>
new Promise((resolve, reject) => {
fs.rename(fromPath, toPath, (error) => {
if (error) {
if (getConfig().debug) {
console.error(`Failed to rename file from ${fromPath} to ${toPath}`, error);
}
return reject(null);
}

return resolve(toPath);
});
}).catch((error) => {
if (getConfig().debug) {
console.error(`Failed to rename file from ${fromPath} to ${toPath}`, error);
}
});

module.exports = {
sortMaps,
};
41 changes: 41 additions & 0 deletions scripts/unsort-maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const path = require('path');
const fs = require('fs');

const { getConfig } = require('./configuration');
const { getRecursiveFileList, moveFile } = require('./util');

/**
* Given a path, unsorts files, essentially flattening all directories.
*
* @param {string} targetDir Target directory to sort files in.
*/
const unsortMaps = async (targetDir) => {
console.log(`\nUn-sorting maps...`);

const targetDirFilelist = (await getRecursiveFileList(targetDir)).filter((filePath) => {
try {
return fs.statSync(path.resolve(targetDir, filePath)).isFile();
} catch (error) {
if (getConfig().debug) {
console.error('Failed to stat file', error);
}
return false;
}
});

return targetDirFilelist.map(async (filePath) => {
try {
const fileName = filePath.slice(filePath.lastIndexOf('/') + 1);

await moveFile(path.resolve(targetDir, filePath), path.resolve(targetDir, fileName));
} catch (error) {
if (getConfig().debug) {
console.error('Failed to move file', error);
}
}
});
};

module.exports = {
unsortMaps,
};
84 changes: 84 additions & 0 deletions scripts/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const path = require('path');
const fs = require('fs');

const { resolve } = require('path');
const { readdir } = require('fs').promises;
const { getConfig } = require('./configuration');
Expand Down Expand Up @@ -63,9 +66,90 @@ const getRecursiveFileList = async (targetDir) => {
return Array.prototype.concat(...files);
};

/**
* Checks if a file exists.
*
* @param {string} filePath
*/
const doesFileExist = async (filePath) =>
fs.promises
.access(filePath, fs.constants.F_OK)
.then(() => true)
.catch(() => false);

/**
* Moves a file from a path to another
*
* @param {string} fromPath
* @param {string} toPath
*/
const moveFile = (fromPath, toPath) =>
new Promise((resolve, reject) => {
fs.rename(fromPath, toPath, (error) => {
if (error) {
if (getConfig().debug) {
console.error(`Failed to rename file from ${fromPath} to ${toPath}`, error);
}
return reject(null);
}

return resolve(toPath);
});
}).catch((error) => {
if (getConfig().debug) {
console.error(`Failed to rename file from ${fromPath} to ${toPath}`, error);
}
});

/**
* Remove a file.
*
* @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);
}

resolve(path.resolve(targetDir, filePath));
})
).catch((error) => {
if (getConfig().debug) {
console.error(`Failed to remove ${filePath}`, error);
}
});

/**
* Remove a directory.
*
* @param { string } targetDir
*/
const removeDirectory = (targetDir) => (dirPath) =>
new Promise((resolve, reject) =>
fs.rmdir(path.resolve(targetDir, dirPath), (error) => {
if (error) {
console.error(`Failed to remove ${dirPath}`, error);
reject(null);
}

resolve(path.resolve(targetDir, dirPath));
})
).catch((error) => {
if (getConfig().debug) {
console.error(`Failed to remove ${dirPath}`, error);
}
});

module.exports = {
runPromisesWithProgress,
replaceLine,
replaceOraLine,
getRecursiveFileList,
moveFile,
removeFile,
removeDirectory,
doesFileExist,
};

0 comments on commit 0e2de52

Please sign in to comment.