UNPKG

tdpw

Version:

CLI tool for uploading Playwright test reports to TestDino platform with TestDino storage support

277 lines 10.2 kB
#!/usr/bin/env node "use strict"; /** * TestDino CLI Entry Point * Main entry point for the tdpw CLI tool */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TestDinoCLI = void 0; exports.main = main; const commands_1 = require("./commands"); const types_1 = require("../types"); /** * CLI Application main class */ class TestDinoCLI { /** * Main application entry point */ async run(args = process.argv) { try { // Handle help and version flags manually for better control if (this.shouldShowHelp(args)) { const helpResult = this.handleHelpCommand(args); if (helpResult.handled) { if (helpResult.showCache) { commands_1.commandManager.showCacheHelp(); } else if (helpResult.showLastFailed) { commands_1.commandManager.showLastFailedHelp(); } else if (helpResult.showUpload) { commands_1.commandManager.showUploadHelp(); } else { commands_1.commandManager.showHelp(); } process.exit(types_1.ExitCode.SUCCESS); } } if (this.shouldShowVersion(args)) { console.log(`tdpw v${commands_1.commandManager.getVersion()}`); process.exit(types_1.ExitCode.SUCCESS); } // Check for legacy syntax and transform to new format const transformedArgs = this.handleLegacySyntax(args); // Execute commands through the command manager await commands_1.commandManager.parseAndExecute(transformedArgs); process.exit(types_1.ExitCode.SUCCESS); } catch (error) { await this.handleError(error); } } /** * Check if help should be displayed */ shouldShowHelp(args) { return (args.includes('--help') || args.includes('-h') || args.includes('help')); } /** * Handle help command and determine which help to show */ handleHelpCommand(args) { const userArgs = args.slice(2); // Remove node and script path // Check for subcommand help: "cache --help", "last-failed --help", "upload --help" if (userArgs.length >= 2) { if (userArgs[0] === 'cache' && (userArgs[1] === '--help' || userArgs[1] === '-h')) { return { handled: true, showCache: true, showLastFailed: false, showUpload: false, }; } if (userArgs[0] === 'last-failed' && (userArgs[1] === '--help' || userArgs[1] === '-h')) { return { handled: true, showCache: false, showLastFailed: true, showUpload: false, }; } if (userArgs[0] === 'upload' && (userArgs[1] === '--help' || userArgs[1] === '-h')) { return { handled: true, showCache: false, showLastFailed: false, showUpload: true, }; } } // Check for "help cache", "help last-failed", or "help upload" if (userArgs.length >= 2 && userArgs[0] === 'help') { if (userArgs[1] === 'cache') { return { handled: true, showCache: true, showLastFailed: false, showUpload: false, }; } if (userArgs[1] === 'last-failed') { return { handled: true, showCache: false, showLastFailed: true, showUpload: false, }; } if (userArgs[1] === 'upload') { return { handled: true, showCache: false, showLastFailed: false, showUpload: true, }; } } // Check for main help: "--help", "-h", or just "help" if (userArgs.length === 0 || (userArgs.length === 1 && (userArgs[0] === '--help' || userArgs[0] === '-h' || userArgs[0] === 'help'))) { return { handled: true, showCache: false, showLastFailed: false, showUpload: false, }; } return { handled: false, showCache: false, showLastFailed: false, showUpload: false, }; } /** * Check if version should be displayed */ shouldShowVersion(args) { return args.includes('--version') || args.includes('-V'); } /** * Handle legacy syntax for backward compatibility * Transforms: npx tdpw ./path --flags * To: npx tdpw upload ./path --flags */ handleLegacySyntax(args) { const userArgs = args.slice(2); // Remove node and script path // If no args, return as-is if (userArgs.length === 0) { return args; } const firstArg = userArgs[0]; // Known subcommands - not legacy syntax const knownCommands = ['cache', 'last-failed', 'upload', 'help']; // If first arg is a known command, return as-is if (firstArg && knownCommands.includes(firstArg)) { return args; } // If first arg is a flag, return as-is if (firstArg?.startsWith('-')) { return args; } // If we reach here, first arg is likely a path (legacy syntax) // Check if there are any upload-related flags const uploadFlags = [ '--upload-images', '--upload-videos', '--upload-html', '--upload-traces', '--upload-files', '--upload-full-json', '--json-report', '--html-report', '--trace-dir', ]; const hasUploadFlag = userArgs.some(arg => uploadFlags.includes(arg)); // Legacy syntax detected: path provided without 'upload' command if (hasUploadFlag || firstArg) { console.error('⚠️ DEPRECATION WARNING: Legacy syntax detected.'); console.error(' Old: npx tdpw ./path --flags'); console.error(' New: npx tdpw upload ./path --flags'); console.error(' Legacy syntax will be removed in a future version.\n'); // Transform: insert 'upload' command after script name const nodePath = args[0] || 'node'; const scriptPath = args[1] || 'tdpw'; return [nodePath, scriptPath, 'upload', ...userArgs]; } return args; } /** * Professional error handling with appropriate messaging */ async handleError(error) { const isVerbose = process.argv.includes('--verbose') || process.argv.includes('-v'); if (error instanceof types_1.BaseError) { console.error(`❌ ${error.message}`); // Show helpful suggestions without being verbose switch (true) { case error instanceof types_1.ConfigurationError: console.error('💡 Check your API token and configuration'); break; case error instanceof types_1.ValidationError: console.error('💡 Use --help for correct usage'); break; case error instanceof types_1.AuthenticationError: console.error('💡 Verify your API token has proper permissions'); break; case error instanceof types_1.NetworkError: console.error('💡 Check your internet connection'); break; case error instanceof types_1.FileSystemError: console.error('💡 Verify the specified paths exist and are readable'); break; } // Show stack trace only in verbose mode if (isVerbose && error.stack) { console.error('\nStack trace:', error.stack); } process.exit(this.getExitCode(error)); } else if (error instanceof Error) { console.error(`❌ ${error.message}`); if (!isVerbose) { console.error('💡 Use --verbose for detailed error information'); } else if (error.stack) { console.error('\nStack trace:', error.stack); } process.exit(types_1.ExitCode.GENERAL_ERROR); } else { console.error('❌ An unexpected error occurred'); if (isVerbose) { console.error('Error details:', error); } process.exit(types_1.ExitCode.GENERAL_ERROR); } } /** * Get appropriate exit code for error type */ getExitCode(error) { if (error instanceof types_1.AuthenticationError) return types_1.ExitCode.AUTHENTICATION_ERROR; if (error instanceof types_1.NetworkError) return types_1.ExitCode.NETWORK_ERROR; if (error instanceof types_1.FileSystemError) return types_1.ExitCode.FILE_NOT_FOUND_ERROR; return types_1.ExitCode.GENERAL_ERROR; } } exports.TestDinoCLI = TestDinoCLI; /** * Application execution */ async function main() { const cli = new TestDinoCLI(); await cli.run(); } // Execute CLI when this module is run (async () => { try { await main(); } catch (error) { console.error('Fatal error during CLI execution:', error); process.exit(types_1.ExitCode.GENERAL_ERROR); } })(); //# sourceMappingURL=index.js.map