Check if the loaded yml or json files are files not directories

This commit is contained in:
Mohammad Fares 2018-08-25 02:23:32 +03:00
parent 034f36df1e
commit e06c12dd2d
1 changed files with 59 additions and 27 deletions

View File

@ -5,29 +5,54 @@
*/ */
/** /**
* Check, load, and parse YAML files * Check if a path represents a valid path for a file
* *
* - Add .yml extension when needed * @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
* *
* Throws * Throws
* - The provided file doesn't exit * - 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 * @param {String} filePath an absolute or a relative path
* @return {Object} * @param {String} extension
* @return {String}
*/ */
function loadYAML(filePath) { function loadFile(filePath, extension) {
var file = null; var content = null;
// Resolve the path into an absolute path // Resolve the path into an absolute path
filePath = di.path.resolve(filePath); filePath = di.path.resolve(filePath);
// The file doesn't exist // The file doesn't exist
if (!di.fs.existsSync(filePath)) { if (!isFile(filePath)) {
// A file with .yml suffix also doesn't exist // A file with .yml suffix also doesn't exist
if (!di.fs.existsSync(filePath + '.yml')) { if (!isFile(filePath + '.' + extension)) {
throw new Error('The provided file doesn\'t exit'); throw new Error('The provided file doesn\'t exit');
} else { } else {
filePath = filePath + '.yml'; filePath = filePath + '.yml';
@ -37,11 +62,32 @@ function loadYAML(filePath) {
// Read the file // Read the file
try { try {
file = di.fs.readFileSync(filePath); content = di.fs.readFileSync(filePath);
} catch (error) { } catch (error) {
throw new Error(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');
// Parse the file // Parse the file
try { try {
@ -66,28 +112,14 @@ function loadYAML(filePath) {
* Throws * Throws
* - The provided file doesn't exit * - The provided file doesn't exit
* - The provided file is not a valid JSON file * - The provided file is not a valid JSON file
* - Any reading errors
* *
* @param {String} filePath an absolute or a relative path * @param {String} filePath an absolute or a relative path
* @return {Object} * @return {Object}
*/ */
function loadJSON(filePath) { function loadJSON(filePath) {
var file = null; var file = loadFile(filePath, 'json');
// Resolve the path into an absolute path
filePath = di.path.resolve(filePath);
// The file doesn't exist
if (!di.fs.existsSync(filePath)) {
// A file with .json suffix also doesn't exist
if (!di.fs.existsSync(filePath + '.json')) {
throw new Error('The provided file doesn\'t exit');
} else {
filePath = filePath + '.json';
}
}
// Read the file // Read the file
try { try {