Use locate theme dir in eject script

This commit is contained in:
garronej 2023-03-25 04:56:17 +01:00
parent b83e4bef3f
commit bf6c846fac
3 changed files with 43 additions and 33 deletions

View File

@ -16,6 +16,7 @@ import { existsSync } from "fs";
import { join as pathJoin, relative as pathRelative } from "path";
import { kebabCaseToCamelCase } from "./tools/kebabCaseToSnakeCase";
import { assert, Equals } from "tsafe/assert";
import { getThemeSrcDirPath } from "./getThemeSrcDirPath";
(async () => {
const projectRootDir = getProjectRoot();
@ -50,7 +51,13 @@ import { assert, Equals } from "tsafe/assert";
const pageBasename = capitalize(kebabCaseToCamelCase(pageId)).replace(/ftl$/, "tsx");
const targetFilePath = pathJoin(process.cwd(), "src", "keycloak-theme", themeType, "pages", pageBasename);
const { themeSrcDirPath } = getThemeSrcDirPath();
if (themeSrcDirPath === undefined) {
throw new Error("Couldn't locate your theme sources");
}
const targetFilePath = pathJoin(themeSrcDirPath, themeType, "pages", pageBasename);
if (existsSync(targetFilePath)) {
console.log(`${pageId} is already ejected, ${pathRelative(process.cwd(), targetFilePath)} already exists`);

View File

@ -0,0 +1,33 @@
import { join as pathJoin } from "path";
import * as fs from "fs";
import { crawl } from "./tools/crawl";
import { exclude } from "tsafe/exclude";
const reactProjectDirPath = process.cwd();
const themeSrcDirBasename = "keycloak-theme";
export function getThemeSrcDirPath() {
const srcDirPath = pathJoin(reactProjectDirPath, "src");
const themeSrcDirPath: string | undefined = crawl(srcDirPath)
.map(fileRelativePath => {
const split = fileRelativePath.split(themeSrcDirBasename);
if (split.length !== 2) {
return undefined;
}
return pathJoin(srcDirPath, split[0] + themeSrcDirBasename);
})
.filter(exclude(undefined))[0];
if (themeSrcDirBasename === undefined) {
if (fs.existsSync(pathJoin(srcDirPath, "login")) || fs.existsSync(pathJoin(srcDirPath, "account"))) {
return { "themeSrcDirPath": srcDirPath };
}
return { "themeSrcDirPath": undefined };
}
return { themeSrcDirPath };
}

View File

@ -7,37 +7,7 @@ import { promptKeycloakVersion } from "./promptKeycloakVersion";
import * as fs from "fs";
import { getCliOptions } from "./tools/cliOptions";
import { getLogger } from "./tools/logger";
import { crawl } from "./tools/crawl";
import { exclude } from "tsafe/exclude";
const reactProjectDirPath = process.cwd();
const themeSrcDirBasename = "keycloak-theme";
function getThemeSrcDirPath() {
const srcDirPath = pathJoin(reactProjectDirPath, "src");
const themeSrcDirPath: string | undefined = crawl(srcDirPath)
.map(fileRelativePath => {
const split = fileRelativePath.split(themeSrcDirBasename);
if (split.length !== 2) {
return undefined;
}
return pathJoin(srcDirPath, split[0] + themeSrcDirBasename);
})
.filter(exclude(undefined))[0];
if (themeSrcDirBasename === undefined) {
if (fs.existsSync(pathJoin(srcDirPath, "login")) || fs.existsSync(pathJoin(srcDirPath, "account"))) {
return { "themeSrcDirPath": srcDirPath };
}
return { "themeSrcDirPath": undefined };
}
return { themeSrcDirPath };
}
import { getThemeSrcDirPath } from "./getThemeSrcDirPath";
export function getEmailThemeSrcDirPath() {
const { themeSrcDirPath } = getThemeSrcDirPath();
@ -54,7 +24,7 @@ async function main() {
const { emailThemeSrcDirPath } = getEmailThemeSrcDirPath();
if (emailThemeSrcDirPath === undefined) {
logger.warn(`Couldn't locate you ${themeSrcDirBasename} directory`);
logger.warn("Couldn't locate your theme source directory");
process.exit(-1);
}