-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (166 loc) · 5.38 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
#!/usr/bin/env node
const chalk = require("chalk");
const ora = require("ora");
const axios = require("axios");
const { getInstalledPath } = require("get-installed-path");
const { prompt, Separator } = require("inquirer");
const { type } = require("os");
const { spawn } = require("child_process");
const { promises, createWriteStream } = require("fs");
(async function main() {
let aswr;
let repoCreated = false;
const oraInstace = ora({
stream: process.stdout,
});
const preset = JSON.stringify({
useConfigFiles: true,
cssPreprocessor: 'scss',
plugins: {
'@vue/cli-plugin-babel': {},
'@vue/cli-plugin-eslint': {
config: "airbnb",
lintOn: [
"save",
"commit"
]
},
"@vue/cli-plugin-router": {},
"@vue/cli-plugin-vuex": {}
}
});
async function checkConfig() {
await getInstalledPath("@vue");
if (type() !== "Darwin") throw new Error("Only accessible on macOS for now.");
}
async function questions() {
aswr = await prompt([
{
type: "confirm",
message: chalk`Create new repo at this path ? ${new Separator(process.cwd())}`,
name: "start",
},
{
type: "input",
message: "What is your github account name ?",
name: "githubName",
when: (answer) => !!answer.start,
validate: (answer) => !!answer.length,
},
{
type: "input",
message: "Name of your new repo",
name: "githubRepoName",
when: (answer) => !!answer.start,
validate: (answer) => !!answer.length,
},
{
type: "input",
message: `In order to create a repo we need a token created by github, we don't store any datas. ${new Separator(
chalk`more informations here {cyan https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token}`
)}`,
name: "githubToken",
when: (answer) => !!answer.start,
validate: (answer) => !!answer.length,
},
]);
if (!aswr.start)
throw new Error("Go to the folder where you want create fast-mvp template");
}
async function run() {
oraInstace.start(chalk`{cyan Create new repo with fast-mvp template}`);
const newRepo = await axios.post(
"https://api.github.com/repos/luctst/fast-mvp/generate",
{ owner: aswr.githubName, name: aswr.githubRepoName },
{
headers: {
accept: "application/vnd.github.v3+json",
"User-Agent": aswr.githubName,
Authorization: `token ${aswr.githubToken}`,
},
}
);
repoCreated = true;
return new Promise((resolve, reject) => {
const firstSubProcess = spawn(`git clone ${newRepo.data.ssh_url}`, [], {
cwd: process.cwd(),
shell: true,
});
firstSubProcess
.on("error", (error) => reject(error))
.on("close", (code) => {
if (code === 0) {
const subProcess = spawn(`vue create client -i '${preset}' --merge`, [], {
cwd: `${process.cwd()}/${aswr.githubRepoName}`,
shell: true,
});
subProcess.stdout.setEncoding("utf-8");
subProcess.stdout.on("data", (data) => {
if (oraInstace.isSpinning) {
oraInstace.stopAndPersist();
}
console.log(data);
});
return subProcess
.on("error", async (error) => {
await promises.rm(`${process.cwd()}/${aswr.githubRepoName}`, {
recursive: true,
force: true,
});
reject(error);
})
.on("close", async (code) => {
if (code === 0) {
createWriteStream(`${process.cwd()}/${aswr.githubRepoName}/client/.env`);
createWriteStream(`${process.cwd()}/${aswr.githubRepoName}/api/.env`);
return resolve(
oraInstace.succeed("Done you can now run docker-compose up -d")
);
}
await promises.rm(`${process.cwd()}/${aswr.githubRepoName}`, {
recursive: true,
force: true,
});
reject(new Error("Cannot create vue app with vue-cli."));
});
}
reject(new Error("Cannot clone fast-mvp repo"));
});
});
}
try {
await checkConfig();
await questions();
await run();
} catch (error) {
if (repoCreated) {
oraInstace.text = "Error detected, delete fresh new repo";
await axios.delete(
`https://api.github.com/repos/${aswr.githubName}/${aswr.githubRepoName}`,
{
headers: {
accept: "application/vnd.github.v3+json",
"User-Agent": aswr.githubName,
Authorization: `token ${aswr.githubToken}`,
},
}
);
}
let errorMessage;
if (error.response) {
if (error.response.data.errors) {
errorMessage = error.response.data.errors[0];
} else {
errorMessage = error.response.data.message;
}
} else {
errorMessage = error.message;
}
if (oraInstace.isSpinning) {
oraInstace.fail(chalk`{red ${errorMessage}}`);
return process.exit(-1);
}
process.stderr.write(chalk`{red ${errorMessage}}`);
return process.exit(-1);
}
})();