Skip to content

Commit

Permalink
Refactor environment variable setup and streamline build process
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hoshinotsuyoshi committed Dec 19, 2024
1 parent eaf40bd commit 924806a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 66 deletions.
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": "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 .",
Expand Down
63 changes: 0 additions & 63 deletions frontend/packages/cli/scripts/set-env.mjs

This file was deleted.

1 change: 1 addition & 0 deletions frontend/packages/cli/vite-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './set-env.js'
70 changes: 70 additions & 0 deletions frontend/packages/cli/vite-plugins/set-env.ts
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
3 changes: 2 additions & 1 deletion frontend/packages/cli/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -25,7 +26,7 @@ export default defineConfig({
],
},
},
plugins: [react(), tsconfigPaths()],
plugins: [react(), tsconfigPaths(), setEnvPlugin()],
test: {
globals: true,
environment: 'node',
Expand Down

0 comments on commit 924806a

Please sign in to comment.