2023-04-02 03:10:16 +02:00
|
|
|
import * as fs from "fs";
|
|
|
|
import { exclude } from "tsafe";
|
2024-05-15 05:14:01 +02:00
|
|
|
import { crawl } from "../tools/crawl";
|
2023-04-02 03:10:16 +02:00
|
|
|
import { join as pathJoin } from "path";
|
2023-09-03 07:14:57 +02:00
|
|
|
import { themeTypes } from "./constants";
|
2023-04-02 03:10:16 +02:00
|
|
|
|
2023-10-08 18:12:57 -07:00
|
|
|
const themeSrcDirBasenames = ["keycloak-theme", "keycloak_theme"];
|
2023-04-02 03:10:16 +02:00
|
|
|
|
2023-06-21 18:06:12 +02:00
|
|
|
/** Can't catch error, if the directory isn't found, this function will just exit the process with an error message. */
|
2024-06-09 09:03:43 +02:00
|
|
|
export function getThemeSrcDirPath(params: { projectDirPath: string }) {
|
|
|
|
const { projectDirPath } = params;
|
2023-04-02 03:10:16 +02:00
|
|
|
|
2024-06-09 09:03:43 +02:00
|
|
|
const srcDirPath = pathJoin(projectDirPath, "src");
|
2023-04-02 03:10:16 +02:00
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
const themeSrcDirPath: string | undefined = crawl({
|
|
|
|
dirPath: srcDirPath,
|
|
|
|
returnedPathsType: "relative to dirPath"
|
|
|
|
})
|
2023-04-02 03:10:16 +02:00
|
|
|
.map(fileRelativePath => {
|
2023-10-08 18:12:57 -07:00
|
|
|
for (const themeSrcDirBasename of themeSrcDirBasenames) {
|
|
|
|
const split = fileRelativePath.split(themeSrcDirBasename);
|
|
|
|
if (split.length === 2) {
|
|
|
|
return pathJoin(srcDirPath, split[0] + themeSrcDirBasename);
|
|
|
|
}
|
2023-04-02 03:10:16 +02:00
|
|
|
}
|
2023-10-08 18:12:57 -07:00
|
|
|
return undefined;
|
2023-04-02 03:10:16 +02:00
|
|
|
})
|
|
|
|
.filter(exclude(undefined))[0];
|
|
|
|
|
2023-06-21 18:06:12 +02:00
|
|
|
if (themeSrcDirPath !== undefined) {
|
|
|
|
return { themeSrcDirPath };
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const themeType of [...themeTypes, "email"]) {
|
|
|
|
if (!fs.existsSync(pathJoin(srcDirPath, themeType))) {
|
|
|
|
continue;
|
2023-04-02 03:10:16 +02:00
|
|
|
}
|
2024-05-20 15:48:51 +02:00
|
|
|
return { themeSrcDirPath: srcDirPath };
|
2023-04-02 03:10:16 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:06:12 +02:00
|
|
|
console.error(
|
|
|
|
[
|
|
|
|
"Can't locate your theme source directory. It should be either: ",
|
2023-10-08 18:12:57 -07:00
|
|
|
"src/ or src/keycloak-theme or src/keycloak_theme.",
|
2023-06-21 18:06:12 +02:00
|
|
|
"Example in the starter: https://github.com/keycloakify/keycloakify-starter/tree/main/src/keycloak-theme"
|
|
|
|
].join("\n")
|
|
|
|
);
|
|
|
|
|
|
|
|
process.exit(-1);
|
2023-04-02 03:10:16 +02:00
|
|
|
}
|