Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/logging #335

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@storybook/manager-webpack5": "^6.5.13",
"@storybook/react": "^6.5.13",
"@storybook/testing-library": "^0.0.13",
"@types/debug": "^4.1.7",
"@types/make-fetch-happen": "^10.0.1",
"@types/minimist": "^1.2.2",
"@types/node": "^18.15.3",
Expand Down Expand Up @@ -110,6 +111,7 @@
"@types/yazl": "^2.4.2",
"cheerio": "^1.0.0-rc.5",
"cli-select": "^1.1.2",
"debug": "^4.3.4",
"evt": "^2.4.18",
"make-fetch-happen": "^11.0.3",
"minimal-polyfills": "^2.2.2",
Expand Down
15 changes: 8 additions & 7 deletions scripts/generate-i18n-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@ import { join as pathJoin, relative as pathRelative, dirname as pathDirname, sep
import { crawl } from "../src/bin/tools/crawl";
import { downloadBuiltinKeycloakTheme } from "../src/bin/download-builtin-keycloak-theme";
import { getProjectRoot } from "../src/bin/tools/getProjectRoot";
import { getLogger } from "../src/bin/tools/logger";
import { getLogger, setLogLevel } from "../src/bin/tools/logger";

// NOTE: To run without argument when we want to generate src/i18n/generated_kcMessages files,
// update the version array for generating for newer version.

//@ts-ignore
const propertiesParser = require("properties-parser");

const isSilent = true;

const logger = getLogger({ isSilent });
const logger = getLogger("generate-i18n-messages");

async function main() {
const keycloakVersion = "21.0.1";
logger.info(`Generating i18n message files from keycloak ${keycloakVersion} sources`);

const tmpDirPath = pathJoin(getProjectRoot(), "tmp_xImOef9dOd44");

fs.rmSync(tmpDirPath, { "recursive": true, "force": true });

await downloadBuiltinKeycloakTheme({
keycloakVersion,
"destDirPath": tmpDirPath,
isSilent
"destDirPath": tmpDirPath
});

type Dictionary = { [idiomId: string]: string };
Expand Down Expand Up @@ -93,7 +91,7 @@ async function main() {
)
);

logger.log(`${filePath} wrote`);
logger.debug(`Wrote ${filePath}`);
});

fs.writeFileSync(
Expand All @@ -114,9 +112,12 @@ async function main() {
"utf8"
)
);

logger.debug(`Wrote ${pathJoin(baseMessagesDirPath, "index.ts")}`);
});
}

if (require.main === module) {
setLogLevel();
main();
}
1 change: 0 additions & 1 deletion src/bin/copy-keycloak-resources-to-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as fs from "fs";

