-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.ts
More file actions
173 lines (148 loc) · 6.03 KB
/
Copy pathindex.ts
File metadata and controls
173 lines (148 loc) · 6.03 KB
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
import { execSync } from 'child_process';
import { async as glob } from 'fast-glob';
import sistemaArquivos from 'fs';
import caminho from 'path';
import safeStringify from 'safe-stable-stringify';
export function criarDiretorioAplicacao(nomeAplicacao: string): string {
const caminhoDiretorioProjeto = process.cwd() + caminho.sep + nomeAplicacao;
const diretorioJaExiste = sistemaArquivos.existsSync(nomeAplicacao)
if (!diretorioJaExiste) {
sistemaArquivos.mkdirSync(nomeAplicacao);
console.log(`Diretório criado: ${caminhoDiretorioProjeto}`);
} else console.log(`Diretório já existe: ${caminhoDiretorioProjeto}`);
return caminhoDiretorioProjeto;
}
export async function copiarArquivosDeExemploParaNovoProjeto(
nomeProjeto: string,
tipoDeProjeto: string,
linguagemDeBackEnd: string,
diretorioProjeto: string
) {
const diretorioExemplos = caminho.join(
__dirname, `../exemplos/${linguagemDeBackEnd}/` + tipoDeProjeto
);
const formatoGlob =
(diretorioExemplos + '/**/*.{delegua,pitu,delprops,foles,lmht,md}')
.replace(/\\/gi, '/');
const caminhosArquivos = await glob([formatoGlob], {
dot: true,
absolute: false,
stats: false,
});
return Promise.all(
caminhosArquivos.map(async (caminhoArquivo) => {
const caminhoArquivoResolvido = caminho.resolve(caminhoArquivo);
const novoCaminhoArquivo = caminhoArquivoResolvido.replace(
diretorioExemplos,
diretorioProjeto
);
await sistemaArquivos.promises.mkdir(
caminho.dirname(novoCaminhoArquivo),
{ recursive: true }
)
if (novoCaminhoArquivo.endsWith('configuracao.delprops')) {
let codigoConfiguracaoDelegua = await sistemaArquivos.promises.readFile(
caminhoArquivoResolvido,
'utf-8'
);
codigoConfiguracaoDelegua = codigoConfiguracaoDelegua.replace(
"'Minha aplicação'",
`'${nomeProjeto}'`
);
return sistemaArquivos.promises.writeFile(
novoCaminhoArquivo,
codigoConfiguracaoDelegua
);
} else {
return sistemaArquivos.promises.copyFile(
caminhoArquivoResolvido,
novoCaminhoArquivo
);
}
})
);
}
export async function gerarRepositorioGit(
inicializarRepositorioGit: boolean,
diretorioProjeto: string,
) {
if (inicializarRepositorioGit) {
execSync('git init', { cwd: diretorioProjeto });
execSync(`git config --global --add safe.directory "${diretorioProjeto}"`);
const conteudoGitIgnore = 'node_modules/\ndist/\nbuild/\n.env\n.env.local\n.env.development\n.env.production\ncoverage/\n*.log\nnpm-debug.log*\nyarn-debug.log*\nyarn-error.log*\n.DS_Store\nThumbs.db';
await sistemaArquivos.promises.writeFile(
`${diretorioProjeto}/.gitignore`,
conteudoGitIgnore
);
execSync('git config user.email "liquido@designliquido.com.br"', { cwd: diretorioProjeto });
execSync('git config user.name "Liquido"', { cwd: diretorioProjeto });
execSync('git add .', { cwd: diretorioProjeto });
execSync('git commit -m "Versionamento Inicial"', { cwd: diretorioProjeto });
execSync('git config --unset user.email', { cwd: diretorioProjeto });
execSync('git config --unset user.name', { cwd: diretorioProjeto });
}
}
export async function detectarGerenciadorDePacotes(
gerenciadorDePacotes: string,
diretorioProjeto: string
) {
const caminhoPackageJson = caminho.join(diretorioProjeto, 'package.json');
switch (gerenciadorDePacotes) {
case 'npm': {
execSync('npm init -y', { cwd: diretorioProjeto });
execSync('npm install liquido@latest', { cwd: diretorioProjeto });
break;
}
case 'yarn': {
execSync('yarn init -y', { cwd: diretorioProjeto });
execSync('yarn add liquido@latest', { cwd: diretorioProjeto });
break;
}
case 'bun': {
if (!sistemaArquivos.existsSync(caminhoPackageJson)) {
const conteudoPackageJson = {
name: caminho.basename(diretorioProjeto),
version: '1.0.0',
private: true,
dependencies: {}
};
await sistemaArquivos.promises.writeFile(
caminhoPackageJson,
safeStringify(conteudoPackageJson, null, 2) + '\n'
);
}
execSync('bun add liquido@latest', { cwd: diretorioProjeto });
break;
}
}
await adicionarScriptsLiquido(caminhoPackageJson);
}
async function adicionarScriptsLiquido(caminhoPackageJson: string) {
const packageJson = JSON.parse(
await sistemaArquivos.promises.readFile(caminhoPackageJson, 'utf8')
);
packageJson.scripts ??= {};
packageJson.scripts.liquido = 'node ./node_modules/liquido/index.js';
await sistemaArquivos.promises.writeFile(
caminhoPackageJson,
safeStringify(packageJson, null, 2) + '\n'
);
}
/** Valida se o tipo de projeto informado é 'mvc' ou 'api-rest'. */
export function validarTipoProjeto(
tipo: string | undefined
): tipo is 'mvc' | 'api-rest' {
return tipo === 'mvc' || tipo === 'api-rest';
}
/** Valida se a linguagem informada é 'delegua' ou 'pitugues'. */
export function validarLinguagem(
linguagem: string | undefined
): linguagem is 'delegua' | 'pitugues' {
return linguagem === 'delegua' || linguagem === 'pitugues';
}
/** Valida se o gerenciador de pacotes informado é 'npm', 'yarn' ou 'bun'. */
export function validarGerenciadorDePacotes(
gerenciador: string | undefined
): gerenciador is 'npm' | 'yarn' | 'bun' {
return gerenciador === 'npm' || gerenciador === 'yarn' || gerenciador === 'bun';
}