|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Generate Supabase TypeScript types |
| 4 | + * |
| 5 | + * Loads SUPABASE_ACCESS_TOKEN from .env file and runs type generation. |
| 6 | + * |
| 7 | + * Usage: npm run gen:types |
| 8 | + * |
| 9 | + * Requires SUPABASE_ACCESS_TOKEN in .env file: |
| 10 | + * SUPABASE_ACCESS_TOKEN=your-token-here |
| 11 | + * |
| 12 | + * Get your token from: https://supabase.com/dashboard/account/tokens |
| 13 | + */ |
| 14 | + |
| 15 | +import { execSync } from 'child_process'; |
| 16 | +import { readFileSync, existsSync } from 'fs'; |
| 17 | +import { resolve, dirname } from 'path'; |
| 18 | +import { fileURLToPath } from 'url'; |
| 19 | + |
| 20 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 21 | +const rootDir = resolve(__dirname, '..'); |
| 22 | +const envPath = resolve(rootDir, '.env'); |
| 23 | + |
| 24 | +// Project ID for bluePLM |
| 25 | +const PROJECT_ID = 'vvyhpdzqdizvorrhjhvq'; |
| 26 | +const OUTPUT_PATH = 'src/types/supabase.ts'; |
| 27 | + |
| 28 | +// Load .env file manually (simple parser) |
| 29 | +function loadEnv() { |
| 30 | + if (!existsSync(envPath)) { |
| 31 | + console.error('❌ No .env file found at:', envPath); |
| 32 | + console.error('\nCreate a .env file with:'); |
| 33 | + console.error(' SUPABASE_ACCESS_TOKEN=your-token-here'); |
| 34 | + console.error('\nGet your token from: https://supabase.com/dashboard/account/tokens'); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + const content = readFileSync(envPath, 'utf-8'); |
| 39 | + for (const line of content.split('\n')) { |
| 40 | + const trimmed = line.trim(); |
| 41 | + if (!trimmed || trimmed.startsWith('#')) continue; |
| 42 | + |
| 43 | + const eqIndex = trimmed.indexOf('='); |
| 44 | + if (eqIndex === -1) continue; |
| 45 | + |
| 46 | + const key = trimmed.slice(0, eqIndex).trim(); |
| 47 | + let value = trimmed.slice(eqIndex + 1).trim(); |
| 48 | + |
| 49 | + // Remove quotes if present |
| 50 | + if ((value.startsWith('"') && value.endsWith('"')) || |
| 51 | + (value.startsWith("'") && value.endsWith("'"))) { |
| 52 | + value = value.slice(1, -1); |
| 53 | + } |
| 54 | + |
| 55 | + process.env[key] = value; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Main |
| 60 | +loadEnv(); |
| 61 | + |
| 62 | +if (!process.env.SUPABASE_ACCESS_TOKEN) { |
| 63 | + console.error('❌ SUPABASE_ACCESS_TOKEN not found in .env file'); |
| 64 | + console.error('\nAdd this line to your .env file:'); |
| 65 | + console.error(' SUPABASE_ACCESS_TOKEN=your-token-here'); |
| 66 | + console.error('\nGet your token from: https://supabase.com/dashboard/account/tokens'); |
| 67 | + process.exit(1); |
| 68 | +} |
| 69 | + |
| 70 | +console.log('🔄 Generating Supabase types...'); |
| 71 | +console.log(` Project: ${PROJECT_ID}`); |
| 72 | +console.log(` Output: ${OUTPUT_PATH}`); |
| 73 | + |
| 74 | +try { |
| 75 | + execSync( |
| 76 | + `npx supabase gen types typescript --project-id ${PROJECT_ID} > ${OUTPUT_PATH}`, |
| 77 | + { |
| 78 | + cwd: rootDir, |
| 79 | + stdio: 'inherit', |
| 80 | + env: { |
| 81 | + ...process.env, |
| 82 | + SUPABASE_ACCESS_TOKEN: process.env.SUPABASE_ACCESS_TOKEN, |
| 83 | + }, |
| 84 | + } |
| 85 | + ); |
| 86 | + console.log('✅ Types generated successfully!'); |
| 87 | +} catch (error) { |
| 88 | + console.error('❌ Failed to generate types'); |
| 89 | + process.exit(1); |
| 90 | +} |
0 commit comments