for (const themeType of themeTypes) {
await downloadKeycloakStaticResources({
"isSilent": false,
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
"themeType": themeType,
"themeDirPath": keycloakDirInPublicDir
Expand Down
13 changes: 7 additions & 6 deletions src/bin/download-builtin-keycloak-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import { join as pathJoin } from "path";
import { downloadAndUnzip } from "./tools/downloadAndUnzip";
import { promptKeycloakVersion } from "./promptKeycloakVersion";
import { getLogger } from "./tools/logger";
import { getLogger, setLogLevel } from "./tools/logger";
import { readBuildOptions } from "./keycloakify/BuildOptions";

export async function downloadBuiltinKeycloakTheme(params: { keycloakVersion: string; destDirPath: string; isSilent: boolean }) {
const logger = getLogger("download-builtint-keycloak-theme");

export async function downloadBuiltinKeycloakTheme(params: { keycloakVersion: string; destDirPath: string }) {
const { keycloakVersion, destDirPath } = params;

await Promise.all(
Expand All @@ -25,20 +27,19 @@ async function main() {
"processArgv": process.argv.slice(2)
});

const logger = getLogger({ "isSilent": buildOptions.isSilent });
const { keycloakVersion } = await promptKeycloakVersion();

const destDirPath = pathJoin(buildOptions.keycloakifyBuildDirPath, "src", "main", "resources", "theme");

logger.log(`Downloading builtins theme of Keycloak ${keycloakVersion} here ${destDirPath}`);
logger.info(`Downloading builtins theme of Keycloak ${keycloakVersion} here ${destDirPath}`);

await downloadBuiltinKeycloakTheme({
keycloakVersion,
destDirPath,
"isSilent": buildOptions.isSilent
destDirPath
});
}

if (require.main === module) {
setLogLevel();
main();
}
18 changes: 6 additions & 12 deletions src/bin/initialize-email-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ import { downloadBuiltinKeycloakTheme } from "./download-builtin-keycloak-theme"
import { join as pathJoin, relative as pathRelative } from "path";
import { transformCodebase } from "./tools/transformCodebase";
import { promptKeycloakVersion } from "./promptKeycloakVersion";
import { readBuildOptions } from "./keycloakify/BuildOptions";
import * as fs from "fs";
import { getLogger } from "./tools/logger";
import { getLogger, setLogLevel } from "./tools/logger";
import { getEmailThemeSrcDirPath } from "./getSrcDirPath";

export async function main() {
const { isSilent } = readBuildOptions({
"projectDirPath": process.cwd(),
"processArgv": process.argv.slice(2)
});

const logger = getLogger({ isSilent });
const logger = getLogger("initialize-email-theme");

export async function main() {
const { emailThemeSrcDirPath } = getEmailThemeSrcDirPath({
"projectDirPath": process.cwd()
});
Expand All @@ -39,8 +33,7 @@ export async function main() {

await downloadBuiltinKeycloakTheme({
keycloakVersion,
"destDirPath": builtinKeycloakThemeTmpDirPath,
isSilent
"destDirPath": builtinKeycloakThemeTmpDirPath
});

transformCodebase({
Expand All @@ -54,11 +47,12 @@ export async function main() {
fs.writeFileSync(themePropertyFilePath, Buffer.from(`parent=base\n${fs.readFileSync(themePropertyFilePath).toString("utf8")}`, "utf8"));
}

logger.log(`${pathRelative(process.cwd(), emailThemeSrcDirPath)} ready to be customized, feel free to remove every file you do not customize`);
logger.info(`${pathRelative(process.cwd(), emailThemeSrcDirPath)} ready to be customized, feel free to remove every file you do not customize`);

fs.rmSync(builtinKeycloakThemeTmpDirPath, { "recursive": true, "force": true });
}

if (require.main === module) {
setLogLevel();
main();
}
5 changes: 1 addition & 4 deletions src/bin/keycloakify/BuildOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export type BuildOptions = BuildOptions.Standalone | BuildOptions.ExternalAssets

export namespace BuildOptions {
export type Common = {
isSilent: boolean;
themeVersion: string;
themeName: string;
extraLoginPages: string[] | undefined;
Expand Down Expand Up @@ -57,11 +56,10 @@ export namespace BuildOptions {
export function readBuildOptions(params: { projectDirPath: string; processArgv: string[] }): BuildOptions {
const { projectDirPath, processArgv } = params;

const { isExternalAssetsCliParamProvided, isSilentCliParamProvided } = (() => {
const { isExternalAssetsCliParamProvided } = (() => {
const argv = parseArgv(processArgv);

return {
"isSilentCliParamProvided": typeof argv["silent"] === "boolean" ? argv["silent"] : false,
"isExternalAssetsCliParamProvided": typeof argv["external-assets"] === "boolean" ? argv["external-assets"] : false
};
})();
Expand Down Expand Up @@ -153,7 +151,6 @@ export function readBuildOptions(params: { projectDirPath: string; processArgv:
"extraLoginPages": [...(extraPages ?? []), ...(extraLoginPages ?? [])],
extraAccountPages,
extraThemeProperties,
"isSilent": isSilentCliParamProvided,
"keycloakVersionDefaultAssets": keycloakVersionDefaultAssets ?? "11.0.3",
"reactAppBuildDirPath": (() => {
let { reactAppBuildDirPath = undefined } = parsedPackageJson.keycloakify ?? {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ export async function downloadKeycloakStaticResources(
params: {
themeType: ThemeType;
themeDirPath: string;
isSilent: boolean;
keycloakVersion: string;
}
) {
const { themeType, isSilent, themeDirPath, keycloakVersion } = params;
const { themeType, themeDirPath, keycloakVersion } = params;

const tmpDirPath = pathJoin(
themeDirPath,
Expand All @@ -29,8 +28,7 @@ export async function downloadKeycloakStaticResources(

await downloadBuiltinKeycloakTheme({
keycloakVersion,
"destDirPath": tmpDirPath,
isSilent
"destDirPath": tmpDirPath
});

transformCodebase({
Expand Down
3 changes: 0 additions & 3 deletions src/bin/keycloakify/generateTheme/generateTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export namespace BuildOptionsLike {
extraLoginPages?: string[];
extraAccountPages?: string[];
extraThemeProperties?: string[];
isSilent: boolean;
customUserAttributes: string[];
themeVersion: string;
keycloakVersionDefaultAssets: string;
Expand Down Expand Up @@ -182,7 +181,6 @@ export async function generateTheme(params: {
}

await downloadKeycloakStaticResources({
"isSilent": buildOptions.isSilent,
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
"themeDirPath": keycloakDirInPublicDir,
themeType
Expand All @@ -207,7 +205,6 @@ export async function generateTheme(params: {
}

await downloadKeycloakStaticResources({
"isSilent": buildOptions.isSilent,
"keycloakVersion": buildOptions.keycloakVersionDefaultAssets,
themeDirPath,
themeType
Expand Down
2 changes: 2 additions & 0 deletions src/bin/keycloakify/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node

export * from "./keycloakify";
import { setLogLevel } from "../tools/logger";
import { main } from "./keycloakify";

if (require.main === module) {
setLogLevel();
main();
}
13 changes: 7 additions & 6 deletions src/bin/keycloakify/keycloakify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { Equals } from "tsafe";
import { getEmailThemeSrcDirPath } from "../getSrcDirPath";
import { getProjectRoot } from "../tools/getProjectRoot";

const logger = getLogger();

export async function main() {
const projectDirPath = process.cwd();

Expand All @@ -20,8 +22,7 @@ export async function main() {
"processArgv": process.argv.slice(2)
});

const logger = getLogger({ "isSilent": buildOptions.isSilent });
logger.log("🔏 Building the keycloak theme...⌚");
logger.info("🔏 Building the keycloak theme...⌚");

const { doBundlesEmailTemplate } = await generateTheme({
keycloakThemeBuildingDirPath: buildOptions.keycloakifyBuildDirPath,
Expand Down Expand Up @@ -53,10 +54,10 @@ export async function main() {

switch (buildOptions.bundler) {
case "none":
logger.log("😱 Skipping bundling step, there will be no jar");
logger.info("😱 Skipping bundling step, there will be no jar");
break;
case "keycloakify":
logger.log("🫶 Let keycloakify do its thang");
logger.info("🫶 Let keycloakify do its thang");
await jar({
"rootPath": pathJoin(buildOptions.keycloakifyBuildDirPath, "src", "main", "resources"),
"version": buildOptions.themeVersion,
Expand All @@ -66,7 +67,7 @@ export async function main() {
});
break;
case "mvn":
logger.log("🫙 Run maven to deliver a jar");
logger.info("🫙 Run maven to deliver a jar");
child_process.execSync("mvn package", { "cwd": buildOptions.keycloakifyBuildDirPath });
break;
default:
Expand All @@ -82,7 +83,7 @@ export async function main() {
buildOptions
});

logger.log(
logger.info(
[
"",
`✅ Your keycloak theme has been generated and bundled into .${pathSep}${pathRelative(projectDirPath, jarFilePath)} 🚀`,
Expand Down
13 changes: 12 additions & 1 deletion src/bin/tools/downloadAndUnzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { dirname as pathDirname, join as pathJoin } from "path";
import { assert } from "tsafe";
import { promisify } from "util";
import { getProjectRoot } from "./getProjectRoot";
import { getLogger } from "./logger";
import { transformCodebase } from "./transformCodebase";
import { unzip } from "./unzip";

const log = getLogger("downloadAndUnzip");
const exec = promisify(execCallback);

function hash(s: string) {
Expand All @@ -18,9 +20,13 @@ function hash(s: string) {
async function exists(path: string) {
try {
await stat(path);
log.trace(`File '${path} exists`);
return true;
} catch (error) {
if ((error as Error & { code: string }).code === "ENOENT") return false;
if ((error as Error & { code: string }).code === "ENOENT") {
log.trace(`File '${path} does not exist`);
return false;
}
throw error;
}
}
Expand Down Expand Up @@ -50,6 +56,7 @@ async function getNmpConfig() {

async function readNpmConfig() {
const { stdout } = await exec("npm config get", { encoding: "utf8" });
log.debug(`NPM Config:\n${stdout}\n---`);
return stdout;
}

Expand All @@ -71,6 +78,7 @@ function chunks<T>(arr: T[], size: number = 2) {
}

async function readCafile(cafile: string) {
log.debug(`Reading additional ca entries from '${cafile}'`);
const cafileContent = await readFile(cafile, "utf-8");
return chunks(cafileContent.split(/(-----END CERTIFICATE-----)/), 2).map(ca => ca.join("").replace(/^\n/, "").replace(/\n/g, "\\n"));
}
Expand Down Expand Up @@ -105,8 +113,11 @@ export async function downloadAndUnzip(params: { url: string; destDirPath: strin
const zipFilePath = pathJoin(cacheRoot, "keycloakify", "zip", `_${downloadHash}.zip`);
const extractDirPath = pathJoin(cacheRoot, "keycloakify", "unzip", `_${downloadHash}`);

log.debug(`Load '${url}' to '${destDirPath}' ${pathOfDirToExtractInArchive ? ` (only ${pathOfDirToExtractInArchive})` : ""}`);

if (!(await exists(zipFilePath))) {
const opts = await getFetchOptions();
log.debug(`Load '${url}' to '${zipFilePath}' using options ${JSON.stringify(opts)}`);
const response = await fetch(url, opts);
await mkdir(pathDirname(zipFilePath), { "recursive": true });
/**
Expand Down