keycloakify 11.0.1 → 11.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -235,9 +235,7 @@ function getBuildContext(params) {
235
235
  })
236
236
  .parse(JSON.parse(external_fs_.readFileSync(packageJsonFilePath).toString("utf8")));
237
237
  if (((_a = parsedPackageJson.dependencies) === null || _a === void 0 ? void 0 : _a.keycloakify) === undefined &&
238
- ((_b = parsedPackageJson.devDependencies) === null || _b === void 0 ? void 0 : _b.keycloakify) === undefined &&
239
- parsedPackageJson.name !== "keycloakify" // NOTE: For local storybook build
240
- ) {
238
+ ((_b = parsedPackageJson.devDependencies) === null || _b === void 0 ? void 0 : _b.keycloakify) === undefined) {
241
239
  break success;
242
240
  }
243
241
  return packageJsonFilePath;
@@ -380,22 +378,34 @@ function getBuildContext(params) {
380
378
  process.exit(-1);
381
379
  }
382
380
  const themeNames = (() => {
383
- if (buildOptions.themeName === undefined) {
384
- return parsedPackageJson.name === undefined
385
- ? ["keycloakify"]
386
- : [
387
- parsedPackageJson.name
388
- .replace(/^@(.*)/, "$1")
389
- .split("/")
390
- .join("-")
391
- ];
392
- }
393
- if (typeof buildOptions.themeName === "string") {
394
- return [buildOptions.themeName];
381
+ const themeNames = (() => {
382
+ if (buildOptions.themeName === undefined) {
383
+ return parsedPackageJson.name === undefined
384
+ ? ["keycloakify"]
385
+ : [
386
+ parsedPackageJson.name
387
+ .replace(/^@(.*)/, "$1")
388
+ .split("/")
389
+ .join("-")
390
+ ];
391
+ }
392
+ if (typeof buildOptions.themeName === "string") {
393
+ return [buildOptions.themeName];
394
+ }
395
+ const [mainThemeName, ...themeVariantNames] = buildOptions.themeName;
396
+ (0,assert.assert)(mainThemeName !== undefined);
397
+ return [mainThemeName, ...themeVariantNames];
398
+ })();
399
+ for (const themeName of themeNames) {
400
+ if (!/^[a-zA-Z0-9_-]+$/.test(themeName)) {
401
+ console.error(source_default().red([
402
+ `Invalid theme name: ${themeName}`,
403
+ `Theme names should only contain letters, numbers, and "_" or "-"`
404
+ ].join(" ")));
405
+ process.exit(-1);
406
+ }
395
407
  }
396
- const [mainThemeName, ...themeVariantNames] = buildOptions.themeName;
397
- (0,assert.assert)(mainThemeName !== undefined);
398
- return [mainThemeName, ...themeVariantNames];
408
+ return themeNames;
399
409
  })();
400
410
  const projectBuildDirPath = (() => {
401
411
  webpack: {
@@ -1,373 +0,0 @@
1
- import { transformCodebase } from "../../tools/transformCodebase";
2
- import * as fs from "fs";
3
- import { join as pathJoin, relative as pathRelative, dirname as pathDirname } from "path";
4
- import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode";
5
- import { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode";
6
- import {
7
- generateFtlFilesCodeFactory,
8
- type BuildContextLike as BuildContextLike_kcContextExclusionsFtlCode
9
- } from "../generateFtl";
10
- import {
11
- type ThemeType,
12
- LOGIN_THEME_PAGE_IDS,
13
- ACCOUNT_THEME_PAGE_IDS,
14
- WELL_KNOWN_DIRECTORY_BASE_NAME
15
- } from "../../shared/constants";
16
- import type { BuildContext } from "../../shared/buildContext";
17
- import { assert, type Equals } from "tsafe/assert";
18
- import { readFieldNameUsage } from "./readFieldNameUsage";
19
- import { readExtraPagesNames } from "./readExtraPageNames";
20
- import {
21
- generateMessageProperties,
22
- type BuildContextLike as BuildContextLike_generateMessageProperties
23
- } from "./generateMessageProperties";
24
- import { rmSync } from "../../tools/fs.rmSync";
25
- import { readThisNpmPackageVersion } from "../../tools/readThisNpmPackageVersion";
26
- import {
27
- writeMetaInfKeycloakThemes,
28
- type MetaInfKeycloakTheme
29
- } from "../../shared/metaInfKeycloakThemes";
30
- import { objectEntries } from "tsafe/objectEntries";
31
- import { escapeStringForPropertiesFile } from "../../tools/escapeStringForPropertiesFile";
32
- import * as child_process from "child_process";
33
- import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
34
-
35
- export type BuildContextLike = BuildContextLike_kcContextExclusionsFtlCode &
36
- BuildContextLike_generateMessageProperties & {
37
- extraThemeProperties: string[] | undefined;
38
- projectDirPath: string;
39
- projectBuildDirPath: string;
40
- environmentVariables: { name: string; default: string }[];
41
- implementedThemeTypes: BuildContext["implementedThemeTypes"];
42
- themeSrcDirPath: string;
43
- bundler: "vite" | "webpack";
44
- packageJsonFilePath: string;
45
- };
46
-
47
- assert<BuildContext extends BuildContextLike ? true : false>();
48
-
49
- export async function generateResourcesForMainTheme(params: {
50
- buildContext: BuildContextLike;
51
- themeName: string;
52
- resourcesDirPath: string;
53
- }): Promise<{
54
- writeMessagePropertiesFilesForThemeVariant: (params: {
55
- getMessageDirPath: (params: { themeType: ThemeType }) => string;
56
- themeName: string;
57
- }) => void;
58
- }> {
59
- const { themeName, resourcesDirPath, buildContext } = params;
60
-
61
- const getThemeTypeDirPath = (params: { themeType: ThemeType | "email" }) => {
62
- const { themeType } = params;
63
- return pathJoin(resourcesDirPath, "theme", themeName, themeType);
64
- };
65
-
66
- const writeMessagePropertiesFilesByThemeType: Partial<
67
- Record<ThemeType, (params: { messageDirPath: string; themeName: string }) => void>
68
- > = {};
69
-
70
- for (const themeType of ["login", "account"] as const) {
71
- if (!buildContext.implementedThemeTypes[themeType].isImplemented) {
72
- continue;
73
- }
74
-
75
- const isForAccountSpa =
76
- themeType === "account" &&
77
- (assert(buildContext.implementedThemeTypes.account.isImplemented),
78
- buildContext.implementedThemeTypes.account.type === "Single-Page");
79
-
80
- const themeTypeDirPath = getThemeTypeDirPath({ themeType });
81
-
82
- apply_replacers_and_move_to_theme_resources: {
83
- const destDirPath = pathJoin(
84
- themeTypeDirPath,
85
- "resources",
86
- WELL_KNOWN_DIRECTORY_BASE_NAME.DIST
87
- );
88
-
89
- // NOTE: Prevent accumulation of files in the assets dir, as names are hashed they pile up.
90
- rmSync(destDirPath, { recursive: true, force: true });
91
-
92
- if (
93
- themeType === "account" &&
94
- buildContext.implementedThemeTypes.login.isImplemented
95
- ) {
96
- // NOTE: We prevent doing it twice, it has been done for the login theme.
97
-
98
- transformCodebase({
99
- srcDirPath: pathJoin(
100
- getThemeTypeDirPath({
101
- themeType: "login"
102
- }),
103
- "resources",
104
- WELL_KNOWN_DIRECTORY_BASE_NAME.DIST
105
- ),
106
- destDirPath
107
- });
108
-
109
- break apply_replacers_and_move_to_theme_resources;
110
- }
111
-
112
- {
113
- const dirPath = pathJoin(
114
- buildContext.projectBuildDirPath,
115
- WELL_KNOWN_DIRECTORY_BASE_NAME.KEYCLOAKIFY_DEV_RESOURCES
116
- );
117
-
118
- if (fs.existsSync(dirPath)) {
119
- assert(buildContext.bundler === "webpack");
120
-
121
- throw new Error(
122
- [
123
- `Keycloakify build error: The ${WELL_KNOWN_DIRECTORY_BASE_NAME.KEYCLOAKIFY_DEV_RESOURCES} directory shouldn't exist in your build directory.`,
124
- `(${pathRelative(process.cwd(), dirPath)}).\n`,
125
- `Theses assets are only required for local development with Storybook.",
126
- "Please remove this directory as an additional step of your command.\n`,
127
- `For example: \`"build": "... && rimraf ${pathRelative(buildContext.projectDirPath, dirPath)}"\``
128
- ].join(" ")
129
- );
130
- }
131
- }
132
-
133
- transformCodebase({
134
- srcDirPath: buildContext.projectBuildDirPath,
135
- destDirPath,
136
- transformSourceCode: ({ filePath, fileRelativePath, sourceCode }) => {
137
- if (filePath.endsWith(".css")) {
138
- const { fixedCssCode } = replaceImportsInCssCode({
139
- cssCode: sourceCode.toString("utf8"),
140
- cssFileRelativeDirPath: pathDirname(fileRelativePath),
141
- buildContext
142
- });
143
-
144
- return {
145
- modifiedSourceCode: Buffer.from(fixedCssCode, "utf8")
146
- };
147
- }
148
-
149
- if (filePath.endsWith(".js")) {
150
- const { fixedJsCode } = replaceImportsInJsCode({
151
- jsCode: sourceCode.toString("utf8"),
152
- buildContext
153
- });
154
-
155
- return {
156
- modifiedSourceCode: Buffer.from(fixedJsCode, "utf8")
157
- };
158
- }
159
-
160
- return { modifiedSourceCode: sourceCode };
161
- }
162
- });
163
- }
164
-
165
- const { generateFtlFilesCode } = generateFtlFilesCodeFactory({
166
- themeName,
167
- indexHtmlCode: fs
168
- .readFileSync(pathJoin(buildContext.projectBuildDirPath, "index.html"))
169
- .toString("utf8"),
170
- buildContext,
171
- keycloakifyVersion: readThisNpmPackageVersion(),
172
- themeType,
173
- fieldNames: readFieldNameUsage({
174
- themeSrcDirPath: buildContext.themeSrcDirPath,
175
- themeType
176
- })
177
- });
178
-
179
- [
180
- ...(() => {
181
- switch (themeType) {
182
- case "login":
183
- return LOGIN_THEME_PAGE_IDS;
184
- case "account":
185
- return isForAccountSpa ? ["index.ftl"] : ACCOUNT_THEME_PAGE_IDS;
186
- }
187
- })(),
188
- ...(isForAccountSpa
189
- ? []
190
- : readExtraPagesNames({
191
- themeType,
192
- themeSrcDirPath: buildContext.themeSrcDirPath
193
- }))
194
- ].forEach(pageId => {
195
- const { ftlCode } = generateFtlFilesCode({ pageId });
196
-
197
- fs.writeFileSync(
198
- pathJoin(themeTypeDirPath, pageId),
199
- Buffer.from(ftlCode, "utf8")
200
- );
201
- });
202
-
203
- let languageTags: string[] | undefined = undefined;
204
-
205
- i18n_messages_generation: {
206
- if (isForAccountSpa) {
207
- break i18n_messages_generation;
208
- }
209
-
210
- const wrap = generateMessageProperties({
211
- buildContext,
212
- themeType
213
- });
214
-
215
- languageTags = wrap.languageTags;
216
- const { writeMessagePropertiesFiles } = wrap;
217
-
218
- writeMessagePropertiesFilesByThemeType[themeType] =
219
- writeMessagePropertiesFiles;
220
-
221
- writeMessagePropertiesFiles({
222
- messageDirPath: pathJoin(themeTypeDirPath, "messages"),
223
- themeName
224
- });
225
- }
226
-
227
- bring_in_account_v3_i18n_messages: {
228
- if (!buildContext.implementedThemeTypes.account.isImplemented) {
229
- break bring_in_account_v3_i18n_messages;
230
- }
231
- if (buildContext.implementedThemeTypes.account.type !== "Single-Page") {
232
- break bring_in_account_v3_i18n_messages;
233
- }
234
-
235
- const accountUiDirPath = child_process
236
- .execSync("npm list @keycloakify/keycloak-account-ui --parseable", {
237
- cwd: pathDirname(buildContext.packageJsonFilePath)
238
- })
239
- .toString("utf8")
240
- .trim();
241
-
242
- const messagesDirPath = pathJoin(accountUiDirPath, "messages");
243
-
244
- if (!fs.existsSync(messagesDirPath)) {
245
- throw new Error(
246
- `Please update @keycloakify/keycloak-account-ui to 25.0.4-rc.5 or later.`
247
- );
248
- }
249
-
250
- transformCodebase({
251
- srcDirPath: messagesDirPath,
252
- destDirPath: pathJoin(
253
- getThemeTypeDirPath({ themeType: "account" }),
254
- "messages"
255
- )
256
- });
257
- }
258
-
259
- keycloak_static_resources: {
260
- if (isForAccountSpa) {
261
- break keycloak_static_resources;
262
- }
263
-
264
- transformCodebase({
265
- srcDirPath: pathJoin(
266
- getThisCodebaseRootDirPath(),
267
- "res",
268
- "public",
269
- WELL_KNOWN_DIRECTORY_BASE_NAME.KEYCLOAKIFY_DEV_RESOURCES,
270
- themeType
271
- ),
272
- destDirPath: pathJoin(themeTypeDirPath, "resources")
273
- });
274
- }
275
-
276
- fs.writeFileSync(
277
- pathJoin(themeTypeDirPath, "theme.properties"),
278
- Buffer.from(
279
- [
280
- `parent=${(() => {
281
- switch (themeType) {
282
- case "account":
283
- return isForAccountSpa ? "base" : "account-v1";
284
- case "login":
285
- return "keycloak";
286
- }
287
- assert<Equals<typeof themeType, never>>(false);
288
- })()}`,
289
- ...(isForAccountSpa ? ["deprecatedMode=false"] : []),
290
- ...(buildContext.extraThemeProperties ?? []),
291
- ...buildContext.environmentVariables.map(
292
- ({ name, default: defaultValue }) =>
293
- `${name}=\${env.${name}:${escapeStringForPropertiesFile(defaultValue)}}`
294
- ),
295
- ...(languageTags === undefined
296
- ? []
297
- : [`locales=${languageTags.join(",")}`])
298
- ].join("\n\n"),
299
- "utf8"
300
- )
301
- );
302
- }
303
-
304
- email: {
305
- if (!buildContext.implementedThemeTypes.email.isImplemented) {
306
- break email;
307
- }
308
-
309
- const emailThemeSrcDirPath = pathJoin(buildContext.themeSrcDirPath, "email");
310
-
311
- transformCodebase({
312
- srcDirPath: emailThemeSrcDirPath,
313
- destDirPath: getThemeTypeDirPath({ themeType: "email" })
314
- });
315
- }
316
-
317
- bring_in_account_v1: {
318
- if (!buildContext.implementedThemeTypes.account.isImplemented) {
319
- break bring_in_account_v1;
320
- }
321
-
322
- if (buildContext.implementedThemeTypes.account.type !== "Multi-Page") {
323
- break bring_in_account_v1;
324
- }
325
-
326
- transformCodebase({
327
- srcDirPath: pathJoin(getThisCodebaseRootDirPath(), "res", "account-v1"),
328
- destDirPath: pathJoin(resourcesDirPath, "theme", "account-v1", "account")
329
- });
330
- }
331
-
332
- {
333
- const metaInfKeycloakThemes: MetaInfKeycloakTheme = { themes: [] };
334
-
335
- metaInfKeycloakThemes.themes.push({
336
- name: themeName,
337
- types: objectEntries(buildContext.implementedThemeTypes)
338
- .filter(([, { isImplemented }]) => isImplemented)
339
- .map(([themeType]) => themeType)
340
- });
341
-
342
- if (buildContext.implementedThemeTypes.account.isImplemented) {
343
- metaInfKeycloakThemes.themes.push({
344
- name: "account-v1",
345
- types: ["account"]
346
- });
347
- }
348
-
349
- writeMetaInfKeycloakThemes({
350
- resourcesDirPath,
351
- getNewMetaInfKeycloakTheme: () => metaInfKeycloakThemes
352
- });
353
- }
354
-
355
- return {
356
- writeMessagePropertiesFilesForThemeVariant: ({
357
- getMessageDirPath,
358
- themeName
359
- }) => {
360
- objectEntries(writeMessagePropertiesFilesByThemeType).forEach(
361
- ([themeType, writeMessagePropertiesFiles]) => {
362
- if (writeMessagePropertiesFiles === undefined) {
363
- return;
364
- }
365
- writeMessagePropertiesFiles({
366
- messageDirPath: getMessageDirPath({ themeType }),
367
- themeName
368
- });
369
- }
370
- );
371
- }
372
- };
373
- }
@@ -1,76 +0,0 @@
1
- import { join as pathJoin, extname as pathExtname, sep as pathSep } from "path";
2
- import { transformCodebase } from "../../tools/transformCodebase";
3
- import { writeMetaInfKeycloakThemes } from "../../shared/metaInfKeycloakThemes";
4
- import { assert } from "tsafe/assert";
5
- import type { ThemeType } from "../../shared/constants";
6
-
7
- export function generateResourcesForThemeVariant(params: {
8
- resourcesDirPath: string;
9
- themeName: string;
10
- themeVariantName: string;
11
- writeMessagePropertiesFiles: (params: {
12
- getMessageDirPath: (params: { themeType: ThemeType }) => string;
13
- themeName: string;
14
- }) => void;
15
- }) {
16
- const { resourcesDirPath, themeName, themeVariantName, writeMessagePropertiesFiles } =
17
- params;
18
-
19
- const mainThemeDirPath = pathJoin(resourcesDirPath, "theme", themeName);
20
- const themeVariantDirPath = pathJoin(mainThemeDirPath, "..", themeVariantName);
21
-
22
- transformCodebase({
23
- srcDirPath: mainThemeDirPath,
24
- destDirPath: themeVariantDirPath,
25
- transformSourceCode: ({ fileRelativePath, sourceCode }) => {
26
- if (
27
- pathExtname(fileRelativePath) === ".ftl" &&
28
- fileRelativePath.split(pathSep).length === 2
29
- ) {
30
- const modifiedSourceCode = Buffer.from(
31
- Buffer.from(sourceCode)
32
- .toString("utf-8")
33
- .replace(
34
- `"themeName": "${themeName}"`,
35
- `"themeName": "${themeVariantName}"`
36
- ),
37
- "utf8"
38
- );
39
-
40
- assert(Buffer.compare(modifiedSourceCode, sourceCode) !== 0);
41
-
42
- return { modifiedSourceCode };
43
- }
44
-
45
- return { modifiedSourceCode: sourceCode };
46
- }
47
- });
48
-
49
- writeMetaInfKeycloakThemes({
50
- resourcesDirPath,
51
- getNewMetaInfKeycloakTheme: ({ metaInfKeycloakTheme }) => {
52
- assert(metaInfKeycloakTheme !== undefined);
53
-
54
- const newMetaInfKeycloakTheme = metaInfKeycloakTheme;
55
-
56
- newMetaInfKeycloakTheme.themes.push({
57
- name: themeVariantName,
58
- types: (() => {
59
- const theme = newMetaInfKeycloakTheme.themes.find(
60
- ({ name }) => name === themeName
61
- );
62
- assert(theme !== undefined);
63
- return theme.types;
64
- })()
65
- });
66
-
67
- return newMetaInfKeycloakTheme;
68
- }
69
- });
70
-
71
- writeMessagePropertiesFiles({
72
- getMessageDirPath: ({ themeType }) =>
73
- pathJoin(themeVariantDirPath, themeType, "messages"),
74
- themeName: themeVariantName
75
- });
76
- }