Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New ReleaseVersion component into HelpButton #310

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frontend/.changeset/funny-jokes-add.md
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`
4 changes: 2 additions & 2 deletions frontend/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",

This comment was marked as outdated.

"dev:css": "tcm src --watch",
"fmt": "pnpm run '/^fmt:.*/'",
"fmt:biome": "biome check --write --unsafe .",
Expand Down
63 changes: 63 additions & 0 deletions frontend/packages/cli/scripts/set-env.mjs
Original file line number Diff line number Diff line change
@@ -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 })
22 changes: 20 additions & 2 deletions frontend/packages/cli/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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 <ERDRenderer />
return (
<CliVersionProvider cliVersion={cliVersion}>
<ERDRenderer />
</CliVersionProvider>
)
}

export default App
14 changes: 14 additions & 0 deletions frontend/packages/cli/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -41,6 +42,7 @@ export const HelpButton = forwardRef<HTMLButtonElement>((_, ref) => {
sideOffset={4}
className={styles.menuContent}
>
<ReleaseVersion />
<DropdownMenuItem
size="sm"
leftIcon={<BookText />}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: If we use margin, it would be simple to implement without nesting divs.

Suggested change
.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);
}
.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);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see!

Original file line number Diff line number Diff line change
@@ -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 (
<CliVersionProvider cliVersion={cliVersion}>
<div className={styles.cliVersion}>
<div className={styles.cliVersionInner}>
<span>{`v${cliVersion.version}`}</span>
<span>
{' '}
{cliVersion.isReleasedGitHash ||
`+ ${cliVersion.gitHash.slice(0, 7)} `}
</span>
<span>{cliVersion.date.length > 0 && ` (${cliVersion.date})`}</span>
</div>
</div>
</CliVersionProvider>
)
}
2 changes: 2 additions & 0 deletions frontend/packages/erd-core/src/index.ts
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 frontend/packages/erd-core/src/providers/CliVersionProvider.tsx
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>
)
}
1 change: 1 addition & 0 deletions frontend/packages/erd-core/src/providers/index.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a new directory, but is it appropriate? 💭

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CliVersionProvider'
2 changes: 2 additions & 0 deletions frontend/packages/erd-core/src/schemas/cliVersion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './schemas'
export * from './types'
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(),
})
4 changes: 4 additions & 0 deletions frontend/packages/erd-core/src/schemas/cliVersion/types.ts
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>
3 changes: 3 additions & 0 deletions frontend/packages/erd-core/src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './cliVersion'
export * from './queryParam'
export * from './showMode'
Comment on lines +1 to +3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only wanted the cliVersion here, but I'm also exporting the others for consistency

Loading