|
|
@@ -2,22 +2,33 @@ import { refresh as refreshAirbnb } from '../airbnb/refresh-cookies';
|
|
|
import { refresh as refreshBooking } from '../booking/refresh-cookies';
|
|
|
import { handleOutput } from '../shared/api-client';
|
|
|
import { parseOutputMode } from '../shared/cookie-utils';
|
|
|
+import type { CredentialsPayload } from '../shared/types';
|
|
|
import * as path from 'path';
|
|
|
import * as os from 'os';
|
|
|
|
|
|
const DATA_HOME = process.env.ROOM_CAL_DATA_HOME ?? path.join(os.homedir(), '.local', 'room_cal');
|
|
|
const mode = parseOutputMode(process.argv);
|
|
|
|
|
|
+async function refreshService(
|
|
|
+ name: string,
|
|
|
+ refreshFn: () => Promise<CredentialsPayload>,
|
|
|
+ envPath: string,
|
|
|
+): Promise<void> {
|
|
|
+ const creds = await refreshFn();
|
|
|
+ await handleOutput(name, creds, mode, envPath);
|
|
|
+}
|
|
|
+
|
|
|
async function main(): Promise<void> {
|
|
|
- const [airbnbCreds, bookingCreds] = await Promise.all([
|
|
|
- refreshAirbnb(mode),
|
|
|
- refreshBooking(mode),
|
|
|
- ]);
|
|
|
- await Promise.all([
|
|
|
- handleOutput('airbnb', airbnbCreds, mode, path.join(DATA_HOME, 'airbnb.env')),
|
|
|
- handleOutput('booking', bookingCreds, mode, path.join(DATA_HOME, 'booking.env')),
|
|
|
+ const results = await Promise.allSettled([
|
|
|
+ refreshService('airbnb', () => refreshAirbnb(mode), path.join(DATA_HOME, 'airbnb.env')),
|
|
|
+ refreshService('booking', () => refreshBooking(mode), path.join(DATA_HOME, 'booking.env')),
|
|
|
]);
|
|
|
- if (mode !== 'stdout') console.error('\nAll credentials refreshed.');
|
|
|
+ const failures = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected');
|
|
|
+ if (mode !== 'stdout') {
|
|
|
+ if (failures.length === 0) console.error('\nAll credentials refreshed.');
|
|
|
+ else console.error(`\n${failures.length} service(s) failed to refresh.`);
|
|
|
+ }
|
|
|
+ if (failures.length > 0) process.exit(1);
|
|
|
}
|
|
|
|
|
|
main().catch(err => { console.error(err.message); process.exit(1); });
|