atualização de biome
This commit is contained in:
parent
8684840a05
commit
b4d005fdae
2 changed files with 98 additions and 1 deletions
96
scripts/atualizar-biome.js
Normal file
96
scripts/atualizar-biome.js
Normal 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lê 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}`);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue