Store vite plugin output in cache dir path

This commit is contained in:
Joseph Garrone 2024-02-11 16:17:38 +01:00
parent 9706338182
commit b5cfdb9d0a
9 changed files with 86 additions and 118 deletions

View File

@ -1,14 +0,0 @@
import fs from "fs";
import path from "path";
import zodToJsonSchema from "zod-to-json-schema";
import { zParsedPackageJson } from "../src/bin/keycloakify/parsedPackageJson";
const jsonSchemaName = "keycloakifyPackageJsonSchema";
const jsonSchema = zodToJsonSchema(zParsedPackageJson, jsonSchemaName);
const baseProperties = {
// merges package.json schema with keycloakify properties
"allOf": [{ "$ref": "https://json.schemastore.org/package.json" }, { "$ref": jsonSchemaName }]
};
fs.writeFileSync(path.join(process.cwd(), "keycloakify-json-schema.json"), JSON.stringify({ ...baseProperties, ...jsonSchema }, null, 2));

View File

@ -2,7 +2,7 @@ export const nameOfTheGlobal = "kcContext";
export const keycloak_resources = "keycloak-resources"; export const keycloak_resources = "keycloak-resources";
export const resources_common = "resources-common"; export const resources_common = "resources-common";
export const lastKeycloakVersionWithAccountV1 = "21.1.2"; export const lastKeycloakVersionWithAccountV1 = "21.1.2";
export const resolvedViteConfigJsonBasename = ".keycloakifyViteConfig.json"; export const resolvedViteConfigJsonBasename = "vite.json";
export const basenameOfTheKeycloakifyResourcesDir = "build"; export const basenameOfTheKeycloakifyResourcesDir = "build";
export const themeTypes = ["login", "account"] as const; export const themeTypes = ["login", "account"] as const;

View File

