Skip to content

Commit

Permalink
fix: change path management
Browse files Browse the repository at this point in the history
  • Loading branch information
silvaezequias committed Aug 4, 2022
1 parent 72ad581 commit 6b35f90
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions handler/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs-extra');
const format = require('./format');
const settings = require('../settings.json');

function validate(expected_path, target_file){
expected_path = format.names(expected_path);
target_file = format.names([target_file])[0];
target_file = target_file?.length ? target_file : ['main'];

var arrayPath = ['database', ...expected_path];
arrayPath.forEach((_, index) => {
var parent = arrayPath.slice(0, index + 1);
var current_path = parent.join('/');
var current_file_state = !fs.existsSync(current_path);
if (current_file_state) fs.mkdirSync(current_path);
});

var final_parents_path = arrayPath.join('/');
var file_extension = settings.file_extension;
var file_path = `${final_parents_path}/${target_file}.${file_extension}`;

if (!fs.existsSync(file_path)) fs.writeFileSync(file_path, '{}');
return file_path;
};

function exists(path){
var arrayPath = ['database', ...path]
var file_path = arrayPath.join('/');
file_path = `${file_path}.${settings.file_extension}`;
return fs.existsSync(file_path);
};

function existsDir(path){
var arrayPath = ['database', ...path]
var dir_path = arrayPath.join('/');
return fs.existsSync(dir_path);
};

module.exports = { validate, exists, existsDir };

0 comments on commit 6b35f90

Please sign in to comment.