Skip to content

Commit 38f796e

Browse files
committed
style: minor refactor
1 parent e738a7d commit 38f796e

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/index.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { cwd } from 'node:process'
33
import { createConfigLoader as createLoader } from 'unconfig'
44
import { type ConfigEnv, type Plugin, type UserConfig, loadEnv, normalizePath } from 'vite'
55

6-
import { initUi } from './ui.js'
7-
import type { UI } from './ui.js'
6+
import { initUi, type UI } from './ui.js'
87
import { zodValidation } from './validators/zod/index.js'
98
import { builtinValidation } from './validators/builtin/index.js'
109
import type { FullPluginOptions, PluginOptions, Schema } from './types.js'
@@ -14,12 +13,7 @@ import type { FullPluginOptions, PluginOptions, Schema } from './types.js'
1413
*/
1514
async function loadConfig(rootDir: string) {
1615
const loader = createLoader<PluginOptions>({
17-
sources: [
18-
{
19-
files: 'env',
20-
extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'],
21-
},
22-
],
16+
sources: [{ files: 'env', extensions: ['ts', 'cts', 'mts', 'js', 'cjs', 'mjs'] }],
2317
cwd: rootDir,
2418
})
2519

@@ -107,16 +101,19 @@ async function validateAndLog(ui: UI, env: ReturnType<typeof loadEnv>, options:
107101
const { schema, validator } = getNormalizedOptions(options)
108102
const showDebug = shouldLogVariables(options)
109103
const validate = { zod: zodValidation, builtin: builtinValidation }[validator]
104+
110105
try {
111106
const variables = await validate(ui, env, schema as any)
107+
112108
if (showDebug) logVariables(ui, variables)
109+
113110
return variables
114111
} catch (error) {
115-
if (showDebug)
116-
logVariables(
117-
ui,
118-
Object.entries(schema).map(([key]) => ({ key, value: env[key] })),
119-
)
112+
if (showDebug) {
113+
const variables = Object.entries(schema).map(([key]) => ({ key, value: env[key] }))
114+
logVariables(ui, variables)
115+
}
116+
120117
throw error
121118
}
122119
}
@@ -127,7 +124,7 @@ async function validateAndLog(ui: UI, env: ReturnType<typeof loadEnv>, options:
127124
export const ValidateEnv = (options?: PluginOptions): Plugin => {
128125
const ui = initUi()
129126
return {
130-
// @ts-expect-error -- only used for testing as we need to keep each instance of the plugin unique to a test
127+
// @ts-expect-error - only used for testing as we need to keep each instance of the plugin unique to a test
131128
ui: process.env.NODE_ENV === 'testing' ? ui : undefined,
132129
name: 'vite-plugin-validate-env',
133130
config: (config, env) => validateEnv(ui, config, env, options),

0 commit comments

Comments
 (0)