Skip to content

Commit 6772097

Browse files
committed
chore: wip
1 parent e33afd2 commit 6772097

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

.stacks/core/actions/src/generate/env-files.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { path as p } from '@stacksjs/path'
12
import { storage } from '@stacksjs/storage'
2-
import { envKeys } from '~/storage/framework/stacks/env'
3+
import { enums, env as e } from '@stacksjs/env'
4+
import { envKeys, EnvKey } from '~/storage/framework/stacks/env'
5+
6+
console.log('Generating env files...')
37

48
// generate ./storage/framework/types/env.d.ts file from .env
59
const envTypes = `
@@ -11,20 +15,30 @@ const envTypes = `
1115
declare module 'bun' {
1216
namespace env {
1317
${envKeys.map((key) => {
14-
const value = Bun.env[key]
18+
console.log(` - ${key}`)
19+
const value = e[key]
20+
console.log(` - ${value}`)
1521
let type = 'string'
16-
if (value === 'true' || value === 'false')
17-
type = 'boolean'
18-
else if (!Number.isNaN(Number(value)))
19-
type = 'number'
22+
if (typeof value === 'string') {
23+
if (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
24+
type = 'boolean'
25+
} else if (!isNaN(parseFloat(value)) && isFinite(Number(value))) {
26+
type = 'number'
27+
} else if (enums[key]) {
28+
// @ts-ignore
29+
type = enums[key].map(item => `'${item}'`).join(' | ')
30+
}
31+
}
2032
2133
return `const ${key}: ${type}`
2234
}).join('\n ')}
2335
}
2436
}
2537
`
2638

27-
await storage.writeFile('framework/types/env.d.ts', envTypes)
39+
await storage.writeFile(p.projectStoragePath('framework/types/env.d.ts'), envTypes)
40+
41+
console.log(' - ./storage/framework/stacks/env.ts')
2842

2943
// generate ./storage/framework/stacks/env.ts file based on Bun.env
3044
const env = `
@@ -40,4 +54,6 @@ export const envKeys = [
4054
export type EnvKey = typeof envKeys[number]
4155
`
4256

43-
await storage.writeFile('framework/stacks/env.ts', env)
57+
await storage.writeFile(p.projectStoragePath('framework/stacks/env.ts'), env)
58+
59+
console.log(' - ./storage/framework/stacks/env.d.ts')

.stacks/core/env/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
interface EnumObject {
2+
[key: string]: string[];
3+
}
4+
5+
export const enums: EnumObject = {
6+
APP_ENV: ['development', 'staging', 'production'],
7+
DB_CONNECTION: ['mysql', 'sqlite', 'postgres', 'planetscale'],
8+
MAIL_MAILER: ['smtp', 'mailgun', 'ses', 'postmark', 'sendmail', 'log'],
9+
SEARCH_ENGINE_DRIVER: ['meilisearch', 'algolia', 'typesense'],
10+
FRONTEND_APP_ENV: ['development', 'staging', 'production'],
11+
}
12+
113
const handler = {
214
get: (target: typeof Bun.env, key: string) => {
315
// @ts-ignore
416
const value = target[key]
17+
if (enums[key]?.includes(value)) return value
518
if (value === 'true') return true
619
if (value === 'false') return false
720
if (!isNaN(Number(value))) return Number(value)

storage/framework/stacks/env.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// auto-generated by Stacks
1+
2+
// This file is auto-generated by Stacks. Do not edit this file manually.
3+
// If you want to change the environment variables, please edit the .env file.
4+
//
5+
// For more information, please visit: https://stacksjs.com/docs
6+
27
export const envKeys = [
38
'APP_NAME',
49
'APP_ENV',
@@ -27,7 +32,7 @@ export const envKeys = [
2732
'MEILISEARCH_HOST',
2833
'MEILISEARCH_KEY',
2934
'FRONTEND_APP_ENV',
30-
'FRONTEND_APP_URL',
35+
'FRONTEND_APP_URL'
3136
] as const
3237

3338
export type EnvKey = typeof envKeys[number]

storage/framework/types/env.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
// Stacks auto-generated
1+
2+
// This file is auto-generated by Stacks. Do not edit this file manually.
3+
// If you want to change the environment variables, please edit the .env file.
4+
//
5+
// For more information, please visit: https://stacksjs.com/docs
6+
27
declare module 'bun' {
38
namespace env {
49
const APP_NAME: string
510
const APP_ENV: 'development' | 'staging' | 'production'
611
const APP_KEY: string
712
const APP_URL: string
8-
const DEBUG: boolean
9-
13+
const DEBUG: string
1014
const DB_CONNECTION: 'mysql' | 'sqlite' | 'postgres' | 'planetscale'
1115
const DB_HOST: string
12-
const DB_PORT: number
16+
const DB_PORT: string
1317
const DB_DATABASE: string
1418
const DB_USERNAME: string
1519
const DB_PASSWORD: string
16-
1720
const AWS_ACCOUNT_ID: string
1821
const AWS_ACCESS_KEY_ID: string
1922
const AWS_SECRET_ACCESS_KEY: string
2023
const AWS_DEFAULT_REGION: string
21-
2224
const MAIL_MAILER: 'smtp' | 'mailgun' | 'ses' | 'postmark' | 'sendmail' | 'log'
2325
const MAIL_HOST: string
24-
const MAIL_PORT: number
26+
const MAIL_PORT: string
2527
const MAIL_USERNAME: string
2628
const MAIL_PASSWORD: string
2729
const MAIL_ENCRYPTION: string
2830
const MAIL_FROM_NAME: string
2931
const MAIL_FROM_ADDRESS: string
30-
3132
const SEARCH_ENGINE_DRIVER: 'meilisearch' | 'algolia' | 'typesense'
3233
const MEILISEARCH_HOST: string
3334
const MEILISEARCH_KEY: string
34-
3535
const FRONTEND_APP_ENV: 'development' | 'staging' | 'production'
3636
const FRONTEND_APP_URL: string
3737
}

0 commit comments

Comments
 (0)