From 6ccb12a17ec371ba13fe144f8d37fdfb607dfce2 Mon Sep 17 00:00:00 2001 From: JC Franco Date: Mon, 15 Jul 2024 14:37:31 -0700 Subject: [PATCH] feat: provide info message on stamped version instead of warning (#9739) **Related Issue:** #9721 ## Summary This updates the message displayed when components load to show the version, build date and revision (similar to Maps SDK). It will log an info message instead of a warning as it could be misinterpreted and would also show up multiple times when a bundled `calcite-components` version would be overridden. **Note**: `console.info` messages will be omitted by default across all tests to avoid noise --- .../calcite-components/src/tests/setupTests.ts | 9 +++++++-- .../src/utils/config.spec.ts | 17 +++++++++++++---- .../calcite-components/src/utils/config.ts | 18 ++++++++++-------- packages/calcite-components/stencil.config.ts | 3 +++ 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/packages/calcite-components/src/tests/setupTests.ts b/packages/calcite-components/src/tests/setupTests.ts index 6f4004006e1..cf9ae4f3c09 100644 --- a/packages/calcite-components/src/tests/setupTests.ts +++ b/packages/calcite-components/src/tests/setupTests.ts @@ -1,15 +1,20 @@ let globalError: jest.SpyInstance; +let globalLog: jest.SpyInstance; beforeAll(() => { globalError = jest.spyOn(global.console, "error"); + globalLog = jest.spyOn(global.console, "info").mockImplementation(() => null); }); -beforeEach(() => globalError.mockClear()); +beforeEach(() => { + globalError.mockClear(); + globalLog.mockClear(); +}); // eslint-disable-next-line jest/no-standalone-expect afterEach(() => expect(globalError).not.toHaveBeenCalled()); afterAll(() => { - globalError.mockClear(); globalError.mockRestore(); + globalLog.mockRestore(); }); diff --git a/packages/calcite-components/src/utils/config.spec.ts b/packages/calcite-components/src/utils/config.spec.ts index 2795b1e0190..8b43721e749 100644 --- a/packages/calcite-components/src/utils/config.spec.ts +++ b/packages/calcite-components/src/utils/config.spec.ts @@ -30,6 +30,8 @@ describe("config", () => { describe("stampVersion", () => { const calciteVersionPreBuildPlaceholder = "__CALCITE_VERSION__"; + beforeEach(() => delete globalThis.calciteConfig); + it("creates global config and stamps the version onto it", async () => { config = await loadConfig(); config.stampVersion(); @@ -43,12 +45,19 @@ describe("config", () => { expect(globalThis.calciteConfig.version).toBe(calciteVersionPreBuildPlaceholder); }); - it("warns if the version is already set", async () => { - globalThis.calciteConfig = { version: "1.33.7" }; + it("bails if version is already stamped onto existing config", async () => { + const testVersion = "1.33.7"; + globalThis.calciteConfig = { version: testVersion }; + config = await loadConfig(); + config.stampVersion(); + expect(globalThis.calciteConfig.version).toBe(testVersion); + }); + + it("logs info with registered version", async () => { + expect(console.info).not.toHaveBeenCalled(); config = await loadConfig(); - const warnSpy = jest.spyOn(console, "warn"); config.stampVersion(); - expect(warnSpy).toHaveBeenCalled(); + expect(console.info).toHaveBeenCalled(); }); }); }); diff --git a/packages/calcite-components/src/utils/config.ts b/packages/calcite-components/src/utils/config.ts index 05d0705ed4d..988a5250a0d 100644 --- a/packages/calcite-components/src/utils/config.ts +++ b/packages/calcite-components/src/utils/config.ts @@ -22,24 +22,26 @@ export interface CalciteConfig { version?: string; } -const customConfig: CalciteConfig = globalThis["calciteConfig"]; +const existingConfig: CalciteConfig = globalThis["calciteConfig"]; -export const focusTrapStack: FocusTrap[] = customConfig?.focusTrapStack || []; +export const focusTrapStack: FocusTrap[] = existingConfig?.focusTrapStack || []; -const version = "__CALCITE_VERSION__"; // version number is set by build +// the following placeholders are replaced by the build +const version = "__CALCITE_VERSION__"; +const buildDate = "__CALCITE_BUILD_DATE__"; +const revision = "__CALCITE_REVISION__"; /** * Stamp the version onto the global config. */ export function stampVersion(): void { - if (customConfig && customConfig.version) { - console.warn( - `[calcite-components] while initializing v${version}, an existing configuration with version "${customConfig.version}" was found. This may cause unexpected behavior. The version will not be added to the existing global configuration.`, - ); + if (existingConfig && existingConfig.version) { return; } - const target = customConfig || globalThis["calciteConfig"] || {}; + console.info(`Using Calcite Components ${version} [Date: ${buildDate}, Revision: ${revision}]`); + + const target = existingConfig || globalThis["calciteConfig"] || {}; Object.defineProperty(target, "version", { value: version, diff --git a/packages/calcite-components/stencil.config.ts b/packages/calcite-components/stencil.config.ts index 098bb854c2d..cecfb1664c6 100644 --- a/packages/calcite-components/stencil.config.ts +++ b/packages/calcite-components/stencil.config.ts @@ -1,3 +1,4 @@ +import { execSync } from "child_process"; import { Config } from "@stencil/core"; import { postcss } from "@stencil-community/postcss"; import { sass } from "@stencil/sass"; @@ -146,6 +147,8 @@ export const create: () => Config = () => ({ before: [ replace({ values: { + __CALCITE_BUILD_DATE__: () => new Date().toISOString().split("T")[0], + __CALCITE_REVISION__: execSync("git rev-parse HEAD", { encoding: "utf-8" }).trim(), __CALCITE_VERSION__: version, }, include: ["src/utils/config.ts"],