From b400891e01849d9b717b00933a187fcbeeef21c5 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Thu, 19 Dec 2024 08:56:48 +0900 Subject: [PATCH 1/7] feat(cli): inject new `VITE_CLI_VERSION_*` env vars into Vite build process - Introduced `scripts/set-env.mjs` to dynamically set `VITE_CLI_VERSION_*` environment variables during build and dev processes. - `VITE_CLI_VERSION_VERSION`: Package version from `package.json`. - `VITE_CLI_VERSION_GIT_HASH`: Current Git commit hash. - `VITE_CLI_VERSION_IS_RELEASED_GIT_HASH`: Flag indicating if the commit matches the latest tagged release. - `VITE_CLI_VERSION_DATE`: Commit date or fallback to the current date. --- frontend/packages/cli/package.json | 4 +- frontend/packages/cli/scripts/set-env.mjs | 63 +++++++++++++++++++++++ frontend/packages/cli/vite.config.ts | 14 +++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 frontend/packages/cli/scripts/set-env.mjs diff --git a/frontend/packages/cli/package.json b/frontend/packages/cli/package.json index 1e0f90b6..24e500d6 100644 --- a/frontend/packages/cli/package.json +++ b/frontend/packages/cli/package.json @@ -37,11 +37,11 @@ "scripts": { "build": "pnpm run '/^build:.*/'", "build:cli": "rollup -c && pnpm run cp:prism", - "build:vite": "vite build", + "build:vite": "node scripts/set-env.mjs vite build", "command:build": "node ./dist-cli/bin/cli.js erd build --input fixtures/input.schema.rb --format schemarb", "cp:prism": "cp ../db-structure/node_modules/@ruby/prism/src/prism.wasm ./dist-cli/bin/prism.wasm", "dev": "pnpm command:build && cp dist/schema.json public/ && pnpm run '/^dev:.*/'", - "dev:app": "vite", + "dev:app": "node scripts/set-env.mjs vite", "dev:css": "tcm src --watch", "fmt": "pnpm run '/^fmt:.*/'", "fmt:biome": "biome check --write --unsafe .", diff --git a/frontend/packages/cli/scripts/set-env.mjs b/frontend/packages/cli/scripts/set-env.mjs new file mode 100644 index 00000000..b9f809f0 --- /dev/null +++ b/frontend/packages/cli/scripts/set-env.mjs @@ -0,0 +1,63 @@ +import { execSync } from 'node:child_process' +import { dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' + +const __dirname = dirname(fileURLToPath(import.meta.url)) + +const packageJson = JSON.parse( + await (await import('node:fs/promises')).readFile( + join(__dirname, '../package.json'), + 'utf-8', + ), +) +const gitHash = (() => { + try { + return execSync('git rev-parse HEAD').toString().trim() + } catch (error) { + console.error('Failed to get git hash:', error) + return '' + } +})() + +const date = (() => { + try { + const gitDate = execSync('git log -1 --format=%ci').toString().trim() + return gitDate.split(' ')[0] + } catch (error) { + console.error('Failed to get git date:', error) + return new Date().toISOString().split('T')[0] // fallback to current date + } +})() + +const packageJsonVersion = packageJson.version +const latestTagName = `@liam-hq/cli@${packageJsonVersion}` +const isReleasedGitHash = (() => { + try { + execSync('git fetch --tags') + const tagCommit = execSync(`git rev-parse ${latestTagName}`) + .toString() + .trim() + if (gitHash === tagCommit) { + return 1 + } + return 0 + } catch (error) { + console.error('Failed to get git tag:', error) + return 0 + } +})() + +process.env.VITE_CLI_VERSION_VERSION = packageJsonVersion +process.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH = isReleasedGitHash +process.env.VITE_CLI_VERSION_GIT_HASH = gitHash +process.env.VITE_CLI_VERSION_DATE = date + +const args = process.argv.slice(2) +if (args.length === 0) { + console.error('No command provided to run with the environment variable.') + process.exit(1) +} + +const command = args.join(' ') + +execSync(command, { stdio: 'inherit', env: process.env }) diff --git a/frontend/packages/cli/vite.config.ts b/frontend/packages/cli/vite.config.ts index e64d99c9..4c08c225 100644 --- a/frontend/packages/cli/vite.config.ts +++ b/frontend/packages/cli/vite.config.ts @@ -25,6 +25,20 @@ export default defineConfig({ ], }, }, + define: { + 'import.meta.env.VITE_CLI_VERSION_VERSION': JSON.stringify( + process.env.VITE_CLI_VERSION_VERSION || '', + ), + 'import.meta.env.VITE_CLI_VERSION_GIT_HASH': JSON.stringify( + process.env.VITE_CLI_VERSION_GIT_HASH || '', + ), + 'import.meta.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH': JSON.stringify( + process.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH || '', + ), + 'import.meta.env.VITE_CLI_VERSION_DATE': JSON.stringify( + process.env.VITE_CLI_VERSION_DATE || '', + ), + }, plugins: [react(), tsconfigPaths()], test: { globals: true, From 6fdbbdd30840f20fa32252fd39660dde930ae547 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Thu, 19 Dec 2024 08:58:46 +0900 Subject: [PATCH 2/7] feat: add `CliVersionProvider` and schema definition for CLI version context --- .../src/providers/CliVersionProvider.tsx | 29 +++++++++++++++++++ .../packages/erd-core/src/providers/index.ts | 1 + .../erd-core/src/schemas/cliVersion/index.ts | 2 ++ .../src/schemas/cliVersion/schemas.ts | 8 +++++ .../erd-core/src/schemas/cliVersion/types.ts | 4 +++ 5 files changed, 44 insertions(+) create mode 100644 frontend/packages/erd-core/src/providers/CliVersionProvider.tsx create mode 100644 frontend/packages/erd-core/src/providers/index.ts create mode 100644 frontend/packages/erd-core/src/schemas/cliVersion/index.ts create mode 100644 frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts create mode 100644 frontend/packages/erd-core/src/schemas/cliVersion/types.ts diff --git a/frontend/packages/erd-core/src/providers/CliVersionProvider.tsx b/frontend/packages/erd-core/src/providers/CliVersionProvider.tsx new file mode 100644 index 00000000..db45cff8 --- /dev/null +++ b/frontend/packages/erd-core/src/providers/CliVersionProvider.tsx @@ -0,0 +1,29 @@ +import type { CliVersion } from '@/schemas/cliVersion' +import { type FC, type ReactNode, createContext, useContext } from 'react' + +interface CliVersionContextProps { + cliVersion: CliVersion +} + +const CliVersionContext = createContext( + undefined, +) + +export const useCliVersion = (): CliVersionContextProps => { + const context = useContext(CliVersionContext) + if (!context) { + throw new Error('useCliVersion must be used within a CliVersionProvider') + } + return context +} + +export const CliVersionProvider: FC<{ + cliVersion: CliVersion + children: ReactNode +}> = ({ cliVersion, children }) => { + return ( + + {children} + + ) +} diff --git a/frontend/packages/erd-core/src/providers/index.ts b/frontend/packages/erd-core/src/providers/index.ts new file mode 100644 index 00000000..9a99577b --- /dev/null +++ b/frontend/packages/erd-core/src/providers/index.ts @@ -0,0 +1 @@ +export * from './CliVersionProvider' diff --git a/frontend/packages/erd-core/src/schemas/cliVersion/index.ts b/frontend/packages/erd-core/src/schemas/cliVersion/index.ts new file mode 100644 index 00000000..68de0559 --- /dev/null +++ b/frontend/packages/erd-core/src/schemas/cliVersion/index.ts @@ -0,0 +1,2 @@ +export * from './schemas' +export * from './types' diff --git a/frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts b/frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts new file mode 100644 index 00000000..b01dd50e --- /dev/null +++ b/frontend/packages/erd-core/src/schemas/cliVersion/schemas.ts @@ -0,0 +1,8 @@ +import * as v from 'valibot' + +export const cliVersionSchema = v.object({ + version: v.string(), + gitHash: v.string(), + isReleasedGitHash: v.boolean(), + date: v.string(), +}) diff --git a/frontend/packages/erd-core/src/schemas/cliVersion/types.ts b/frontend/packages/erd-core/src/schemas/cliVersion/types.ts new file mode 100644 index 00000000..7022dd2b --- /dev/null +++ b/frontend/packages/erd-core/src/schemas/cliVersion/types.ts @@ -0,0 +1,4 @@ +import type { InferOutput } from 'valibot' +import type { cliVersionSchema } from './schemas' + +export type CliVersion = InferOutput From 343e01d972e41d85b47d19cc0e89f5a155544678 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Thu, 19 Dec 2024 09:01:06 +0900 Subject: [PATCH 3/7] :sparkles: New `ReleaseVersion` component into `HelpButton` - Added `ReleaseVersion` component: - Displays the current CLI version, including: - Released version (e.g., `v0.0.11 (2024-12-19)`). - Unreleased version with short Git hash (e.g., `v0.0.11 + 0d6169a (2024-12-19)`). - Fetches version information from the `CliVersionProvider`. - Updated `App.tsx`: - Initialized `cliVersion` using `import.meta.env.VITE_CLI_VERSION_*` environment variables. - Wrapped `ERDRenderer` with `CliVersionProvider` to provide version context globally. --- frontend/.changeset/funny-jokes-add.md | 6 ++++ frontend/packages/cli/src/App.tsx | 22 +++++++++++-- .../AppBar/HelpButton/HelpButton.tsx | 2 ++ .../HelpButton/ReleaseVersion.module.css | 12 +++++++ .../AppBar/HelpButton/ReleaseVersion.tsx | 32 +++++++++++++++++++ frontend/packages/erd-core/src/index.ts | 2 ++ .../packages/erd-core/src/schemas/index.ts | 3 ++ 7 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 frontend/.changeset/funny-jokes-add.md create mode 100644 frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css create mode 100644 frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx create mode 100644 frontend/packages/erd-core/src/schemas/index.ts diff --git a/frontend/.changeset/funny-jokes-add.md b/frontend/.changeset/funny-jokes-add.md new file mode 100644 index 00000000..3a1ad940 --- /dev/null +++ b/frontend/.changeset/funny-jokes-add.md @@ -0,0 +1,6 @@ +--- +"@liam-hq/erd-core": patch +"@liam-hq/cli": patch +--- + +New `ReleaseVersion` component into `HelpButton` diff --git a/frontend/packages/cli/src/App.tsx b/frontend/packages/cli/src/App.tsx index d1f31861..ed696fd6 100644 --- a/frontend/packages/cli/src/App.tsx +++ b/frontend/packages/cli/src/App.tsx @@ -1,5 +1,10 @@ import { dbStructureSchema } from '@liam-hq/db-structure' -import { ERDRenderer, initDBStructureStore } from '@liam-hq/erd-core' +import { + CliVersionProvider, + ERDRenderer, + cliVersionSchema, + initDBStructureStore, +} from '@liam-hq/erd-core' import * as v from 'valibot' async function loadSchemaContent() { @@ -20,8 +25,21 @@ async function loadSchemaContent() { loadSchemaContent() +const cliVersionData = { + version: import.meta.env.VITE_CLI_VERSION_VERSION, + gitHash: import.meta.env.VITE_CLI_VERSION_GIT_HASH, + isReleasedGitHash: + import.meta.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH === '1', + date: import.meta.env.VITE_CLI_VERSION_DATE, +} +const cliVersion = v.parse(cliVersionSchema, cliVersionData) + function App() { - return + return ( + + + + ) } export default App diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/HelpButton.tsx b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/HelpButton.tsx index 7cad801e..d4baac28 100644 --- a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/HelpButton.tsx +++ b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/HelpButton.tsx @@ -14,6 +14,7 @@ import { } from '@liam-hq/ui' import { forwardRef } from 'react' import styles from './HelpButton.module.css' +import { ReleaseVersion } from './ReleaseVersion' const handleSelect = (url: string) => () => { window.open(url, '_blank', 'noreferrer') @@ -41,6 +42,7 @@ export const HelpButton = forwardRef((_, ref) => { sideOffset={4} className={styles.menuContent} > + } diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css new file mode 100644 index 00000000..e24891f1 --- /dev/null +++ b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css @@ -0,0 +1,12 @@ +.cliVersion { + padding: var(--spacing-2); +} + +.cliVersionInner { + border-radius: var(--border-radius-full); + background: var(--pane-muted-background); + padding: var(--spacing-1) var(--spacing-2); + color: var(--global-mute-text); + font-family: var(--code-font); + font-size: var(--font-size-1); +} diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx new file mode 100644 index 00000000..213deae4 --- /dev/null +++ b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx @@ -0,0 +1,32 @@ +import { CliVersionProvider, useCliVersion } from '@/providers' +import type { FC } from 'react' +import styles from './ReleaseVersion.module.css' + +export const ReleaseVersion: FC = () => { + const { cliVersion } = useCliVersion() + + // Example output for cliVersion: + // - Released version: + // v0.0.11 (2024-12-19) + // - Unreleased version: + // v0.0.11 + 0d6169a (2024-12-19) + // + // Explanation: + // - "Released version" means the current Git hash matches a tagged release. + // - "Unreleased version" includes a short Git hash prefix to indicate changes since the last release. + return ( + +
+
+ {`v${cliVersion.version}`} + + {' '} + {cliVersion.isReleasedGitHash || + `+ ${cliVersion.gitHash.slice(0, 7)} `} + + {cliVersion.date.length > 0 && ` (${cliVersion.date})`} +
+
+
+ ) +} diff --git a/frontend/packages/erd-core/src/index.ts b/frontend/packages/erd-core/src/index.ts index 384c84dc..1d32115b 100644 --- a/frontend/packages/erd-core/src/index.ts +++ b/frontend/packages/erd-core/src/index.ts @@ -1,2 +1,4 @@ export * from './components' +export * from './providers' +export * from './schemas' export * from './stores' diff --git a/frontend/packages/erd-core/src/schemas/index.ts b/frontend/packages/erd-core/src/schemas/index.ts new file mode 100644 index 00000000..678e9682 --- /dev/null +++ b/frontend/packages/erd-core/src/schemas/index.ts @@ -0,0 +1,3 @@ +export * from './cliVersion' +export * from './queryParam' +export * from './showMode' From f9dc614071bb0213150ea424ed399db7d83450b7 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Thu, 19 Dec 2024 11:49:42 +0900 Subject: [PATCH 4/7] Remove duplicate `CliVersionProvider` calling --- .../AppBar/HelpButton/ReleaseVersion.tsx | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx index 213deae4..b2a7d75b 100644 --- a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx +++ b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx @@ -1,4 +1,4 @@ -import { CliVersionProvider, useCliVersion } from '@/providers' +import { useCliVersion } from '@/providers' import type { FC } from 'react' import styles from './ReleaseVersion.module.css' @@ -15,18 +15,16 @@ export const ReleaseVersion: FC = () => { // - "Released version" means the current Git hash matches a tagged release. // - "Unreleased version" includes a short Git hash prefix to indicate changes since the last release. return ( - -
-
- {`v${cliVersion.version}`} - - {' '} - {cliVersion.isReleasedGitHash || - `+ ${cliVersion.gitHash.slice(0, 7)} `} - - {cliVersion.date.length > 0 && ` (${cliVersion.date})`} -
+
+
+ {`v${cliVersion.version}`} + + {' '} + {cliVersion.isReleasedGitHash || + `+ ${cliVersion.gitHash.slice(0, 7)} `} + + {cliVersion.date.length > 0 && ` (${cliVersion.date})`}
- +
) } From eaf40bd86b35d0652c072c17b861f4d66bed9d8b Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Thu, 19 Dec 2024 11:47:45 +0900 Subject: [PATCH 5/7] Redundant env var defining --- frontend/packages/cli/vite.config.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/frontend/packages/cli/vite.config.ts b/frontend/packages/cli/vite.config.ts index 4c08c225..e64d99c9 100644 --- a/frontend/packages/cli/vite.config.ts +++ b/frontend/packages/cli/vite.config.ts @@ -25,20 +25,6 @@ export default defineConfig({ ], }, }, - define: { - 'import.meta.env.VITE_CLI_VERSION_VERSION': JSON.stringify( - process.env.VITE_CLI_VERSION_VERSION || '', - ), - 'import.meta.env.VITE_CLI_VERSION_GIT_HASH': JSON.stringify( - process.env.VITE_CLI_VERSION_GIT_HASH || '', - ), - 'import.meta.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH': JSON.stringify( - process.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH || '', - ), - 'import.meta.env.VITE_CLI_VERSION_DATE': JSON.stringify( - process.env.VITE_CLI_VERSION_DATE || '', - ), - }, plugins: [react(), tsconfigPaths()], test: { globals: true, From 924806af47f9b745ca9b1a6cf22d7c019725d5bc Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Thu, 19 Dec 2024 13:07:53 +0900 Subject: [PATCH 6/7] Refactor environment variable setup and streamline build process This commit removes the 'set-env.mjs' script and integrates environment variable setup directly within the Vite build process using a custom Vite plugin. This change simplifies the build and development commands by removing the dependency on the external environment setup script. --- frontend/packages/cli/package.json | 4 +- frontend/packages/cli/scripts/set-env.mjs | 63 ----------------- frontend/packages/cli/vite-plugins/index.ts | 1 + frontend/packages/cli/vite-plugins/set-env.ts | 70 +++++++++++++++++++ frontend/packages/cli/vite.config.ts | 3 +- 5 files changed, 75 insertions(+), 66 deletions(-) delete mode 100644 frontend/packages/cli/scripts/set-env.mjs create mode 100644 frontend/packages/cli/vite-plugins/index.ts create mode 100644 frontend/packages/cli/vite-plugins/set-env.ts diff --git a/frontend/packages/cli/package.json b/frontend/packages/cli/package.json index 24e500d6..1e0f90b6 100644 --- a/frontend/packages/cli/package.json +++ b/frontend/packages/cli/package.json @@ -37,11 +37,11 @@ "scripts": { "build": "pnpm run '/^build:.*/'", "build:cli": "rollup -c && pnpm run cp:prism", - "build:vite": "node scripts/set-env.mjs vite build", + "build:vite": "vite build", "command:build": "node ./dist-cli/bin/cli.js erd build --input fixtures/input.schema.rb --format schemarb", "cp:prism": "cp ../db-structure/node_modules/@ruby/prism/src/prism.wasm ./dist-cli/bin/prism.wasm", "dev": "pnpm command:build && cp dist/schema.json public/ && pnpm run '/^dev:.*/'", - "dev:app": "node scripts/set-env.mjs vite", + "dev:app": "vite", "dev:css": "tcm src --watch", "fmt": "pnpm run '/^fmt:.*/'", "fmt:biome": "biome check --write --unsafe .", diff --git a/frontend/packages/cli/scripts/set-env.mjs b/frontend/packages/cli/scripts/set-env.mjs deleted file mode 100644 index b9f809f0..00000000 --- a/frontend/packages/cli/scripts/set-env.mjs +++ /dev/null @@ -1,63 +0,0 @@ -import { execSync } from 'node:child_process' -import { dirname, join } from 'node:path' -import { fileURLToPath } from 'node:url' - -const __dirname = dirname(fileURLToPath(import.meta.url)) - -const packageJson = JSON.parse( - await (await import('node:fs/promises')).readFile( - join(__dirname, '../package.json'), - 'utf-8', - ), -) -const gitHash = (() => { - try { - return execSync('git rev-parse HEAD').toString().trim() - } catch (error) { - console.error('Failed to get git hash:', error) - return '' - } -})() - -const date = (() => { - try { - const gitDate = execSync('git log -1 --format=%ci').toString().trim() - return gitDate.split(' ')[0] - } catch (error) { - console.error('Failed to get git date:', error) - return new Date().toISOString().split('T')[0] // fallback to current date - } -})() - -const packageJsonVersion = packageJson.version -const latestTagName = `@liam-hq/cli@${packageJsonVersion}` -const isReleasedGitHash = (() => { - try { - execSync('git fetch --tags') - const tagCommit = execSync(`git rev-parse ${latestTagName}`) - .toString() - .trim() - if (gitHash === tagCommit) { - return 1 - } - return 0 - } catch (error) { - console.error('Failed to get git tag:', error) - return 0 - } -})() - -process.env.VITE_CLI_VERSION_VERSION = packageJsonVersion -process.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH = isReleasedGitHash -process.env.VITE_CLI_VERSION_GIT_HASH = gitHash -process.env.VITE_CLI_VERSION_DATE = date - -const args = process.argv.slice(2) -if (args.length === 0) { - console.error('No command provided to run with the environment variable.') - process.exit(1) -} - -const command = args.join(' ') - -execSync(command, { stdio: 'inherit', env: process.env }) diff --git a/frontend/packages/cli/vite-plugins/index.ts b/frontend/packages/cli/vite-plugins/index.ts new file mode 100644 index 00000000..d315dddb --- /dev/null +++ b/frontend/packages/cli/vite-plugins/index.ts @@ -0,0 +1 @@ +export * from './set-env.js' diff --git a/frontend/packages/cli/vite-plugins/set-env.ts b/frontend/packages/cli/vite-plugins/set-env.ts new file mode 100644 index 00000000..a0ca80a9 --- /dev/null +++ b/frontend/packages/cli/vite-plugins/set-env.ts @@ -0,0 +1,70 @@ +import { execSync } from 'node:child_process' +import { type Plugin, loadEnv } from 'vite' + +/** + * This Vite plugin initializes and sets the following environment variables for the client-side environment: + * - VITE_CLI_VERSION_VERSION: The current version of the package from package.json. + * - VITE_CLI_VERSION_IS_RELEASED_GIT_HASH: A flag indicating whether the current GIT hash corresponds to a released tag. + * - VITE_CLI_VERSION_GIT_HASH: The current GIT commit hash. + * - VITE_CLI_VERSION_DATE: The commit date of the latest commit. + * + * These variables are essential for maintaining version consistency and tracking within the deployment environment. + */ +export function setEnvPlugin(): Plugin { + const fetchGitHash = () => { + try { + return execSync('git rev-parse HEAD').toString().trim() + } catch (error) { + console.error('Failed to get git hash:', error) + return '' + } + } + + const date = () => { + try { + const gitDate = execSync('git log -1 --format=%ci').toString().trim() + return gitDate.split(' ')[0] + } catch (error) { + console.error('Failed to get git date:', error) + return new Date().toISOString().split('T')[0] // fallback to current date + } + } + + const versionPrefix = '@liam-hq/cli@' + + const isReleasedGitHash = (gitHash: string, packageJsonVersion: string) => { + const latestTagName = `${versionPrefix}${packageJsonVersion}` + try { + execSync('git fetch --tags') + const tagCommit = execSync(`git rev-parse ${latestTagName}`) + .toString() + .trim() + if (gitHash === tagCommit) { + return 1 + } + return 0 + } catch (error) { + console.error('Failed to get git tag:', error) + return 0 + } + } + + return { + name: 'set-env', + config(_, { mode }) { + const env = loadEnv(mode, process.cwd(), '') + + const packageJsonVersion = env.npm_package_version + const gitHash = fetchGitHash() + + process.env.VITE_CLI_VERSION_VERSION = packageJsonVersion + process.env.VITE_CLI_VERSION_IS_RELEASED_GIT_HASH = JSON.stringify( + isReleasedGitHash(gitHash, packageJsonVersion), + ) + process.env.VITE_CLI_VERSION_GIT_HASH = gitHash + process.env.VITE_CLI_VERSION_DATE = date() + }, + } +} + +export default setEnvPlugin diff --git a/frontend/packages/cli/vite.config.ts b/frontend/packages/cli/vite.config.ts index e64d99c9..945ea7d7 100644 --- a/frontend/packages/cli/vite.config.ts +++ b/frontend/packages/cli/vite.config.ts @@ -2,6 +2,7 @@ import { rmSync } from 'node:fs' import react from '@vitejs/plugin-react' import tsconfigPaths from 'vite-tsconfig-paths' import { defineConfig } from 'vitest/config' +import { setEnvPlugin } from './vite-plugins/index.js' const outDir = 'dist-cli/html' @@ -25,7 +26,7 @@ export default defineConfig({ ], }, }, - plugins: [react(), tsconfigPaths()], + plugins: [react(), tsconfigPaths(), setEnvPlugin()], test: { globals: true, environment: 'node', From 56217fa44222e6165be6b877e08aecaf99bad2a6 Mon Sep 17 00:00:00 2001 From: hoshinotsuyoshi Date: Thu, 19 Dec 2024 15:31:52 +0900 Subject: [PATCH 7/7] Refactor `ReleaseVersion` component to reduce DOM nesting --- .../AppBar/HelpButton/ReleaseVersion.module.css | 5 +---- .../AppBar/HelpButton/ReleaseVersion.tsx | 15 ++++++--------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css index e24891f1..60f2c826 100644 --- a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css +++ b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css @@ -1,8 +1,5 @@ .cliVersion { - padding: var(--spacing-2); -} - -.cliVersionInner { + margin: var(--spacing-2); border-radius: var(--border-radius-full); background: var(--pane-muted-background); padding: var(--spacing-1) var(--spacing-2); diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx index b2a7d75b..1a2bb8dc 100644 --- a/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx +++ b/frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx @@ -16,15 +16,12 @@ export const ReleaseVersion: FC = () => { // - "Unreleased version" includes a short Git hash prefix to indicate changes since the last release. return (
-
- {`v${cliVersion.version}`} - - {' '} - {cliVersion.isReleasedGitHash || - `+ ${cliVersion.gitHash.slice(0, 7)} `} - - {cliVersion.date.length > 0 && ` (${cliVersion.date})`} -
+ {`v${cliVersion.version}`} + + {' '} + {cliVersion.isReleasedGitHash || `+ ${cliVersion.gitHash.slice(0, 7)} `} + + {cliVersion.date.length > 0 && ` (${cliVersion.date})`}
) }