-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
170 lines (163 loc) · 5.45 KB
/
generator.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
const prompts = require('prompts');
const fs = require("fs");
const {execSync} = require('child_process');
const unpack = require('tar-pack').unpack;
const path = require('path');
const request = require('request');
const envs = ["development", "production"];
const questions = [
{
type: 'text',
name: 'projectName',
message: 'Enter project name',
validate: function (value) {
if (value.length > 0) {
return true;
}
return "Please enter a valid project name.";
}
},{
type: 'text',
name: 'portNumber',
message: 'Enter port number',
initial: 8080,
validate: function (value) {
value = parseInt(value);
if (/^[0-9]{1,5}$/.test(value) && value >= 0 && value <= 65535) {
return true;
}
return "Please enter a valid port number.";
}
},{
type: 'text',
name: 'dbName',
message: 'Enter database name',
validate: function (value) {
if (value.length > 0) {
return true;
}
return "Please enter a valid database name.";
}
},
{
type: 'confirm',
name: 'enableClustering',
message: 'Do you want to enable clustering through child_process module?',
default: false
},
{
type: 'confirm',
name: 'enableDebugLogs',
message: 'Do you want to enable Debug logs for the app?',
default: false
},
{
type: 'confirm',
name: 'enableAccessLogs',
message: 'Do you want to enable Access logs for the app?',
default: false
},
{
type: 'select',
name: 'env',
message: 'Please select environment to set',
choices: envs
}
];
const checkIfDirExistsOrEmpty = (root) => {
try {
const files = fs.readdirSync(root);
return { exists: true, isEmpty: files.length === 0 };
} catch (e) {
// Directory doesn't exists, OK to continue
return { exists: false };
}
}
const downloadAndUnPackKit = (root, templateToInstall, templateFormat, templateHost, templateHostUser) => {
return new Promise((resolve, reject)=>{
request({
uri: `https://${templateHost}/repos/${templateHostUser}/${templateToInstall}/${templateFormat}/master`,
headers: {
'User-Agent': templateToInstall
}
}).pipe(unpack(root, (err)=>{
if (err) {
return reject(err);
}
resolve();
}));
});
}
const createApp = (root, appName, templateToInstall, templateFormat, templateHost, templateHostUser) => {
const dirInfo = checkIfDirExistsOrEmpty(root);
if (dirInfo.exists && !dirInfo.isEmpty) {
// directory already exists and is not empty, throw error
throw new Error(`Directory with name '${appName}' already exists. Please specify some other project name`);
}
// create dir if doesn't exists
if (!dirInfo.exists) {
fs.mkdirSync(root);
}
// directory is empty, OK to continue
return downloadAndUnPackKit(root, templateToInstall, templateFormat, templateHost, templateHostUser);
}
const getPackageJsonContents = (appName, path) => {
const packageJson = require(path);
return {
name: appName,
version: "1.0.0",
main: packageJson.main,
scripts: packageJson.scripts,
dependencies: packageJson.dependencies,
devDependencies: packageJson.devDependencies,
}
}
const installNpmPackages = (root) => {
console.log("Installing dependencies...");
execSync(`cd "${root}" && npm i`, {stdio: 'inherit'});
console.log("Dependencies installed.");
}
const addDotEnv = (root, data) => {
const envPath = path.join(root, ".env");
fs.writeFileSync(envPath, `PORT=${data.portNumber}
ENV="${envs[parseInt(data.env)]}"
IS_CLUSTERING_ENABLED=${data.enableClustering}
ENABLE_DEBUG_LOGS=${data.enableDebugLogs}
ENABLE_ACCESS_LOGS=${data.enableAccessLogs}
SECRET="some secret"
MONGO_URI="mongodb://localhost:27017/${data.dbName}"`);
}
(async () => {
try {
const response = await prompts(questions);
const root = path.resolve(response.projectName);
const appName = path.basename(root);
const templateToInstall = "node-mongo-starter-kit";
const templateFormat = "tarball";
const templateHost = "api.github.com";
const templateHostUser = "habilelabs";
console.log();
console.log("Note:- These settings can be changed later in .env file");
console.log();
console.log("Creating app...");
await createApp(root, appName, templateToInstall, templateFormat, templateHost, templateHostUser);
console.log("Created folder structure.");
const pjPath = path.join(root, "package.json");
const contents = getPackageJsonContents(appName, pjPath);
fs.writeFileSync(pjPath, JSON.stringify(contents, null, 4));
addDotEnv(root, response);
console.log("Created '.env' file");
installNpmPackages(root);
console.log("App created.");
console.log();
console.log(`Start your app by moving into the "${appName}" directory and running
npm start`);
console.log();
console.log("NOTE:- Don't forget to start mongoDB :)");
console.log();
console.log("Visit https://github.com/habilelabs/node-mongo-starter-kit for more information on this starter kit.");
} catch (err) {
console.log(err.message);
process.exit(1);
}
})();