-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·148 lines (118 loc) · 4.13 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
#!/usr/bin/env node
async function create() {
const spawn = require('cross-spawn');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const pjson = require('./package.json');
console.log('ℹ️ create-nuxt-base started in version ', pjson.version);
const packageInfos = await getPackageData(pjson.name);
if (packageInfos) {
const latestVersion = packageInfos['dist-tags'].latest;
if (latestVersion !== pjson.version) {
console.log('📣 Its a newer version of create-nuxt-base available!', pjson.version);
console.log('Your version', pjson.version);
console.log('Available version', latestVersion);
}
}
const projectName = process.argv[2];
const autoStart = process.argv[3] === 'auto-start';
if (!projectName) {
console.error('Please provide a valid project name.');
process.exit(-1);
}
const currentDir = process.cwd();
const projectDir = path.resolve(currentDir, projectName);
if (fs.existsSync(projectDir)) {
console.log(`❌ Directory with name ${projectName} already exists.`);
process.exit(-1);
}
await copyFiles(__dirname + '/nuxt-base-template', projectDir);
// Copy .env
await copyFiles(__dirname + '/nuxt-base-template/.env.example', projectDir + '/.env');
const projectPackageJson = require(path.join(projectDir, 'package.json'));
// Update the project's package.json with the new project name
projectPackageJson.name = projectName;
fs.writeFileSync(path.join(projectDir, 'package.json'), JSON.stringify(projectPackageJson, null, 2));
process.chdir(projectName);
const gitInit = childProcess.exec('git init');
await waitForChildProcess(gitInit);
console.log('Installing dependencies ...');
const npmInstall = spawn('npm', ['install'], { stdio: 'inherit' });
await waitForSpawn(npmInstall);
const removeGit = spawn('npx', ['rimraf', '.git'], { stdio: 'inherit' });
await waitForSpawn(removeGit);
console.log('Success! Your new project is ready. 🎉');
if (autoStart) {
console.log('Building Project ...');
const npmRunBuild = spawn('npm', ['run', 'build'], { stdio: 'inherit' });
await waitForSpawn(npmRunBuild)
.then(() => console.log('✅ Project was successfully built'))
.catch(() => console.log('❌ Error while building the project'));
console.log(`Created ${projectName} at ${projectDir} 💾`);
console.log(`Launching ${projectName} in: 3 💥`);
await waitForMS(1000);
console.log(`Launching ${projectName} in: 2 🔥`);
await waitForMS(1000);
console.log(`Launching ${projectName} in: 1 🧨`);
await waitForMS(1000);
console.log(`Launching ${projectName} ... 🚀`);
await waitForMS(1000);
const npmRunDev = spawn('npm', ['run', 'start'], { stdio: 'inherit' });
await waitForSpawn(npmRunDev);
}
}
function waitForSpawn(spawn) {
return new Promise((resolve, reject) => {
spawn.on('exit', () => resolve(true));
spawn.on('error', () => reject());
});
}
function waitForMS(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, ms);
});
}
function waitForChildProcess(process) {
return new Promise((resolve, reject) => {
process.stdout.on('data', (data) => {
resolve(data);
});
process.stdout.on('error', (err) => {
reject(err);
});
});
}
async function copyFiles(from, to) {
const fse = require('fs-extra');
try {
await fse.copy(from, to);
console.log('Copied nuxt base template successfully!');
} catch (err) {
console.error(err);
process.exit(-1);
}
}
function getPackageData(packageName) {
const https = require('https');
return new Promise((resolve, reject) => {
https
.get('https://registry.npmjs.org/' + packageName, (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(JSON.parse(data));
});
})
.on('error', (err) => {
reject(err);
});
});
}
create();