Files
OmniRoute/scripts/ad-hoc/migrate-env.mjs
diegosouzapw f3b944a55a refactor(scripts): organize into build/dev/check/docs/i18n/ad-hoc subfolders
Reorganizes the 29 active scripts under scripts/ into purpose-driven
subfolders:

- scripts/build/    (11) — Build, install, publish, runtime env
- scripts/dev/      (13) — Dev servers, test runners, healthchecks
- scripts/check/    (10) — Lint/validation/coverage checks
- scripts/docs/      (2) — Docs index and provider reference generation
- scripts/i18n/     (+3) — Adds Python translation utilities (check/validate/autotranslate)
- scripts/ad-hoc/    (4) — One-shot maintenance utilities

Updates all references in package.json, electron/package.json,
.husky/pre-commit, .github/workflows/ci.yml, Dockerfile, src/,
tests/, scripts/ internal cross-imports, playwright.config.ts,
and English docs (CODEBASE_DOCUMENTATION, ENVIRONMENT, FEATURES,
RELEASE_CHECKLIST, COVERAGE_PLAN, ELECTRON_GUIDE, I18N, GEMINI).

Also patches scripts/build/pack-artifact-policy.ts so the npm pack
allowlist mirrors the new layout.

Validates with:
- npm run lint            (exit 0 — pre-existing minified-bundle errors only)
- npm run typecheck:core  (exit 0)
- npm run check:docs-all  (exit 0)
- unit tests for moved scripts (57 tests pass)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 10:14:25 -03:00

68 lines
2.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* OmniRoute v3.3 -> v3.4 Environment Migration Script
* Resolves breaking changes in environment variables format and validation.
*/
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;
// 1. Migrate NEXTAUTH_SECRET to JWT_SECRET if missing
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();
// Enforce 32 char minimum for secretsValidator.ts
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;
}
// 2. Ensure API_KEY_SECRET exists (required in 3.4.0)
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) {
// Backup old .env
fs.writeFileSync(envPath + ".bak", fs.readFileSync(envPath));
console.log("Created backup at .env.bak");
// Write new .env
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.");
}