@stacksjs/env 0.59.1 → 0.59.3
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/dist/index.d.ts +16 -0
- package/dist/index.js +129 -0
- package/dist/types.d.ts +25 -0
- package/package.json +2 -2
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
import p from "process";
|
|
4
|
+
import fs from "fs-extra";
|
|
5
|
+
import {projectPath} from "@stacksjs/path";
|
|
6
|
+
import {ValidationBoolean, ValidationEnum, ValidationNumber} from "@stacksjs/validation";
|
|
7
|
+
function process() {
|
|
8
|
+
return typeof Bun !== "undefined" ? Bun.env : p.env;
|
|
9
|
+
}
|
|
10
|
+
function writeEnv(key, value, options) {
|
|
11
|
+
const envPath = options?.path || projectPath(".env");
|
|
12
|
+
const env2 = fs.readFileSync(envPath, "utf-8");
|
|
13
|
+
const lines = env2.split("\n");
|
|
14
|
+
const index = lines.findIndex((line) => line.startsWith(`${key}=`));
|
|
15
|
+
if (index !== -1)
|
|
16
|
+
lines[index] = `${key}=${value}`;
|
|
17
|
+
else
|
|
18
|
+
lines.push(`${key}=${value}`);
|
|
19
|
+
fs.writeFileSync(envPath, lines.join("\n"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/types.ts
|
|
23
|
+
import {validator} from "@stacksjs/validation";
|
|
24
|
+
|
|
25
|
+
// ../../../../config/env.ts
|
|
26
|
+
import {validate} from "@stacksjs/validation";
|
|
27
|
+
var env_default = {
|
|
28
|
+
APP_NAME: validate.string(),
|
|
29
|
+
APP_ENV: validate.enum(["local", "dev", "stage", "prod"]),
|
|
30
|
+
APP_KEY: validate.string(),
|
|
31
|
+
APP_URL: validate.string(),
|
|
32
|
+
PORT: validate.number(),
|
|
33
|
+
PORT_BACKEND: validate.number(),
|
|
34
|
+
PORT_ADMIN: validate.number(),
|
|
35
|
+
PORT_LIBRARY: validate.number(),
|
|
36
|
+
PORT_DESKTOP: validate.number(),
|
|
37
|
+
PORT_EMAIL: validate.number(),
|
|
38
|
+
PORT_DOCS: validate.number(),
|
|
39
|
+
PORT_INSPECT: validate.number(),
|
|
40
|
+
PORT_API: validate.number(),
|
|
41
|
+
PORT_SYSTEM_TRAY: validate.number(),
|
|
42
|
+
APP_MAINTENANCE: validate.boolean(),
|
|
43
|
+
DEBUG: validate.boolean(),
|
|
44
|
+
API_PREFIX: validate.string(),
|
|
45
|
+
DOCS_PREFIX: validate.string(),
|
|
46
|
+
DB_CONNECTION: validate.enum(["mysql", "sqlite", "postgres", "planetscale"]),
|
|
47
|
+
DB_HOST: validate.string(),
|
|
48
|
+
DB_PORT: validate.number(),
|
|
49
|
+
DB_DATABASE: validate.string(),
|
|
50
|
+
DB_USERNAME: validate.string(),
|
|
51
|
+
DB_PASSWORD: validate.string(),
|
|
52
|
+
AWS_ACCOUNT_ID: validate.string(),
|
|
53
|
+
AWS_ACCESS_KEY_ID: validate.string(),
|
|
54
|
+
AWS_SECRET_ACCESS_KEY: validate.string(),
|
|
55
|
+
AWS_DEFAULT_REGION: validate.string(),
|
|
56
|
+
AWS_DEFAULT_PASSWORD: validate.string(),
|
|
57
|
+
MAIL_MAILER: validate.enum(["ses", "sendmail", "log", "smtp"]),
|
|
58
|
+
MAIL_HOST: validate.string(),
|
|
59
|
+
MAIL_PORT: validate.number(),
|
|
60
|
+
MAIL_USERNAME: validate.string(),
|
|
61
|
+
MAIL_PASSWORD: validate.string(),
|
|
62
|
+
MAIL_ENCRYPTION: validate.string(),
|
|
63
|
+
MAIL_FROM_NAME: validate.string(),
|
|
64
|
+
MAIL_FROM_ADDRESS: validate.string(),
|
|
65
|
+
SEARCH_ENGINE_DRIVER: validate.enum(["meilisearch", "algolia", "typesense"]),
|
|
66
|
+
MEILISEARCH_HOST: validate.string(),
|
|
67
|
+
MEILISEARCH_KEY: validate.string(),
|
|
68
|
+
FRONTEND_APP_ENV: validate.enum(["development", "staging", "production"]),
|
|
69
|
+
FRONTEND_APP_URL: validate.string()
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// src/types.ts
|
|
73
|
+
var envStructure = Object.entries(env_default).reduce((acc, [key, value]) => {
|
|
74
|
+
let validatorType;
|
|
75
|
+
switch (typeof value) {
|
|
76
|
+
case "string":
|
|
77
|
+
validatorType = validator.string();
|
|
78
|
+
break;
|
|
79
|
+
case "number":
|
|
80
|
+
validatorType = validator.number();
|
|
81
|
+
break;
|
|
82
|
+
case "boolean":
|
|
83
|
+
validatorType = validator.boolean();
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
if (Array.isArray(value)) {
|
|
87
|
+
validatorType = validator.enum(value);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
throw new Error(`Invalid env value for ${key}`);
|
|
91
|
+
}
|
|
92
|
+
const envKey = key;
|
|
93
|
+
acc[envKey] = validatorType;
|
|
94
|
+
return acc;
|
|
95
|
+
}, {});
|
|
96
|
+
var envSchema = validator.object(envStructure);
|
|
97
|
+
|
|
98
|
+
// src/index.ts
|
|
99
|
+
var enums = {
|
|
100
|
+
APP_ENV: ["local", "dev", "development", "staging", "prod", "production"],
|
|
101
|
+
DB_CONNECTION: ["mysql", "sqlite", "postgres", "planetscale"],
|
|
102
|
+
MAIL_MAILER: ["smtp", "mailgun", "ses", "postmark", "sendmail", "log"],
|
|
103
|
+
SEARCH_ENGINE_DRIVER: ["meilisearch", "algolia", "typesense"],
|
|
104
|
+
FRONTEND_APP_ENV: ["development", "staging", "production"]
|
|
105
|
+
};
|
|
106
|
+
var handler = {
|
|
107
|
+
get: (target, key) => {
|
|
108
|
+
const value = target[key];
|
|
109
|
+
if (typeof value === "string" && /^\d+$/.test(value) && key !== "AWS_ACCOUNT_ID")
|
|
110
|
+
return Number(value);
|
|
111
|
+
if (typeof value === "string" && /^(true|false)$/.test(value))
|
|
112
|
+
return value === "true";
|
|
113
|
+
if (value instanceof ValidationEnum)
|
|
114
|
+
return target[key];
|
|
115
|
+
if (value instanceof ValidationBoolean)
|
|
116
|
+
return !!target[key];
|
|
117
|
+
if (value instanceof ValidationNumber)
|
|
118
|
+
return Number(target[key]);
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
var env2 = new Proxy(process(), handler);
|
|
123
|
+
export {
|
|
124
|
+
writeEnv,
|
|
125
|
+
process,
|
|
126
|
+
envSchema,
|
|
127
|
+
env2 as env,
|
|
128
|
+
enums
|
|
129
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
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 {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/env",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.59.
|
|
4
|
+
"version": "0.59.3",
|
|
5
5
|
"description": "Stacks env helper methods.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
],
|
|
27
27
|
"exports": {
|
|
28
28
|
".": {
|
|
29
|
-
"bun": "./src/index.ts",
|
|
30
29
|
"import": "./dist/index.js"
|
|
31
30
|
},
|
|
32
31
|
"./*": {
|
|
@@ -36,6 +35,7 @@
|
|
|
36
35
|
},
|
|
37
36
|
"files": [
|
|
38
37
|
"README.md",
|
|
38
|
+
"dist",
|
|
39
39
|
"src"
|
|
40
40
|
],
|
|
41
41
|
"scripts": {
|