some cleanup
This commit is contained in:
parent
92c7696bb8
commit
2eafaa17ff
|
@ -17,7 +17,7 @@ export const optionsSchema = z.object({
|
|||
/**
|
||||
* Optional: Allows the user to enable verbose logging.
|
||||
*/
|
||||
verbose: z.boolean().optional(),
|
||||
verbose: z.boolean().default(false),
|
||||
}).optional().default({});
|
||||
|
||||
export type astroGistsUserConfig = z.infer<typeof optionsSchema>
|
|
@ -41,7 +41,7 @@ export default defineIntegration({
|
|||
checkVerbose: boolean,
|
||||
) => {
|
||||
// if checkVerbose is true and isVerbose is true, log the message
|
||||
if (checkVerbose && isVerbose) {
|
||||
if (!checkVerbose || checkVerbose && isVerbose) {
|
||||
if (type === "info") {
|
||||
logger.info(message);
|
||||
} else if (type === "warn") {
|
||||
|
@ -50,16 +50,7 @@ export default defineIntegration({
|
|||
logger.error(message);
|
||||
}
|
||||
}
|
||||
// if checkVerbose is false, force log the message
|
||||
if (!checkVerbose) {
|
||||
if (type === "info") {
|
||||
logger.info(message);
|
||||
} else if (type === "warn") {
|
||||
logger.warn(message);
|
||||
} else if (type === "error") {
|
||||
logger.error(message);
|
||||
}
|
||||
} };
|
||||
};
|
||||
|
||||
return {
|
||||
"astro:config:setup": ({
|
||||
|
@ -68,50 +59,34 @@ export default defineIntegration({
|
|||
|
||||
// Create a logger for the setup events
|
||||
const configLogger = logger.fork("astro-gists : setup");
|
||||
const configDone = logger.fork("astro-gists : setup-done")
|
||||
|
||||
// Create a verbose logger
|
||||
const verboseLogger = (
|
||||
type: "info"|"warn"|"error",
|
||||
message:string
|
||||
) => gistLogger(configLogger, type, message, true);
|
||||
gistLogger(configLogger, "info", "Setting up Astro Gists Integration.", false);
|
||||
|
||||
// Create a logger that ignores the verbose check
|
||||
const ignoreVerboseLoggerCheck = (
|
||||
type: "info"|"warn"|"error",
|
||||
message:string
|
||||
) => gistLogger(configLogger, type, message, false);
|
||||
|
||||
ignoreVerboseLoggerCheck("info", "Setting up Astro Gists Integration.");
|
||||
|
||||
verboseLogger("warn","Verbose logging is enabled.")
|
||||
gistLogger(configLogger, "warn", "Verbose logging is enabled.", true);
|
||||
|
||||
// WATCH INTEGRATION FOR CHANGES
|
||||
watchIntegration(resolve())
|
||||
|
||||
// Check for GITHUB_PERSONAL_TOKEN
|
||||
if (!isThereAToken()) {
|
||||
ignoreVerboseLoggerCheck("warn",TOKEN_MISSING_ERROR)
|
||||
gistLogger(configLogger,"error",TOKEN_MISSING_ERROR, false)
|
||||
}
|
||||
|
||||
// Add virtual imports
|
||||
verboseLogger("info", "Adding virtual imports.");
|
||||
gistLogger(configLogger, "info", "Adding virtual imports.", true);
|
||||
addVirtualImports({
|
||||
"virtual:astro-gists/config": `export default ${JSON.stringify(options)}`,
|
||||
"astro-gists:components": `export * from "@matthiesenxyz/astro-gists/components";`
|
||||
});
|
||||
|
||||
// Add .d.ts file
|
||||
verboseLogger("info", "Injecting astro-gists.d.ts file.");
|
||||
gistLogger(configLogger, "info", "Injecting astro-gists.d.ts file.", true);
|
||||
addDts({
|
||||
name: "astro-gists",
|
||||
content: readFileSync(resolve("./stubs/astro-gists.d.ts"), "utf-8")
|
||||
})
|
||||
|
||||
},
|
||||
"astro:config:done": ({ logger }) => {
|
||||
// Create a logger for the config done event
|
||||
const configDone = logger.fork("astro-gists : setup-done")
|
||||
|
||||
// Log that the configuration is complete
|
||||
gistLogger(
|
||||
configDone,
|
||||
|
@ -119,44 +94,7 @@ export default defineIntegration({
|
|||
"Configuration for Astro Gists Integration is complete.",
|
||||
false
|
||||
);
|
||||
|
||||
},
|
||||
"astro:server:setup": ({ logger }) => {
|
||||
// Create a logger for the server setup event
|
||||
const serverSetup = logger.fork("astro-gists : dev")
|
||||
|
||||
// Log that the server is being set up
|
||||
gistLogger(
|
||||
serverSetup,
|
||||
"info",
|
||||
"Setting up Astro Gists Integration for development.",
|
||||
true
|
||||
);
|
||||
},
|
||||
"astro:build:start": ({ logger }) => {
|
||||
// Create a logger for the build start event
|
||||
const buildStart = logger.fork("astro-gists : build")
|
||||
|
||||
// Log that the build is starting
|
||||
gistLogger(
|
||||
buildStart,
|
||||
"info",
|
||||
"Starting Build for Astro Gists Integration.",
|
||||
true
|
||||
);
|
||||
},
|
||||
"astro:build:done": ({ logger }) => {
|
||||
// Create a logger for the build done event
|
||||
const buildDone = logger.fork("astro-gists : done")
|
||||
|
||||
// Log that the build is complete
|
||||
gistLogger(
|
||||
buildDone,
|
||||
"info",
|
||||
"Build for Astro Gists Integration is complete.",
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -14,7 +14,7 @@ const gistLogger = async (
|
|||
VerboseCheck: boolean
|
||||
) => {
|
||||
// if checkVerbose is true and isVerbose is true, log the message
|
||||
if (VerboseCheck && isVerbose) {
|
||||
if (!VerboseCheck || VerboseCheck && isVerbose) {
|
||||
if (type === "info") {
|
||||
console.log(`[astro-gists : octokit] ${message}`);
|
||||
} else if (type === "warn") {
|
||||
|
@ -23,17 +23,6 @@ const gistLogger = async (
|
|||
console.log(`[ERROR] [astro-gists : octokit] ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!VerboseCheck) {
|
||||
if (type === "info") {
|
||||
console.log(`[astro-gists : octokit]" ${message}`);
|
||||
} else if (type === "warn") {
|
||||
console.log(`[WARN] [astro-gists : octokit] ${message}`);
|
||||
} else if (type === "error") {
|
||||
console.log(`[ERROR] [astro-gists : octokit] ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Load environment variables
|
||||
|
@ -55,7 +44,7 @@ const retry: typeof pRretry = (fn, opts) =>
|
|||
|
||||
// Handle the response from the Octokit API
|
||||
// biome-ignore lint/suspicious/noExplicitAny: any is used to handle the response from the Octokit API
|
||||
function handleResponse(response: OctokitResponse<any>) {
|
||||
function getStatusCode(response: OctokitResponse<any>) {
|
||||
switch (response.status) {
|
||||
case 200:
|
||||
return response.data;
|
||||
|
@ -72,26 +61,28 @@ function handleResponse(response: OctokitResponse<any>) {
|
|||
// Gist Grabber
|
||||
const gistGrabber = async (gistId: string) => {
|
||||
const response = await retry(() => octokit.request('GET /gists/{gist_id}', { gist_id: gistId }));
|
||||
if (handleResponse(response) === "E404") {
|
||||
const statusCode = getStatusCode(response);
|
||||
|
||||
if (statusCode === "E404") {
|
||||
gistLogger("error", `Gist ${gistId} not found.`, false);
|
||||
return null;
|
||||
}
|
||||
if (handleResponse(response) === "E403") {
|
||||
if (statusCode === "E403") {
|
||||
gistLogger("error", "Rate limit exceeded. Please try again later.", false);
|
||||
return null;
|
||||
}
|
||||
if (handleResponse(response) === "E500") {
|
||||
if (statusCode === "E500") {
|
||||
gistLogger("error", "Internal server error. Please try again later.", false);
|
||||
return null;
|
||||
}
|
||||
if (handleResponse(response) === "E000") {
|
||||
if (statusCode === "E000") {
|
||||
gistLogger("error", "An unknown error occurred. Please try again later.", false);
|
||||
return null;
|
||||
}
|
||||
if (handleResponse(response) === response.data) {
|
||||
if (statusCode === response.data) {
|
||||
gistLogger("info", `Gist ${gistId} found.`, true);
|
||||
}
|
||||
return handleResponse(response);
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
// Get a file from a Gist by ID and filename
|
||||
|
|
|
@ -4,6 +4,6 @@ import mdx from "@astrojs/mdx"
|
|||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
integrations: [astroGist(), mdx()]
|
||||
integrations: [astroGist({ verbose: true }), mdx()]
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue