mirror of
https://github.com/LukeHagar/redocly-cli.git
synced 2025-12-06 04:21:09 +00:00
Co-authored-by: IhorKarpiuk <ihor.karpiuk@redocly.com> Co-authored-by: SmoliyY <yevhen.smoliy@gmail.com>
27 lines
861 B
TypeScript
27 lines
861 B
TypeScript
import { resolve } from 'path';
|
|
import { homedir } from 'os';
|
|
import { existsSync, readFileSync } from 'fs';
|
|
import { isNotEmptyObject } from '@redocly/openapi-core/lib/utils';
|
|
import { TOKEN_FILENAME } from '@redocly/openapi-core/lib/redocly';
|
|
|
|
function readCredentialsFile(credentialsPath: string) {
|
|
return existsSync(credentialsPath) ? JSON.parse(readFileSync(credentialsPath, 'utf-8')) : {};
|
|
}
|
|
|
|
export function getApiKeys(domain: string) {
|
|
const apiKey = process.env.REDOCLY_AUTHORIZATION;
|
|
|
|
if (apiKey) {
|
|
return apiKey;
|
|
}
|
|
|
|
const credentialsPath = resolve(homedir(), TOKEN_FILENAME);
|
|
const credentials = readCredentialsFile(credentialsPath);
|
|
|
|
if (isNotEmptyObject(credentials) && credentials[domain]) {
|
|
return credentials[domain];
|
|
}
|
|
|
|
throw new Error('No api key provided, please use environment variable REDOCLY_AUTHORIZATION.');
|
|
}
|