'use strict';
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.
`;
}
function register(program) {
program
.command('docs [tipo]')
.description('Abre a documentação do Vitruvio no navegador (tipo: desktop | mobile)')
.action(function (tipo) {
var platformDir;
try {
platformDir = getPlatformDir();
} catch (e) {
console.error('Erro: ' + e.message);
process.exit(1);
}
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;
}
// 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 html = buildSelectionPage(javaPath, reactNativePath);
var tmpFile = path.join(os.tmpdir(), 'vitruvio-docs-selector.html');
fs.writeFileSync(tmpFile, html, 'utf8');
console.log('Abrindo seletor de documentação...');
openUrl(tmpFile);
});
}
module.exports = { register: register };