'use strict'; const path = require('node:path'); const node_process = require('node:process'); const unconfig = require('unconfig'); const cliui = require('@poppinss/cliui'); const validatorLite = require('@poppinss/validator-lite'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const path__default = /*#__PURE__*/_interopDefaultCompat(path); function initUi() { return cliui.cliui({ mode: process.env.NODE_ENV === "testing" ? "raw" : "normal" }); } function errorReporter$1(ui, errors) { let finalMessage = ui.colors.red("Failed to validate environment variables : \n"); for (const error of errors) { const errorKey = `[${ui.colors.magenta(error.key)}]`; finalMessage += ` ${errorKey}: `; const message = `Invalid value for "${error.key}" : ${error.err.issues[0].message}`; finalMessage += ` ${ui.colors.yellow(message)} `; } return finalMessage; } async function zodValidation(ui, env, schema) { const errors = []; const variables = []; for (const [key, validator] of Object.entries(schema)) { const result = validator.safeParse(env[key]); if (!result.success) { errors.push({ key, err: result.error }); continue; } if (typeof result.data === "undefined") continue; variables.push({ key, value: result.data }); } if (errors.length) { throw new Error(errorReporter$1(ui, errors)); } return variables; } function errorReporter(ui, errors) { let finalMessage = ui.colors.red("Failed to validate environment variables : \n"); for (const error of errors) { const errorKey = `[${ui.colors.magenta(error.key)}]`; finalMessage += ` ${errorKey}: `; const message = error.err.message.replace(`${error.err.code}: `, ""); finalMessage += ` ${ui.colors.yellow(message)} `; } return finalMessage; } function builtinValidation(ui, env, schema) { const errors = []; const variables = []; for (const [key, validator] of Object.entries(schema)) { try { const res = validator(key, env[key]); if (typeof res === "undefined") continue; variables.push({ key, value: res }); } catch (err) { errors.push({ key, err }); } } if (errors.length) { throw new Error(errorReporter(ui, errors)); } return variables; } async function loadOptions(rootDir, inlineConfig) { let source = "env"; if (inlineConfig && "configFile" in inlineConfig && inlineConfig.configFile) { source = inlineConfig.configFile; } const loader = unconfig.createConfigLoader({ sources: [{ files: source, extensions: ["ts", "cts", "mts", "js", "cjs", "mjs"] }], cwd: rootDir, defaults: inlineConfig }); const result = await loader.load(); const config = result.config; if (!config) throw new Error("Missing configuration for vite-plugin-validate-env"); return config; } function getNormalizedOptions(options) { let schema2; let validator; const isSchemaNested = "schema" in options && "validator" in options; if (isSchemaNested) { schema2 = options.schema; validator = options.validator; } else { validator = "builtin"; schema2 = options; } return { schema: schema2, validator }; } function logVariables(ui, variables) { ui.logger.log(`${ui.colors.cyan("[vite-plugin-validate-env]")} debug process.env content`); for (const { key, value } of variables) { ui.logger.log(`${ui.icons.pointer} ${ui.colors.cyan(key)}: ${value}`); } } function shouldLogVariables(options) { return "debug" in options && options.debug === true; } async function validateEnv(ui, userConfig, envConfig, inlineOptions) { const { normalizePath, loadEnv } = await import('vite'); const rootDir = userConfig.root || node_process.cwd(); const resolvedRoot = normalizePath( userConfig.root ? path__default.resolve(userConfig.root) : process.cwd() ); const envDir = userConfig.envDir ? normalizePath(path__default.resolve(resolvedRoot, userConfig.envDir)) : resolvedRoot; const env = loadEnv(envConfig.mode, envDir, userConfig.envPrefix); const options = await loadOptions(rootDir, inlineOptions); const variables = await validateAndLog(ui, env, options); return { define: variables.reduce( (acc, { key, value }) => { acc[`import.meta.env.${key}`] = JSON.stringify(value); return acc; }, {} ) }; } async function validateAndLog(ui, env, options) { const { schema: schema2, validator } = getNormalizedOptions(options); const showDebug = shouldLogVariables(options); const validate = { zod: zodValidation, builtin: builtinValidation }[validator]; try { const variables = await validate(ui, env, schema2); if (showDebug) logVariables(ui, variables); return variables; } catch (error) { if (showDebug) { const variables = Object.entries(schema2).map(([key]) => ({ key, value: env[key] })); logVariables(ui, variables); } throw error; } } const ValidateEnv = (options) => { const ui = initUi(); return { // @ts-expect-error - only used for testing as we need to keep each instance of the plugin unique to a test ui: process.env.NODE_ENV === "testing" ? ui : void 0, name: "vite-plugin-validate-env", config: (config, env) => validateEnv(ui, config, env, options) }; }; const defineConfig = (config) => config; exports.Schema = validatorLite.schema; exports.ValidateEnv = ValidateEnv; exports.defineConfig = defineConfig;