|
| 1 | +import path from 'node:path'; |
| 2 | + |
| 3 | +import { cyanLog, redLog, yellowLog } from './consoleColor'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Get human-readable engine name |
| 7 | + */ |
| 8 | +function getEngineName(engine: string): string { |
| 9 | + const names: Record<string, string> = { |
| 10 | + psychic: 'PsychicHttpServer', |
| 11 | + psychic2: 'PsychicHttpServer V2', |
| 12 | + async: 'ESPAsyncWebServer', |
| 13 | + espidf: 'ESP-IDF' |
| 14 | + }; |
| 15 | + return names[engine] ?? engine; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Error: Missing index.html or index.htm |
| 20 | + */ |
| 21 | +export function getMissingIndexError(engine: string): string { |
| 22 | + const hints: Record<string, string> = { |
| 23 | + psychic: ` 1. Add an index.html file to your source directory |
| 24 | + 2. The file will automatically be set as the default route ("/") |
| 25 | + 3. PsychicHttpServer uses: server->defaultEndpoint = ...`, |
| 26 | + |
| 27 | + psychic2: ` 1. Add an index.html file to your source directory |
| 28 | + 2. The file will automatically be set as the default route ("/") |
| 29 | + 3. PsychicHttpServer V2 uses: server->defaultEndpoint = ...`, |
| 30 | + |
| 31 | + async: ` 1. Add an index.html file to your source directory |
| 32 | + 2. The file will automatically create a "/" route handler |
| 33 | + 3. ESPAsyncWebServer uses: server.on("/", HTTP_GET, ...)`, |
| 34 | + |
| 35 | + espidf: ` 1. Add an index.html file to your source directory |
| 36 | + 2. The file will register both "/" and "/index.html" routes |
| 37 | + 3. ESP-IDF uses: httpd_register_uri_handler(server, &route_def_...)` |
| 38 | + }; |
| 39 | + |
| 40 | + const hint = hints[engine] ?? hints['psychic']; |
| 41 | + |
| 42 | + return ( |
| 43 | + redLog('[ERROR] No index.html or index.htm found in source files') + |
| 44 | + ` |
| 45 | +
|
| 46 | +Why this matters: |
| 47 | + Web applications typically need a default entry point. Without index.html, |
| 48 | + users visiting http://your-esp32/ will get a 404 error. |
| 49 | +
|
| 50 | +How to fix (for ${getEngineName(engine)}): |
| 51 | +${hint} |
| 52 | +
|
| 53 | +Alternative: |
| 54 | + If you use a different entry point (e.g., main.html), you can add --no-index-check flag, |
| 55 | + but users must navigate to http://your-esp32/main.html explicitly.` |
| 56 | + ); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Error: Invalid engine specified |
| 61 | + */ |
| 62 | +export function getInvalidEngineError(attempted: string): string { |
| 63 | + return ( |
| 64 | + redLog(`[ERROR] Invalid engine: '${attempted}'`) + |
| 65 | + ` |
| 66 | +
|
| 67 | +Valid engines are: |
| 68 | + ${cyanLog('• psychic')} - PsychicHttpServer (ESP32 only, fastest performance) |
| 69 | + ${cyanLog('• psychic2')} - PsychicHttpServer V2 (ESP32 only, modern API) |
| 70 | + ${cyanLog('• async')} - ESPAsyncWebServer (ESP32/ESP8266 compatible) |
| 71 | + ${cyanLog('• espidf')} - Native ESP-IDF web server (ESP32 only, no Arduino) |
| 72 | +
|
| 73 | +How to fix: |
| 74 | + npx svelteesp32 --engine=psychic --sourcepath=./dist |
| 75 | +
|
| 76 | +Example RC file (.svelteesp32rc.json): |
| 77 | + { |
| 78 | + "engine": "psychic", |
| 79 | + "sourcepath": "./dist" |
| 80 | + } |
| 81 | +
|
| 82 | +Documentation: https://github.com/hpieroni/svelteesp32#readme` |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Error: Source path not found or not a directory |
| 88 | + */ |
| 89 | +export function getSourcepathNotFoundError(sourcepath: string, reason: 'not_found' | 'not_directory'): string { |
| 90 | + if (reason === 'not_directory') |
| 91 | + return ( |
| 92 | + redLog(`[ERROR] Source path is not a directory: '${sourcepath}'`) + |
| 93 | + ` |
| 94 | +
|
| 95 | +The --sourcepath option must point to a directory containing your built web files, |
| 96 | +not an individual file. |
| 97 | +
|
| 98 | +How to fix: |
| 99 | + npx svelteesp32 --sourcepath=./dist --engine=psychic` |
| 100 | + ); |
| 101 | + |
| 102 | + const resolvedPath = path.resolve(sourcepath); |
| 103 | + const currentDirectory = process.cwd(); |
| 104 | + |
| 105 | + return ( |
| 106 | + redLog(`[ERROR] Source directory not found: '${sourcepath}'`) + |
| 107 | + ` |
| 108 | +
|
| 109 | +Why this matters: |
| 110 | + SvelteESP32 needs your compiled web assets (HTML, CSS, JS) to convert them |
| 111 | + into C++ header files for the ESP32. |
| 112 | +
|
| 113 | +How to fix: |
| 114 | + 1. Build your frontend application first: |
| 115 | + • Svelte: npm run build |
| 116 | + • React: npm run build |
| 117 | + • Vue: npm run build |
| 118 | + • Angular: ng build |
| 119 | +
|
| 120 | + 2. Verify the build output directory exists: |
| 121 | + ${cyanLog(`ls -la ${sourcepath}`)} |
| 122 | +
|
| 123 | + 3. Check your build tool configuration: |
| 124 | + • Vite: vite.config.js → build.outDir |
| 125 | + • Webpack: webpack.config.js → output.path |
| 126 | + • Rollup: rollup.config.js → output.dir |
| 127 | +
|
| 128 | + 4. Update your svelteesp32 command to match: |
| 129 | + ${cyanLog(`npx svelteesp32 --sourcepath=./build --engine=psychic`)} |
| 130 | +
|
| 131 | +Current directory: ${currentDirectory} |
| 132 | +Attempted path: ${resolvedPath} (resolved)` |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Hint: max_uri_handlers configuration (console output, not an error) |
| 138 | + */ |
| 139 | +export function getMaxUriHandlersHint(engine: string, routeCount: number): string { |
| 140 | + const recommended = routeCount + 5; |
| 141 | + |
| 142 | + const hints: Record<string, string> = { |
| 143 | + psychic: `PsychicHttpServer server; |
| 144 | + server.config.max_uri_handlers = ${recommended}; // Default is 8, you need at least ${routeCount} |
| 145 | + initSvelteStaticFiles(&server); |
| 146 | + server.listen(80);`, |
| 147 | + |
| 148 | + psychic2: `PsychicHttpServer server; |
| 149 | + server.config.max_uri_handlers = ${recommended}; // Default is 8, you need at least ${routeCount} |
| 150 | + initSvelteStaticFiles(&server); |
| 151 | + server.listen(80);`, |
| 152 | + |
| 153 | + espidf: `httpd_config_t config = HTTPD_DEFAULT_CONFIG(); |
| 154 | + config.max_uri_handlers = ${recommended}; // Default is 8, you need at least ${routeCount} |
| 155 | + httpd_handle_t server = NULL; |
| 156 | + httpd_start(&server, &config); |
| 157 | + initSvelteStaticFiles(server);` |
| 158 | + }; |
| 159 | + |
| 160 | + const hint = hints[engine]; |
| 161 | + if (!hint) return ''; // No hint for async engine (no limit) |
| 162 | + |
| 163 | + return ( |
| 164 | + yellowLog('[CONFIG TIP] max_uri_handlers configuration') + |
| 165 | + ` |
| 166 | +
|
| 167 | +Your generated code includes ${routeCount} routes. Make sure your server can handle them: |
| 168 | +
|
| 169 | +For ${getEngineName(engine)}: |
| 170 | + ${hint} |
| 171 | +
|
| 172 | +Recommended formula: max_uri_handlers = file_count + 5 (safety margin) |
| 173 | +
|
| 174 | +Runtime symptoms if too low: |
| 175 | + • Routes not registered (HTTP 404 errors) |
| 176 | + • ESP_ERR_HTTPD_HANDLERS_FULL error in logs |
| 177 | + • Some files load, others don't (random behavior)` |
| 178 | + ); |
| 179 | +} |
0 commit comments