Skip to content

Commit

Permalink
feat: provide info message on stamped version instead of warning (#9739)
Browse files Browse the repository at this point in the history
**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
  • Loading branch information
jcfranco authored Jul 15, 2024
1 parent 07d2c82 commit 6ccb12a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
9 changes: 7 additions & 2 deletions packages/calcite-components/src/tests/setupTests.ts
Original file line number Diff line number Diff line change
@@ -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();
});
17 changes: 13 additions & 4 deletions packages/calcite-components/src/utils/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
});
});
18 changes: 10 additions & 8 deletions packages/calcite-components/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions packages/calcite-components/stencil.config.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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"],
Expand Down

0 comments on commit 6ccb12a

Please sign in to comment.