Skip to content

Commit

Permalink
Include build information in bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox committed Dec 11, 2022
1 parent 7da7f94 commit 04f7521
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -21,6 +21,7 @@
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-replace": "^4.0.0",
"@rollup/plugin-typescript": "^8.3.2",
"@types/node": "^16.11.36",
"@typescript-eslint/eslint-plugin": "^5.43.0",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions rollup.config.js
Expand Up @@ -9,6 +9,19 @@ import typescript from "@rollup/plugin-typescript";
import { name } from "./package.json";
import { terser } from "rollup-plugin-terser";
import { defineConfig } from "rollup";
import { execSync } from "child_process";
import replace from "@rollup/plugin-replace";

const quoteCommand = command => {
return JSON.stringify(execSync(command).toString().trim());
};

const quoteCommandOrEnv = (command, env) => {
if (process.env[env]) {
return JSON.stringify(process.env[env]);
}
return quoteCommand(command);
};

export default defineConfig({
input: "src/main.ts",
Expand All @@ -29,6 +42,19 @@ export default defineConfig({
return "window";
},
plugins: [
replace({
values: {
__NAME__: JSON.stringify(name.toUpperCase()),
__BRANCH__: quoteCommand("git rev-parse --abbrev-ref HEAD"),
__COMMIT__: quoteCommandOrEnv("git rev-parse HEAD", "GITHUB_SHA"),
__VERSION__: quoteCommand("git describe --tags --dirty --always"),
__REPO_URL__: quoteCommand("git remote get-url origin").replace(
".git",
""
),
__BUILD_TIME__: JSON.stringify(new Date().toISOString())
}
}),
nodeResolve(),
commonjs(),
typescript({ include: "src/*.ts" }),
Expand Down
14 changes: 9 additions & 5 deletions src/main.ts
@@ -1,6 +1,6 @@
import { html, LitElement, PropertyValues } from "lit";
import { customElement, property } from "lit/decorators.js";
import { styleMap, StyleInfo } from "lit/directives/style-map.js";
import { StyleInfo, styleMap } from "lit/directives/style-map.js";
import { handleAction, hasAction } from "./action";
import { hass } from "card-tools/src/hass";
import {
Expand All @@ -10,7 +10,6 @@ import {
TEMPLATE_OPTIONS
} from "./const";
import { computeStateName, computeTooltip } from "./entity";
import { name, version } from "../package.json";
import deepmerge from "deepmerge";
import { actionHandler } from "./action-handler";
import "./entity-row";
Expand All @@ -33,11 +32,16 @@ import { HassEntity } from "home-assistant-js-websocket";
import styles from "./styles.css";
import { arrayToObject } from "./utils";

console.info(
`%c ${name.toUpperCase()} %c ${version} `,
console.groupCollapsed(
`%c ${__NAME__} %c ${__VERSION__} `,
`color: white; background: #039be5; font-weight: 700;`,
`color: #039be5; background: white; font-weight: 700;`
);
console.info(`branch : ${__BRANCH__}`);
console.info(`commit : ${__COMMIT__}`);
console.info(`built at : ${__BUILD_TIME__}`);
console.info(__REPO_URL__);
console.groupEnd();

const computeStateIcon = config => {
return config.state_icons && config.state_icons[config.state.toLowerCase()];
Expand All @@ -52,7 +56,7 @@ const computeStateText = config => {

const migrateIconAlignment = alignment => {
console.warn(
name,
__NAME__,
"'align_icon' and 'align_icons' is deprecated and will be removed in a future version"
);
switch (alignment) {
Expand Down
15 changes: 12 additions & 3 deletions src/types.ts
Expand Up @@ -6,6 +6,13 @@ declare global {
interface HTMLElementTagNameMap {
"hui-error-card": LovelaceCard;
}

const __NAME__: string;
const __BRANCH__: string;
const __COMMIT__: string;
const __VERSION__: string;
const __REPO_URL__: string;
const __BUILD_TIME__: string;
}

export interface TemplateConfig {
Expand All @@ -18,9 +25,11 @@ export interface TemplateConfig {

export type Template = string | TemplateConfig;

export type StyleConfig = Record<
"button" | "icon" | "name" | "state" | "ripple",
Record<string, string | Template>
export type StyleConfig = Partial<
Record<
"button" | "icon" | "name" | "state" | "ripple",
Record<string, string | Template>
>
>;

export interface ButtonConfig {
Expand Down

0 comments on commit 04f7521

Please sign in to comment.