atualização de biome

This commit is contained in:
Luiz Silva 2026-04-15 09:45:25 -03:00
parent 8684840a05
commit b4d005fdae
2 changed files with 98 additions and 1 deletions

View file

@ -17,7 +17,8 @@
"check": "pnpm run biome && npx tsc --noEmit", "check": "pnpm run biome && npx tsc --noEmit",
"build": "npm --no-git-tag-version version minor && pnpm run biome && tsup --config ./tsup/tsup.config.ts && pnpm run pacote", "build": "npm --no-git-tag-version version minor && pnpm run biome && tsup --config ./tsup/tsup.config.ts && pnpm run pacote",
"teste": "npx vitest run src/testes/TipagemRotas.test.ts", "teste": "npx vitest run src/testes/TipagemRotas.test.ts",
"pacote": "npm pack && npm pack && mv $(npm pack --silent) pacote.tgz" "pacote": "npm pack && npm pack && mv $(npm pack --silent) pacote.tgz",
"postinstall": "node ./scripts/atualizar-biome.js"
}, },
"author": { "author": {
"name": "AZTECA SOFTWARE LTDA", "name": "AZTECA SOFTWARE LTDA",

View file

@ -0,0 +1,96 @@
const fs = require('node:fs');
const path = require('node:path');
const initCwd = process.env.INIT_CWD;
// Se não tiver INIT_CWD, não foi chamado pelo npm/pnpm install
if (!initCwd) {
process.exit(0);
}
// O próprio p-comuns não precisa se atualizar na sua pasta interna
if (initCwd === process.cwd()) {
process.exit(0);
}
const me = require('../package.json');
const biomeVersion = me.devDependencies && me.devDependencies['@biomejs/biome'];
if (!biomeVersion) {
process.exit(0);
}
/**
* os arquivos do diretório em busca de package.json
*/
function findPackageJson(dir) {
let results = [];
try {
const list = fs.readdirSync(dir);
for (const file of list) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
// Ignora pastas que não devemos varrer
if (
![
'node_modules',
'.git',
'dist',
'build',
'.nuxt',
'.output',
'.vscode',
'.idea',
].includes(file)
) {
results = results.concat(findPackageJson(filePath));
}
} else if (file === 'package.json') {
results.push(filePath);
}
}
} catch (err) {
// ignora pastas sem acesso
}
return results;
}
const files = findPackageJson(initCwd);
let updated = 0;
for (const file of files) {
try {
const content = fs.readFileSync(file, 'utf8');
// Detecta se usa tab ou espaço
const indent = content.match(/^[ \t]+/m) ? content.match(/^[ \t]+/m)[0] : '\t';
const json = JSON.parse(content);
let changed = false;
const updateDeps = (tipo) => {
if (json[tipo] && json[tipo]['@biomejs/biome']) {
if (json[tipo]['@biomejs/biome'] !== biomeVersion) {
json[tipo]['@biomejs/biome'] = biomeVersion;
changed = true;
}
}
};
updateDeps('dependencies');
updateDeps('devDependencies');
updateDeps('peerDependencies');
if (changed) {
fs.writeFileSync(file, JSON.stringify(json, null, indent) + '\n', 'utf8');
updated++;
console.log(`[p-comuns] @biomejs/biome atualizado para ${biomeVersion} em: ${path.relative(initCwd, file)}`);
}
} catch (e) {
console.error(`[p-comuns] Erro ao atualizar ${file}: ${e.message}`);
}
}
if (updated > 0) {
console.log(`[p-comuns] Total de package.json atualizados: ${updated}`);
}