From 89d9208f441922d2cb2edac3006e9ccee8c3626e Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Sat, 27 Jul 2024 00:44:32 +0200 Subject: [PATCH] Fix storybook build --- scripts/build-storybook.ts | 15 +++---- ...pyKeycloakResourcesToStorybookStaticDir.ts | 18 ++++++++ scripts/start-storybook.ts | 38 ++++++++--------- src/bin/shared/buildContext.ts | 42 +++++++++---------- src/bin/shared/constants.ts | 2 + 5 files changed, 63 insertions(+), 52 deletions(-) create mode 100644 scripts/copyKeycloakResourcesToStorybookStaticDir.ts diff --git a/scripts/build-storybook.ts b/scripts/build-storybook.ts index dfb8cde3..7f617736 100644 --- a/scripts/build-storybook.ts +++ b/scripts/build-storybook.ts @@ -1,16 +1,13 @@ import * as child_process from "child_process"; -import { join } from "path"; +import { copyKeycloakResourcesToStorybookStaticDir } from "./copyKeycloakResourcesToStorybookStaticDir"; -run("yarn build"); +(async () => { + run("yarn build"); -run(`node ${join("dist", "bin", "main.js")} copy-keycloak-resources-to-public`, { - env: { - ...process.env, - PUBLIC_DIR_PATH: join(".storybook", "static") - } -}); + await copyKeycloakResourcesToStorybookStaticDir(); -run("npx build-storybook"); + run("npx build-storybook"); +})(); function run(command: string, options?: { env?: NodeJS.ProcessEnv }) { console.log(`$ ${command}`); diff --git a/scripts/copyKeycloakResourcesToStorybookStaticDir.ts b/scripts/copyKeycloakResourcesToStorybookStaticDir.ts new file mode 100644 index 00000000..b666c27b --- /dev/null +++ b/scripts/copyKeycloakResourcesToStorybookStaticDir.ts @@ -0,0 +1,18 @@ +import { join as pathJoin } from "path"; +import { copyKeycloakResourcesToPublic } from "../src/bin/shared/copyKeycloakResourcesToPublic"; +import { getProxyFetchOptions } from "../src/bin/tools/fetchProxyOptions"; +import { LOGIN_THEME_RESOURCES_FROMkEYCLOAK_VERSION_DEFAULT } from "../src/bin/shared/constants"; + +export async function copyKeycloakResourcesToStorybookStaticDir() { + await copyKeycloakResourcesToPublic({ + buildContext: { + cacheDirPath: pathJoin(__dirname, "..", "node_modules", ".cache", "scripts"), + fetchOptions: getProxyFetchOptions({ + npmConfigGetCwd: pathJoin(__dirname, "..") + }), + loginThemeResourcesFromKeycloakVersion: + LOGIN_THEME_RESOURCES_FROMkEYCLOAK_VERSION_DEFAULT, + publicDirPath: pathJoin(__dirname, "..", ".storybook", "static") + } + }); +} diff --git a/scripts/start-storybook.ts b/scripts/start-storybook.ts index e31d0986..2c966b57 100644 --- a/scripts/start-storybook.ts +++ b/scripts/start-storybook.ts @@ -1,30 +1,26 @@ import * as child_process from "child_process"; -import * as fs from "fs"; -import { join } from "path"; import { startRebuildOnSrcChange } from "./startRebuildOnSrcChange"; +import { copyKeycloakResourcesToStorybookStaticDir } from "./copyKeycloakResourcesToStorybookStaticDir"; -run("yarn build"); +(async () => { + run("yarn build"); -run(`node ${join("dist", "bin", "main.js")} copy-keycloak-resources-to-public`, { - env: { - ...process.env, - PUBLIC_DIR_PATH: join(".storybook", "static") + await copyKeycloakResourcesToStorybookStaticDir(); + + { + const child = child_process.spawn("npx", ["start-storybook", "-p", "6006"], { + shell: true + }); + + child.stdout.on("data", data => process.stdout.write(data)); + + child.stderr.on("data", data => process.stderr.write(data)); + + child.on("exit", process.exit.bind(process)); } -}); -{ - const child = child_process.spawn("npx", ["start-storybook", "-p", "6006"], { - shell: true - }); - - child.stdout.on("data", data => process.stdout.write(data)); - - child.stderr.on("data", data => process.stderr.write(data)); - - child.on("exit", process.exit.bind(process)); -} - -startRebuildOnSrcChange(); + startRebuildOnSrcChange(); +})(); function run(command: string, options?: { env?: NodeJS.ProcessEnv }) { console.log(`$ ${command}`); diff --git a/src/bin/shared/buildContext.ts b/src/bin/shared/buildContext.ts index 0405f225..d5811e75 100644 --- a/src/bin/shared/buildContext.ts +++ b/src/bin/shared/buildContext.ts @@ -14,7 +14,8 @@ import { assert, type Equals } from "tsafe/assert"; import * as child_process from "child_process"; import { VITE_PLUGIN_SUB_SCRIPTS_ENV_NAMES, - BUILD_FOR_KEYCLOAK_MAJOR_VERSION_ENV_NAME + BUILD_FOR_KEYCLOAK_MAJOR_VERSION_ENV_NAME, + LOGIN_THEME_RESOURCES_FROMkEYCLOAK_VERSION_DEFAULT } from "./constants"; import type { KeycloakVersionRange } from "./KeycloakVersionRange"; import { exclude } from "tsafe"; @@ -506,7 +507,8 @@ export function getBuildContext(params: { buildOptions.artifactId ?? `${themeNames[0]}-keycloak-theme`, loginThemeResourcesFromKeycloakVersion: - buildOptions.loginThemeResourcesFromKeycloakVersion ?? "24.0.4", + buildOptions.loginThemeResourcesFromKeycloakVersion ?? + LOGIN_THEME_RESOURCES_FROMkEYCLOAK_VERSION_DEFAULT, projectDirPath, projectBuildDirPath, keycloakifyBuildDirPath: (() => { @@ -554,27 +556,23 @@ export function getBuildContext(params: { return pathJoin(projectDirPath, resolvedViteConfig.publicDir); })(), - cacheDirPath: (() => { - const cacheDirPath = pathJoin( - (() => { - if (process.env.XDG_CACHE_HOME !== undefined) { - return getAbsoluteAndInOsFormatPath({ - pathIsh: process.env.XDG_CACHE_HOME, - cwd: process.cwd() - }); - } + cacheDirPath: pathJoin( + (() => { + if (process.env.XDG_CACHE_HOME !== undefined) { + return getAbsoluteAndInOsFormatPath({ + pathIsh: process.env.XDG_CACHE_HOME, + cwd: process.cwd() + }); + } - return pathJoin( - pathDirname(packageJsonFilePath), - "node_modules", - ".cache" - ); - })(), - "keycloakify" - ); - - return cacheDirPath; - })(), + return pathJoin( + pathDirname(packageJsonFilePath), + "node_modules", + ".cache" + ); + })(), + "keycloakify" + ), urlPathname: (() => { webpack: { if (bundler !== "webpack") { diff --git a/src/bin/shared/constants.ts b/src/bin/shared/constants.ts index 843cb8cf..255a1fdf 100644 --- a/src/bin/shared/constants.ts +++ b/src/bin/shared/constants.ts @@ -69,3 +69,5 @@ export type AccountThemePageId = (typeof ACCOUNT_THEME_PAGE_IDS)[number]; export const CONTAINER_NAME = "keycloak-keycloakify"; export const FALLBACK_LANGUAGE_TAG = "en"; + +export const LOGIN_THEME_RESOURCES_FROMkEYCLOAK_VERSION_DEFAULT = "24.0.4";