UNPKG

bayascript

Version:

Write computer programming in Hausa language

124 lines (118 loc) 3.26 kB
#!/usr/bin/env node const BayaError = require("./exceptions/error"); const FileSystem = require("./utils/file-system"); const yargs = require("yargs/yargs"); const { hideBin } = require("yargs/helpers"); const inquirer = require("inquirer"); const { exec } = require("child_process"); const argv = yargs(hideBin(process.argv)).argv; const command = argv._[0] || null; const param = argv._[1] || null; const path = process.cwd(); const execute = (command) => { const executionRes = exec(command); executionRes.stdout.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.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, ""); } console.log(data); }); executionRes.on("error", (error) => { console.error(new BayaError(error)); }); executionRes.on("Exit", (code) => { console.error(new BayaError(`ya kasa yin aiki saboda ${code}`)); }); }; const installDependency = () => { console.log(`#### Installing ${param} ####`); execute(`npm install --prefix ${path} ${param} && npm run link`); }; const initializeBSProject = () => { const prompt = inquirer.createPromptModule(); prompt([ { type: "input", name: "name", message: "Rubuta sunan sabon project: ", }, ]).then((project) => { try { const projectPath = `${path}/${project.name}`; FileSystem.writeFileSync( `${projectPath}/index.js`, 'require("bayascript").run(__dirname, "app");' ); FileSystem.writeFileSync( `${projectPath}/app.bs`, `nuna('Barka da yau!');` ); FileSystem.writeFileSync( `${projectPath}/package.json`, `{ "name": "${project.name}", "version": "1.0.0", "description": "Project da aka bude da Bayascript", "main": "index.js", "scripts": { "start": "node index.js", "link": "node index.js link", "build": "node index.js build" }, "keywords": [], "author": "", "license": "ISC" } ` ); execute(`npm install --prefix ${projectPath} bayascript`); } catch (ex) { console.error(new BayaError(ex)); } }); }; let comandOption = command; if (argv && argv.v) execute(`npm info bayascript version`); if (argv && argv.link) comandOption = command.trim() + "--link"; switch (comandOption) { case null: execute(`npm info bayascript version`); break; case "version": execute(`npm info bayascript version`); break; case "init": initializeBSProject(); break; case "start": execute(`npm run start`); break; case "start--link": execute(`npm run link`); break; case "build": execute(`npm run build`); break; case "i": installDependency(); break; case "install": installDependency(); break; default: console.error( new BayaError(`baisan umarnin da aka bayar ba na(bs ${command})...`) ); }