Skip to content

Commit f5768bc

Browse files
committed
template generation successfully added, all logic shifted to utils/custom
1 parent 2d57164 commit f5768bc

File tree

11 files changed

+80
-23
lines changed

11 files changed

+80
-23
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@ assets/img/.DS_Store
7171

7272
# VSCode related files #
7373
# .vscode
74+
75+
test/

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const flags = cli.flags;
1616
const { clear, debug } = flags;
1717
const chalk = require('chalk');
1818

19-
const { showJoke } = require('./utils/joke');
20-
const chat = require('./utils/chat');
21-
const ping = require('./utils/ping');
22-
const { generator } = require('./utils/generator');
19+
const { showJoke } = require('./utils/custom/joke');
20+
const chat = require('./utils/custom/chat');
21+
const ping = require('./utils/custom/ping');
22+
const { generator } = require('./utils/custom/generator');
2323

2424
(async () => {
2525
try {

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@utkarsh1905/node-cli-project",
33
"description": "Development Automation Tool",
4-
"version": "4.0.0",
4+
"version": "4.2.0",
55
"license": "MIT",
66
"bin": {
77
"dat": "./index.js"
@@ -17,7 +17,8 @@
1717
],
1818
"files": [
1919
"index.js",
20-
"utils"
20+
"utils",
21+
"template"
2122
],
2223
"scripts": {
2324
"test": "exit 0",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"setup": "Run `npm install` to install dependencies.",
3+
"changes": "Change the mongoDB connection string in my.env"
4+
}

template/node-socket.io/config.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOCKETURL = "url"

template/node-socket.io/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require('dotenv').config({ path: './config.env' });
2+
3+
const io = require('socket.io-client');
4+
const socket = io.connect(process.env.SOCKETURL);
5+
6+
io.on('connection', socket => {
7+
console.log('a user connected');
8+
socket.on('disconnect', () => {
9+
console.log('user disconnected');
10+
});
11+
});

template/node-socket.io/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "node-socket.io",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "nodemon index.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"socket.io-client": "^4.5.1",
15+
"express": "4.18.1",
16+
"cors": "^2.8.5",
17+
"dotenv": "^16.0.1"
18+
}
19+
}
File renamed without changes.

utils/generator.js renamed to utils/custom/generator.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ async function getTemplateInfo() {
3030
{
3131
type: 'input',
3232
name: 'path',
33-
message: 'Enter the path to the template',
34-
default: './'
33+
message: 'Enter the path to create the project',
34+
default: process.cwd(),
35+
filter: ans => {
36+
if (ans != process.cwd()) {
37+
return process.cwd() + '\\' + ans;
38+
} else return ans;
39+
}
3540
}
3641
])
3742
.then(answers => generateTemplate(answers))
@@ -41,31 +46,45 @@ async function getTemplateInfo() {
4146

4247
function generateTemplate(answers) {
4348
try {
44-
const projectPath = answers.path + answers.projectName;
45-
if (fs.existsSync(projectPath)) {
46-
console.log(chalk.red('Project already exists'));
47-
process.exit(0);
49+
console.log(answers.path);
50+
if (fs.existsSync(answers.path)) {
51+
if (fs.existsSync(answers.path + '\\' + answers.projectName)) {
52+
console.log(chalk.red('Project with this name already exists'));
53+
process.exit(0);
54+
} else {
55+
fs.mkdirSync(answers.path + '\\' + answers.projectName);
56+
}
4857
} else {
49-
fs.mkdirSync(projectPath);
58+
console.log(chalk.red('Path does not exist'));
59+
process.exit(0);
5060
}
5161
templatePath = path.join(templatePath, answers.template);
5262
const filesToCreate = fs.readdirSync(templatePath);
63+
let commands;
5364
filesToCreate.forEach(file => {
5465
const origPath = path.join(templatePath, file);
5566
const stats = fs.statSync(origPath);
56-
if (stats.isFile()) {
57-
let contents = fs.readFileSync(origPath, 'utf8');
58-
const writePath = path.join(
59-
__dirname,
60-
'../',
61-
projectPath,
62-
file
63-
);
64-
fs.writeFileSync(writePath, contents, 'utf-8');
67+
if (file !== 'commands.json') {
68+
if (stats.isFile()) {
69+
let contents = fs.readFileSync(origPath, 'utf8');
70+
const writePath = path.join(
71+
answers.path + '\\' + answers.projectName,
72+
file
73+
);
74+
fs.writeFileSync(writePath, contents, 'utf-8');
75+
}
76+
} else {
77+
commands = JSON.parse(fs.readFileSync(origPath));
6578
}
6679
});
80+
//here we can also add the command to install the dependencies
81+
console.log(chalk.yellow('Project created successfully'));
82+
console.log(chalk.cyan(commands.setup));
83+
console.log(chalk.green(commands.changes));
84+
//give the command list to run the template
6785
} catch (e) {
6886
console.log(e);
87+
console.log(chalk.red("Couldn't create the project"));
6988
process.exit(1);
7089
}
7190
}
File renamed without changes.

0 commit comments

Comments
 (0)