-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.js
97 lines (90 loc) · 1.91 KB
/
global.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const writeJson = require("write-json");
const { mkdir } = require("fs");
const argv = require("argv");
// default language
const defaultLang = "ar";
// API base
const baseURL = "https://www.hisnmuslim.com/api/";
const main = [
{
ID: 1,
LANGUAGE: "العربية",
LANGUAGE_URL: baseURL + "ar/husn_ar.json",
},
{
ID: 2,
LANGUAGE: "English",
LANGUAGE_URL: baseURL + "en/husn_en.json",
},
];
/**
* @function pathExist a custom function to check & create the parent directory if not exists
*
* @module fs
*
* @param {string} filePath the file with related or absolute path
* @param {string} callback callback to execute after the check & parent directory maked
*/
function pathExist(filePath, callback = "") {
let dest = filePath.slice(0, filePath.lastIndexOf("/"));
return mkdir(
dest,
{
recursive: true,
},
(err) => {
if (err) {
console.log(err);
} else {
if (callback) {
callback();
}
}
}
);
}
/**
* @function insert a function to create & fill json files
*
* @module writeJson
* @module pathExist
*
* @param {string} fileName file name with or without path
* @param {object} object data to inject into the file
* @param {string} successMsg !optional: the success message
*
*/
function insert({ fileName, object, successMsg = "inserted successfully" }) {
pathExist(fileName, () => {
writeJson(fileName, object, (err) => {
if (err) {
console.log(err);
} else {
console.log(successMsg);
}
});
});
}
/**
* @function getArg get the passed argument in cli
*
* @module argv
*
* @param {string} name
*/
function getArg(name) {
let listOfArgs = {};
argv.run().targets.forEach((arg) => {
arg = arg.split("=");
listOfArgs[arg[0]] = arg[1];
});
return listOfArgs[name];
}
// exports
module.exports = {
defaultLang,
main,
insert,
pathExist,
getArg,
};