-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpn.js
204 lines (183 loc) · 7.53 KB
/
cpn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env node
const app = require('commander');
const shell = require('shelljs');
const chalk = require('chalk');
const package = require('./package.json');
const dotenv = require('dotenv');
const cliProgress = require('cli-progress');
const colors = require('ansi-colors');
dotenv.config();
const progressBar = new cliProgress.SingleBar({
format: 'Creation progress |' + colors.cyan('{bar}') + '| {percentage}%',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true
}, cliProgress.Presets.shades_classic);
app.version(package.version);
app
.command('init [projectName]')
.description('Create an project')
.option('-js, --javascript', 'Indicates that the API will be in JavaScript')
.option('-ts, --typescript', 'Indicates that the API will be in TypeScript')
.action(async (projectName, options) => {
progressBar.start(100, 0);
if (!projectName) {
shell.exec(`clear`);
app.error('[ERROR]:: Enter the project name');
return;
}
const folder = shell.exec(`mkdir ${projectName}`, { silent: true });
if(folder.code) {
shell.exec(`clear`);
app.error(chalk.red('[ERROR]:: the folder with the same name already exists in the location'));
return;
}
progressBar.update(5);
shell.cd(projectName);
progressBar.update(15);
const dirProject = shell.pwd().stdout;
progressBar.update(20);
shell.exec(`yarn init -y`, { silent: true });
progressBar.update(25);
// Dependencies
shell.exec(`yarn add express`, { silent: true });
progressBar.update(30);
shell.exec(`yarn add cors`, { silent: true });
progressBar.update(35);
shell.exec(`yarn add dotenv`, { silent: true });
progressBar.update(40);
if(options.javascript) {
// Dev Dependencies
shell.exec(`yarn add sucrase nodemon eslint prettier -D`, { silent: true });
progressBar.update(45);
shell.exec(`yarn add eslint-config-prettier eslint-plugin-prettier -D`, { silent: true });
progressBar.update(50);
shell.exec(`sed -i '6i\ "scripts": {\' package.json`);
progressBar.update(55);
shell.exec(`sed -i '7i\ "dev": "nodemon src/server.js",\' package.json`);
progressBar.update(60);
shell.exec(`sed -i '8i\ "dev:debug": "nodemon --inspect src/server.js",\' package.json`);
progressBar.update(65);
shell.exec(`sed -i '9i\ "start": "node -r sucrase/register src/server.js"\' package.json`);
progressBar.update(70);
shell.exec(`sed -i '10i\ },\' package.json`);
progressBar.update(75);
if (process.env.NODE_ENV === "DEV") {
shell.exec(`cp -r $CPN_DIR_DEV/src/models/JS/* ${dirProject}`);
progressBar.update(80);
shell.exec(`cp $CPN_DIR_DEV/src/models/JS/.editorconfig ${dirProject}`);
progressBar.update(81);
shell.exec(`cp $CPN_DIR_DEV/src/models/JS/.env.example ${dirProject}`);
progressBar.update(82);
shell.exec(`cp $CPN_DIR_DEV/src/models/JS/.eslintrc.js ${dirProject}`);
progressBar.update(83);
shell.exec(`cp $CPN_DIR_DEV/src/models/JS/.gitignore ${dirProject}`);
progressBar.update(84);
shell.exec(`cp $CPN_DIR_DEV/src/models/JS/.prettierrc ${dirProject}`);
progressBar.update(85);
shell.exec(`cp $CPN_DIR_DEV/src/models/JS/nodemon.json ${dirProject}`);
progressBar.update(90);
} else {
shell.exec(`cp -r $CPN_DIR/src/models/JS/* ${dirProject}`);
progressBar.update(80);
shell.exec(`cp $CPN_DIR/src/models/JS/.editorconfig ${dirProject}`);
progressBar.update(81);
shell.exec(`cp $CPN_DIR/src/models/JS/.env.example ${dirProject}`);
progressBar.update(82);
shell.exec(`cp $CPN_DIR/src/models/JS/.eslintrc.js ${dirProject}`);
progressBar.update(83);
shell.exec(`cp $CPN_DIR/src/models/JS/.gitignore ${dirProject}`);
progressBar.update(84);
shell.exec(`cp $CPN_DIR/src/models/JS/.prettierrc ${dirProject}`);
progressBar.update(85);
shell.exec(`cp $CPN_DIR/src/models/JS/nodemon.json ${dirProject}`);
progressBar.update(90);
}
} else {
// Dev Dependencies
shell.exec(`yarn add nodemon ts-node typescript -D`, { silent: true });
progressBar.update(45);
shell.exec(`yarn add @types/node @types/express -D`, { silent: true });
progressBar.update(50);
shell.exec(`sed -i '6i\ "scripts": {\' package.json`);
progressBar.update(55);
shell.exec(`sed -i '7i\ "dev": "nodemon",\' package.json`);
progressBar.update(60);
shell.exec(`sed -i '8i\ "build": "npx tsc"\' package.json`);
progressBar.update(65);
shell.exec(`sed -i '9i\ },\' package.json`);
progressBar.update(70);
if (process.env.NODE_ENV === "DEV") {
shell.exec(`cp -r $CPN_DIR_DEV/src/models/TS/* ${dirProject}`);
progressBar.update(75);
shell.exec(`cp $CPN_DIR_DEV/src/models/TS/.editorconfig ${dirProject}`);
progressBar.update(80);
shell.exec(`cp $CPN_DIR_DEV/src/models/TS/.env.example ${dirProject}`);
progressBar.update(82);
shell.exec(`cp $CPN_DIR_DEV/src/models/TS/.gitignore ${dirProject}`);
progressBar.update(84);
shell.exec(`cp $CPN_DIR_DEV/src/models/TS/nodemon.json ${dirProject}`);
progressBar.update(85);
shell.exec(`cp $CPN_DIR_DEV/src/models/TS/tsconfig.json ${dirProject}`);
progressBar.update(90);
} else {
shell.exec(`cp -r $CPN_DIR/src/models/TS/* ${dirProject}`);
progressBar.update(75);
shell.exec(`cp $CPN_DIR/src/models/TS/.editorconfig ${dirProject}`);
progressBar.update(80);
shell.exec(`cp $CPN_DIR/src/models/TS/.env.example ${dirProject}`);
progressBar.update(82);
shell.exec(`cp $CPN_DIR/src/models/TS/.gitignore ${dirProject}`);
progressBar.update(84);
shell.exec(`cp $CPN_DIR/src/models/TS/nodemon.json ${dirProject}`);
progressBar.update(85);
shell.exec(`cp $CPN_DIR/src/models/TS/tsconfig.json ${dirProject}`);
progressBar.update(90);
}
}
shell.touch('README.md');
progressBar.update(91);
shell.exec(`echo "# ${projectName}" >> README.md`);
progressBar.update(92);
// git
shell.exec(`git init`, { silent: true });
progressBar.update(93);
shell.exec(`git add .`, { silent: true });
progressBar.update(94);
shell.exec(`git commit -m "initial commit"`, { silent: true });
progressBar.update(95);
shell.exec(`git branch -m master main`, { silent: true });
progressBar.update(100);
progressBar.stop();
// shell.exec(`clear`);
console.log("To start your application:\n");
console.log(`cd ${projectName}\n`);
console.log("Create the file .dotenv with the content of .env.example and set variable 'PORT'\n");
console.log("Run Your project:");
console.log("yarn dev");
console.log("or");
console.log("yarn start\n");
console.log("will be running on: http://localhost:PORT/api/\n");
console.log("Created by Victor Geruso");
});
app
.command('update')
.description('Update the cpn')
.action(async () => {
if (process.env.NODE_ENV === "DEV") {
shell.exec('$CPN_DIR_DEV/src/update.sh');
} else {
shell.exec('$CPN_DIR/src/update.sh');
}
});
app
.command('uninstall')
.description('uninstall the cpn')
.action(async () => {
if (process.env.NODE_ENV === "DEV") {
shell.exec('$CPN_DIR_DEV/src/uninstall.sh');
} else {
shell.exec('$CPN_DIR/src/uninstall.sh');
}
});
app.parse(process.argv);