@stacksjs/env 0.62.0 → 0.63.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/types.ts", "../../../../../config/env.ts"],
4
+ "sourcesContent": [
5
+ "import p from 'node:process'\nimport { projectPath } from '@stacksjs/path'\nimport { ValidationBoolean, ValidationEnum, ValidationNumber } from '@stacksjs/validation'\nimport fs from 'fs-extra'\nimport type { EnvKey } from '../../../env'\nimport type { Env } from './types'\n\ninterface EnumObject {\n [key: string]: string[]\n}\n\nexport const envEnum: EnumObject = {\n APP_ENV: ['local', 'dev', 'development', 'staging', 'prod', 'production'],\n DB_CONNECTION: ['mysql', 'sqlite', 'postgres', 'dynamodb'],\n MAIL_MAILER: ['smtp', 'mailgun', 'ses', 'postmark', 'sendmail', 'log'],\n SEARCH_ENGINE_DRIVER: ['opensearch'],\n FRONTEND_APP_ENV: ['development', 'staging', 'production'],\n}\n\nconst handler = {\n get: (target: Env, key: EnvKey) => {\n const value = target[key] as any\n\n // if value is a string but only contains numbers, and the key is not AWS_ACCOUNT_ID, return it as a number\n if (typeof value === 'string' && /^\\d+$/.test(value) && key !== 'AWS_ACCOUNT_ID') return Number(value)\n\n // if value is a string but only contains boolean values, return it as a boolean\n if (typeof value === 'string' && /^(true|false)$/.test(value)) return value === 'true'\n\n // at some point, let's see if we can remove the need for below\n if (value instanceof ValidationEnum) return target[key] as string\n\n if (value instanceof ValidationBoolean) return !!target[key]\n\n if (value instanceof ValidationNumber) return Number(target[key])\n\n return value as string\n },\n}\n\nexport function process() {\n return typeof Bun !== 'undefined' ? (Bun.env as unknown as Env) : (p.env as unknown as Env)\n}\n\nexport const env: Env = new Proxy(process(), handler)\n\nexport function writeEnv(key: EnvKey, value: string, options?: { path: string }) {\n const envPath = options?.path || projectPath('.env')\n const env = fs.readFileSync(envPath, 'utf-8')\n\n // Split the file into lines\n const lines = env.split('\\n')\n\n // Find the line with the variable we want to update\n const index = lines.findIndex((line) => line.startsWith(`${key}=`))\n\n // If the variable exists, update it\n if (index !== -1) lines[index] = `${key}=${value}`\n // Otherwise, add a new line\n else lines.push(`${key}=${value}`)\n\n // Join the lines back into a string and write it to the .env file\n fs.writeFileSync(envPath, lines.join('\\n'))\n}\n\nexport * from './types'\n",
6
+ "import { log } from '@stacksjs/logging'\nimport { schema } from '@stacksjs/validation'\nimport type { Infer, VineBoolean, VineEnum, VineNumber, VineString } from '@stacksjs/validation'\nimport env from '~/config/env'\nimport type { EnvKey } from '../../../env'\n\n// we need to get this into the right format so we can infer the type\ntype EnvValue = string | boolean | number | readonly string[]\ntype EnvType = typeof env\ntype EnvKeys = keyof EnvType\ntype EnvMap = {\n [K in EnvKeys]: EnvType[K] extends string\n ? VineString\n : EnvType[K] extends number\n ? VineNumber\n : EnvType[K] extends boolean\n ? VineBoolean\n : EnvType[K] extends readonly string[]\n ? VineEnum<string[]>\n : unknown\n}\n\ntype ValidatorType = VineString | VineNumber | VineBoolean | VineEnum<string[]>\n\nconst envStructure = Object.entries(env).reduce((acc, [key, value]) => {\n let validatorType: ValidatorType\n switch (typeof value) {\n case 'string':\n validatorType = schema.string()\n break\n case 'number':\n validatorType = schema.number()\n break\n case 'boolean':\n validatorType = schema.boolean()\n break\n default:\n if (Array.isArray(value)) {\n validatorType = schema.enum(value as string[])\n break\n }\n\n // check if is on object\n if (typeof value === 'object') {\n const schemaNameSymbol = Symbol.for('schema_name')\n const schemaName = value[schemaNameSymbol]\n // log.debug('value', value)\n // log.debug('schemaName', schemaName)\n\n if (schemaName === 'vine.string') {\n validatorType = schema.string()\n break\n }\n\n if (schemaName === 'vine.number') {\n validatorType = schema.number()\n break\n }\n\n if (schemaName === 'vine.boolean') {\n validatorType = schema.boolean()\n break\n }\n\n // if (schemaName === 'vine.enum') {\n // validatorType = schema.enum(value as string[])\n // break\n // }\n\n // oddly, enums don't trigger schemaName === 'vine.enum'\n if (!schemaName) {\n validatorType = schema.enum(value as string[])\n break\n }\n\n console.error('Unknown env value type', typeof value)\n }\n\n throw new Error(`Invalid env value for ${key}`)\n }\n const envKey = key as EnvKeys\n\n acc[envKey] = validatorType as any\n return acc\n}, {} as EnvMap)\n\nexport const envSchema = schema.object(envStructure)\nexport type Env = Infer<typeof envSchema>\n\nexport type EnvOptions = Env\nexport type EnvConfig = Partial<Record<EnvKey, EnvValue>>\n\nexport interface FrontendEnv {\n FRONTEND_APP_ENV: 'local' | 'development' | 'staging' | 'production'\n FRONTEND_APP_URL: string\n}\nexport type FrontendEnvKeys = keyof FrontendEnv\n",
7
+ "import type { EnvConfig } from '@stacksjs/env'\nimport { schema } from '@stacksjs/validation'\n\n/**\n * **Env Configuration & Validations**\n *\n * This configuration defines all of your Env validations. Because Stacks is fully-typed, you\n * may hover any of the options below and the definitions will be provided. In case you\n * have any questions, feel free to reach out via Discord or GitHub Discussions.\n */\nexport default {\n APP_NAME: schema.string(),\n APP_ENV: schema.enum(['local', 'dev', 'stage', 'prod']),\n APP_KEY: schema.string(),\n APP_URL: schema.string(),\n\n PORT: schema.number(),\n PORT_BACKEND: schema.number(),\n PORT_ADMIN: schema.number(),\n PORT_LIBRARY: schema.number(),\n PORT_DESKTOP: schema.number(),\n PORT_EMAIL: schema.number(),\n PORT_DOCS: schema.number(),\n PORT_INSPECT: schema.number(),\n PORT_API: schema.number(),\n PORT_SYSTEM_TRAY: schema.number(),\n\n APP_MAINTENANCE: schema.boolean(),\n DEBUG: schema.boolean(),\n\n API_PREFIX: schema.string(),\n DOCS_PREFIX: schema.string(),\n\n DB_CONNECTION: schema.enum(['mysql', 'sqlite', 'postgres']),\n DB_HOST: schema.string(),\n DB_PORT: schema.number(),\n DB_DATABASE: schema.string(),\n DB_USERNAME: schema.string(),\n DB_PASSWORD: schema.string(),\n\n AWS_ACCOUNT_ID: schema.string(),\n AWS_ACCESS_KEY_ID: schema.string(),\n AWS_SECRET_ACCESS_KEY: schema.string(),\n AWS_DEFAULT_REGION: schema.string(),\n AWS_DEFAULT_PASSWORD: schema.string(),\n\n MAIL_MAILER: schema.enum(['ses', 'sendmail', 'log', 'smtp']),\n MAIL_HOST: schema.string(),\n MAIL_PORT: schema.number(),\n MAIL_USERNAME: schema.string(),\n MAIL_PASSWORD: schema.string(),\n MAIL_ENCRYPTION: schema.string(),\n MAIL_FROM_NAME: schema.string(),\n MAIL_FROM_ADDRESS: schema.string(),\n\n SEARCH_ENGINE_DRIVER: schema.enum(['meilisearch', 'algolia', 'typesense']),\n MEILISEARCH_HOST: schema.string(),\n MEILISEARCH_KEY: schema.string(),\n\n FRONTEND_APP_ENV: schema.enum(['development', 'staging', 'production']),\n FRONTEND_APP_URL: schema.string(),\n} satisfies EnvConfig\n"
8
+ ],
9
+ "mappings": ";;AAAA;AACA;AACA;AACA;AAqCO,SAAS,OAAO,GAAG;AACxB,gBAAc,QAAQ,cAAe,IAAI,MAA0B,EAAE;AAAA;AAKhE,SAAS,QAAQ,CAAC,KAAa,OAAe,SAA4B;AAC/E,QAAM,UAAU,SAAS,QAAQ,YAAY,MAAM;AACnD,QAAM,OAAM,GAAG,aAAa,SAAS,OAAO;AAG5C,QAAM,QAAQ,KAAI,MAAM,IAAI;AAG5B,QAAM,QAAQ,MAAM,UAAU,CAAC,SAAS,KAAK,WAAW,GAAG,MAAM,CAAC;AAGlE,MAAI,UAAU;AAAI,UAAM,SAAS,GAAG,OAAO;AAAA;AAEtC,UAAM,KAAK,GAAG,OAAO,OAAO;AAGjC,KAAG,cAAc,SAAS,MAAM,KAAK,IAAI,CAAC;AAAA;;;AC7D5C,kBAAS;;;ACAT;AASA,IAAe;AAAA,EACb,UAAU,OAAO,OAAO;AAAA,EACxB,SAAS,OAAO,KAAK,CAAC,SAAS,OAAO,SAAS,MAAM,CAAC;AAAA,EACtD,SAAS,OAAO,OAAO;AAAA,EACvB,SAAS,OAAO,OAAO;AAAA,EAEvB,MAAM,OAAO,OAAO;AAAA,EACpB,cAAc,O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AAEA,IAAM,UAAU;AAAA,EACd,KAAK,CAAC,QAAa,QAAgB;AACjC,UAAM,QAAQ,OAAO;AAGrB,eAAW,UAAU,YAAY,QAAQ,KAAK,KAAK,KAAK,QAAQ;AAAkB,aAAO,OAAO,KAAK;AAGrG,eAAW,UAAU,YAAY,iBAAiB,KAAK,KAAK;AAAG,aAAO,UAAU;AAGhF,QAAI,iBAAiB;AAAgB,aAAO,OAAO;AAEnD,QAAI,iBAAiB;AAAmB,eAAS,OAAO;AAExD,QAAI,iBAAiB;AAAkB,aAAO,OAAO,OAAO,IAAI;AAEhE,WAAO;AAAA;AAEX;AAMO,IAAM,OAAW,IAAI,MAAM,QAAQ,GAAG,OAAO;",
10
+ "debugId": "DFC47E6E2E0CAF4964756E2164756E21",
11
+ "names": []
12
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/env",
3
3
  "type": "module",
4
- "version": "0.62.0",
4
+ "version": "0.63.0",
5
5
  "description": "Stacks env helper methods.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
package/src/types.ts CHANGED
@@ -4,7 +4,7 @@ import type { Infer, VineBoolean, VineEnum, VineNumber, VineString } from '@stac
4
4
  import env from '~/config/env'
5
5
  import type { EnvKey } from '../../../env'
6
6
 
7
- // we need to get this just into right format so we can infer the type
7
+ // we need to get this into the right format so we can infer the type
8
8
  type EnvValue = string | boolean | number | readonly string[]
9
9
  type EnvType = typeof env
10
10
  type EnvKeys = keyof EnvType
@@ -44,8 +44,8 @@ const envStructure = Object.entries(env).reduce((acc, [key, value]) => {
44
44
  if (typeof value === 'object') {
45
45
  const schemaNameSymbol = Symbol.for('schema_name')
46
46
  const schemaName = value[schemaNameSymbol]
47
- log.debug('value', value)
48
- log.debug('schemaName', schemaName)
47
+ // log.debug('value', value)
48
+ // log.debug('schemaName', schemaName)
49
49
 
50
50
  if (schemaName === 'vine.string') {
51
51
  validatorType = schema.string()