Remove the need for creating a temporary vite.json file
This commit is contained in:
@ -1,12 +1,8 @@
|
||||
import * as fs from "fs";
|
||||
import { assert } from "tsafe";
|
||||
import type { Equals } from "tsafe";
|
||||
import { z } from "zod";
|
||||
import { join as pathJoin } from "path";
|
||||
import type { OptionalIfCanBeUndefined } from "../../tools/OptionalIfCanBeUndefined";
|
||||
import { UserProvidedBuildOptions, zUserProvidedBuildOptions } from "./UserProvidedBuildOptions";
|
||||
import { UserProvidedBuildOptions } from "./UserProvidedBuildOptions";
|
||||
import * as child_process from "child_process";
|
||||
import { vitePluginSubScriptEnvNames } from "../constants";
|
||||
import { assert } from "tsafe/assert";
|
||||
|
||||
export type ResolvedViteConfig = {
|
||||
buildDir: string;
|
||||
@ -16,68 +12,30 @@ export type ResolvedViteConfig = {
|
||||
userProvidedBuildOptions: UserProvidedBuildOptions;
|
||||
};
|
||||
|
||||
const zResolvedViteConfig = z.object({
|
||||
"buildDir": z.string(),
|
||||
"publicDir": z.string(),
|
||||
"assetsDir": z.string(),
|
||||
"urlPathname": z.string().optional(),
|
||||
"userProvidedBuildOptions": zUserProvidedBuildOptions
|
||||
});
|
||||
|
||||
{
|
||||
type Got = ReturnType<(typeof zResolvedViteConfig)["parse"]>;
|
||||
type Expected = OptionalIfCanBeUndefined<ResolvedViteConfig>;
|
||||
|
||||
assert<Equals<Got, Expected>>();
|
||||
}
|
||||
|
||||
export function getResolvedViteConfig(params: { cacheDirPath: string; reactAppRootDirPath: string }): {
|
||||
export function getResolvedViteConfig(params: { reactAppRootDirPath: string }): {
|
||||
resolvedViteConfig: ResolvedViteConfig | undefined;
|
||||
} {
|
||||
const { cacheDirPath, reactAppRootDirPath } = params;
|
||||
|
||||
const resolvedViteConfigJsonFilePath = pathJoin(cacheDirPath, "vite.json");
|
||||
const { reactAppRootDirPath } = params;
|
||||
|
||||
if (fs.readdirSync(reactAppRootDirPath).find(fileBasename => fileBasename.startsWith("vite.config")) === undefined) {
|
||||
return { "resolvedViteConfig": undefined };
|
||||
}
|
||||
|
||||
child_process.execSync("npx vite", {
|
||||
"cwd": reactAppRootDirPath,
|
||||
"env": {
|
||||
...process.env,
|
||||
[vitePluginSubScriptEnvNames.createResolvedViteConfig]: resolvedViteConfigJsonFilePath
|
||||
}
|
||||
});
|
||||
const output = child_process
|
||||
.execSync("npx vite", {
|
||||
"cwd": reactAppRootDirPath,
|
||||
"env": {
|
||||
...process.env,
|
||||
[vitePluginSubScriptEnvNames.resolveViteConfig]: "true"
|
||||
}
|
||||
})
|
||||
.toString("utf8");
|
||||
|
||||
const resolvedViteConfig = (() => {
|
||||
if (!fs.existsSync(resolvedViteConfigJsonFilePath)) {
|
||||
throw new Error("Missing Keycloakify Vite plugin output.");
|
||||
}
|
||||
assert(output.includes(vitePluginSubScriptEnvNames.resolveViteConfig), "Seems like the Keycloakify's Vite plugin is not installed.");
|
||||
|
||||
let out: ResolvedViteConfig;
|
||||
const resolvedViteConfigStr = output.split(vitePluginSubScriptEnvNames.resolveViteConfig).reverse()[0];
|
||||
|
||||
try {
|
||||
out = JSON.parse(fs.readFileSync(resolvedViteConfigJsonFilePath).toString("utf8"));
|
||||
} catch {
|
||||
throw new Error("The output of the Keycloakify Vite plugin is not a valid JSON.");
|
||||
}
|
||||
|
||||
try {
|
||||
const zodParseReturn = zResolvedViteConfig.parse(out);
|
||||
|
||||
// So that objectKeys from tsafe return the expected result no matter what.
|
||||
Object.keys(zodParseReturn)
|
||||
.filter(key => !(key in out))
|
||||
.forEach(key => {
|
||||
delete (out as any)[key];
|
||||
});
|
||||
} catch {
|
||||
throw new Error("The output of the Keycloakify Vite plugin do not match the expected schema.");
|
||||
}
|
||||
|
||||
return out;
|
||||
})();
|
||||
const resolvedViteConfig: ResolvedViteConfig = JSON.parse(resolvedViteConfigStr);
|
||||
|
||||
return { resolvedViteConfig };
|
||||
}
|
||||
|
@ -11,5 +11,5 @@ export type ThemeType = (typeof themeTypes)[number];
|
||||
|
||||
export const vitePluginSubScriptEnvNames = {
|
||||
"runPostBuildScript": "KEYCLOAKIFY_RUN_POST_BUILD_SCRIPT",
|
||||
"createResolvedViteConfig": "KEYCLOAKIFY_CREATE_RESOLVED_VITE_CONFIG"
|
||||
};
|
||||
"resolveViteConfig": "KEYCLOAKIFY_RESOLVE_VITE_CONFIG"
|
||||
} as const;
|
||||
|
@ -1,6 +1,5 @@
|
||||
import { join as pathJoin, relative as pathRelative, sep as pathSep, dirname as pathDirname } from "path";
|
||||
import { join as pathJoin, relative as pathRelative, sep as pathSep } from "path";
|
||||
import type { Plugin } from "vite";
|
||||
import * as fs from "fs";
|
||||
import { nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir, keycloak_resources, vitePluginSubScriptEnvNames } from "../bin/shared/constants";
|
||||
import { id } from "tsafe/id";
|
||||
import { rm } from "../bin/tools/fs.rm";
|
||||
@ -71,36 +70,24 @@ export function keycloakify(params?: Params) {
|
||||
|
||||
buildDirPath = pathJoin(reactAppRootDirPath, resolvedConfig.build.outDir);
|
||||
|
||||
create_resolved_vite_config_case: {
|
||||
const resolvedViteConfigJsonFilePath = process.env[vitePluginSubScriptEnvNames.createResolvedViteConfig];
|
||||
resolve_vite_config_case: {
|
||||
const envValue = process.env[vitePluginSubScriptEnvNames.resolveViteConfig];
|
||||
|
||||
if (resolvedViteConfigJsonFilePath === undefined) {
|
||||
break create_resolved_vite_config_case;
|
||||
if (envValue === undefined) {
|
||||
break resolve_vite_config_case;
|
||||
}
|
||||
|
||||
{
|
||||
const dirPath = pathDirname(resolvedViteConfigJsonFilePath);
|
||||
console.log(vitePluginSubScriptEnvNames.resolveViteConfig);
|
||||
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { "recursive": true });
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
resolvedViteConfigJsonFilePath,
|
||||
Buffer.from(
|
||||
JSON.stringify(
|
||||
id<ResolvedViteConfig>({
|
||||
"publicDir": pathRelative(reactAppRootDirPath, resolvedConfig.publicDir),
|
||||
"assetsDir": resolvedConfig.build.assetsDir,
|
||||
"buildDir": resolvedConfig.build.outDir,
|
||||
urlPathname,
|
||||
userProvidedBuildOptions
|
||||
}),
|
||||
null,
|
||||
2
|
||||
),
|
||||
"utf8"
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
id<ResolvedViteConfig>({
|
||||
"publicDir": pathRelative(reactAppRootDirPath, resolvedConfig.publicDir),
|
||||
"assetsDir": resolvedConfig.build.assetsDir,
|
||||
"buildDir": resolvedConfig.build.outDir,
|
||||
urlPathname,
|
||||
userProvidedBuildOptions
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user