UNPKG

internal-error

Version:

This gives internal error message based on error code

63 lines (57 loc) 2.23 kB
const fs = require('fs'); const path = require('path'); const yaml = require('js-yaml'); const shell = require('shelljs'); // @ts-ignore let FOLDER_NAME = 'yamldir' //let LIVE_DIRECTORY = 'node_modules/internal-error/yamldir' let message = 'Message not found!' //let testDirectoryPath = path.join(__dirname,TEST_FOLDER_NAME); let liveDirectoryPath = path.join(__dirname,FOLDER_NAME); const directoryPath = liveDirectoryPath; function internalerrormessage(code = '',it8n = '',callback) { let arrayOfYaml = []; if (code === '') { return callback(null,'Enter code') } if (it8n === '') { return callback(null,'Enter it8n') } fs.readdir(directoryPath,function (err,files) { if (err) { return callback(null,'Unable to scan directory: ' + err) } else { // listing all files using forEach files.forEach(async function (file) { let ext = path.extname(file); if (ext.toLowerCase() === '.yaml') { let filedir = path.join(directoryPath,file); let fileContents = fs.readFileSync(filedir,'utf8'); let data = yaml.loadAll(fileContents); let jsonArray = { ...data } for (let value of Object.values(jsonArray)) { if (value !== null && value !== undefined) { arrayOfYaml.push(value); // @ts-ignore } } } }); let arrayObj = arrayOfYaml.filter(p => p.code === code); if (arrayObj.length > 0) { // @ts-ignore for (let msg of Object.values(arrayObj)) { let msgObj = msg.messages.find(p => p.it8n === it8n); if (msgObj !== undefined && msgObj !== null && msgObj !== 'undefined') { if (msgObj.it8n === it8n) { message = msgObj.message; } } } } return callback(null,message) } }) } module.exports = internalerrormessage;