Skip to content

Commit

Permalink
template generation successfully added, all logic shifted to utils/cu…
Browse files Browse the repository at this point in the history
…stom
  • Loading branch information
utkarsh-1905 committed Dec 3, 2022
1 parent 2d57164 commit f5768bc
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ assets/img/.DS_Store

# VSCode related files #
# .vscode

test/
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const flags = cli.flags;
const { clear, debug } = flags;
const chalk = require('chalk');

const { showJoke } = require('./utils/joke');
const chat = require('./utils/chat');
const ping = require('./utils/ping');
const { generator } = require('./utils/generator');
const { showJoke } = require('./utils/custom/joke');
const chat = require('./utils/custom/chat');
const ping = require('./utils/custom/ping');
const { generator } = require('./utils/custom/generator');

(async () => {
try {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@utkarsh1905/node-cli-project",
"description": "Development Automation Tool",
"version": "4.0.0",
"version": "4.2.0",
"license": "MIT",
"bin": {
"dat": "./index.js"
Expand All @@ -17,7 +17,8 @@
],
"files": [
"index.js",
"utils"
"utils",
"template"
],
"scripts": {
"test": "exit 0",
Expand Down
4 changes: 4 additions & 0 deletions template/node-mongoose-setup/commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"setup": "Run `npm install` to install dependencies.",
"changes": "Change the mongoDB connection string in my.env"
}
1 change: 1 addition & 0 deletions template/node-socket.io/config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOCKETURL = "url"
11 changes: 11 additions & 0 deletions template/node-socket.io/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('dotenv').config({ path: './config.env' });

const io = require('socket.io-client');
const socket = io.connect(process.env.SOCKETURL);

io.on('connection', socket => {
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
19 changes: 19 additions & 0 deletions template/node-socket.io/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "node-socket.io",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"socket.io-client": "^4.5.1",
"express": "4.18.1",
"cors": "^2.8.5",
"dotenv": "^16.0.1"
}
}
File renamed without changes.
51 changes: 35 additions & 16 deletions utils/generator.js → utils/custom/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ async function getTemplateInfo() {
{
type: 'input',
name: 'path',
message: 'Enter the path to the template',
default: './'
message: 'Enter the path to create the project',
default: process.cwd(),
filter: ans => {
if (ans != process.cwd()) {
return process.cwd() + '\\' + ans;
} else return ans;
}
}
])
.then(answers => generateTemplate(answers))
Expand All @@ -41,31 +46,45 @@ async function getTemplateInfo() {

function generateTemplate(answers) {
try {
const projectPath = answers.path + answers.projectName;
if (fs.existsSync(projectPath)) {
console.log(chalk.red('Project already exists'));
process.exit(0);
console.log(answers.path);
if (fs.existsSync(answers.path)) {
if (fs.existsSync(answers.path + '\\' + answers.projectName)) {
console.log(chalk.red('Project with this name already exists'));
process.exit(0);
} else {
fs.mkdirSync(answers.path + '\\' + answers.projectName);
}
} else {
fs.mkdirSync(projectPath);
console.log(chalk.red('Path does not exist'));
process.exit(0);
}
templatePath = path.join(templatePath, answers.template);
const filesToCreate = fs.readdirSync(templatePath);
let commands;
filesToCreate.forEach(file => {
const origPath = path.join(templatePath, file);
const stats = fs.statSync(origPath);
if (stats.isFile()) {
let contents = fs.readFileSync(origPath, 'utf8');
const writePath = path.join(
__dirname,
'../',
projectPath,
file
);
fs.writeFileSync(writePath, contents, 'utf-8');
if (file !== 'commands.json') {
if (stats.isFile()) {
let contents = fs.readFileSync(origPath, 'utf8');
const writePath = path.join(
answers.path + '\\' + answers.projectName,
file
);
fs.writeFileSync(writePath, contents, 'utf-8');
}
} else {
commands = JSON.parse(fs.readFileSync(origPath));
}
});
//here we can also add the command to install the dependencies
console.log(chalk.yellow('Project created successfully'));
console.log(chalk.cyan(commands.setup));
console.log(chalk.green(commands.changes));
//give the command list to run the template
} catch (e) {
console.log(e);
console.log(chalk.red("Couldn't create the project"));
process.exit(1);
}
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion utils/ping.js → utils/custom/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const chalk = require('chalk');
async function ping() {
try {
const res = await axios.get(
'https://node-cli-chat-server.herokuapp.com/ping'
'https://node-chat-server-qkuiprdusq-em.a.run.app/ping'
);
console.log(chalk.yellowBright.bgCyan(res.data.res.toUpperCase()));
process.exit(0);
Expand Down

0 comments on commit f5768bc

Please sign in to comment.