2023-03-21 19:29:22 +02:00
|
|
|
import { readFile } from 'fs-extra';
|
2023-04-05 22:27:26 +02:00
|
|
|
import { getRootDir } from '@joplin/utils';
|
2023-03-21 19:29:22 +02:00
|
|
|
import { fetchWithRetry } from '@joplin/utils/net';
|
2023-04-03 20:38:40 +02:00
|
|
|
import { githubOauthToken } from '../tool-utils';
|
2023-03-21 19:29:22 +02:00
|
|
|
|
|
|
|
export interface GithubSponsor {
|
|
|
|
name: string;
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Sponsors {
|
|
|
|
github: GithubSponsor[];
|
|
|
|
orgs: OrgSponsor[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface OrgSponsor {
|
|
|
|
url: string;
|
|
|
|
urlWebsite?: string;
|
|
|
|
title: string;
|
|
|
|
imageName: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadSponsors = async (): Promise<Sponsors> => {
|
2023-04-05 22:27:26 +02:00
|
|
|
const sponsorsPath = `${await getRootDir()}/packages/tools/sponsors.json`;
|
2023-03-21 19:29:22 +02:00
|
|
|
const output: Sponsors = JSON.parse(await readFile(sponsorsPath, 'utf8'));
|
|
|
|
|
|
|
|
output.orgs = output.orgs.map(o => {
|
|
|
|
if (o.urlWebsite) o.url = o.urlWebsite;
|
|
|
|
return o;
|
|
|
|
});
|
|
|
|
|
2023-04-03 20:38:40 +02:00
|
|
|
const oauthToken = await githubOauthToken();
|
|
|
|
|
2023-03-21 19:29:22 +02:00
|
|
|
for (const ghSponsor of output.github) {
|
2023-04-03 20:38:40 +02:00
|
|
|
const userResponse = await fetchWithRetry(`https://api.github.com/users/${ghSponsor.name}`, {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `token ${oauthToken}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!userResponse.ok) throw new Error(await userResponse.text());
|
2023-03-21 19:29:22 +02:00
|
|
|
const user = await userResponse.json();
|
|
|
|
ghSponsor.id = user.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|