UNPKG

bayascript

Version:

Write computer programming in Hausa language

188 lines (175 loc) 6.36 kB
const os = require("os"); const keywords = require("./base/keywords"); const BayaError = require("./exceptions/error"); const FileSystem = require("./utils/file-system"); const yargs = require("yargs/yargs"); const { hideBin } = require("yargs/helpers"); const { exec } = require("child_process"); const argv = yargs(hideBin(process.argv)).argv; const tmpDir = os.tmpdir(); let workDir = `${tmpDir}/bayascript/build`; if (os.platform() === "win32" || os.platform() === "win64") { workDir = `${tmpDir}\\bayascript\\build`; } const idxJsFile = `${workDir}/app.js`; function translate(msg) { new Promise((resolve, reject) => { keywords.errorDictionary.english.forEach((be, idx) => { let bh = keywords.errorDictionary.hausa[ keywords.errorDictionary.english.indexOf(be) ]; msg = msg.toString().toLowerCase().replaceAll(be, bh); if (idx >= keywords.errorDictionary.english.length) { resolve(msg); } }); }); return msg; } function compile(data) { new Promise((resolve, reject) => { keywords.bs.forEach((bk, idx) => { let jk = keywords.js[keywords.bs.indexOf(bk)]; let re = new RegExp("\\b" + bk + "\\b", "g"); data = data.replaceAll(re, jk); if (idx >= keywords.bs.length) { resolve(data); } }); }); return data; } const baya = { override: function (bits, callback) { try { if (bits.includes("shigoda('./") || bits.includes("require('./")) { let currentData = bits.toString().split(/(?:\r\n|\r|\n)/g); currentData.forEach(async (importLine) => { if ( importLine.includes("shigoda('./") || importLine.includes("require('./") ) { let declaredVar = `${importLine.substring( 0, importLine.indexOf("('./") )}`; let absImportPath = `${workDir}${importLine.substring( importLine.indexOf("('./") + 3, importLine.length - 2 )}`; if (!absImportPath.match(/\.js/)) { if (!absImportPath.match(/\.bs/)) absImportPath = absImportPath + ".bs"; absJsPath = absImportPath.substring(0, absImportPath.length - 3) + ".js"; let replacableImport = `${declaredVar}('${absJsPath}');`; bits = bits.replaceAll(importLine, replacableImport); const importData = FileSystem.readFileAsync(absImportPath); const nbsData = await compile(importData); FileSystem.writeFileSync(absJsPath, nbsData); this.override(importData, async (result) => { const nextNbsData = await compile(result); const nextAbsJsPath = absImportPath.substring(0, absImportPath.length - 3) + ".js"; FileSystem.writeFileSync(nextAbsJsPath, nextNbsData); }); if (callback) callback(bits); } else { let replacableImport = `${declaredVar}('${absImportPath});`; bits = bits.replaceAll(importLine, replacableImport); if (callback) callback(bits); } } }); } else { const nbsData = compile(bits); FileSystem.writeFileSync(idxJsFile, nbsData); if (callback) callback(bits); } } catch (ex) { console.error(new BayaError("ya kasa aiki")); console.error(translate(ex)); } }, rebuild: function (bits, callback) { this.override(bits, (result) => { const actlJs = compile(result); callback(actlJs); }); }, run: async function (indexPath, fileName) { let bsData; if (!fileName) console.error(new BayaError("ya kasa gano babban .bs fayil")); const param = argv._[0] || null; if (!fileName.match(/.bs/g)) fileName = fileName + ".bs"; const idxFile = `${workDir}/${fileName}`; try { await FileSystem.sourceCopy(indexPath, indexPath, workDir, param); bsData = FileSystem.readFileAsync(idxFile); } catch (ex) { console.error(new BayaError(`ya kasa gano .bs fayil (${fileName})`)); } var executionList = 0; return this.rebuild(bsData, (result) => { const idxJsPath = idxFile.substring(0, idxFile.length - 3) + ".js"; FileSystem.writeFileSync(idxJsPath, result); try { if (executionList <= 0) { const executionRes = exec(`node ${idxJsPath}`); executionRes.stdout.on("data", async (data) => { if (data.match(/node index.js/)) { const unwrap = data.substring(0, data.indexOf(".js") + 3); data = data .replace(unwrap, "") .replaceAll(/\s/g, "") .replace(/ /g, "") .trim(); } if (param === "build") { const projectName = indexPath.substring( indexPath.lastIndexOf("/") + 1, indexPath.length ); console.log(`#### Tattara Aikin Bayascript [${projectName}]####`); await FileSystem.cleanup(workDir); await FileSystem.sourceCopy( indexPath, workDir, `${indexPath}/build` ); console.log( `An kammala tattara aikin an saka shi a folda ${projectName}/build` ); console.log(`#### Angama ####`); } else { console.log(data); } }); executionRes.stderr.on("data", (data) => { if (data.match(/node index.js/)) { const unwrap = data.substring(0, data.indexOf(".js") + 3); data = data .replace(unwrap, "") .replaceAll(/\s/g, "") .replace(/ /g, "") .trim(); } console.log(data); }); executionRes.on("error", (error) => { console.error(new BayaError(`${error.message}`)); }); executionRes.on("Exit", (code) => { console.error(new BayaError(`ya kasa yin aiki saboda ${code}`)); }); } executionList++; } catch (ex) { console.error(translate(ex)); } }); }, }; module.exports = baya;