-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
99 lines (88 loc) · 4.1 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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs-extra');
const nodeCMD = require('node-cmd');
const program = require('commander');
const chalk = require('chalk');
const { getInstalledPathSync } = require('get-installed-path')
let projectName;
let currentPath = process.cwd();
const pathOfInstalledModule = getInstalledPathSync('mern-app-generator');
const packageJSON = require('./package.json');
program
.version(packageJSON.version)
.description(packageJSON.version)
.usage('<project-directory>')
.action(name => {
projectName = name;
})
.parse(process.argv);
if (projectName !== undefined) {
fs.mkdirSync(path.join(currentPath, projectName));
let files = fs.readdirSync(path.join(pathOfInstalledModule, 'project'));
files.forEach(file => {
fs.copySync(path.join(pathOfInstalledModule, 'project', file), (path.join(currentPath, projectName, file)));
});
fs.mkdirSync(path.join(currentPath, projectName, 'build'));
fs.mkdirSync(path.join(currentPath, projectName, 'deploy'));
fs.mkdirSync(path.join(currentPath, projectName, 'app', 'server', 'src'));
fs.closeSync(fs.openSync(path.join(currentPath, projectName, '.gitignore'), 'w'));
fs.writeFileSync(path.join(currentPath, projectName, '.gitignore'), `node_modules/\r\n.env\r\ndeploy/\r\nbuild/`, 'utf-8');
fs.closeSync(fs.openSync(path.join(currentPath, projectName, '.env'), 'w'));
fs.writeFileSync(path.join(currentPath, projectName, '.env'), `URL=mongodb://<dbuser>:<dbpassword>@ds<dbID>.mlab.com:<dbPort>/<dbname>`, 'utf-8');
console.log();
console.log(chalk.blue.bgGreenBright(`Your project `) + chalk.black.bgGreenBright(projectName) + chalk.blue.bgGreenBright(' is generated.'));
let data = fs.readFileSync(path.join(currentPath, projectName, 'package.json'), 'utf-8');
data = data.replace("create-mern-app", projectName);
data = data.replace(`"description": "Most simplest way to start and build scalable MERN apps."`, `"description": "That's my ${projectName} project."`)
data = data.replace('"author": "Shahzaib Khalid"', `"author": "Author of ${projectName}"`);
fs.writeFileSync(path.join(currentPath, projectName, 'package.json'), data, 'utf-8');
console.log();
console.log(chalk.blue.bgGreenBright(`Installing necessary node packages in `) + chalk.black.bgGreenBright(projectName) + chalk.blue.bgGreenBright('. Please wait...'));
console.log();
nodeCMD.get(
` cd ${projectName} && npm install
`, (error, data, stderr) => {
console.log(chalk.blue.bgGreenBright(`All packages are successfully installed in '${chalk.black(projectName)}'.`));
printAfterInstallation();
if (error) console.error(error);
});
} else {
console.error(chalk.whiteBright.bgRed(`
Couldn't generate project.
Project name is not specified.
Please specify the project name and try again.
Example: mern-app-generator my-app
`));
}
function printAfterInstallation() {
console.log();
console.log(chalk.blue.bgGreenBright('All set now! You are ready to develop your project.'));
console.log();
console.log();
console.log(chalk.blue.bgGreenBright('Now run the following commands:'));
console.log();
console.log(chalk.blue.bgGreenBright('Getting into your project directory:'));
console.log();
console.log(chalk.black.bgGreenBright(`cd ${projectName}`));
console.log();
console.log(chalk.blue.bgGreenBright('Run client development server:'));
console.log();
console.log(chalk.black.bgGreenBright('npm run client-dev'));
console.log();
console.log(chalk.blue.bgGreenBright('Run back-end development server (ExpressJS server):'));
console.log();
console.log(chalk.black.bgGreenBright('npm run server-dev'));
console.log();
console.log(chalk.blue.bgGreenBright('Build your application for production:'));
console.log();
console.log(chalk.black.bgGreenBright('npm run build'));
console.log();
console.log(chalk.blue.bgGreenBright('Deploy your application for the world to see:'));
console.log();
console.log(chalk.black.bgGreenBright('npm run deploy'));
console.log();
console.log();
console.log(chalk.blue.bgGreenBright('Happy Coding! :)'));
console.log();
}