Files
CoHaiSe-CLOUD/backend/src/cli.ts
T
Esiah 68d4d5dea0
PR — Ouverture automatique / create-pr (push) Successful in 3s
refactor: restructure frontend and backend by domain services
Split the monolithic backend (lib/, routes/, access.ts, storage.ts, media.ts)
into a layered structure inspired by JeanXXIII-CRM:
- config/: appConfig, db
- middleware/: auth (session, roles, cookies)
- models/: shared types
- routes/: *Routes.ts (thin HTTP handlers)
- services/: *Service.ts (business logic, one per domain)
- utils/: errors, http, migrate, ratelimit, validators, service bootstrap

Frontend likewise: services/*Service.ts + apiClient, contexts/, types/, utils/format.
Route files now carry the "Routes" suffix; service files are named
authService.ts, ratelimit.ts, etc. and live in their own files, frontend and
backend separated.

Updated docs and READMEs to reflect the new layout. Typecheck and builds pass.
2026-11-13 09:45:21 +01:00

126 lines
3.8 KiB
JavaScript

#!/usr/bin/env node
import { pathToFileURL } from "node:url";
import { resolve } from "node:path";
import { ConfigError, loadDbConfig } from "./config/appConfig.js";
import { Db } from "./config/db.js";
import { AppError, toSafeError } from "./utils/errors.js";
import { listMigrations, runMigrations } from "./utils/migrate.js";
interface Options {
dir: string;
dryRun: boolean;
check: boolean;
help: boolean;
}
const USAGE = `Utilisation : cohaise-migrate --dir <dossier> [--dry-run] [--check]
--dir <dossier> Dossier contenant les fichiers NNN_nom.sql (obligatoire)
--dry-run Analyse sans ecrire en base
--check Echoue s'il reste une migration a appliquer
--help Cette aide`;
const parseArgs = (argv: string[]): Options => {
const options: Options = { dir: "", dryRun: false, check: false, help: false };
for (let index = 0; index < argv.length; index += 1) {
const arg = argv[index];
if (arg === undefined) continue;
if (arg === "--dry-run") options.dryRun = true;
else if (arg === "--check") options.check = true;
else if (arg === "--help" || arg === "-h") options.help = true;
else if (arg === "--dir") {
options.dir = argv[index + 1] ?? "";
index += 1;
} else if (arg.startsWith("--dir=")) {
options.dir = arg.slice("--dir=".length);
} else {
throw AppError.badRequest(`Argument inconnu : ${arg}`, { argument: arg });
}
}
return options;
};
const main = async (argv: string[]): Promise<number> => {
let options: Options;
try {
options = parseArgs(argv);
} catch (error) {
const safe = toSafeError(error);
console.error(` ${safe.message}\n\n${USAGE}`);
return 2;
}
if (options.help) {
console.log(USAGE);
return 0;
}
if (!options.dir) {
console.error(` --dir est obligatoire\n\n${USAGE}`);
return 2;
}
let config;
try {
config = loadDbConfig();
} catch (error) {
if (error instanceof ConfigError) {
console.error(` Configuration invalide :\n - ${error.issues.join("\n - ")}`);
return 2;
}
throw error;
}
const dir = resolve(options.dir);
const db = new Db(config);
try {
const migrations = await listMigrations(dir);
if (migrations.length === 0) {
console.log(` Aucune migration trouvee dans ${dir}`);
return 0;
}
const result = await runMigrations(db, dir, { dryRun: options.dryRun });
const prefix = options.dryRun ? "[dry-run] " : "";
console.log(
`${prefix}${migrations.length} migration(s) · ${result.applied.length} appliquee(s) · ` +
`${result.skipped.length} deja en place · ${result.drifted.length} derivee(s)`,
);
for (const version of result.applied) console.log(` + ${version}`);
for (const drift of result.drifted) {
console.error(` ! ${drift.version} modifiee depuis son execution (checksum attendu ${drift.recorded.slice(0, 12)}…, trouve ${drift.current.slice(0, 12)}…)`);
}
if (result.drifted.length > 0) return 1;
if (options.check && result.applied.length > 0) return 1;
return 0;
} catch (error) {
const safe = toSafeError(error);
console.error(` Echec : ${safe.code} — ${safe.message}`);
if (error instanceof AppError && error.status >= 500 && error.message.includes("migree")) {
console.error(" Appliquez d'abord les migrations : cohaise-migrate --dir <dossier>");
}
return 1;
} finally {
await db.close().catch(() => undefined);
}
};
const isDirectRun =
process.argv[1] !== undefined &&
import.meta.url === pathToFileURL(process.argv[1]).href;
if (isDirectRun) {
main(process.argv.slice(2)).then(
(code) => process.exit(code),
(error) => {
console.error(` Erreur inattendue : ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
},
);
}
export { main, parseArgs };