40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import { $ } from "bun";
|
|
import { Array, Option, Order, pipe } from "effect";
|
|
import { readdir } from "node:fs/promises";
|
|
|
|
const launchContainers = async (): Promise<string> => {
|
|
return await $`podman compose up -d`.text();
|
|
};
|
|
|
|
const getLatestDbExport = async (): Promise<string> => {
|
|
return pipe(
|
|
await readdir(`../db`),
|
|
(paths: ReadonlyArray<string>) => Array.sort(paths, Order.string),
|
|
(sortedPaths: ReadonlyArray<string>) => Array.last(sortedPaths),
|
|
(last: Option.Option<string>) =>
|
|
Option.getOrThrowWith(last, () => new Error("Aucun export de BDD n'est disponible.")),
|
|
);
|
|
};
|
|
|
|
const importLatestDbInWordpressContainer = async (exportPath: string) => {
|
|
await $`podman exec -it haikuatelier.fr-wordpress fish -c "cd web && wp --allow-root db import ${exportPath}"`;
|
|
};
|
|
|
|
try {
|
|
// S'assure que les conteneurs soient lancées.
|
|
await launchContainers();
|
|
|
|
const latestExportPath: string = `../db/${await getLatestDbExport()}`;
|
|
console.log(`Dernier export : ${latestExportPath}`);
|
|
|
|
// Exécute l'opération d'import dans le conteneur WordPress via wp-cli.
|
|
await importLatestDbInWordpressContainer(latestExportPath);
|
|
} catch (error: unknown) {
|
|
if (error instanceof $.ShellError) {
|
|
console.error(`Commande échouée avec code d'erreur: ${error.exitCode}`);
|
|
console.log(error.stdout.toString());
|
|
console.log(error.stderr.toString());
|
|
} else {
|
|
console.error(error);
|
|
}
|
|
}
|