|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Lua Script Syntax Validator |
| 5 | + * Validates all Lua scripts for syntax errors before deployment |
| 6 | + */ |
| 7 | + |
| 8 | +import { execSync } from 'child_process'; |
| 9 | +import { readdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'; |
| 10 | +import { join } from 'path'; |
| 11 | + |
| 12 | +const LUA_DIR = 'src/lua'; |
| 13 | +const REDIS_CLI = 'redis-cli'; |
| 14 | + |
| 15 | +console.log('🔍 Validating Lua scripts for syntax errors...\n'); |
| 16 | + |
| 17 | +// Get all Lua files |
| 18 | +const luaFiles = readdirSync(LUA_DIR) |
| 19 | + .filter((file) => file.endsWith('.lua')) |
| 20 | + .sort(); |
| 21 | + |
| 22 | +let hasErrors = false; |
| 23 | + |
| 24 | +for (const file of luaFiles) { |
| 25 | + const filePath = join(LUA_DIR, file); |
| 26 | + const content = readFileSync(filePath, 'utf8'); |
| 27 | + |
| 28 | + console.log(`📄 Validating ${file}...`); |
| 29 | + |
| 30 | + try { |
| 31 | + // Write content to temporary file to avoid shell escaping issues |
| 32 | + const tempFile = `/tmp/validate-${file}`; |
| 33 | + writeFileSync(tempFile, content); |
| 34 | + |
| 35 | + // Use redis-cli to validate the script with dummy arguments |
| 36 | + // This ensures the script is actually compiled, not just parsed |
| 37 | + const result = execSync(`${REDIS_CLI} --eval ${tempFile} dummy`, { |
| 38 | + encoding: 'utf8', |
| 39 | + stdio: 'pipe', |
| 40 | + timeout: 5000, |
| 41 | + }); |
| 42 | + |
| 43 | + // Clean up temp file |
| 44 | + unlinkSync(tempFile); |
| 45 | + |
| 46 | + // Check if the result contains error messages |
| 47 | + if ( |
| 48 | + result.includes('ERR Error compiling script') || |
| 49 | + result.includes('syntax error') |
| 50 | + ) { |
| 51 | + throw new Error(`Lua syntax error: ${result.trim()}`); |
| 52 | + } |
| 53 | + |
| 54 | + console.log(`✅ ${file} - OK`); |
| 55 | + } catch (error) { |
| 56 | + console.log(`❌ ${file} - SYNTAX ERROR`); |
| 57 | + console.log(` Error: ${error.message}`); |
| 58 | + hasErrors = true; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +if (hasErrors) { |
| 63 | + console.log('\n🚨 Lua script validation failed!'); |
| 64 | + process.exit(1); |
| 65 | +} else { |
| 66 | + console.log('\n✅ All Lua scripts are syntactically valid!'); |
| 67 | + process.exit(0); |
| 68 | +} |
0 commit comments