@reliverse/rse
Version:
@reliverse/rse is your all-in-one companion for bootstrapping and improving any kind of projects (especially web apps built with frameworks like Next.js) — whether you're kicking off something new or upgrading an existing app. It is also a little AI-power
61 lines (60 loc) • 2.35 kB
JavaScript
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";
import { Octokit } from "@octokit/rest";
import { relinka } from "@reliverse/relinka";
import { inputPrompt } from "@reliverse/rempts";
import { cliVersion } from "../constants.js";
import { askUsernameGithub } from "./prompts/askUsernameGithub.js";
import { updateReliverseMemory } from "./reliverseMemory.js";
export const OctokitWithRest = Octokit.plugin(restEndpointMethods);
export const octokitUserAgent = `rse/${cliVersion}`;
function initOctokitSDK(githubKey) {
return new OctokitWithRest({
auth: githubKey.trim(),
userAgent: octokitUserAgent,
throttle: {
onRateLimit: (_retryAfter, options, octokit) => {
octokit.log.warn(
`Request quota exhausted for ${options.method} ${options.url}`
);
return options.request.retryCount === 0;
},
onSecondaryRateLimit: (_retryAfter, options, octokit) => {
octokit.log.warn(
`Secondary rate limit encountered for ${options.method} ${options.url}`
);
return options.request.retryCount === 0;
}
}
});
}
export async function ensureGithubToken(memory, maskInput) {
if (memory.githubKey) {
try {
const octokit = initOctokitSDK(memory.githubKey);
await octokit.rest.users.getAuthenticated();
return memory.githubKey.trim();
} catch (error) {
relinka(
"warn",
"Existing GitHub token is invalid. Please provide a new one."
);
relinka(
"verbose",
error instanceof Error ? error.message : String(error)
);
}
}
const token = await inputPrompt({
title: "Please enter your GitHub personal access token.\n(It will be securely stored on your machine):",
content: "Create one at https://github.com/settings/tokens/new\nEnsure you select the `repo` scope, then click `Generate token`.",
mode: maskInput ? "password" : "plain"
});
await updateReliverseMemory({ githubKey: token });
return token;
}
export async function initGithubSDK(memory, frontendUsername, maskInput) {
const githubUsername = await askUsernameGithub(memory, frontendUsername);
const githubToken = await ensureGithubToken(memory, maskInput);
const githubInstance = initOctokitSDK(githubToken);
return [githubToken, githubInstance, githubUsername];
}