laniakea
Version:
A renaming utility for classic ROMs
146 lines (112 loc) • 4.43 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: helpers.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: helpers.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const fs = require('fs');
const path = require('path');
const consoles = require('./../dictionaries/consoles.json');
const crypto = require('./crypto');
module.exports = {
/**
* Formats source file to expose it's filename and extension in a digestable format
* @param {string} filepath - Where the file to be renamed is located. Should be a fullpath.
* @return {object} - An object containing the filename and extension
*/
getFileInfo(filepath) {
return {
filename: path.basename(filepath).split('.').shift(),
extension: filepath.split('.').pop()
};
},
/**
* Returns the dictionary represented by the filepath.
* @param {string} filepath - Where the file to be renamed is located. Should be a fullpath.
* @return {object} - The appropriate dictionary information for the game that is represented by filepath.
*/
getDictionaryByFile(filepath) {
let konsole = this.getConsoleByExtension(filepath).dictionaryName;
// Use __dirname so that relative filepaths work
let dictPath = path.join(__dirname, '..', 'dictionaries', 'games', `${konsole}.json`);
let dictionary = JSON.parse(fs.readFileSync(dictPath, 'utf8'));
if (!dictionary) {
throw new Error(`Could not find associated dictionary for given console. Console: ${konsole}`);
}
return dictionary;
},
/**
* Returns the game represented by the filepath from the associated dictionary.
* @param {string} filepath - Where the file to be renamed is located. Should be a fullpath.
* @return {object} - The appropriate game information from the dictionary that is represented by filepath.
*/
getGameByFilepath(filepath) {
let hash = crypto.MD5Checksum(filepath);
let dict = this.getDictionaryByFile(filepath);
let game = dict.find((obj) => {
return obj.checksums.md5 == hash;
});
if (!game) {
throw new Error(`Could not find associated game for given file checksum. Checksum: ${hash}`);
}
return game;
},
/**
* Returns the appropriate console for a given file extension. Information is from the console dictionary
* @param {string} filepath - Where the file to be renamed is located. Should be a fullpath.
* @return {object} - The appropriate consle information associated with a given filetype.
*/
getConsoleByExtension(filepath) {
let ext = filepath.split('.').pop().toLowerCase();
// Consoles as defined above as a json require
let konsole = consoles.find((obj) => {
// return the first console to use the file extension form above
return obj.extension.includes(ext);
});
if (!konsole) {
throw new Error(`Could not find associated console for given file extension. Extension: ${ext}`);
}
return konsole;
},
/**
* IN PROGRESS: Returns a list of extensions that are valid for the consoles included.
* To be used to filter what files will be filtered from a directory walk.
* @return {array} - A list of extensions found in the console dictionary
*/
getValidExtensions() {
let validExtensions = [];
// Consoles as defined above as a json require
consoles.map((obj) => {
// Concat to make sure it's one big array, instead of array of arrays
validExtensions = validExtensions.concat(obj.extension);
});
return validExtensions;
}
};
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Feb 28 2017 19:33:36 GMT-0800 (PST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>