@stacksjs/env 0.59.11 → 0.61.1

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/env",
3
3
  "type": "module",
4
- "version": "0.59.11",
4
+ "version": "0.61.1",
5
5
  "description": "Stacks env helper methods.",
6
6
  "author": "Chris Breuer",
7
7
  "license": "MIT",
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import p from 'node:process'
2
- import fs from 'fs-extra'
3
2
  import { projectPath } from '@stacksjs/path'
4
3
  import { ValidationBoolean, ValidationEnum, ValidationNumber } from '@stacksjs/validation'
4
+ import fs from 'fs-extra'
5
5
  import type { EnvKey } from '../../../env'
6
6
  import type { Env } from './types'
7
7
 
@@ -9,11 +9,11 @@ interface EnumObject {
9
9
  [key: string]: string[]
10
10
  }
11
11
 
12
- export const enums: EnumObject = {
12
+ export const envEnum: EnumObject = {
13
13
  APP_ENV: ['local', 'dev', 'development', 'staging', 'prod', 'production'],
14
- DB_CONNECTION: ['mysql', 'sqlite', 'postgres', 'planetscale'],
14
+ DB_CONNECTION: ['mysql', 'sqlite', 'postgres', 'dynamodb'],
15
15
  MAIL_MAILER: ['smtp', 'mailgun', 'ses', 'postmark', 'sendmail', 'log'],
16
- SEARCH_ENGINE_DRIVER: ['meilisearch', 'algolia', 'typesense'],
16
+ SEARCH_ENGINE_DRIVER: ['opensearch'],
17
17
  FRONTEND_APP_ENV: ['development', 'staging', 'production'],
18
18
  }
19
19
 
@@ -22,31 +22,24 @@ const handler = {
22
22
  const value = target[key] as any
23
23
 
24
24
  // if value is a string but only contains numbers, and the key is not AWS_ACCOUNT_ID, return it as a number
25
- if (typeof value === 'string' && /^\d+$/.test(value) && key !== 'AWS_ACCOUNT_ID')
26
- return Number(value)
25
+ if (typeof value === 'string' && /^\d+$/.test(value) && key !== 'AWS_ACCOUNT_ID') return Number(value)
27
26
 
28
27
  // if value is a string but only contains boolean values, return it as a boolean
29
- if (typeof value === 'string' && /^(true|false)$/.test(value))
30
- return value === 'true'
28
+ if (typeof value === 'string' && /^(true|false)$/.test(value)) return value === 'true'
31
29
 
32
30
  // at some point, let's see if we can remove the need for below
33
- if (value instanceof ValidationEnum)
34
- return target[key] as string
31
+ if (value instanceof ValidationEnum) return target[key] as string
35
32
 
36
- if (value instanceof ValidationBoolean)
37
- return !!target[key]
33
+ if (value instanceof ValidationBoolean) return !!target[key]
38
34
 
39
- if (value instanceof ValidationNumber)
40
- return Number(target[key])
35
+ if (value instanceof ValidationNumber) return Number(target[key])
41
36
 
42
37
  return value as string
43
38
  },
44
39
  }
45
40
 
46
41
  export function process() {
47
- return typeof Bun !== 'undefined'
48
- ? Bun.env as Env
49
- : p.env as unknown as Env
42
+ return typeof Bun !== 'undefined' ? (Bun.env as unknown as Env) : (p.env as unknown as Env)
50
43
  }
51
44
 
52
45
  export const env: Env = new Proxy(process(), handler)
@@ -59,15 +52,12 @@ export function writeEnv(key: EnvKey, value: string, options?: { path: string })
59
52
  const lines = env.split('\n')
60
53
 
61
54
  // Find the line with the variable we want to update
62
- const index = lines.findIndex(line => line.startsWith(`${key}=`))
55
+ const index = lines.findIndex((line) => line.startsWith(`${key}=`))
63
56
 
64
57
  // If the variable exists, update it
65
- if (index !== -1)
66
- lines[index] = `${key}=${value}`
67
-
58
+ if (index !== -1) lines[index] = `${key}=${value}`
68
59
  // Otherwise, add a new line
69
- else
70
- lines.push(`${key}=${value}`)
60
+ else lines.push(`${key}=${value}`)
71
61
 
72
62
  // Join the lines back into a string and write it to the .env file
73
63
  fs.writeFileSync(envPath, lines.join('\n'))
package/src/types.ts CHANGED
@@ -1,18 +1,24 @@
1
+ import { log } from '@stacksjs/logging'
2
+ import { schema } from '@stacksjs/validation'
1
3
  import type { Infer, VineBoolean, VineEnum, VineNumber, VineString } from '@stacksjs/validation'
2
- import { validator } from '@stacksjs/validation'
3
- import type { EnvKey } from '../../../env'
4
4
  import env from '~/config/env'
5
-
6
- // import type { Validate } from '@stacksjs/validation'
5
+ import type { EnvKey } from '../../../env'
7
6
 
8
7
  // we need to get this just into right format so we can infer the type
9
8
  type EnvValue = string | boolean | number | readonly string[]
10
9
  type EnvType = typeof env
11
10
  type EnvKeys = keyof EnvType
12
- type EnvMap = { [K in EnvKeys]: EnvType[K] extends string ? VineString :
13
- EnvType[K] extends number ? VineNumber :
14
- EnvType[K] extends boolean ? VineBoolean :
15
- EnvType[K] extends readonly string[] ? VineEnum<string[]> : unknown }
11
+ type EnvMap = {
12
+ [K in EnvKeys]: EnvType[K] extends string
13
+ ? VineString
14
+ : EnvType[K] extends number
15
+ ? VineNumber
16
+ : EnvType[K] extends boolean
17
+ ? VineBoolean
18
+ : EnvType[K] extends readonly string[]
19
+ ? VineEnum<string[]>
20
+ : unknown
21
+ }
16
22
 
