|
|
@@ -34,9 +34,12 @@ export async function putCredentials(service: string, credentials: CredentialsPa
|
|
|
if (!baseUrl) throw new Error('ROOM_CAL_API_URL not set');
|
|
|
const url = `${baseUrl.replace(/\/$/, '')}/credentials/${service}`;
|
|
|
console.error(`[${service}] PUT ${url} ...`);
|
|
|
+ const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
|
+ const token = process.env.ROOM_CAL_API_TOKEN;
|
|
|
+ if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
|
const res = await fetch(url, {
|
|
|
method: 'PUT',
|
|
|
- headers: { 'Content-Type': 'application/json' },
|
|
|
+ headers,
|
|
|
body: JSON.stringify(credentials),
|
|
|
});
|
|
|
if (!res.ok) {
|
|
|
@@ -46,6 +49,15 @@ export async function putCredentials(service: string, credentials: CredentialsPa
|
|
|
console.error(`[${service}] PUT ${res.status} OK`);
|
|
|
}
|
|
|
|
|
|
+function writeEnvFile(service: string, credentials: CredentialsPayload, envPath: string): void {
|
|
|
+ let env = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : '';
|
|
|
+ for (const [key, value] of Object.entries(credentials)) {
|
|
|
+ env = upsertEnvLine(env, key, value);
|
|
|
+ }
|
|
|
+ fs.writeFileSync(envPath, env);
|
|
|
+ console.error(`[${service}] Credentials written to ${envPath}`);
|
|
|
+}
|
|
|
+
|
|
|
export async function handleOutput(
|
|
|
service: string,
|
|
|
credentials: CredentialsPayload,
|
|
|
@@ -55,14 +67,15 @@ export async function handleOutput(
|
|
|
if (mode === 'stdout') {
|
|
|
process.stdout.write(JSON.stringify(credentials) + '\n');
|
|
|
} else if (mode === 'api') {
|
|
|
- await putCredentials(service, credentials);
|
|
|
- console.error(`[${service}] Credentials sent to server.`);
|
|
|
- } else {
|
|
|
- let env = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : '';
|
|
|
- for (const [key, value] of Object.entries(credentials)) {
|
|
|
- env = upsertEnvLine(env, key, value);
|
|
|
+ try {
|
|
|
+ await putCredentials(service, credentials);
|
|
|
+ console.error(`[${service}] Credentials sent to server.`);
|
|
|
+ } catch (err) {
|
|
|
+ // A captured session may have cost a login/2FA round-trip — never drop it.
|
|
|
+ console.error(`[${service}] API upload failed (${(err as Error).message}) — falling back to env file.`);
|
|
|
+ writeEnvFile(service, credentials, envPath);
|
|
|
}
|
|
|
- fs.writeFileSync(envPath, env);
|
|
|
- console.error(`[${service}] Credentials written to ${envPath}`);
|
|
|
+ } else {
|
|
|
+ writeEnvFile(service, credentials, envPath);
|
|
|
}
|
|
|
}
|