forked from hagopj13/node-express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createNodejsApp.js
111 lines (97 loc) · 2.99 KB
/
createNodejsApp.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
#!/usr/bin/env node
const util = require('util');
const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
// Utility functions
const exec = util.promisify(require('child_process').exec);
async function runCmd(command) {
try {
const { stdout, stderr } = await exec(command);
console.log(stdout);
console.log(stderr);
} catch {
(error) => {
console.log(error);
};
}
}
async function hasYarn() {
try {
await execSync('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
// Validate arguments
if (process.argv.length < 3) {
console.log('Please specify the target project directory.');
console.log('For example:');
console.log(' npx create-nodejs-app my-app');
console.log(' OR');
console.log(' npm init nodejs-app my-app');
process.exit(1);
}
// Define constants
const ownPath = process.cwd();
const folderName = process.argv[2];
const appPath = path.join(ownPath, folderName);
const repo = 'https://github.com/hagopj13/node-express-boilerplate.git';
// Check if directory already exists
try {
fs.mkdirSync(appPath);
} catch (err) {
if (err.code === 'EEXIST') {
console.log('Directory already exists. Please choose another name for the project.');
} else {
console.log(error);
}
process.exit(1);
}
async function setup() {
try {
// Clone repo
console.log(`Downloading files from repo ${repo}`);
await runCmd(`git clone --depth 1 ${repo} ${folderName}`);
console.log('Cloned successfully.');
console.log('');
// Change directory
process.chdir(appPath);
// Install dependencies
const useYarn = await hasYarn();
console.log('Installing dependencies...');
if (useYarn) {
await runCmd('yarn install');
} else {
await runCmd('npm install');
}
console.log('Dependencies installed successfully.');
console.log();
// Copy envornment variables
fs.copyFileSync(path.join(appPath, '.env.example'), path.join(appPath, '.env'));
console.log('Environment files copied.');
// Delete .git folder
await runCmd('npx rimraf ./.git');
// Remove extra files
fs.unlinkSync(path.join(appPath, 'CHANGELOG.md'));
fs.unlinkSync(path.join(appPath, 'CODE_OF_CONDUCT.md'));
fs.unlinkSync(path.join(appPath, 'CONTRIBUTING.md'));
fs.unlinkSync(path.join(appPath, 'bin', 'createNodejsApp.js'));
fs.rmdirSync(path.join(appPath, 'bin'));
if (!useYarn) {
fs.unlinkSync(path.join(appPath, 'yarn.lock'));
}
console.log('Installation is now complete!');
console.log();
console.log('We suggest that you start by typing:');
console.log(` cd ${folderName}`);
console.log(useYarn ? ' yarn dev' : ' npm run dev');
console.log();
console.log('Enjoy your production-ready Node.js app, which already supports a large number of ready-made features!');
console.log('Check README.md for more info.');
} catch (error) {
console.log(error);
}
}
setup();