-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
77 lines (63 loc) · 1.86 KB
/
init.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
/* eslint-disable @typescript-eslint/no-require-imports */
const ora = require('ora');
const rimraf = require('rimraf');
const childProcess = require('child_process');
const cleanup = require('./scripts/cleanup');
const askParams = require('./scripts/ask-params');
async function init() {
await awaitWarning();
await checkDeps();
const params = await askParams();
let promise = cleanup(params);
ora.promise(promise, 'Renomeando projeto...');
await promise;
promise = selfDestruction(params);
ora.promise(promise, 'Auto destruição...');
await promise;
promise = resetGit(params);
ora.promise(promise, 'Resetando Git...');
await promise;
}
async function awaitWarning() {
return new Promise(resolve => {
setTimeout(() => {
console.log('\nPROJECT GENERATOR\n');
resolve();
});
});
}
async function checkDeps() {
await execCommand('yarn -v').catch(() => {
throw new Error('Yarn is required');
});
await execCommand('git --version').catch(() => {
throw new Error('Git is required');
});
}
async function resetGit(params) {
const originalRepo = await execCommand('git remote get-url origin');
await execCommand('git remote remove origin');
await execCommand(`git remote add seed ${originalRepo}`);
if (params.repository) {
await execCommand(`git remote add origin ${params.repository}`);
}
await execCommand('git add . && git commit -am "initial"');
}
async function selfDestruction() {
await new Promise((resolve, reject) => rimraf('./init.js', err => (err ? reject(err) : resolve())));
}
async function execCommand(command) {
return new Promise((resolve, reject) => {
childProcess.exec(command, (err, stdout) => {
err ? reject(err) : resolve((stdout || '').trim());
});
});
}
init()
.then(() => {
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(-1);
});