From 9ca7b933ac4c7514f4b71f39a0763389f3c2089e Mon Sep 17 00:00:00 2001 From: "Pranto, Nazmuz-CW" Date: Fri, 1 Jul 2022 09:57:37 -0400 Subject: [PATCH] #51 - adding command for vanilla js project setup --- .gitignore | 3 ++ content/vanilla-app/.gitignore | 24 +++++++++ content/vanilla-app/favicon.svg | 15 ++++++ content/vanilla-app/index.html | 13 +++++ content/vanilla-app/main.js | 6 +++ content/vanilla-app/package.json | 18 +++++++ content/vanilla-app/style.css | 8 +++ src/executeCommand.js | 35 +++++++------ src/init.js | 22 ++++++++ src/initializeGit.js | 6 +-- src/isGitInitialized.js | 4 +- src/onCreateProject.js | 89 ++++++++++++++++++++++++++++++++ 12 files changed, 223 insertions(+), 20 deletions(-) create mode 100644 content/vanilla-app/.gitignore create mode 100644 content/vanilla-app/favicon.svg create mode 100644 content/vanilla-app/index.html create mode 100644 content/vanilla-app/main.js create mode 100644 content/vanilla-app/package.json create mode 100644 content/vanilla-app/style.css create mode 100644 src/onCreateProject.js diff --git a/.gitignore b/.gitignore index 6704566..090fb29 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,6 @@ dist # TernJS port file .tern-port + +# macOS +.DS_Store diff --git a/content/vanilla-app/.gitignore b/content/vanilla-app/.gitignore new file mode 100644 index 0000000..a547bf3 --- /dev/null +++ b/content/vanilla-app/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/content/vanilla-app/favicon.svg b/content/vanilla-app/favicon.svg new file mode 100644 index 0000000..de4aedd --- /dev/null +++ b/content/vanilla-app/favicon.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/content/vanilla-app/index.html b/content/vanilla-app/index.html new file mode 100644 index 0000000..f1dedb5 --- /dev/null +++ b/content/vanilla-app/index.html @@ -0,0 +1,13 @@ + + + + + + + Vite App + + +
+ + + diff --git a/content/vanilla-app/main.js b/content/vanilla-app/main.js new file mode 100644 index 0000000..4c0070f --- /dev/null +++ b/content/vanilla-app/main.js @@ -0,0 +1,6 @@ +import './style.css' + +document.querySelector('#app').innerHTML = ` +

Hello Vite!

+ Documentation +` diff --git a/content/vanilla-app/package.json b/content/vanilla-app/package.json new file mode 100644 index 0000000..5d0fe5b --- /dev/null +++ b/content/vanilla-app/package.json @@ -0,0 +1,18 @@ +{ + "name": "vanilla-app", + "private": true, + "version": "0.0.0", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview" + }, + "devDependencies": { + "vite": "^2.9.9" + }, + "main": "main.js", + "keywords": [], + "author": "", + "license": "ISC", + "description": "" +} diff --git a/content/vanilla-app/style.css b/content/vanilla-app/style.css new file mode 100644 index 0000000..852de7a --- /dev/null +++ b/content/vanilla-app/style.css @@ -0,0 +1,8 @@ +#app { + font-family: Avenir, Helvetica, Arial, sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + text-align: center; + color: #2c3e50; + margin-top: 60px; +} diff --git a/src/executeCommand.js b/src/executeCommand.js index 2e616b4..51cdbbb 100644 --- a/src/executeCommand.js +++ b/src/executeCommand.js @@ -1,23 +1,28 @@ const { exec } = require('child_process'); +const process = require('process'); -const executeCommand = (command) => +const executeCommand = (command, options = {}) => new Promise((resolve) => { - exec(`${command}`, (error, stdout, stderr) => { - if (error) { + exec( + `${command}`, + { cwd: options?.cwd || process.cwd() }, + (error, stdout, stderr) => { + if (error) { + resolve({ + success: false, + }); + } + if (stderr) { + resolve({ + success: false, + }); + } resolve({ - success: false, + success: true, + output: stdout, }); - } - if (stderr) { - resolve({ - success: false, - }); - } - resolve({ - success: true, - output: stdout, - }); - }); + }, + ); }); module.exports = executeCommand; diff --git a/src/init.js b/src/init.js index db25bef..247d91e 100644 --- a/src/init.js +++ b/src/init.js @@ -1,5 +1,6 @@ const chalk = require('chalk'); const { program } = require('commander'); +const onCreateProject = require('./onCreateProject'); const onEditorConfig = require('./onEditorConfig'); const onESLint = require('./onESLint'); const onGitHubActions = require('./onGitHubActions'); @@ -103,4 +104,25 @@ program ) .action(onGitHubActions); +program + .command('create project') + .description( + ` + ${chalk.blue.bold.underline('WHAT IT DOES')}: + Based on list of questionnaires, generates a project template using Vite.js + Ref: https://github.com/vitejs/vite/blob/main/packages/create-vite/README.md + + ${chalk.yellow.underline('Supported Templates')}: + - vanilla (generates a vanilla JavaScript project w/ support for HTML, CSS and JavaScript) + + ${chalk.blue.bold.underline('BEFORE YOU RUN')}: + - only run this command from the root of your project, otherwise project may not setup properly + `, + ) + .option( + '-CT, --create-tag', + 'automatically bumps patch version and creates tag on merge to `master` branch', + ) + .action(onCreateProject); + program.parse(); diff --git a/src/initializeGit.js b/src/initializeGit.js index da0fbde..fc2c85e 100644 --- a/src/initializeGit.js +++ b/src/initializeGit.js @@ -2,8 +2,8 @@ const chalk = require('chalk'); const executeCommand = require('./executeCommand'); const isGitInitialized = require('./isGitInitialized'); -const initializeGit = async () => { - if (isGitInitialized()) { +const initializeGit = async ({ path } = {}) => { + if (isGitInitialized(path)) { console.log( chalk.gray.bold( `(ℹ) Looks like \`git\` is already initialized, skipping`, @@ -11,7 +11,7 @@ const initializeGit = async () => { ); } else { console.log(chalk.gray('> Initializing git...')); - await executeCommand('git init'); + await executeCommand('git init', { cwd: path }); } }; diff --git a/src/isGitInitialized.js b/src/isGitInitialized.js index feadd3c..4e2b37c 100644 --- a/src/isGitInitialized.js +++ b/src/isGitInitialized.js @@ -2,7 +2,7 @@ const path = require('path'); const process = require('process'); const doesDirectoryExist = require('./doesDirectoryExist'); -const isGitInitialized = () => - doesDirectoryExist(path.join(process.cwd(), '.git')); +const isGitInitialized = (p = process.cwd()) => + doesDirectoryExist(path.join(p, '.git')); module.exports = isGitInitialized; diff --git a/src/onCreateProject.js b/src/onCreateProject.js new file mode 100644 index 0000000..b5ed90b --- /dev/null +++ b/src/onCreateProject.js @@ -0,0 +1,89 @@ +const path = require('path'); +const process = require('process'); +const chalk = require('chalk'); +const { copySync } = require('fs-extra'); +const inquirer = require('inquirer'); +const doesDirectoryExist = require('./doesDirectoryExist'); +const initializeGit = require('./initializeGit'); + +const createVanillaProject = ({ name, gitInit } = {}) => { + console.log(chalk.red('Generating `vanilla` project...')); + copySync( + path.join(process.cwd(), 'content/vanilla-app'), + path.join(process.cwd(), `${name}`), + ); + if (gitInit) { + initializeGit({ path: path.join(process.cwd(), `${name}`) }); + } + console.log( + chalk.green.bold('✅ Success! `vanilla` project has been created!'), + ); +}; + +const onCreateProject = () => { + console.log(chalk.green.bold("👉 Let's create a project...")); + return inquirer + .prompt([ + { + type: 'input', + name: 'name', + message: `${chalk.blue('Name?')} (i.e., 'todo-app')`, + validate(value) { + if ( + !value || + typeof value !== 'string' || + !value.length || + !/^[a-zA-Z-\d]+$/.test(value) + ) { + return 'Please enter a valid project name (only letter, numbers and hyphens allowed)'; + } + if (doesDirectoryExist(path.join(process.cwd(), `${value}`))) { + return `(ℹ) Looks like \`${value}\` directory already exists. Please retry creating a project with a different name`; + } + return true; + }, + }, + { + type: 'list', + name: 'template', + message: `${chalk.blue('Choose a template?')}`, + choices: [ + { + key: 'v', + name: `${chalk.yellow.bold( + 'Vanilla', + )} (generates a vanilla JavaScript project w/ support for HTML, CSS and JavaScript)`, + value: 'vanilla', + }, + ], + }, + { + type: 'list', + name: 'gitInit', + message: `${chalk.blue.bold( + 'Do you want to initialize git in your project?', + )}`, + choices: [ + { + name: 'true', + value: true, + }, + { + name: 'false', + value: false, + }, + ], + }, + ]) + .then((projectOptions = {}) => { + const { name, template, gitInit = false } = projectOptions; + if (template === 'vanilla') { + createVanillaProject({ name, gitInit }); + } + }) + .catch((error) => { + throw error; + }); +}; + +module.exports = onCreateProject;