| #!/usr/bin/env node |
|
|
| |
| |
| |
| |
|
|
| import fs from "fs"; |
| import path from "path"; |
| import crypto from "crypto"; |
|
|
| const envPath = path.resolve(process.cwd(), ".env"); |
|
|
| if (!fs.existsSync(envPath)) { |
| console.log("No .env file found. Migration skipped."); |
| process.exit(0); |
| } |
|
|
| let content = fs.readFileSync(envPath, "utf8"); |
| let modified = false; |
|
|
| |
| const nextAuthMatch = content.match(/^NEXTAUTH_SECRET=(.+)$/m); |
| const jwtMatch = content.match(/^JWT_SECRET=(.+)$/m); |
|
|
| if (nextAuthMatch && !jwtMatch) { |
| console.log("Migrating NEXTAUTH_SECRET to JWT_SECRET..."); |
| let newJwt = nextAuthMatch[1].trim(); |
|
|
| |
| if (newJwt.length < 32) { |
| console.warn( |
| `Original NEXTAUTH_SECRET was too short (${newJwt.length} chars). Generating a secure one...` |
| ); |
| newJwt = crypto.randomBytes(48).toString("base64"); |
| } |
|
|
| content += `\n# Migrated from NEXTAUTH_SECRET\nJWT_SECRET=${newJwt}\n`; |
| modified = true; |
| } else if (jwtMatch && jwtMatch[1].trim().length < 32) { |
| console.warn( |
| `JWT_SECRET is too short (${jwtMatch[1].trim().length} chars). Generating a secure one for v3.4.0+...` |
| ); |
| const newJwt = crypto.randomBytes(48).toString("base64"); |
| content = content.replace(/^JWT_SECRET=(.*)$/m, `JWT_SECRET=${newJwt}`); |
| modified = true; |
| } |
|
|
| |
| if (!content.match(/^API_KEY_SECRET=/m)) { |
| console.log("Adding required API_KEY_SECRET for v3.4.0..."); |
| const newApiSecret = crypto.randomBytes(32).toString("hex"); |
| content += `\n# Required for v3.4.0 API Key HMAC\nAPI_KEY_SECRET=${newApiSecret}\n`; |
| modified = true; |
| } |
|
|
| if (modified) { |
| |
| fs.writeFileSync(envPath + ".bak", fs.readFileSync(envPath)); |
| console.log("Created backup at .env.bak"); |
|
|
| |
| fs.writeFileSync(envPath, content, "utf8"); |
| console.log("Successfully migrated .env file for OmniRoute 3.4.x."); |
| } else { |
| console.log(".env file is already compatible with OmniRoute 3.4.x."); |
| } |
|
|