-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (69 loc) · 2.18 KB
/
index.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
import { exec } from 'node:child_process';
import fs from 'fs/promises';
import chalk from 'chalk';
import {
DEPENDENCIES,
DEV_DEPENDENCIES,
ROOT_FILES,
ROOT_FOLDERS,
SRC_FILES,
SRC_FOLDERS
} from './constant.js';
import cliProgress from 'cli-progress';
export default class NodeKickStart {
constructor(path, name) {
this._path = path,
this._name = name,
this._projectPath = `${this._path}/${this._name}`;
}
async execute() {
try {
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
progressBar.start(200, 0);
await this.initialseProject();
progressBar.update(50);
await this.installDependencies();
progressBar.update(100);
await this.createFiles();
progressBar.update(150);
await this.writeStarterCode();
progressBar.update(200);
progressBar.stop();
} catch (err) {
console.log(chalk.red(err.message));
process.exit(0);
}
}
async initialseProject() {
await Promise.all([
exec(`mkdir ${this._projectPath}`),
exec(`cd ${this._projectPath};npm init -y`),
]);
}
async installDependencies() {
exec(`
cd ${this._projectPath};
npm i ${DEPENDENCIES.join(" ")};
npm i -D ${DEV_DEPENDENCIES.join(" ")}
`);
}
async createFiles() {
await this.createRootFilesAndFolders();
await this.createSrcFilesAndFolders();
}
async createRootFilesAndFolders() {
await Promise.all([
exec(`cd ${this._projectPath}; touch ${ROOT_FILES.join(" ")}`),
exec(`cd ${this._projectPath}; mkdir ${ROOT_FOLDERS.join(" ")}`),
]);
}
async createSrcFilesAndFolders() {
await Promise.all([
exec(`cd ${this._projectPath}/src; touch ${SRC_FILES.join(" ")}`),
exec(`cd ${this._projectPath}/src; mkdir ${SRC_FOLDERS.join(" ")}`),
]);
}
async writeStarterCode() {
await fs.copyFile('./sample-code/server.js', this._projectPath + '/src/server.js');
}
}