-
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.
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.
- Loading branch information
1 parent
eaf40bd
commit 924806a
Showing
5 changed files
with
75 additions
and
66 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
This file was deleted.
Oops, something went wrong.
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