-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from liam-hq/erd-display-cli-version
New `ReleaseVersion` component into `HelpButton`
- Loading branch information
Showing
15 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@liam-hq/erd-core": patch | ||
"@liam-hq/cli": patch | ||
--- | ||
|
||
New `ReleaseVersion` component into `HelpButton` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './set-env.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
.../packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.cliVersion { | ||
margin: var(--spacing-2); | ||
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); | ||
} |
27 changes: 27 additions & 0 deletions
27
frontend/packages/erd-core/src/components/ERDRenderer/AppBar/HelpButton/ReleaseVersion.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { 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 ( | ||
<div className={styles.cliVersion}> | ||
<span>{`v${cliVersion.version}`}</span> | ||
<span> | ||
{' '} | ||
{cliVersion.isReleasedGitHash || `+ ${cliVersion.gitHash.slice(0, 7)} `} | ||
</span> | ||
<span>{cliVersion.date.length > 0 && ` (${cliVersion.date})`}</span> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './components' | ||
export * from './providers' | ||
export * from './schemas' | ||
export * from './stores' |
29 changes: 29 additions & 0 deletions
29
frontend/packages/erd-core/src/providers/CliVersionProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<CliVersionContextProps | undefined>( | ||
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 ( | ||
<CliVersionContext.Provider value={{ cliVersion }}> | ||
{children} | ||
</CliVersionContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './CliVersionProvider' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './schemas' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import type { InferOutput } from 'valibot' | ||
import type { cliVersionSchema } from './schemas' | ||
|
||
export type CliVersion = InferOutput<typeof cliVersionSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './cliVersion' | ||
export * from './queryParam' | ||
export * from './showMode' |