Move tools config to tools.json

Refactor tools functions to use Records and object literals
This commit is contained in:
Shivam Mathur
2021-07-05 16:03:10 +05:30
parent 60ff774608
commit 6449431df2
11 changed files with 1239 additions and 833 deletions

View File

@ -207,10 +207,14 @@ export async function addLog(
* Read the scripts
*
* @param filename
* @param directory
*/
export async function readScript(filename: string): Promise<string> {
export async function readFile(
filename: string,
directory: string
): Promise<string> {
return fs.readFileSync(
path.join(__dirname, '../src/scripts/' + filename),
path.join(__dirname, '../' + directory, filename),
'utf8'
);
}
@ -231,6 +235,41 @@ export async function writeScript(
return script_path;
}
/**
* Function to get information about a tool
*
* @param tool
*/
export async function getToolData(
tool: string
): Promise<Record<string, string>> {
const tools_json: string = await readFile('tools.json', 'src/configs');
const json_data: Record<string, Record<string, string>> =
JSON.parse(tools_json);
let tool_data: Record<string, string>;
const tools: string[] = Object.keys(json_data);
if (tools.includes(tool)) {
tool_data = json_data[tool];
tool_data['tool'] = tool;
} else {
const tool_key: string | undefined = Object.keys(json_data).find(
(key: string) => {
return (
json_data[key]['alias'] == tool ||
json_data[key]['repository'] == tool
);
}
);
if (tool_key) {
tool_data = json_data[tool_key];
tool_data['tool'] = tool_key;
} else {
tool_data = {tool: tool};
}
}
return tool_data;
}
/**
* Function to break extension csv into an array
*