diff --git a/src/commands/docs.js b/src/commands/docs.js index cad8534..f6a0d6e 100644 --- a/src/commands/docs.js +++ b/src/commands/docs.js @@ -2,14 +2,203 @@ var path = require('path'); var fs = require('fs'); +var os = require('os'); var { exec } = require('child_process'); var { getPlatformDir } = require('../utils/platform'); +var DOCS = { + desktop: { label: 'Desktop (Java)', subpath: path.join('docs', 'java', 'index.html') }, + mobile: { label: 'Mobile (React Native)', subpath: path.join('docs', 'react-native', 'index.html') }, +}; + +function openUrl(filePath) { + var cmd = process.platform === 'win32' + ? 'start "" "' + filePath + '"' + : 'xdg-open "' + filePath + '"'; + + exec(cmd, function (err) { + if (err) { + console.error('Erro ao abrir o navegador: ' + err.message); + console.error('Abra manualmente: ' + filePath); + process.exit(1); + } + }); +} + +function buildSelectionPage(javaPath, reactNativePath) { + return ` + + + + + Vitruvio – Documentação + + + +
+ + +

Documentação Vitruvio

+

Selecione abaixo qual documentação você deseja consultar.

+ +
+ + + + + + + + Desktop (Java) + API do módulo desktop / back-end + + + + + + + + + + + Mobile (React Native) + API do módulo mobile + + +
+ + +
+ +`; +} + function register(program) { program - .command('docs') - .description('Abre a documentação Java do Vitruvio no navegador padrão') - .action(function () { + .command('docs [tipo]') + .description('Abre a documentação do Vitruvio no navegador (tipo: desktop | mobile)') + .action(function (tipo) { var platformDir; try { platformDir = getPlatformDir(); @@ -18,32 +207,45 @@ function register(program) { process.exit(1); } - var docsPath = path.join(platformDir, 'docs', 'java', 'index.html'); + if (tipo) { + var key = tipo.toLowerCase(); + if (!DOCS[key]) { + console.error('Tipo inválido: "' + tipo + '". Use "desktop" ou "mobile".'); + process.exit(1); + } + var docPath = path.join(platformDir, DOCS[key].subpath); + if (!fs.existsSync(docPath)) { + console.error('Erro: documentação não encontrada em:'); + console.error(' ' + docPath); + console.error(''); + console.error('Execute "vitruvio update-base" para baixar os arquivos de plataforma.'); + process.exit(1); + } + console.log('Abrindo documentação ' + DOCS[key].label + '...'); + openUrl(docPath); + return; + } - if (!fs.existsSync(docsPath)) { - console.error('Erro: documentação não encontrada em:'); - console.error(' ' + docsPath); - console.error(''); + // No argument — generate and open selection page + var javaPath = path.join(platformDir, DOCS.desktop.subpath); + var reactNativePath = path.join(platformDir, DOCS.mobile.subpath); + + var missingDocs = []; + if (!fs.existsSync(javaPath)) missingDocs.push('desktop (Java)'); + if (!fs.existsSync(reactNativePath)) missingDocs.push('mobile (React Native)'); + + if (missingDocs.length === 2) { + console.error('Erro: nenhuma documentação encontrada em: ' + platformDir); console.error('Execute "vitruvio update-base" para baixar os arquivos de plataforma.'); process.exit(1); } - var cmd; - if (process.platform === 'win32') { - cmd = 'start "" "' + docsPath + '"'; - } else { - cmd = 'xdg-open "' + docsPath + '"'; - } + var html = buildSelectionPage(javaPath, reactNativePath); + var tmpFile = path.join(os.tmpdir(), 'vitruvio-docs-selector.html'); + fs.writeFileSync(tmpFile, html, 'utf8'); - exec(cmd, function (err) { - if (err) { - console.error('Erro ao abrir o navegador: ' + err.message); - console.error('Abra manualmente: ' + docsPath); - process.exit(1); - } - }); - - console.log('Abrindo documentação...'); + console.log('Abrindo seletor de documentação...'); + openUrl(tmpFile); }); }