terminalizer/utility.js

204 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-07-22 22:34:35 +00:00
/**
* Provide utility functions
*
* @author Mohammad Fares <faressoft.com@gmail.com>
*/
/**
* Check if a path represents a valid path for a file
2018-07-22 22:34:35 +00:00
*
* @param {String} filePath an absolute or a relative path
* @return {Boolean}
*/
function isFile(filePath) {
// Resolve the path into an absolute path
filePath = di.path.resolve(filePath);
try {
return di.fs.lstatSync(filePath).isFile();
} catch (error) {
return false;
}
}
/**
* Load a file's content
*
* - Check if the file exists, if not found check
* if the file exists with appending the extension
*
2018-07-22 22:34:35 +00:00
* Throws
* - The provided file doesn't exit
* - Any reading errors
2018-07-22 22:34:35 +00:00
*
* @param {String} filePath an absolute or a relative path
* @param {String} extension
* @return {String}
2018-07-22 22:34:35 +00:00
*/
function loadFile(filePath, extension) {
2018-07-22 22:34:35 +00:00
var content = null;
2018-07-22 22:34:35 +00:00
2018-08-04 23:46:01 +00:00
// Resolve the path into an absolute path
2018-07-22 22:34:35 +00:00
filePath = di.path.resolve(filePath);
// The file doesn't exist
if (!isFile(filePath)) {
2018-07-22 22:34:35 +00:00
// A file with .yml suffix also doesn't exist
if (!isFile(filePath + '.' + extension)) {
2018-07-22 22:34:35 +00:00
throw new Error('The provided file doesn\'t exit');
} else {
filePath = filePath + '.yml';
}
}
// Read the file
try {
content = di.fs.readFileSync(filePath);
2018-07-22 22:34:35 +00:00
} catch (error) {
throw new Error(error);
}
return content;
}
/**
* Check, load, and parse YAML files
*
* - Add .yml extension when needed
*
* Throws
* - The provided file doesn't exit
* - The provided file is not a valid YAML file
* - Any reading errors
*
* @param {String} filePath an absolute or a relative path
* @return {Object}
*/
function loadYAML(filePath) {
var file = loadFile(filePath, 'yml');
2018-07-22 22:34:35 +00:00
// Parse the file
try {
return {
json: di.yaml.load(file),
raw: file.toString()
};
} catch (error) {
throw new Error('The provided file is not a valid YAML file');
}
}
/**
* Check, load, and parse JSON files
*
* - Add .json extension when needed
*
* Throws
* - The provided file doesn't exit
* - The provided file is not a valid JSON file
* - Any reading errors
2018-07-22 22:34:35 +00:00
*
* @param {String} filePath an absolute or a relative path
* @return {Object}
*/
function loadJSON(filePath) {
var file = loadFile(filePath, 'json');
2018-07-22 22:34:35 +00:00
// Read the file
try {
file = di.fs.readFileSync(filePath);
} catch (error) {
throw new Error(error);
}
// Parse the file
try {
return JSON.parse(file);
} catch (error) {
throw new Error('The provided file is not a valid JSON file');
}
}
/**
* Resolve a path and add an extension to the file name
*
* - Add the extension if not already added
*
* @param {String} filePath an absolute or a relative path
* @param {String} extension
* @return {String}
*/
function resolveFilePath(filePath, extension) {
var resolvedPath = di.path.resolve(filePath);
// The extension is not added
if (di.path.extname(resolvedPath) != '.' + extension) {
resolvedPath += '.' + extension;
}
return resolvedPath;
}
/**
* Get the default configurations
*
* @return {Object} {json, raw}
*/
function getDefaultConfig() {
var filePath = di.path.join(__dirname, 'config.yml');
return loadYAML(filePath);
}
/**
* Change a value for a specific key in YAML
*
* - Works only with the first level keys
2018-08-04 23:46:01 +00:00
* - Works only with keys with a single value
2018-07-22 22:34:35 +00:00
* - Apply the changes on the json and raw
*
* @param {Object} data {json, raw}
* @param {String} key
* @param {*} value
*/
function changeYAMLValue(data, key, value) {
data.json[key] = value;
data.raw = data.raw.replace(new RegExp('^' + key + ':.+$', 'm'), key + ': ' + value);
}
////////////////////////////////////////////////////
// Module //////////////////////////////////////////
////////////////////////////////////////////////////
module.exports = {
loadYAML: loadYAML,
loadJSON: loadJSON,
resolveFilePath: resolveFilePath,
getDefaultConfig: getDefaultConfig,
changeYAMLValue: changeYAMLValue
};