Skip to content

Commit 7c98b59

Browse files
authored
Evita arquivos desnecessarios ao usar Bun (#84)
Co-authored-by: LubuSeb <187313664+LubuSeb@users.noreply.github.com>
1 parent 9fdee9a commit 7c98b59

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

fontes/interface-linha-comando/novo/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ export async function detectarGerenciadorDePacotes(
101101
gerenciadorDePacotes: string,
102102
diretorioProjeto: string
103103
) {
104+
const caminhoPackageJson = caminho.join(diretorioProjeto, 'package.json');
105+
104106
switch (gerenciadorDePacotes) {
105107
case 'npm': {
106108
execSync('npm init -y', { cwd: diretorioProjeto });
@@ -113,7 +115,20 @@ export async function detectarGerenciadorDePacotes(
113115
break;
114116
}
115117
case 'bun': {
116-
execSync('bun init -y', { cwd: diretorioProjeto });
118+
if (!sistemaArquivos.existsSync(caminhoPackageJson)) {
119+
const conteudoPackageJson = {
120+
name: caminho.basename(diretorioProjeto),
121+
version: '1.0.0',
122+
private: true,
123+
dependencies: {}
124+
};
125+
126+
await sistemaArquivos.promises.writeFile(
127+
caminhoPackageJson,
128+
JSON.stringify(conteudoPackageJson, null, 2) + '\n'
129+
);
130+
}
131+
117132
execSync('bun add liquido@latest', { cwd: diretorioProjeto });
118133
break;
119134
}

testes/liquido.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,70 @@ describe('Liquido', () => {
370370
);
371371

372372
});
373+
374+
it('Deve inicializar projeto com Bun sem criar arquivos desnecessários', async () => {
375+
jest.clearAllMocks();
376+
const caminhoDiretorioBun = caminho.join(
377+
process.cwd(),
378+
'teste-comando-novo-bun'
379+
);
380+
381+
(ChildProcess.execSync as jest.Mock).mockImplementation(
382+
() => Buffer.from('')
383+
);
384+
385+
await sistemaArquivos.promises.rm(
386+
caminhoDiretorioBun,
387+
{ recursive: true, force: true }
388+
);
389+
await sistemaArquivos.promises.mkdir(caminhoDiretorioBun);
390+
391+
try {
392+
await detectarGerenciadorDePacotes(
393+
'bun',
394+
caminhoDiretorioBun
395+
);
396+
397+
const conteudoPackageJson = JSON.parse(
398+
await sistemaArquivos.promises.readFile(
399+
`${caminhoDiretorioBun}/package.json`,
400+
'utf-8'
401+
)
402+
);
403+
404+
expect(ChildProcess.execSync).not.toHaveBeenCalledWith(
405+
'bun init -y',
406+
{ cwd: caminhoDiretorioBun }
407+
);
408+
expect(ChildProcess.execSync).toHaveBeenCalledWith(
409+
'bun add liquido@latest',
410+
{ cwd: caminhoDiretorioBun }
411+
);
412+
expect(conteudoPackageJson).toMatchObject({
413+
name: 'teste-comando-novo-bun',
414+
version: '1.0.0',
415+
private: true,
416+
dependencies: {}
417+
});
418+
expect(conteudoPackageJson).not.toHaveProperty(
419+
'peerDependencies.typescript'
420+
);
421+
expect(
422+
sistemaArquivos.existsSync(`${caminhoDiretorioBun}/tsconfig.json`)
423+
).toBeFalsy();
424+
expect(
425+
sistemaArquivos.existsSync(`${caminhoDiretorioBun}/index.ts`)
426+
).toBeFalsy();
427+
expect(
428+
sistemaArquivos.existsSync(`${caminhoDiretorioBun}/README.md`)
429+
).toBeFalsy();
430+
} finally {
431+
await sistemaArquivos.promises.rm(
432+
caminhoDiretorioBun,
433+
{ recursive: true, force: true }
434+
);
435+
}
436+
});
373437

374438

375439
it('Deve usar o nome da pasta quando o nome do projeto é criado com "." ou "./"', async () => {

0 commit comments

Comments
 (0)