From 97113bedb00bf8a4b7a1fd548f232b2dec55e3df Mon Sep 17 00:00:00 2001 From: Jongwoo Han Date: Sun, 18 Aug 2024 11:19:36 +0900 Subject: [PATCH] style: Update files to format code --- src/constants.ts | 22 +++++----- src/restore.ts | 2 +- src/restoreImpl.ts | 86 ++++++++++++++++++++-------------------- src/save.ts | 2 +- src/saveImpl.ts | 70 ++++++++++++++++---------------- src/stateProvider.ts | 26 ++++++------ src/utils/actionUtils.ts | 80 ++++++++++++++++++------------------- 7 files changed, 144 insertions(+), 144 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 70d33e7..69973ea 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,30 +1,30 @@ export enum Inputs { - UseCache = "use-cache", - Key = "key", - RestoreKeys = "restore-keys", + UseCache = "use-cache", + Key = "key", + RestoreKeys = "restore-keys", } export enum Outputs { - CacheHit = "cache-hit", + CacheHit = "cache-hit", } export enum State { - CachePrimaryKey = "CACHE_KEY", - CacheMatchedKey = "CACHE_RESULT", + CachePrimaryKey = "CACHE_KEY", + CacheMatchedKey = "CACHE_RESULT", } export enum Events { - Key = "GITHUB_EVENT_NAME", + Key = "GITHUB_EVENT_NAME", } export enum Platform { - RunnerOs = "RUNNER_OS", + RunnerOs = "RUNNER_OS", } export enum Gatsby { - CacheDir = ".cache", - PublicDir = "public", - Env = "GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES", + CacheDir = ".cache", + PublicDir = "public", + Env = "GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES", } export const RefKey = "GITHUB_REF"; diff --git a/src/restore.ts b/src/restore.ts index 7b63f9d..a233e6f 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -2,7 +2,7 @@ import restoreImpl from "./restoreImpl"; import { StateProvider } from "./stateProvider"; async function run(): Promise { - await restoreImpl(new StateProvider()); + await restoreImpl(new StateProvider()); } void run(); diff --git a/src/restoreImpl.ts b/src/restoreImpl.ts index 70f13a2..220381c 100644 --- a/src/restoreImpl.ts +++ b/src/restoreImpl.ts @@ -7,62 +7,62 @@ import { BaseStateProvider } from "./stateProvider"; type Runner = "Linux" | "Windows" | "macOS"; async function restoreImpl( - stateProvider: BaseStateProvider, + stateProvider: BaseStateProvider, ): Promise { - try { - if (!utils.isCacheFeatureAvailable()) { - core.setOutput(Outputs.CacheHit, "false"); - return; - } + try { + if (!utils.isCacheFeatureAvailable()) { + core.setOutput(Outputs.CacheHit, "false"); + return; + } - if (!utils.isValidEvent()) { - const eventName = process.env[Events.Key] || ""; - utils.logWarning( - `Event Validation Error: The event type ${eventName} is not supported because it's not tied to a branch or tag ref.`, - ); - return; - } + if (!utils.isValidEvent()) { + const eventName = process.env[Events.Key] || ""; + utils.logWarning( + `Event Validation Error: The event type ${eventName} is not supported because it's not tied to a branch or tag ref.`, + ); + return; + } - utils.setConditionalPageBuild(); + utils.setConditionalPageBuild(); - const cachePaths = await utils.getBuildOutputPaths(); - const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys); + const cachePaths = await utils.getBuildOutputPaths(); + const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys); - let primaryKey = core.getInput(Inputs.Key); - if (!primaryKey) { - const platform = process.env[Platform.RunnerOs] as Runner; - primaryKey = `${platform}-gatsby-build-`; - } + let primaryKey = core.getInput(Inputs.Key); + if (!primaryKey) { + const platform = process.env[Platform.RunnerOs] as Runner; + primaryKey = `${platform}-gatsby-build-`; + } - core.debug(`primary key is ${primaryKey}`); - stateProvider.setState(State.CachePrimaryKey, primaryKey); + core.debug(`primary key is ${primaryKey}`); + stateProvider.setState(State.CachePrimaryKey, primaryKey); - const cacheKey: string | undefined = await cache.restoreCache( - cachePaths, - primaryKey, - restoreKeys, - ); + const cacheKey: string | undefined = await cache.restoreCache( + cachePaths, + primaryKey, + restoreKeys, + ); - if (!cacheKey) { - core.info( - `Cache not found for keys: ${[primaryKey, ...restoreKeys].join(", ")}`, - ); + if (!cacheKey) { + core.info( + `Cache not found for keys: ${[primaryKey, ...restoreKeys].join(", ")}`, + ); - return; - } + return; + } - stateProvider.setState(State.CacheMatchedKey, cacheKey); + stateProvider.setState(State.CacheMatchedKey, cacheKey); - const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey); + const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey); - core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString()); - core.info(`Cache restored from key: ${cacheKey}`); + core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString()); + core.info(`Cache restored from key: ${cacheKey}`); - return cacheKey; - } catch (error: unknown) { - core.setFailed((error as Error).message); - return; - } + return cacheKey; + } catch (error: unknown) { + core.setFailed((error as Error).message); + return; + } } export default restoreImpl; diff --git a/src/save.ts b/src/save.ts index 8c96a6c..d435b76 100644 --- a/src/save.ts +++ b/src/save.ts @@ -2,7 +2,7 @@ import saveImpl from "./saveImpl"; import { StateProvider } from "./stateProvider"; async function run(): Promise { - await saveImpl(new StateProvider()); + await saveImpl(new StateProvider()); } void run(); diff --git a/src/saveImpl.ts b/src/saveImpl.ts index f6b0bae..2b8c2af 100644 --- a/src/saveImpl.ts +++ b/src/saveImpl.ts @@ -7,42 +7,42 @@ import { BaseStateProvider } from "./stateProvider"; process.on("uncaughtException", (e) => utils.logWarning(e.message)); async function saveImpl( - stateProvider: BaseStateProvider, + stateProvider: BaseStateProvider, ): Promise { - let cacheId = -1; - try { - if (!utils.isCacheFeatureAvailable()) { - return; - } - - const state = stateProvider.getCacheState(); - - const primaryKey = - stateProvider.getState(State.CachePrimaryKey) || - core.getInput(Inputs.Key); - - if (!primaryKey) { - utils.logWarning(`Key is not specified.`); - return; - } - - if (utils.isExactKeyMatch(primaryKey, state)) { - core.info( - `Cache hit occurred on the primary key ${primaryKey}, not saving cache.`, - ); - return; - } - - const cachePaths = await utils.getBuildOutputPaths(); - cacheId = await cache.saveCache(cachePaths, primaryKey); - - if (cacheId != -1) { - core.info(`Cache saved with key: ${primaryKey}`); - } - } catch (error: unknown) { - utils.logWarning((error as Error).message); - } - return cacheId; + let cacheId = -1; + try { + if (!utils.isCacheFeatureAvailable()) { + return; + } + + const state = stateProvider.getCacheState(); + + const primaryKey = + stateProvider.getState(State.CachePrimaryKey) || + core.getInput(Inputs.Key); + + if (!primaryKey) { + utils.logWarning(`Key is not specified.`); + return; + } + + if (utils.isExactKeyMatch(primaryKey, state)) { + core.info( + `Cache hit occurred on the primary key ${primaryKey}, not saving cache.`, + ); + return; + } + + const cachePaths = await utils.getBuildOutputPaths(); + cacheId = await cache.saveCache(cachePaths, primaryKey); + + if (cacheId != -1) { + core.info(`Cache saved with key: ${primaryKey}`); + } + } catch (error: unknown) { + utils.logWarning((error as Error).message); + } + return cacheId; } export default saveImpl; diff --git a/src/stateProvider.ts b/src/stateProvider.ts index 206704e..6e289c9 100644 --- a/src/stateProvider.ts +++ b/src/stateProvider.ts @@ -2,22 +2,22 @@ import * as core from "@actions/core"; import { State } from "./constants"; export interface BaseStateProvider { - getCacheState(): string | undefined; - getState(key: string): string; - setState(key: string, value: string): void; + getCacheState(): string | undefined; + getState(key: string): string; + setState(key: string, value: string): void; } export class StateProvider implements BaseStateProvider { - getCacheState(): string | undefined { - const cacheKey = this.getState(State.CacheMatchedKey); - if (cacheKey) { - core.debug(`Cache state/key: ${cacheKey}`); - return cacheKey; - } + getCacheState(): string | undefined { + const cacheKey = this.getState(State.CacheMatchedKey); + if (cacheKey) { + core.debug(`Cache state/key: ${cacheKey}`); + return cacheKey; + } - return undefined; - } + return undefined; + } - getState = core.getState; - setState = core.saveState; + getState = core.getState; + setState = core.saveState; } diff --git a/src/utils/actionUtils.ts b/src/utils/actionUtils.ts index 2f797a9..91091c3 100644 --- a/src/utils/actionUtils.ts +++ b/src/utils/actionUtils.ts @@ -4,71 +4,71 @@ import path from "path"; import { Gatsby, RefKey } from "../constants"; export function isGhes(): boolean { - const url = process.env.GITHUB_SERVER_URL ?? "https://github.com"; - const ghUrl = new URL(url); - return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; + const url = process.env.GITHUB_SERVER_URL ?? "https://github.com"; + const ghUrl = new URL(url); + return ghUrl.hostname.toUpperCase() !== "GITHUB.COM"; } export function isExactKeyMatch(key: string, cacheKey?: string): boolean { - return !!( - cacheKey && - cacheKey.localeCompare(key, undefined, { - sensitivity: "accent", - }) === 0 - ); + return !!( + cacheKey && + cacheKey.localeCompare(key, undefined, { + sensitivity: "accent", + }) === 0 + ); } export function logWarning(message: string): void { - const warningPrefix = "[warning]"; - core.info(`${warningPrefix}${message}`); + const warningPrefix = "[warning]"; + core.info(`${warningPrefix}${message}`); } export function isValidEvent(): boolean { - return RefKey in process.env && Boolean(process.env[RefKey]); + return RefKey in process.env && Boolean(process.env[RefKey]); } export function getInputAsArray( - name: string, - options?: core.InputOptions, + name: string, + options?: core.InputOptions, ): string[] { - return core - .getInput(name, options) - .split("\n") - .map((s) => s.replace(/^!\s+/, "!").trim()) - .filter((x) => x !== ""); + return core + .getInput(name, options) + .split("\n") + .map((s) => s.replace(/^!\s+/, "!").trim()) + .filter((x) => x !== ""); } export function isCacheFeatureAvailable(): boolean { - if (cache.isFeatureAvailable()) { - return true; - } + if (cache.isFeatureAvailable()) { + return true; + } - if (isGhes()) { - logWarning( - `Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not. + if (isGhes()) { + logWarning( + `Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not. Otherwise please upgrade to GHES version >= 3.5 and If you are also using Github Connect, please unretire the actions/cache namespace before upgrade (see https://docs.github.com/en/enterprise-server@3.5/admin/github-actions/managing-access-to-actions-from-githubcom/enabling-automatic-access-to-githubcom-actions-using-github-connect#automatic-retirement-of-namespaces-for-actions-accessed-on-githubcom)`, - ); - return false; - } + ); + return false; + } - logWarning( - "An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.", - ); - return false; + logWarning( + "An internal error has occurred in cache backend. Please check https://www.githubstatus.com/ for any ongoing issue in actions.", + ); + return false; } export function setConditionalPageBuild(): void { - process.env[Gatsby.Env] = "true"; - core.debug(`Set ${Gatsby.Env}=${process.env[Gatsby.Env]}`); + process.env[Gatsby.Env] = "true"; + core.debug(`Set ${Gatsby.Env}=${process.env[Gatsby.Env]}`); } export async function getBuildOutputPaths(): Promise { - const targetPaths = [Gatsby.CacheDir, Gatsby.PublicDir]; - const buildOutputPaths = []; + const targetPaths = [Gatsby.CacheDir, Gatsby.PublicDir]; + const buildOutputPaths = []; - for await (const target of targetPaths) { - buildOutputPaths.push(path.join(process.cwd(), target)); - } + for await (const target of targetPaths) { + buildOutputPaths.push(path.join(process.cwd(), target)); + } - return buildOutputPaths; + return buildOutputPaths; }