UNPKG

waw

Version:

waw is a minimalistic Node.js framework that serves as a foundation for building various types of projects. The core framework itself contains almost no code—its functionality is driven entirely by the modules you choose to include, making it highly flexi

110 lines (105 loc) 3.34 kB
const fs = require("fs"); // Main module export: adds functions to the waw object module.exports = function (waw) { // Create readline interface for CLI user input waw.readline = require("readline").createInterface({ input: process.stdin, output: process.stdout, }); /** * Ensures the creation of a new module/component, sets up paths, checks for existing folders, * handles git repo input, and processes CLI arguments. */ waw.ensure = (base, folder, message_exists, is_component = true) => { if (waw.argv.length < 2) { console.log("Provide name"); process.exit(0); } if (waw.argv[waw.argv.length - 1].endsWith(".git")) { waw.repo = waw.argv.pop(); } else if (waw.argv[waw.argv.length - 1].split("-").length === 2) { waw.repo = waw.argv.pop(); if (waw.argv.length === 1) { waw.argv.push(waw.repo.split("-")[1]); } waw.repo = "https://github.com/WebArtWork/" + waw.repo + ".git"; } waw.name = waw.argv[waw.argv.length - 1].toLowerCase(); if (waw.argv.length > 2) { waw.argv[1] = folder + "/" + waw.argv[1]; } while (waw.argv.length > 2) { waw.argv[1] += "/" + waw.argv.splice(2, 1); } if (!fs.existsSync(base + folder)) { fs.mkdirSync(base + folder); } if (waw.argv[1].indexOf("/") >= 0) { waw.path = waw.argv[1]; waw.name = waw.name.split("/").pop(); } else { waw.path = folder + "/" + waw.argv[1]; } waw.base = base + waw.path; if (fs.existsSync(waw.base)) { console.log(message_exists); process.exit(0); } if (waw.repo) { fs.mkdirSync(waw.base); waw.fetch(waw.base, waw.repo, (err) => { if (err) console.log("Repository was not found"); else console.log("Code is successfully installed"); process.exit(1); }); return true; } if (is_component) { waw.base += "/" + waw.name; } waw.Name = waw.name.slice(0, 1).toUpperCase() + waw.name.slice(1); return waw.repo; }; /** * Reads and lets the user select a customization/template element, * asks the user which template to use if multiple are available. */ waw.read_customization = (defaults, element, next) => { // let elements = waw.getDirectories(process.cwd() + '/template/' + element); // for (var i = 0; i < elements.length; i++) { // defaults[element][path.basename(elements[i])] = elements[i]; // } if (defaults && Object.keys(defaults[element]).length > 1) { waw.template = defaults[element]; let text = "Which element you want to use?", counter = 0, repos = {}; for (let key in defaults[element]) { repos[++counter] = defaults[element][key]; text += "\n" + counter + ") " + key; } text += "\nChoose number: "; return waw.readline.question(text, (answer) => { if (!answer || !repos[parseInt(answer)]) { return this.read_customization(waw, element, next); } waw.template = repos[parseInt(answer)]; next(); }); } else { waw.template = defaults[element].default; next(); } }; /** * Modifies a file in place: replaces code matching opts.search with opts.replace if present. */ waw.add_code = (opts) => { if (!fs.existsSync(opts.file)) return; let code = fs.readFileSync(opts.file, "utf8"); if (code && code.indexOf(opts.search) > -1) { code = code.replace(opts.search, opts.replace); fs.writeFileSync(opts.file, code, "utf8"); } }; };