From 113637dab52f31b9092767405139524ead35c90e Mon Sep 17 00:00:00 2001 From: Jose V Sebastian Date: Thu, 22 Feb 2024 00:12:52 +0530 Subject: [PATCH] chore: add debug logs and dependabot --- .github/dependabot.yaml | 6 +++ src/getLatestReleaseOfGoogleJavaFormat.ts | 29 ++++++----- src/getReleaseOfGoogleJavaFormatByVersion.ts | 31 +++++++----- src/getUriFromString.ts | 23 ++++++--- src/logFunction.ts | 51 ++++++++++++++++++++ src/resolveExecutableFileFromConfig.ts | 6 +-- 6 files changed, 112 insertions(+), 34 deletions(-) create mode 100644 .github/dependabot.yaml create mode 100644 src/logFunction.ts diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml new file mode 100644 index 0000000..9b580b8 --- /dev/null +++ b/.github/dependabot.yaml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "npm" + directory: "/" + schedule: + interval: "daily" diff --git a/src/getLatestReleaseOfGoogleJavaFormat.ts b/src/getLatestReleaseOfGoogleJavaFormat.ts index 03e57af..f140901 100644 --- a/src/getLatestReleaseOfGoogleJavaFormat.ts +++ b/src/getLatestReleaseOfGoogleJavaFormat.ts @@ -1,17 +1,24 @@ +import { LogOutputChannel } from "vscode"; import { GoogleJavaFormatReleaseResponse, parseGoogleJavaFormatReleaseResponse, } from "./GoogleJavaFormatRelease"; +import { logAsyncFunction } from "./logFunction"; -export const getLatestReleaseOfGoogleJavaFormat = async () => { - const response = await fetch( - "https://api.github.com/repos/google/google-java-format/releases/latest", - ); - if (!response.ok) { - throw new Error("Failed to get latest release of Google Java Format."); - } +export const getLatestReleaseOfGoogleJavaFormat = logAsyncFunction( + async function getLatestReleaseOfGoogleJavaFormat(log: LogOutputChannel) { + const url = + "https://api.github.com/repos/google/google-java-format/releases/latest"; + log.debug("Fetching:", url); + const response = await fetch(url); + if (!response.ok) { + throw new Error( + "Failed to get latest release of Google Java Format.", + ); + } - return parseGoogleJavaFormatReleaseResponse( - (await response.json()) as GoogleJavaFormatReleaseResponse, - ); -}; + return parseGoogleJavaFormatReleaseResponse( + (await response.json()) as GoogleJavaFormatReleaseResponse, + ); + }, +); diff --git a/src/getReleaseOfGoogleJavaFormatByVersion.ts b/src/getReleaseOfGoogleJavaFormatByVersion.ts index 5268cbb..1b0363e 100644 --- a/src/getReleaseOfGoogleJavaFormatByVersion.ts +++ b/src/getReleaseOfGoogleJavaFormatByVersion.ts @@ -1,20 +1,25 @@ +import { LogOutputChannel } from "vscode"; import { GoogleJavaFormatVersion } from "./ExtensionConfiguration"; import { GoogleJavaFormatReleaseResponse, parseGoogleJavaFormatReleaseResponse, } from "./GoogleJavaFormatRelease"; +import { logAsyncFunction } from "./logFunction"; -export const getReleaseOfGoogleJavaFormatByVersion = async ( - version: Exclude, -) => { - const response = await fetch( - `https://api.github.com/repos/google/google-java-format/releases/tags/v${version}`, - ); - if (!response.ok) { - throw new Error(`Failed to get v${version} of Google Java Format.`); - } +export const getReleaseOfGoogleJavaFormatByVersion = logAsyncFunction( + async function getReleaseOfGoogleJavaFormatByVersion( + log: LogOutputChannel, + version: Exclude, + ) { + const url = `https://api.github.com/repos/google/google-java-format/releases/tags/v${version}`; + log.debug("Fetching:", url); + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Failed to get v${version} of Google Java Format.`); + } - return parseGoogleJavaFormatReleaseResponse( - (await response.json()) as GoogleJavaFormatReleaseResponse, - ); -}; + return parseGoogleJavaFormatReleaseResponse( + (await response.json()) as GoogleJavaFormatReleaseResponse, + ); + }, +); diff --git a/src/getUriFromString.ts b/src/getUriFromString.ts index dca9f22..61d4a29 100644 --- a/src/getUriFromString.ts +++ b/src/getUriFromString.ts @@ -1,9 +1,18 @@ -import { Uri } from "vscode"; +import { LogOutputChannel, Uri } from "vscode"; +import { logFunction } from "./logFunction"; -export function getUriFromString(value: string) { - try { - return Uri.parse(value, true); - } catch (e) { - return Uri.file(value); - } +function isRemote(value: string | null) { + return ( + value !== null && + (value.startsWith("http:/") || + value.startsWith("https:/") || + value.startsWith("file:/")) + ); } + +export const getUriFromString = logFunction(function getUriFromString( + log: LogOutputChannel, + value: string, +) { + return isRemote(value) ? Uri.parse(value, true) : Uri.file(value); +}); diff --git a/src/logFunction.ts b/src/logFunction.ts new file mode 100644 index 0000000..07fc5dc --- /dev/null +++ b/src/logFunction.ts @@ -0,0 +1,51 @@ +import { LogOutputChannel } from "vscode"; + +export function logFunction( + fn: (log: LogOutputChannel, ...args: Args) => R, +) { + return function (log: LogOutputChannel, ...args: Args) { + log.debug( + `Calling ${`"${fn.name}"` || "anonymous function"} with arguments:`, + args, + ); + try { + const result = fn(log, ...args); + log.debug( + `${`"${fn.name}"` || "anonymous function"} returned:`, + result, + ); + return result; + } catch (error) { + log.error( + `Error in ${`"${fn.name}"` || "anonymous function"}:`, + error, + ); + throw error; + } + }; +} + +export function logAsyncFunction( + fn: (log: LogOutputChannel, ...args: Args) => Promise, +) { + return async function (log: LogOutputChannel, ...args: Args) { + log.debug( + `Calling ${`"${fn.name}"` || "anonymous function"} with arguments:`, + args, + ); + try { + const result = await fn(log, ...args); + log.debug( + `${`"${fn.name}"` || "anonymous function"} returned:`, + result, + ); + return result; + } catch (error) { + log.error( + `Error in ${`"${fn.name}"` || "anonymous function"}:`, + error, + ); + throw error; + } + }; +} diff --git a/src/resolveExecutableFileFromConfig.ts b/src/resolveExecutableFileFromConfig.ts index 9a8cbe9..f73e3ca 100644 --- a/src/resolveExecutableFileFromConfig.ts +++ b/src/resolveExecutableFileFromConfig.ts @@ -11,7 +11,7 @@ export async function resolveExecutableFileFromConfig( if (executable) { log.debug(`Using config key 'executable': ${executable}`); - return getUriFromString(executable); + return getUriFromString(log, executable); } const shouldCheckNativeBinary = mode === "native-binary"; @@ -26,8 +26,8 @@ export async function resolveExecutableFileFromConfig( const { assets } = version && version !== "latest" - ? await getReleaseOfGoogleJavaFormatByVersion(version) - : await getLatestReleaseOfGoogleJavaFormat(); + ? await getReleaseOfGoogleJavaFormatByVersion(log, version) + : await getLatestReleaseOfGoogleJavaFormat(log); const url = (shouldCheckNativeBinary && assets.get(system)) || assets.get("java")!;