-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
180 lines (154 loc) · 4.22 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
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
/* eslint-disable @typescript-eslint/no-require-imports */
const fs = require('fs');
const inquirer = require('inquirer');
const ora = require('ora');
const lodash = require('lodash');
const rimraf = require('rimraf');
const childProcess = require('child_process');
async function init() {
await awaitWarning();
await checkYarn();
const params = await askParams();
let promise = cleanup(params);
ora.promise(promise, 'Renomeando projeto...');
await promise;
if (params.delete) {
promise = removePackages(params);
ora.promise(promise, 'Limpando dependencias...');
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 checkYarn() {
await execCommand('yarn -v').catch(() => {
throw new Error('Yarn is required');
});
}
async function askParams(answers = {}) {
const params = await inquirer.prompt([
{
name: 'project',
default: answers.project,
message: 'Nome do projeto',
validate: i => (i.length >= 3 ? true : 'Pelo menos 3 letras')
},
{
name: 'repository',
default: answers.repository,
message: 'Repositorio'
},
{
name: 'delete',
type: 'confirm',
message: 'Remover arquivos e dependencias de inicialização?'
},
{
name: 'confirmed',
type: 'confirm',
message: 'Confirma as configurações?'
}
]);
if (!params.confirmed) {
console.log('---- Responda novamente:');
return askParams(params);
}
return {
...params,
slug: lodash.kebabCase(params.project.endsWith('-api') ? params.project : `${params.project}-api`).toLowerCase()
};
}
async function cleanup(params) {
await replaceContent('./package.json', [
{
from: 'kinja-base',
to: params.slug
},
{
from: /kinja\/api-base/gi,
to: `kinja/${params.slug}`
},
{
from: 'kinja-repository',
to: params.repository
}
]);
await replaceContent('./docker-compose.yml', [
{
from: 'kinja-api',
to: params.slug
},
{
from: 'kinja-database',
to: params.slug.replace('-api', '-database')
},
{
from: 'POSTGRES_DB=kinja',
to: `POSTGRES_DB=${lodash.camelCase(params.slug.replace('-api', ''))}`
},
{
from: 'DATABASE_DB=kinja',
to: `DATABASE_DB=${lodash.camelCase(params.slug.replace('-api', ''))}`
}
]);
await replaceContent('./src/index.ts', [
{
from: 'Kinja API',
to: params.project
}
]);
fs.copyFileSync('./.env.example', './.env');
}
async function replaceContent(file, replacers) {
let content = await new Promise((resolve, reject) =>
fs.readFile(file, 'utf8', (err, data) => (err ? reject(err) : resolve(data)))
);
for (let replacer of replacers) {
content = content.replace(replacer.from, replacer.to);
}
await new Promise((resolve, reject) =>
fs.writeFile(file, content, (err, data) => (err ? reject(err) : resolve(data)))
);
}
async function removePackages() {
await execCommand('yarn remove inquirer ora rimraf');
}
async function resetGit(params) {
if (params.repository) {
const originalRepo = await execCommand('git remote get-url origin');
await execCommand('git remote remove origin');
await execCommand(`git remote add seed ${originalRepo}`);
await execCommand(`git remote add origin ${params.repository}`);
}
await execCommand('git add . && git commit -am "Initial Setup" --no-verify');
await execCommand('git branch development');
await execCommand('git branch homolog');
}
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, std) => (err ? reject(err) : resolve(std)));
});
}
init()
.then(() => {
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(-1);
});