-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathenv.ts
More file actions
168 lines (136 loc) · 4.76 KB
/
env.ts
File metadata and controls
168 lines (136 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { spawn } from 'child_process';
import { Inquirerer, ParsedArgs } from 'inquirerer';
import { defaultPgConfig, PgConfig } from 'pg-env';
const envUsageText = `
Environment Command:
pgpm env [OPTIONS] [COMMAND...]
Manage environment variables for local development with profile support.
Database Profiles:
(default) Use local Postgres development profile
--supabase Use Supabase local development profile
Additional Services:
--minio Include MinIO/S3 environment variables
Modes:
No command Print export statements for shell evaluation
With command Execute command with environment variables applied
Options:
--help, -h Show this help message
--supabase Use Supabase profile instead of default Postgres
--minio Include CDN_ENDPOINT, AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_REGION
Examples:
pgpm env Print default Postgres env exports
pgpm env --supabase Print Supabase env exports
pgpm env --minio Print Postgres + MinIO env exports
pgpm env --supabase --minio Print Supabase + MinIO env exports
eval "$(pgpm env)" Load default Postgres env into shell
eval "$(pgpm env --minio)" Load Postgres + MinIO env into shell
eval "$(pgpm env --supabase --minio)" Load Supabase + MinIO env into shell
pgpm env pgpm deploy --database db1 Run command with default Postgres env
pgpm env --minio pgpm deploy --database db1 Run command with Postgres + MinIO env
`;
const SUPABASE_PROFILE: PgConfig = {
host: 'localhost',
port: 54322,
user: 'supabase_admin',
password: 'postgres',
database: 'postgres'
};
const DEFAULT_PROFILE: PgConfig = {
...defaultPgConfig
};
interface MinioConfig {
endpoint: string;
accessKey: string;
secretKey: string;
region: string;
}
const MINIO_PROFILE: MinioConfig = {
endpoint: 'http://localhost:9000',
accessKey: 'minioadmin',
secretKey: 'minioadmin',
region: 'us-east-1',
};
function configToEnvVars(config: PgConfig, minio?: MinioConfig): Record<string, string> {
const vars: Record<string, string> = {
PGHOST: config.host,
PGPORT: String(config.port),
PGUSER: config.user,
PGPASSWORD: config.password,
PGDATABASE: config.database
};
if (minio) {
vars.CDN_ENDPOINT = minio.endpoint;
vars.AWS_ACCESS_KEY = minio.accessKey;
vars.AWS_SECRET_KEY = minio.secretKey;
vars.AWS_REGION = minio.region;
}
return vars;
}
function printExports(config: PgConfig, minio?: MinioConfig): void {
const envVars = configToEnvVars(config, minio);
for (const [key, value] of Object.entries(envVars)) {
console.log(`export ${key}=${value}`);
}
}
function executeCommand(config: PgConfig, command: string, args: string[], minio?: MinioConfig): Promise<number> {
return new Promise((resolve, reject) => {
const envVars = configToEnvVars(config, minio);
const env = {
...process.env,
...envVars
};
const child = spawn(command, args, {
env,
stdio: 'inherit',
shell: false
});
child.on('error', (error) => {
reject(error);
});
child.on('close', (code) => {
resolve(code ?? 0);
});
});
}
export default async (
argv: Partial<ParsedArgs>,
_prompter: Inquirerer
) => {
if (argv.help || argv.h) {
console.log(envUsageText);
process.exit(0);
}
const useSupabase = argv.supabase === true || typeof argv.supabase === 'string';
const useMinio = argv.minio === true || typeof argv.minio === 'string';
const profile = useSupabase ? SUPABASE_PROFILE : DEFAULT_PROFILE;
const minioProfile = useMinio ? MINIO_PROFILE : undefined;
const knownFlags = ['--supabase', '--minio'];
const rawArgs = process.argv.slice(2);
let envIndex = rawArgs.findIndex(arg => arg === 'env');
if (envIndex === -1) {
envIndex = 0;
}
const argsAfterEnv = rawArgs.slice(envIndex + 1);
let commandArgs = argsAfterEnv.filter(arg => !knownFlags.includes(arg));
commandArgs = commandArgs.filter(arg => arg !== '--cwd' && !arg.startsWith('--cwd='));
const cwdIndex = commandArgs.findIndex(arg => arg === '--cwd');
if (cwdIndex !== -1 && cwdIndex + 1 < commandArgs.length) {
commandArgs.splice(cwdIndex, 2);
}
if (commandArgs.length === 0) {
printExports(profile, minioProfile);
return;
}
const [command, ...args] = commandArgs;
try {
const exitCode = await executeCommand(profile, command, args, minioProfile);
process.exit(exitCode);
} catch (error) {
if (error instanceof Error) {
console.error(`Error executing command: ${error.message}`);
} else {
console.error(`Error executing command: ${String(error)}`);
}
process.exit(1);
}
};