Add chalk dependency and update octokit logger
This commit is contained in:
parent
c9a731aee8
commit
c60f80fa52
|
@ -46,6 +46,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@expressive-code/plugin-line-numbers": "^0.33.4",
|
"@expressive-code/plugin-line-numbers": "^0.33.4",
|
||||||
"astro-integration-kit": "^0.5.1",
|
"astro-integration-kit": "^0.5.1",
|
||||||
|
"chalk": "5.3.0",
|
||||||
"expressive-code": "^0.33.4",
|
"expressive-code": "^0.33.4",
|
||||||
"hast-util-to-html": "8.0.4",
|
"hast-util-to-html": "8.0.4",
|
||||||
"p-retry": "6.2.0",
|
"p-retry": "6.2.0",
|
||||||
|
|
|
@ -1,26 +1,34 @@
|
||||||
import { Octokit } from "octokit";
|
import { Octokit } from "octokit";
|
||||||
import type { OctokitResponse } from "@octokit/types";
|
import type { Endpoints } from "@octokit/types";
|
||||||
import { loadEnv } from "vite";
|
import { loadEnv } from "vite";
|
||||||
|
import chalk from 'chalk';
|
||||||
import pRretry from 'p-retry';
|
import pRretry from 'p-retry';
|
||||||
import config from "virtual:astro-gists/config";
|
import config from "virtual:astro-gists/config";
|
||||||
|
|
||||||
// Load config options to check if verbose logging is enabled
|
// Load config options to check if verbose logging is enabled
|
||||||
const isVerbose = config.verbose;
|
const isVerbose = config.verbose;
|
||||||
|
|
||||||
|
// Define the GistResponse type
|
||||||
|
type GistResponse = Endpoints["GET /gists/{gist_id}"]["response"];
|
||||||
|
|
||||||
// Create Gist Logger interface
|
// Create Gist Logger interface
|
||||||
const gistLogger = async (
|
const gistLogger = async (
|
||||||
type: "info"|"warn"|"error",
|
type: "info"|"warn"|"error",
|
||||||
message: string,
|
message: string,
|
||||||
VerboseCheck: boolean
|
VerboseCheck: boolean
|
||||||
) => {
|
) => {
|
||||||
|
// simplify console.log
|
||||||
|
const log = console.log;
|
||||||
|
// Create a log banner to identify logs from this package
|
||||||
|
const logBanner = chalk.blue("[")+chalk.blue("astro-gists : octokit")+chalk.blue("] ");
|
||||||
// if checkVerbose is true and isVerbose is true, log the message
|
// if checkVerbose is true and isVerbose is true, log the message
|
||||||
if (!VerboseCheck || VerboseCheck && isVerbose) {
|
if (!VerboseCheck || VerboseCheck && isVerbose) {
|
||||||
if (type === "info") {
|
if (type === "info") {
|
||||||
console.log(`[astro-gists : octokit] ${message}`);
|
log(logBanner+message);
|
||||||
} else if (type === "warn") {
|
} else if (type === "warn") {
|
||||||
console.log(`[WARN] [astro-gists : octokit] ${message}`);
|
log(chalk.yellow("[WARN] ")+logBanner+chalk.yellow(message));
|
||||||
} else if (type === "error") {
|
} else if (type === "error") {
|
||||||
console.log(`[ERROR] [astro-gists : octokit] ${message}`);
|
log(chalk.bold.red("[ERROR] ")+logBanner+chalk.red(message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -43,20 +51,20 @@ const retry: typeof pRretry = (fn, opts) =>
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle the response from the Octokit API
|
// Handle the response from the Octokit API
|
||||||
// biome-ignore lint/suspicious/noExplicitAny: any is used to handle the response from the Octokit API
|
function getStatusCode(response: GistResponse) {
|
||||||
function getStatusCode(response: OctokitResponse<any>) {
|
if (response.status !== 200) {
|
||||||
switch (response.status) {
|
if (response.status === 404) {
|
||||||
case 200:
|
|
||||||
return response.data;
|
|
||||||
case 404:
|
|
||||||
return "E404";
|
return "E404";
|
||||||
case 403:
|
}
|
||||||
|
if (response.status === 403) {
|
||||||
return "E403";
|
return "E403";
|
||||||
case 500:
|
}
|
||||||
|
if (response.status === 500) {
|
||||||
return "E500";
|
return "E500";
|
||||||
default:
|
}
|
||||||
return "E000";
|
return "E000";
|
||||||
}
|
}
|
||||||
|
return response.data;
|
||||||
}
|
}
|
||||||
// Gist Grabber
|
// Gist Grabber
|
||||||
const gistGrabber = async (gistId: string) => {
|
const gistGrabber = async (gistId: string) => {
|
||||||
|
@ -80,7 +88,7 @@ const gistGrabber = async (gistId: string) => {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (statusCode === response.data) {
|
if (statusCode === response.data) {
|
||||||
gistLogger("info", `Gist ${gistId} found.`, true);
|
gistLogger("info", `Gist for "${response.data.description || gistId}" grabbed successfully.`, true);
|
||||||
}
|
}
|
||||||
return statusCode;
|
return statusCode;
|
||||||
}
|
}
|
||||||
|
@ -91,8 +99,11 @@ export const getGistFile = async (
|
||||||
filename: string
|
filename: string
|
||||||
) => {
|
) => {
|
||||||
const gist = await gistGrabber(gistId);
|
const gist = await gistGrabber(gistId);
|
||||||
const file = gist.files[filename];
|
if (gist?.files){
|
||||||
return file ? file : null;
|
const file = gist.files[filename];
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get a Group of Gist files by ID
|
// Get a Group of Gist files by ID
|
||||||
|
|
|
@ -26,6 +26,9 @@ importers:
|
||||||
astro-integration-kit:
|
astro-integration-kit:
|
||||||
specifier: ^0.5.1
|
specifier: ^0.5.1
|
||||||
version: 0.5.1(astro@4.4.11)
|
version: 0.5.1(astro@4.4.11)
|
||||||
|
chalk:
|
||||||
|
specifier: 5.3.0
|
||||||
|
version: 5.3.0
|
||||||
expressive-code:
|
expressive-code:
|
||||||
specifier: ^0.33.4
|
specifier: ^0.33.4
|
||||||
version: 0.33.4
|
version: 0.33.4
|
||||||
|
|
Loading…
Reference in New Issue