-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
225 lines (202 loc) · 9.48 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env node
'use strict';
const inquirer = require('inquirer');
const CURR_DIR = process.cwd();
const fs = require('fs');
const childProcess = require('child_process');
const { paramCase } = require('change-case');
const packageInfo = require('./package.json');
const axios = require('axios');
const ora = require('ora');
const chalk = require('chalk');
const moduleGenerator = require('./src/module-generator');
const text = `
███▄ ▄███▓ ▒█████ ▒█████ ███▄ █ ██▓ ██▓ ▄████ ██░ ██ ▄▄▄█████▓
▓██▒▀█▀ ██▒▒██▒ ██▒▒██▒ ██▒ ██ ▀█ █ ▓██▒ ▓██▒ ██▒ ▀█▒▓██░ ██▒▓ ██▒ ▓▒
▓██ ▓██░▒██░ ██▒▒██░ ██▒▓██ ▀█ ██▒▒██░ ▒██▒▒██░▄▄▄░▒██▀▀██░▒ ▓██░ ▒░
▒██ ▒██ ▒██ ██░▒██ ██░▓██▒ ▐▌██▒▒██░ ░██░░▓█ ██▓░▓█ ░██ ░ ▓██▓ ░
▒██▒ ░██▒░ ████▓▒░░ ████▓▒░▒██░ ▓██░░██████▒░██░░▒▓███▀▒░▓█▒░██▓ ▒██▒ ░
░ ▒░ ░ ░░ ▒░▒░▒░ ░ ▒░▒░▒░ ░ ▒░ ▒ ▒ ░ ▒░▓ ░░▓ ░▒ ▒ ▒ ░░▒░▒ ▒ ░░
░ ░ ░ ░ ▒ ▒░ ░ ▒ ▒░ ░ ░░ ░ ▒░░ ░ ▒ ░ ▒ ░ ░ ░ ▒ ░▒░ ░ ░
░ ░ ░ ░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ▒ ░░ ░ ░ ░ ░░ ░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
`;
const QUESTIONS = [
{
name: 'project-name',
type: 'input',
message: 'Project name:',
validate: function(input) {
const inputSanitized = input.trim();
const projectDirectoryName = paramCase(inputSanitized);
if(!(/^([A-Za-z\-_ \d])+$/.test(inputSanitized))) {
return 'Project name may only include letters, numbers, underscores and space.';
// eslint-disable-next-line no-use-before-define
} else if(directoryExistsCheck(projectDirectoryName)) {
return `Directory already exists with name "${projectDirectoryName}"`;
}
return true;
}
},
{
name: 'mongo-uri',
type: 'input',
message: 'MongoDB URI:',
default: (answers) => `mongodb://localhost:27017/${paramCase(answers[ 'project-name' ])}`,
// validate: function(input) {
// if (/^(mongodb(\+srv)?:(?:\/{2})?)((\w+?):(\w+?)@|:?@?)(\w+?):(\d+)\/(\w+?)$/.test(input)) {
// return true;
// }
// return 'Invalid MongoDB URI';
// }
}
];
// eslint-disable-next-line func-style
async function projectGenerator() {
console.log(chalk.blueBright(text));
console.log('::: WELCOME TO CALM API :::');
console.log(`CLI Version: ${packageInfo.version}\n`);
let spinner;
try {
spinner = ora('Checking for new version').start();
const { data: npmInfo } = await axios.get('https://registry.npmjs.org/calmapi');
if( npmInfo && npmInfo[ 'dist-tags' ] ) {
spinner.stop();
if(npmInfo[ 'dist-tags' ][ 'latest' ] !== packageInfo.version) {
console.log(chalk.blueBright(`A newer version ${npmInfo[ 'dist-tags' ][ 'latest' ]} is available. Please update by running "npm i -g calmapi"`));
} else {
console.log(chalk.blueBright('You are using the latest version of calmapi'));
}
}
} catch (e) {
spinner.stop();
}
const answers = await inquirer.prompt(QUESTIONS);
const projectName = answers[ 'project-name' ];
const mongoUri = answers[ 'mongo-uri' ];
const projectDirectoryName = paramCase(projectName);
const templatePath = `${__dirname}/resource/project`;
fs.mkdirSync(`${CURR_DIR}/${projectDirectoryName}`);
// eslint-disable-next-line no-use-before-define
createDirectoryContents(templatePath, projectDirectoryName, projectName, mongoUri);
// eslint-disable-next-line no-use-before-define
fs.writeFileSync(`${CURR_DIR}/${projectDirectoryName}/calmapi.json`, JSON.stringify(getCalmApiJson(), null, 3), 'utf8');
console.log(`:: Setting up : ${projectName}.`);
console.log(':: Installing dependencies...');
// eslint-disable-next-line no-use-before-define
await npmInstall(`${CURR_DIR}/${projectDirectoryName}`);
console.log(':: Setting up git...');
// eslint-disable-next-line no-use-before-define
await gitSetup(`${CURR_DIR}/${projectDirectoryName}`);
console.log(chalk.blueBright(':: Project Setup Complete'));
console.log('\nWhat next?');
console.log('\nGo to the project directory by running');
console.log(chalk.greenBright(`\ncd ${projectDirectoryName}\n`));
console.log('\nStart the app by running');
console.log(chalk.greenBright('\nnpm start\n'));
console.log('\n\nPre Installed Modules\n\n');
console.log('1. Auth: Register, Login, Password Reset, Profile\n');
console.log('1. Post: CRUD\n');
console.log('Edit the .env file located at the root of the project.');
console.log(chalk.blueBright('...::: Thank you for using CALM API :::...'));
}
// eslint-disable-next-line func-style
function createDirectoryContents(templatePath, newProjectPath, projectName, mongoUri) {
const filesToCreate = fs.readdirSync(templatePath);
const skippedFiles = [ '.env', 'package-lock.json' ];
filesToCreate.forEach(file => {
const origFilePath = `${templatePath}/${file}`;
// get stats about the current file
const stats = fs.statSync(origFilePath);
if (stats.isFile()) {
if(skippedFiles.includes(file)) {
return;
}
let contents = fs.readFileSync(origFilePath, 'utf8');
// Rename
if (file === '.npmignore') {
// eslint-disable-next-line no-param-reassign
file = '.gitignore';
}
if(file === 'package.json') {
contents = contents.replace('"name": "calmapi"', `"name": "${newProjectPath}"`);
contents = contents.replace('{{CALMAPI_VERSION}}', packageInfo.version);
}
if(file === '.env.sample') {
contents = contents.replace('{{MONGODB_URI}}', mongoUri);
// eslint-disable-next-line no-param-reassign
file = '.env';
}
if(file === '.gitignore.sample') {
// eslint-disable-next-line no-param-reassign
file = '.gitignore';
}
if(file === '.eslintrc.json.sample') {
// eslint-disable-next-line no-param-reassign
file = '.eslintrc.json';
}
const writePath = `${CURR_DIR}/${newProjectPath}/${file}`;
fs.writeFileSync(writePath, contents, 'utf8');
} else if (stats.isDirectory()) {
fs.mkdirSync(`${CURR_DIR}/${newProjectPath}/${file}`);
// recursive call
createDirectoryContents(`${templatePath}/${file}`, `${newProjectPath}/${file}`, projectName);
}
});
}
// Performs `npm install`
// eslint-disable-next-line func-style
async function npmInstall(where) {
try {
childProcess.execSync('npm install --quiet', { cwd: where, env: process.env, stdio: 'pipe' });
childProcess.execSync('npm ci --quiet', { cwd: where, env: process.env, stdio: 'pipe' });
} catch (e) {
console.error(`Error Installing Packages ${ e.stderr}` ) ;
}
}
// eslint-disable-next-line func-style
async function gitSetup(where) {
try {
childProcess.execSync('git init', { cwd: where, env: process.env, stdio: 'pipe' });
childProcess.execSync('git add .', { cwd: where, env: process.env, stdio: 'pipe' });
childProcess.execSync('git commit -m "Initial Setup"', { cwd: where, env: process.env, stdio: 'pipe' });
} catch (e) {
console.error(`Error Setting up Git ${ e.stderr}` ) ;
}
}
// eslint-disable-next-line func-style
function directoryExistsCheck(projectDirectoryName) {
try {
return fs.existsSync(`${CURR_DIR}/${projectDirectoryName}`);
} catch (e) {
console.log(e);
}
}
// eslint-disable-next-line func-style
function getCalmApiJson() {
return{
'name': 'calmapi',
'version': packageInfo.version
};
}
// eslint-disable-next-line func-style
async function main() {
try {
const argumentsArr = process.argv.slice(2);
if(!argumentsArr.length) {
await projectGenerator();
}else if(argumentsArr.length === 3 && argumentsArr[ 0 ] === 'generate' && argumentsArr[ 1 ] === 'module') {
const isRootFile = fs.readdirSync(CURR_DIR).find(file => file === 'calmapi.json');
if(!isRootFile) {
throw new Error('Please Run inside a calmapi Project.');
}else {
await moduleGenerator(argumentsArr[ 2 ]);
}
}else {
throw new Error('Invalid Command');
}
} catch (error) {
console.log(error.message);
}
}
main();