From 1535532230f118f96140d4d5e7f321c689793034 Mon Sep 17 00:00:00 2001 From: fadhlaouir Date: Mon, 18 Mar 2024 16:05:29 +0100 Subject: [PATCH] feat: Implement npx to create-express-node-starter --- README.md | 5 +- {cli => boilerplate}/README.md | 0 {cli => boilerplate}/_/deleteCrud.js | 0 {cli => boilerplate}/_/generateEmptyCrud.js | 0 {cli => boilerplate}/_/generateMinimalCrud.js | 0 {cli => boilerplate}/_/helpers.js | 0 cli/index.js => boilerplate/crudOperation.js | 0 boilerplate/index.js | 85 +++++++++++++++++++ package.json | 7 +- server.js | 1 + 10 files changed, 92 insertions(+), 6 deletions(-) rename {cli => boilerplate}/README.md (100%) rename {cli => boilerplate}/_/deleteCrud.js (100%) rename {cli => boilerplate}/_/generateEmptyCrud.js (100%) rename {cli => boilerplate}/_/generateMinimalCrud.js (100%) rename {cli => boilerplate}/_/helpers.js (100%) rename cli/index.js => boilerplate/crudOperation.js (100%) create mode 100644 boilerplate/index.js diff --git a/README.md b/README.md index d8cbac8..1c51854 100644 --- a/README.md +++ b/README.md @@ -104,14 +104,13 @@ npm install │ │ └── husky.sh │ ├── pre-commit │ └── pre-push -├── bin -│ └── cli.js -├── cli +├── boilerplate │ ├── _ │ │ ├── deleteCrud.js │ │ ├── generateEmptyCrud.js │ │ ├── generateMinimalCrud.js │ │ └── helpers.js +│ ├── crudOperation.js │ ├── index.js │ └── README.md ├── src diff --git a/cli/README.md b/boilerplate/README.md similarity index 100% rename from cli/README.md rename to boilerplate/README.md diff --git a/cli/_/deleteCrud.js b/boilerplate/_/deleteCrud.js similarity index 100% rename from cli/_/deleteCrud.js rename to boilerplate/_/deleteCrud.js diff --git a/cli/_/generateEmptyCrud.js b/boilerplate/_/generateEmptyCrud.js similarity index 100% rename from cli/_/generateEmptyCrud.js rename to boilerplate/_/generateEmptyCrud.js diff --git a/cli/_/generateMinimalCrud.js b/boilerplate/_/generateMinimalCrud.js similarity index 100% rename from cli/_/generateMinimalCrud.js rename to boilerplate/_/generateMinimalCrud.js diff --git a/cli/_/helpers.js b/boilerplate/_/helpers.js similarity index 100% rename from cli/_/helpers.js rename to boilerplate/_/helpers.js diff --git a/cli/index.js b/boilerplate/crudOperation.js similarity index 100% rename from cli/index.js rename to boilerplate/crudOperation.js diff --git a/boilerplate/index.js b/boilerplate/index.js new file mode 100644 index 0000000..bd75fb8 --- /dev/null +++ b/boilerplate/index.js @@ -0,0 +1,85 @@ +#!/usr/bin/env node + +const { exec } = require('child_process'); +const fs = require('fs'); +const path = require('path'); +const inquirer = require('inquirer'); + +const templatesDir = path.join(__dirname, 'templates'); + +async function main() { + try { + // Prompt user for project name and directory + const { projectName, projectDirectory } = await promptProjectDetails(); + + // Prompt user to choose template + const selectedTemplate = await promptTemplateSelection(); + + // Copy template to new project directory + copyTemplate(selectedTemplate, projectDirectory); + + // Install dependencies + await installDependencies(projectDirectory); + + console.log( + `Project '${projectName}' created successfully in ${projectDirectory}`, + ); + } catch (error) { + console.error('An error occurred:', error); + } +} + +async function promptProjectDetails() { + const questions = [ + { + type: 'input', + name: 'projectName', + message: 'Enter project name:', + validate: (input) => !!input.trim(), + }, + { + type: 'input', + name: 'projectDirectory', + message: 'Enter project directory:', + default: (answers) => `./${answers.projectName}`, + }, + ]; + + return inquirer.prompt(questions); +} + +async function promptTemplateSelection() { + const templates = fs.readdirSync(templatesDir); + const questions = [ + { + type: 'list', + name: 'template', + message: 'Choose a template:', + choices: templates, + }, + ]; + + const { template } = await inquirer.prompt(questions); + return template; +} + +function copyTemplate(template, projectDirectory) { + const templatePath = path.join(templatesDir, template); + exec(`cp -r ${templatePath} ${projectDirectory}`); +} + +async function installDependencies(projectDirectory) { + return new Promise((resolve, reject) => { + exec(`cd ${projectDirectory} && npm install`, (error, stdout, stderr) => { + if (error) { + reject(error); + } + if (stderr) { + reject(new Error(stderr)); + } + resolve(); + }); + }); +} + +main(); diff --git a/package.json b/package.json index b4e845d..214e502 100644 --- a/package.json +++ b/package.json @@ -6,13 +6,14 @@ "description": "Express, MongoDB, Firebase, and Swagger for API documentation starter for building RESTful APIs", "license": "MIT", "bin": { - "express-node-starter": "./cli/index.js" + "express-node-starter": "./boilerplate/crudOperation.js", + "create-express-node-starter": "./boilerplate/index.js" }, "scripts": { "start": "NODE_ENV=production && node server.js", "develop": "SET NODE_ENV=development && nodemon server.js", "develop:mac": "NODE_ENV=development && nodemon server.js", - "crud-operation": "node cli/index.js generate", + "crud-operation": "node boilerplate/crudOperation.js generate", "lint:check": "eslint --ext .js,.jsx,.ts,.tsx .", "format:check": "prettier --check .", "lint:fix": "eslint --ext .js,.jsx,.ts,.tsx --fix .", @@ -76,4 +77,4 @@ "node": ">=14.16.0 <=20.11.0", "npm": ">=6.14.11 <=10.2.4" } -} +} \ No newline at end of file diff --git a/server.js b/server.js index 8ffd9ff..b9b2e8f 100644 --- a/server.js +++ b/server.js @@ -46,6 +46,7 @@ app.use(cors()); const userRoutes = require('./src/routes/user.route'); const authRoutes = require('./src/routes/auth.route'); + // local APIs app.use('/v1/api', authRoutes); app.use('/v1/api', userRoutes);