()
| 38 | |
| 39 | // Custom sqlite3 command using better-sqlite3 (bypasses broken sql.js ESM bundling in just-bash) |
| 40 | function createSqlite3Command() { |
| 41 | return defineCommand('sqlite3', async (args) => { |
| 42 | let showHeader = false; |
| 43 | let separator = '|'; |
| 44 | const filteredArgs: string[] = []; |
| 45 | |
| 46 | for (let i = 0; i < args.length; i++) { |
| 47 | const arg = args[i]; |
| 48 | if (arg === '-header') { |
| 49 | showHeader = true; |
| 50 | } else if (arg === '-separator' && i + 1 < args.length) { |
| 51 | separator = args[++i]; |
| 52 | } else if (arg === '-csv') { |
| 53 | separator = ','; |
| 54 | showHeader = true; |
| 55 | } else if (arg === '-json') { |
| 56 | separator = 'json'; |
| 57 | } else if (!arg.startsWith('-')) { |
| 58 | filteredArgs.push(arg); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | const [dbPath, ...sqlParts] = filteredArgs; |
| 63 | const sql = sqlParts.join(' '); |
| 64 | |
| 65 | if (!dbPath) { |
| 66 | return { |
| 67 | stdout: '', |
| 68 | stderr: |
| 69 | 'Usage: sqlite3 [OPTIONS] DATABASE [SQL]\nOptions: -header, -separator SEP, -csv, -json', |
| 70 | exitCode: 1, |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | if (!sql) { |
| 75 | return { stdout: '', stderr: 'Error: no SQL statement provided', exitCode: 1 }; |
| 76 | } |
| 77 | |
| 78 | const actualPath = dbPath.startsWith('/') |
| 79 | ? join(DATA_DIR, dbPath.slice(1)) |
| 80 | : join(DATA_DIR, dbPath); |
| 81 | |
| 82 | try { |
| 83 | const require = createRequire(join(process.cwd(), 'package.json')); |
| 84 | const Database = require('better-sqlite3'); |
| 85 | const db = new Database(actualPath, { readonly: true }); |
| 86 | |
| 87 | try { |
| 88 | const stmt = db.prepare(sql); |
| 89 | const results = stmt.all(); |
| 90 | |
| 91 | if (results.length === 0) { |
| 92 | return { stdout: '', stderr: '', exitCode: 0 }; |
| 93 | } |
| 94 | |
| 95 | let output: string; |
| 96 | if (separator === 'json') { |
| 97 | output = JSON.stringify(results, null, 2); |
no outgoing calls
no test coverage detected