diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63912b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +template/node_modules +template/package-lock.json \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3b66410 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "git.ignoreLimitWarning": true +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..7d687f1 --- /dev/null +++ b/index.js @@ -0,0 +1,94 @@ +const c = require("ansi-colors"); +const inquirer = require("inquirer"); +const { execSync } = require("child_process"); +const fs = require("fs"); +const { resolve } = require("path"); + +const USER_DIRECTORY = process.env.PWD; +const TEMPLATE_DIRECTORY = resolve(__dirname, "template"); +const QUESTIONS = [ + { + type: "input", + prefix: c.greenBright(">"), + message: "What is name of your scaffold?", + name: "name", + validate: (name) => { + if (name === "") { + return "Name cannot be left blank" + } + // Regular expression for valid npm package name + if (!RegExp("^(?:@[a-z0-9-~][a-z0-9-._~]*/)?[a-z0-9-~][a-z0-9-._~]*$").test(name)){ + return "Invalid name" + } + return true; + } + }, + { + type: "input", + prefix: c.greenBright(">"), + message: "Enter description of your scaffold:", + name: "description", + }, + { + type: "input", + prefix: c.greenBright(">"), + message: "What is your name?", + name: "author", + }, +]; + +const resolveTemplate = (path) => resolve(TEMPLATE_DIRECTORY, path); +const resolveUser = (path) => resolve(USER_DIRECTORY, path); +const l = (message) => { console.log(message); }; +const info = (message) => { l(c.blueBright(i) + c.italic(` ${message}`)); } +const done = () => {l.greenBright("\u2713 Done"); }; + +function start() { + l( +` + ${c.blueBright("webpack-scaffold-starter")} + ------------------------ + Start your webpack scaffold on the go + +` + ) +} + +async function question() { + info("Details of your scaffold"); + let answers = {}; + const details = await inquirer.prompt(QUESTIONS); + answers = {answers, ...details}; + return answers; +} + +function scaffold(answers) { + info("Scaffolding files ..."); + const packageJSON = require("./template/package.json"); + packageJSON.name = answers.name; + packageJSON.description = answers.description; + packageJSON.author = answers.author; + + let index = fs.readFileSync(resolveTemplate("./index.js")); + index = index.replace(//g, answers.name); + index = index.replace(//, ansewers.description); + index = index.replace(//, ansewers.author); + + fs.writeFileSync(resolveUser("./package.json"), packageJSON); + fs.writeFileSync(resolveUser("./index.js"), index); + done(); +} + +function install() { + info("Installing Dependencies"); + l(c.gray(" May take few minutes")); + execSync(`cd ${USER_DIRECTORY} && npm install --quiet`); + done(); +} + +(async() => { + start(); + const answers = await question(); + scaffold(); + install(); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..62becd1 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "webpack-scaffold-starter", + "version": "1.0.0", + "description": "A CLI tool for scaffolding starter webpack-scaffold", + "main": "index.js", + "dependencies": { + "ansi-colors": "^3.2.4", + "inquirer": "^6.2.2" + }, + "devDependencies": {}, + "scripts": { + + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rishabh3112/webpack-scaffold-starter.git" + }, + "author": "Rishabh Chawla", + "license": "MIT", + "bugs": { + "url": "https://github.com/rishabh3112/webpack-scaffold-starter/issues" + }, + "homepage": "https://github.com/rishabh3112/webpack-scaffold-starter#readme" +}