Skip to content

Commit

Permalink
feat: Implement npx to create-express-node-starter
Browse files Browse the repository at this point in the history
  • Loading branch information
fadhlaouir committed Mar 18, 2024
1 parent d1879f3 commit 1535532
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 6 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
85 changes: 85 additions & 0 deletions boilerplate/index.js
Original file line number Diff line number Diff line change
@@ -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();
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down Expand Up @@ -76,4 +77,4 @@
"node": ">=14.16.0 <=20.11.0",
"npm": ">=6.14.11 <=10.2.4"
}
}
}
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1535532

Please sign in to comment.