96 lines
2.2 KiB
JavaScript
96 lines
2.2 KiB
JavaScript
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}`);
|
|
}
|