diff --git a/bin/vitruvio.js b/bin/vitruvio.js index 860382c..f5a31bf 100755 --- a/bin/vitruvio.js +++ b/bin/vitruvio.js @@ -5,11 +5,9 @@ var { Command } = require('commander'); var pkg = require('../package.json'); var initCmd = require('../src/commands/init'); -var updateRepoCmd = require('../src/commands/update-repo'); -var updateBaseCmd = require('../src/commands/update-base'); +var updateCmd = require('../src/commands/update'); var docsCmd = require('../src/commands/docs'); var updateIdeCmd = require('../src/commands/update-ide'); -var updateCliCmd = require('../src/commands/update-cli'); var program = new Command(); @@ -19,10 +17,8 @@ program .version(pkg.version); initCmd.register(program); -updateRepoCmd.register(program); -updateBaseCmd.register(program); +updateCmd.register(program); docsCmd.register(program); updateIdeCmd.register(program); -updateCliCmd.register(program); program.parse(process.argv); diff --git a/src/commands/update-base.js b/src/commands/update-base.js index 24ef92c..bb755cc 100644 --- a/src/commands/update-base.js +++ b/src/commands/update-base.js @@ -9,68 +9,40 @@ var { getPlatformDir } = require('../utils/platform'); var SSH_URL = 'ssh://git@git.davinti.com.br:2222/davinTI/base-claude.git'; var HTTPS_URL = 'https://git.davinti.com.br/davinTI/base-claude.git'; -// Directories inside the platform dir that are fully managed by this command. -// Each of these is replaced in its entirety — files removed from the source -// are also removed locally. Directories not in this list are never touched. var MANAGED_DIRS = ['libs', 'docs', 'examples']; -function register(program) { - program - .command('update-base') - .description('Sincroniza os diretórios gerenciados do diretório de plataforma Vitruvio com o repositório base-claude') - .action(function () { - var platformDir; - try { - platformDir = getPlatformDir(); - } catch (e) { - console.error('Erro: ' + e.message); - process.exit(1); +function run() { + var platformDir; + try { + platformDir = getPlatformDir(); + } catch (e) { + throw new Error(e.message); + } + + console.log(' Diretório de plataforma: ' + platformDir); + console.log(' Buscando repositório base-claude...'); + + var tmpDir = cloneWithFallback(SSH_URL, HTTPS_URL); + + try { + fse.ensureDirSync(platformDir); + + MANAGED_DIRS.forEach(function (dirName) { + var src = path.join(tmpDir, dirName); + var dest = path.join(platformDir, dirName); + + if (!fs.existsSync(src)) { + console.log(' ' + dirName + '/ — não encontrado no repositório'); + return; } - console.log('Diretório de plataforma: ' + platformDir); - console.log('Buscando repositório base-claude...'); - - var tmpDir = null; - try { - tmpDir = cloneWithFallback(SSH_URL, HTTPS_URL); - } catch (e) { - console.error('Erro: não foi possível clonar o repositório base-claude.'); - console.error(e.message); - process.exit(1); - } - - try { - fse.ensureDirSync(platformDir); - - var results = []; - - MANAGED_DIRS.forEach(function (dirName) { - var src = path.join(tmpDir, dirName); - var dest = path.join(platformDir, dirName); - - if (!fs.existsSync(src)) { - results.push({ dir: dirName, status: 'não encontrado no repositório' }); - return; - } - - // Full replacement: remove existing dir, copy fresh from source - if (fs.existsSync(dest)) { - fse.removeSync(dest); - } - fse.copySync(src, dest); - results.push({ dir: dirName, status: 'atualizado' }); - }); - - console.log(''); - results.forEach(function (r) { - console.log(' ' + r.dir + '/ — ' + r.status); - }); - console.log(''); - console.log('Atualização concluída.'); - } finally { - cleanupTemp(tmpDir); - } + if (fs.existsSync(dest)) fse.removeSync(dest); + fse.copySync(src, dest); + console.log(' ' + dirName + '/ — atualizado'); }); + } finally { + cleanupTemp(tmpDir); + } } -module.exports = { register: register }; +module.exports = { run: run }; diff --git a/src/commands/update-cli.js b/src/commands/update-cli.js index 6ae811f..bfd83ae 100644 --- a/src/commands/update-cli.js +++ b/src/commands/update-cli.js @@ -12,48 +12,24 @@ var HTTPS_URL = 'https://git.davinti.com.br/davinTI/vitruvio-cli.git'; var INSTALL_DIR = path.join(os.homedir(), '.local', 'share', 'vitruvio-cli'); -function register(program) { - program - .command('update-cli') - .description('Atualiza o Vitruvio CLI para a versão mais recente') - .action(function () { - console.log('Buscando versão mais recente do Vitruvio CLI...'); +function run() { + console.log(' Buscando versão mais recente do Vitruvio CLI...'); - var tmpDir = null; - try { - tmpDir = cloneWithFallback(SSH_URL, HTTPS_URL); - } catch (e) { - console.error('Erro: não foi possível baixar o repositório do CLI.'); - console.error(e.message); - process.exit(1); - } + var tmpDir = cloneWithFallback(SSH_URL, HTTPS_URL); - try { - // Replace stable install dir with fresh clone - if (fs.existsSync(INSTALL_DIR)) { - fse.removeSync(INSTALL_DIR); - } - fse.copySync(tmpDir, INSTALL_DIR); - } finally { - cleanupTemp(tmpDir); - } + try { + if (fs.existsSync(INSTALL_DIR)) fse.removeSync(INSTALL_DIR); + fse.copySync(tmpDir, INSTALL_DIR); + } finally { + cleanupTemp(tmpDir); + } - try { - console.log('Instalando dependências...'); - execSync('npm install', { cwd: INSTALL_DIR, stdio: 'inherit' }); + console.log(' Instalando dependências...'); + execSync('npm install', { cwd: INSTALL_DIR, stdio: 'inherit' }); - console.log('Vinculando comando vitruvio...'); - try { execSync('npm unlink -g vitruvio', { stdio: 'pipe' }); } catch (_) { } - execSync('npm link', { cwd: INSTALL_DIR, stdio: 'inherit' }); - - console.log(''); - console.log('Vitruvio CLI atualizado com sucesso.'); - console.log('Origem: ' + INSTALL_DIR); - } catch (e) { - console.error('Erro ao instalar o CLI: ' + e.message); - process.exit(1); - } - }); + console.log(' Vinculando comando vitruvio...'); + try { execSync('npm unlink -g vitruvio', { stdio: 'pipe' }); } catch (_) {} + execSync('npm link', { cwd: INSTALL_DIR, stdio: 'inherit' }); } -module.exports = { register: register }; +module.exports = { run: run }; diff --git a/src/commands/update-repo.js b/src/commands/update-repo.js index 3ba97e9..c7279a8 100644 --- a/src/commands/update-repo.js +++ b/src/commands/update-repo.js @@ -8,7 +8,6 @@ var { cloneWithFallback, cleanupTemp } = require('../utils/git'); var SSH_URL = 'ssh://git@git.davinti.com.br:2222/davinTI/projeto-base.git'; var HTTPS_URL = 'https://git.davinti.com.br/davinTI/projeto-base.git'; -// CLAUDE.md files managed by the template — relative paths from repo root var MANAGED_FILES = [ 'CLAUDE.md', 'endpoints/CLAUDE.md', @@ -20,63 +19,50 @@ var MANAGED_FILES = [ 'scripts/CLAUDE.md' ]; -function register(program) { - program - .command('update-repo') - .description('Atualiza os arquivos CLAUDE.md gerenciados neste repositório com a versão mais recente do template') - .action(function () { - var repoDir = process.cwd(); - - if (!fs.existsSync(path.join(repoDir, 'vitruvio.json'))) { - console.error('Erro: nenhum vitruvio.json encontrado no diretório atual.'); - console.error('Execute este comando dentro de um repositório Vitruvio.'); - process.exit(1); - } - - console.log('Buscando template mais recente...'); - - var tmpDir = null; - try { - tmpDir = cloneWithFallback(SSH_URL, HTTPS_URL); - } catch (e) { - console.error('Erro: não foi possível clonar o repositório template.'); - console.error(e.message); - process.exit(1); - } - - try { - var updated = []; - var skipped = []; - - MANAGED_FILES.forEach(function (relPath) { - var src = path.join(tmpDir, relPath); - var dest = path.join(repoDir, relPath); - - if (!fs.existsSync(src)) { - skipped.push(relPath); - return; - } - - fse.ensureDirSync(path.dirname(dest)); - fse.copySync(src, dest, { overwrite: true }); - updated.push(relPath); - }); - - console.log(''); - if (updated.length > 0) { - console.log('Arquivos atualizados (' + updated.length + '):'); - updated.forEach(function (f) { console.log(' ' + f); }); - } - if (skipped.length > 0) { - console.log('Não encontrados no template (' + skipped.length + '):'); - skipped.forEach(function (f) { console.log(' ' + f); }); - } - console.log(''); - console.log('Atualização concluída.'); - } finally { - cleanupTemp(tmpDir); - } - }); +function isVitruvioRepo(dir) { + return fs.existsSync(path.join(dir, 'vitruvio.json')); } -module.exports = { register: register }; +function run() { + var repoDir = process.cwd(); + + if (!isVitruvioRepo(repoDir)) { + throw new Error('nenhum vitruvio.json encontrado no diretório atual.'); + } + + console.log(' Buscando template mais recente...'); + + var tmpDir = cloneWithFallback(SSH_URL, HTTPS_URL); + + try { + var updated = []; + var skipped = []; + + MANAGED_FILES.forEach(function (relPath) { + var src = path.join(tmpDir, relPath); + var dest = path.join(repoDir, relPath); + + if (!fs.existsSync(src)) { + skipped.push(relPath); + return; + } + + fse.ensureDirSync(path.dirname(dest)); + fse.copySync(src, dest, { overwrite: true }); + updated.push(relPath); + }); + + if (updated.length > 0) { + console.log(' Arquivos atualizados (' + updated.length + '):'); + updated.forEach(function (f) { console.log(' ' + f); }); + } + if (skipped.length > 0) { + console.log(' Não encontrados no template (' + skipped.length + '):'); + skipped.forEach(function (f) { console.log(' ' + f); }); + } + } finally { + cleanupTemp(tmpDir); + } +} + +module.exports = { run: run, isVitruvioRepo: isVitruvioRepo }; diff --git a/src/commands/update.js b/src/commands/update.js new file mode 100644 index 0000000..93e5214 --- /dev/null +++ b/src/commands/update.js @@ -0,0 +1,73 @@ +'use strict'; + +var updateBase = require('./update-base'); +var updateRepo = require('./update-repo'); +var updateCli = require('./update-cli'); + +var TARGETS = ['base', 'repo', 'cli']; + +function runTarget(name) { + console.log('\n[' + name + ']'); + if (name === 'base') updateBase.run(); + else if (name === 'repo') updateRepo.run(); + else if (name === 'cli') updateCli.run(); + console.log(' Concluído.'); +} + +function register(program) { + program + .command('update [alvo]') + .description('Atualiza componentes do Vitruvio (alvo: base | repo | cli). Sem alvo, atualiza tudo.') + .action(function (alvo) { + if (alvo) { + var key = alvo.toLowerCase(); + if (!TARGETS.includes(key)) { + console.error('Alvo inválido: "' + alvo + '". Use: base, repo ou cli.'); + process.exit(1); + } + if (key === 'repo' && !updateRepo.isVitruvioRepo(process.cwd())) { + console.error('Erro: nenhum vitruvio.json encontrado no diretório atual.'); + console.error('Execute este comando dentro de um repositório Vitruvio.'); + process.exit(1); + } + try { + runTarget(key); + } catch (e) { + console.error('Erro: ' + e.message); + process.exit(1); + } + return; + } + + // No target — update everything applicable + var errors = []; + var inRepo = updateRepo.isVitruvioRepo(process.cwd()); + + ['base', 'cli'].forEach(function (name) { + try { + runTarget(name); + } catch (e) { + errors.push(name + ': ' + e.message); + } + }); + + if (inRepo) { + try { + runTarget('repo'); + } catch (e) { + errors.push('repo: ' + e.message); + } + } + + console.log(''); + if (errors.length > 0) { + console.error('Atualização concluída com erros:'); + errors.forEach(function (msg) { console.error(' ' + msg); }); + process.exit(1); + } else { + console.log('Tudo atualizado com sucesso.'); + } + }); +} + +module.exports = { register: register };