Skip to content

Commit

Permalink
initial scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed Mar 22, 2019
1 parent 052351c commit 131d8b6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
template/node_modules
template/package-lock.json
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"git.ignoreLimitWarning": true
}
94 changes: 94 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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(/<name>/g, answers.name);
index = index.replace(/<description>/, ansewers.description);
index = index.replace(/<description>/, 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();
});
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 131d8b6

Please # to comment.