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