17
23
  type ValidatorType = VineString | VineNumber | VineBoolean | VineEnum<string[]>
18
24
 
@@ -20,19 +26,56 @@ const envStructure = Object.entries(env).reduce((acc, [key, value]) => {
20
26
  let validatorType: ValidatorType
21
27
  switch (typeof value) {
22
28
  case 'string':
23
- validatorType = validator.string()
29
+ validatorType = schema.string()
24
30
  break
25
31
  case 'number':
26
- validatorType = validator.number()
32
+ validatorType = schema.number()
27
33
  break
28
34
  case 'boolean':
29
- validatorType = validator.boolean()
35
+ validatorType = schema.boolean()
30
36
  break
31
37
  default:
32
38
  if (Array.isArray(value)) {
33
- validatorType = validator.enum(value as string[])
39
+ validatorType = schema.enum(value as string[])
34
40
  break
35
41
  }
42
+
43
+ // check if is on object
44
+ if (typeof value === 'object') {
45
+ const schemaNameSymbol = Symbol.for('schema_name')
46
+ const schemaName = value[schemaNameSymbol]
47
+ log.debug('value', value)
48
+ log.debug('schemaName', schemaName)
49
+
50
+ if (schemaName === 'vine.string') {
51
+ validatorType = schema.string()
52
+ break
53
+ }
54
+
55
+ if (schemaName === 'vine.number') {
56
+ validatorType = schema.number()
57
+ break
58
+ }
59
+
60
+ if (schemaName === 'vine.boolean') {
61
+ validatorType = schema.boolean()
62
+ break
63
+ }
64
+
65
+ // if (schemaName === 'vine.enum') {
66
+ // validatorType = schema.enum(value as string[])
67
+ // break
68
+ // }
69
+
70
+ // oddly, enums don't trigger schemaName === 'vine.enum'
71
+ if (!schemaName) {
72
+ validatorType = schema.enum(value as string[])
73
+ break
74
+ }
75
+
76
+ console.error('Unknown env value type', typeof value)
77
+ }
78
+
36
79
  throw new Error(`Invalid env value for ${key}`)
37
80
  }
38
81
  const envKey = key as EnvKeys
@@ -41,7 +84,7 @@ const envStructure = Object.entries(env).reduce((acc, [key, value]) => {
41
84
  return acc
42
85
  }, {} as EnvMap)
43
86
 
44
- export const envSchema = validator.object(envStructure)
87
+ export const envSchema = schema.object(envStructure)
45
88
  export type Env = Infer<typeof envSchema>
46
89
 
47
90
  export type EnvOptions = Env
package/src/utils.ts CHANGED
@@ -1 +1,18 @@
1
- export { platform, isCI, hasTTY, hasWindow, isDebug, isMinimal, isWindows, isLinux, isMacOS, isColorSupported, providerInfo, provider, isBun, isNode, runtimeInfo, runtime } from 'std-env'
1
+ export {
2
+ platform,
3
+ isCI,
4
+ hasTTY,
5
+ hasWindow,
6
+ isDebug,
7
+ isMinimal,
8
+ isWindows,
9
+ isLinux,
10
+ isMacOS,
11
+ isColorSupported,
12
+ providerInfo,
13
+ provider,
14
+ isBun,
15
+ isNode,
16
+ runtimeInfo,
17
+ runtime,
18
+ } from 'std-env'
package/dist/index.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { EnvKey } from '../../../env';
2
- import type { Env } from './types';
3
- interface EnumObject {
4
- [key: string]: string[];
5
- }
6
- export declare const enums: EnumObject;
7
- export declare function process(): {
8
- [x: string]: any;
9
- [x: number]: any;
10
- [x: symbol]: any;
11
- };
12
- export declare const env: Env;
13
- export declare function writeEnv(key: EnvKey, value: string, options?: {
14
- path: string;
15
- }): void;
16
- export * from './types';
package/dist/types.d.ts DELETED
@@ -1,25 +0,0 @@
1
- import type { Infer, VineBoolean, VineEnum, VineNumber, VineString } from '@stacksjs/validation';
2
- import type { EnvKey } from '../../../env';
3
- import env from '~/config/env';
4
- type EnvValue = string | boolean | number | readonly string[];
5
- type EnvType = typeof env;
6
- type EnvKeys = keyof EnvType;
7
- type EnvMap = {
8
- [K in EnvKeys]: EnvType[K] extends string ? VineString : EnvType[K] extends number ? VineNumber : EnvType[K] extends boolean ? VineBoolean : EnvType[K] extends readonly string[] ? VineEnum<string[]> : unknown;
9
- };
10
- export declare const envSchema: import("@vinejs/vine").VineObject<EnvMap, {
11
- [x: string]: any;
12
- [x: number]: any;
13
- [x: symbol]: any;
14
- }, {
15
- [x: string]: any;
16
- }>;
17
- export type Env = Infer<typeof envSchema>;
18
- export type EnvOptions = Env;
19
- export type EnvConfig = Partial<Record<EnvKey, EnvValue>>;
20
- export interface FrontendEnv {
21
- FRONTEND_APP_ENV: 'local' | 'development' | 'staging' | 'production';
22
- FRONTEND_APP_URL: string;
23
- }
24
- export type FrontendEnvKeys = keyof FrontendEnv;
25
- export {};