-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If a zod value is defaulted we should show the default, not `undefined`. This also fixes the tests so that the logs do not bleed into other tests.
- Loading branch information
Showing
6 changed files
with
131 additions
and
36 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 |
---|---|---|
|
@@ -3,7 +3,7 @@ import { cwd } from 'node:process' | |
import { createConfigLoader as createLoader } from 'unconfig' | ||
import { type ConfigEnv, type Plugin, type UserConfig, loadEnv, normalizePath } from 'vite' | ||
|
||
import { ui } from './utils/cliui.js' | ||
import { UI, initUi } from './utils/cliui.js' | ||
import { zodValidation } from './validators/zod/index.js' | ||
import { builtinValidation } from './validators/builtin/index.js' | ||
import type { FullPluginOptions, PluginOptions, Schema } from './contracts/index.js' | ||
|
@@ -47,11 +47,11 @@ function getNormalizedOptions(options: PluginOptions) { | |
/** | ||
* Log environment variables | ||
*/ | ||
function logVariables(schema: Schema, env: Record<string, string>) { | ||
function logVariables(ui: UI, variables: { key: string; value: any }[]) { | ||
ui.logger.log(`${ui.colors.cyan('[vite-plugin-validate-env]')} debug process.env content`) | ||
|
||
for (const [key] of Object.entries(schema)) { | ||
ui.logger.log(`${ui.icons.pointer} ${ui.colors.cyan(key)}: ${env[key]}`) | ||
for (const { key, value } of variables) { | ||
ui.logger.log(`${ui.icons.pointer} ${ui.colors.cyan(key)}: ${value}`) | ||
} | ||
} | ||
|
||
|
@@ -62,7 +62,12 @@ function shouldLogVariables(options: PluginOptions) { | |
/** | ||
* Main function. Will call each validator defined in the schema and throw an error if any of them fails. | ||
*/ | ||
async function validateEnv(userConfig: UserConfig, envConfig: ConfigEnv, options?: PluginOptions) { | ||
async function validateEnv( | ||
ui: UI, | ||
userConfig: UserConfig, | ||
envConfig: ConfigEnv, | ||
options?: PluginOptions, | ||
) { | ||
const rootDir = userConfig.root || cwd() | ||
|
||
const resolvedRoot = normalizePath( | ||
|
@@ -84,19 +89,7 @@ async function validateEnv(userConfig: UserConfig, envConfig: ConfigEnv, options | |
throw new Error('Missing configuration for vite-plugin-validate-env') | ||
} | ||
|
||
const { schema, validator } = getNormalizedOptions(options) | ||
const validatorFn = { | ||
builtin: builtinValidation, | ||
zod: zodValidation, | ||
}[validator] | ||
|
||
if (!validatorFn) { | ||
throw new Error(`Invalid validator "${validator}"`) | ||
} | ||
|
||
if (shouldLogVariables(options)) logVariables(schema, env) | ||
|
||
const variables = await validatorFn(env, schema as any) | ||
const variables = await validateAndLog(ui, env, options) | ||
|
||
return { | ||
define: variables.reduce( | ||
|
@@ -109,13 +102,36 @@ async function validateEnv(userConfig: UserConfig, envConfig: ConfigEnv, options | |
} | ||
} | ||
|
||
async function validateAndLog(ui: UI, env: ReturnType<typeof loadEnv>, options: PluginOptions) { | ||
const { schema, validator } = getNormalizedOptions(options) | ||
const showDebug = shouldLogVariables(options) | ||
const validate = { zod: zodValidation, builtin: builtinValidation }[validator] | ||
try { | ||
const variables = await validate(ui, env, schema as any) | ||
if (showDebug) logVariables(ui, variables) | ||
return variables | ||
} catch (error) { | ||
if (showDebug) | ||
logVariables( | ||
ui, | ||
Object.entries(schema).map(([key]) => ({ key, value: env[key] })), | ||
) | ||
throw error | ||
} | ||
} | ||
|
||
/** | ||
* Validate environment variables against a schema | ||
*/ | ||
export const ValidateEnv = (options?: PluginOptions): Plugin => ({ | ||
name: 'vite-plugin-validate-env', | ||
config: (config, env) => validateEnv(config, env, options), | ||
}) | ||
export const ValidateEnv = (options?: PluginOptions): Plugin => { | ||
const ui = initUi() | ||
return { | ||
// @ts-ignore -- only used for testing as we need to keep each instance of the plugin unique to a test | ||
Check failure on line 129 in src/index.ts GitHub Actions / lint
|
||
ui: process.env.NODE_ENV === 'testing' ? ui : undefined, | ||
name: 'vite-plugin-validate-env', | ||
config: (config, env) => validateEnv(ui, config, env, options), | ||
} | ||
} | ||
|
||
export const defineConfig = <T extends PluginOptions>(config: T): T => config | ||
|
||
|
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,3 +1,7 @@ | ||
import { cliui } from '@poppinss/cliui' | ||
|
||
export const ui = cliui({ mode: process.env.NODE_ENV === 'testing' ? 'raw' : 'normal' }) | ||
export type UI = ReturnType<typeof initUi> | ||
|
||
export function initUi() { | ||
return cliui({ mode: process.env.NODE_ENV === 'testing' ? 'raw' : 'normal' }) | ||
} |
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
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