@ -11,11 +11,14 @@ import { kebabCaseToCamelCase } from "./tools/kebabCaseToSnakeCase";
import { assert, Equals } from "tsafe/assert"; import { assert, Equals } from "tsafe/assert";
import { getThemeSrcDirPath } from "./getThemeSrcDirPath"; import { getThemeSrcDirPath } from "./getThemeSrcDirPath";
import { themeTypes, type ThemeType } from "./constants"; import { themeTypes, type ThemeType } from "./constants";
import { getReactAppRootDirPath } from "./keycloakify/buildOptions/getReactAppRootDirPath";
(async () => { (async () => {
console.log("Select a theme type"); console.log("Select a theme type");
const reactAppRootDirPath = process.cwd(); const { reactAppRootDirPath } = getReactAppRootDirPath({
"processArgv": process.argv.slice(2)
});
const { value: themeType } = await cliSelect<ThemeType>({ const { value: themeType } = await cliSelect<ThemeType>({
"values": [...themeTypes] "values": [...themeTypes]

View File

@ -4,7 +4,9 @@ import { join as pathJoin } from "path";
import parseArgv from "minimist"; import parseArgv from "minimist";
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath"; import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
import { readResolvedViteConfig } from "./resolvedViteConfig"; import { readResolvedViteConfig } from "./resolvedViteConfig";
import { getKeycloakifyBuildDirPath } from "./getKeycloakifyBuildDirPath"; import * as fs from "fs";
import { getCacheDirPath } from "./getCacheDirPath";
import { getReactAppRootDirPath } from "./getReactAppRootDirPath";
/** Consolidated build option gathered form CLI arguments and config in package.json */ /** Consolidated build option gathered form CLI arguments and config in package.json */
export type BuildOptions = { export type BuildOptions = {
@ -33,29 +35,18 @@ export type BuildOptions = {
export function readBuildOptions(params: { processArgv: string[] }): BuildOptions { export function readBuildOptions(params: { processArgv: string[] }): BuildOptions {
const { processArgv } = params; const { processArgv } = params;
const argv = parseArgv(processArgv); const { reactAppRootDirPath } = getReactAppRootDirPath({ processArgv });
const reactAppRootDirPath = (() => { const { cacheDirPath } = getCacheDirPath({ reactAppRootDirPath });
const arg = argv["project"] ?? argv["p"];
if (typeof arg !== "string") { const { resolvedViteConfig } = readResolvedViteConfig({ cacheDirPath });
return process.cwd();
}
return getAbsoluteAndInOsFormatPath({ if (resolvedViteConfig === undefined && fs.existsSync(pathJoin(reactAppRootDirPath, "vite.config.ts"))) {
"pathIsh": arg, throw new Error("Keycloakify's Vite plugin output not found");
"cwd": process.cwd() }
});
})();
const parsedPackageJson = readParsedPackageJson({ reactAppRootDirPath }); const parsedPackageJson = readParsedPackageJson({ reactAppRootDirPath });
const { resolvedViteConfig } =
readResolvedViteConfig({
"parsedPackageJson_keycloakify_keycloakifyBuildDirPath": parsedPackageJson.keycloakify?.keycloakifyBuildDirPath,
reactAppRootDirPath
}) ?? {};
const themeNames = (() => { const themeNames = (() => {
if (parsedPackageJson.keycloakify?.themeName === undefined) { if (parsedPackageJson.keycloakify?.themeName === undefined) {
return [ return [
@ -73,12 +64,6 @@ export function readBuildOptions(params: { processArgv: string[] }): BuildOption
return parsedPackageJson.keycloakify.themeName; return parsedPackageJson.keycloakify.themeName;
})(); })();
const { keycloakifyBuildDirPath } = getKeycloakifyBuildDirPath({
"parsedPackageJson_keycloakify_keycloakifyBuildDirPath": parsedPackageJson.keycloakify?.keycloakifyBuildDirPath,
reactAppRootDirPath,
"bundler": resolvedViteConfig !== undefined ? "vite" : "webpack"
});
const reactAppBuildDirPath = (() => { const reactAppBuildDirPath = (() => {
webpack: { webpack: {
if (resolvedViteConfig !== undefined) { if (resolvedViteConfig !== undefined) {
@ -98,6 +83,8 @@ export function readBuildOptions(params: { processArgv: string[] }): BuildOption
return pathJoin(reactAppRootDirPath, resolvedViteConfig.buildDir); return pathJoin(reactAppRootDirPath, resolvedViteConfig.buildDir);
})(); })();
const argv = parseArgv(processArgv);
return { return {
"bundler": resolvedViteConfig !== undefined ? "vite" : "webpack", "bundler": resolvedViteConfig !== undefined ? "vite" : "webpack",
"isSilent": typeof argv["silent"] === "boolean" ? argv["silent"] : false, "isSilent": typeof argv["silent"] === "boolean" ? argv["silent"] : false,
@ -124,7 +111,16 @@ export function readBuildOptions(params: { processArgv: string[] }): BuildOption
"loginThemeResourcesFromKeycloakVersion": parsedPackageJson.keycloakify?.loginThemeResourcesFromKeycloakVersion ?? "11.0.3", "loginThemeResourcesFromKeycloakVersion": parsedPackageJson.keycloakify?.loginThemeResourcesFromKeycloakVersion ?? "11.0.3",
reactAppRootDirPath, reactAppRootDirPath,
reactAppBuildDirPath, reactAppBuildDirPath,
keycloakifyBuildDirPath, "keycloakifyBuildDirPath": (() => {
if (parsedPackageJson.keycloakify?.keycloakifyBuildDirPath !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": parsedPackageJson.keycloakify?.keycloakifyBuildDirPath,
"cwd": reactAppRootDirPath
});
}
return resolvedViteConfig?.buildDir === undefined ? "build_keycloak" : `${resolvedViteConfig.buildDir}_keycloak`;
})(),
"publicDirPath": (() => { "publicDirPath": (() => {
webpack: { webpack: {
if (resolvedViteConfig !== undefined) { if (resolvedViteConfig !== undefined) {
@ -143,19 +139,7 @@ export function readBuildOptions(params: { processArgv: string[] }): BuildOption
return pathJoin(reactAppRootDirPath, resolvedViteConfig.publicDir); return pathJoin(reactAppRootDirPath, resolvedViteConfig.publicDir);
})(), })(),
"cacheDirPath": pathJoin( cacheDirPath,
(() => {
if (process.env.XDG_CACHE_HOME !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": process.env.XDG_CACHE_HOME,
"cwd": reactAppRootDirPath
});
}
return pathJoin(reactAppRootDirPath, "node_modules", ".cache");
})(),
"keycloakify"
),
"urlPathname": (() => { "urlPathname": (() => {
webpack: { webpack: {
if (resolvedViteConfig !== undefined) { if (resolvedViteConfig !== undefined) {

View File

@ -0,0 +1,23 @@
import { join as pathJoin } from "path";
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
export function getCacheDirPath(params: { reactAppRootDirPath: string }) {
const { reactAppRootDirPath } = params;
const cacheDirPath = pathJoin(
(() => {
if (process.env.XDG_CACHE_HOME !== undefined) {
return getAbsoluteAndInOsFormatPath({
"pathIsh": process.env.XDG_CACHE_HOME,
"cwd": reactAppRootDirPath
});
}
// TODO: Recursively look up
return pathJoin(reactAppRootDirPath, "node_modules", ".cache");
})(),
"keycloakify"
);
return { cacheDirPath };
}

View File

@ -1,33 +0,0 @@
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
import { join as pathJoin } from "path";
export function getKeycloakifyBuildDirPath(params: {
reactAppRootDirPath: string;
parsedPackageJson_keycloakify_keycloakifyBuildDirPath: string | undefined;
bundler: "vite" | "webpack";
}) {
const { reactAppRootDirPath, parsedPackageJson_keycloakify_keycloakifyBuildDirPath, bundler } = params;
const keycloakifyBuildDirPath = (() => {
if (parsedPackageJson_keycloakify_keycloakifyBuildDirPath !== undefined) {
getAbsoluteAndInOsFormatPath({
"pathIsh": parsedPackageJson_keycloakify_keycloakifyBuildDirPath,
"cwd": reactAppRootDirPath
});
}
return pathJoin(
reactAppRootDirPath,
`${(() => {
switch (bundler) {
case "vite":
return "dist";
case "webpack":
return "build";
}
})()}_keycloak`
);
})();
return { keycloakifyBuildDirPath };
}

View File

@ -0,0 +1,23 @@
import parseArgv from "minimist";
import { getAbsoluteAndInOsFormatPath } from "../../tools/getAbsoluteAndInOsFormatPath";
export function getReactAppRootDirPath(params: { processArgv: string[] }) {
const { processArgv } = params;
const argv = parseArgv(processArgv);
const reactAppRootDirPath = (() => {
const arg = argv["project"] ?? argv["p"];
if (typeof arg !== "string") {
return process.cwd();
}
return getAbsoluteAndInOsFormatPath({
"pathIsh": arg,
"cwd": process.cwd()
});
})();
return { reactAppRootDirPath };
}

View File

@ -5,7 +5,6 @@ import { z } from "zod";
import { join as pathJoin } from "path"; import { join as pathJoin } from "path";
import { resolvedViteConfigJsonBasename } from "../../constants"; import { resolvedViteConfigJsonBasename } from "../../constants";
import type { OptionalIfCanBeUndefined } from "../../tools/OptionalIfCanBeUndefined"; import type { OptionalIfCanBeUndefined } from "../../tools/OptionalIfCanBeUndefined";
import { getKeycloakifyBuildDirPath } from "./getKeycloakifyBuildDirPath";
export type ResolvedViteConfig = { export type ResolvedViteConfig = {
buildDir: string; buildDir: string;
@ -28,31 +27,18 @@ const zResolvedViteConfig = z.object({
assert<Equals<Got, Expected>>(); assert<Equals<Got, Expected>>();
} }
export function readResolvedViteConfig(params: { export function readResolvedViteConfig(params: { cacheDirPath: string }): {
reactAppRootDirPath: string; resolvedViteConfig: ResolvedViteConfig | undefined;
parsedPackageJson_keycloakify_keycloakifyBuildDirPath: string | undefined; } {
}): const { cacheDirPath } = params;
| {
resolvedViteConfig: ResolvedViteConfig;
}
| undefined {
const { reactAppRootDirPath, parsedPackageJson_keycloakify_keycloakifyBuildDirPath } = params;
const viteConfigTsFilePath = pathJoin(reactAppRootDirPath, "vite.config.ts"); const resolvedViteConfigJsonFilePath = pathJoin(cacheDirPath, resolvedViteConfigJsonBasename);
if (!fs.existsSync(viteConfigTsFilePath)) { if (!fs.existsSync(resolvedViteConfigJsonFilePath)) {
return undefined; return { "resolvedViteConfig": undefined };
} }
const { keycloakifyBuildDirPath } = getKeycloakifyBuildDirPath({
reactAppRootDirPath,
parsedPackageJson_keycloakify_keycloakifyBuildDirPath,
"bundler": "vite"
});
const resolvedViteConfig = (() => { const resolvedViteConfig = (() => {
const resolvedViteConfigJsonFilePath = pathJoin(keycloakifyBuildDirPath, resolvedViteConfigJsonBasename);
if (!fs.existsSync(resolvedViteConfigJsonFilePath)) { if (!fs.existsSync(resolvedViteConfigJsonFilePath)) {
throw new Error("Missing Keycloakify Vite plugin output."); throw new Error("Missing Keycloakify Vite plugin output.");
} }

View File

@ -1,11 +1,10 @@
import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path"; import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path";
import { readParsedPackageJson } from "../bin/keycloakify/buildOptions/parsedPackageJson";
import type { Plugin } from "vite"; import type { Plugin } from "vite";
import { assert } from "tsafe/assert"; import { assert } from "tsafe/assert";
import * as fs from "fs"; import * as fs from "fs";
import { resolvedViteConfigJsonBasename, nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, keycloak_resources } from "../bin/constants"; import { resolvedViteConfigJsonBasename, nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, keycloak_resources } from "../bin/constants";
import type { ResolvedViteConfig } from "../bin/keycloakify/buildOptions/resolvedViteConfig"; import type { ResolvedViteConfig } from "../bin/keycloakify/buildOptions/resolvedViteConfig";
import { getKeycloakifyBuildDirPath } from "../bin/keycloakify/buildOptions/getKeycloakifyBuildDirPath"; import { getCacheDirPath } from "../bin/keycloakify/buildOptions/getCacheDirPath";
import { replaceAll } from "../bin/tools/String.prototype.replaceAll"; import { replaceAll } from "../bin/tools/String.prototype.replaceAll";
import { id } from "tsafe/id"; import { id } from "tsafe/id";
import { rm } from "../bin/tools/fs.rm"; import { rm } from "../bin/tools/fs.rm";
@ -40,19 +39,16 @@ export function keycloakify(): Plugin {
buildDirPath = pathJoin(reactAppRootDirPath, resolvedConfig.build.outDir); buildDirPath = pathJoin(reactAppRootDirPath, resolvedConfig.build.outDir);
const { keycloakifyBuildDirPath } = getKeycloakifyBuildDirPath({ const { cacheDirPath } = getCacheDirPath({
"parsedPackageJson_keycloakify_keycloakifyBuildDirPath": readParsedPackageJson({ reactAppRootDirPath }).keycloakify reactAppRootDirPath
?.keycloakifyBuildDirPath,
reactAppRootDirPath,
"bundler": "vite"
}); });
if (!fs.existsSync(keycloakifyBuildDirPath)) { if (!fs.existsSync(cacheDirPath)) {
fs.mkdirSync(keycloakifyBuildDirPath); fs.mkdirSync(cacheDirPath);
} }
fs.writeFileSync( fs.writeFileSync(
pathJoin(keycloakifyBuildDirPath, resolvedViteConfigJsonBasename), pathJoin(cacheDirPath, resolvedViteConfigJsonBasename),
Buffer.from( Buffer.from(
JSON.stringify( JSON.stringify(
id<ResolvedViteConfig>({ id<ResolvedViteConfig>({