From cab8bae68b7e3420f3ad984829ba9c4c5485dd2f Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Thu, 26 Dec 2024 14:54:37 -0800 Subject: [PATCH] feat(config): expose util for standard cache directory per config --- .../src/utils/config/getCacheDirectory.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/practices/config/best-practice/src/utils/config/getCacheDirectory.ts diff --git a/src/practices/config/best-practice/src/utils/config/getCacheDirectory.ts b/src/practices/config/best-practice/src/utils/config/getCacheDirectory.ts new file mode 100644 index 0000000..41e8c5f --- /dev/null +++ b/src/practices/config/best-practice/src/utils/config/getCacheDirectory.ts @@ -0,0 +1,33 @@ +import type { DirectoryToPersistTo } from 'simple-on-disk-cache'; +import { isPresent } from 'type-fns'; + +import { getConfig } from './getConfig'; + +/** + * .what = gets the project's default s3 bucket, based on config + */ +export const getProjectBucket = async (): Promise => { + const config = await getConfig(); + const bucket = [ + config.project, + config.aws.namespace, + config.environment.access, + ].join('-'); + return bucket; +}; + +/** + * .what = gets the project's default cache directory, based on config + */ +export const getCacheDirectory = async ( + input: { + scope?: string; + } = {}, +): Promise => ({ + s3: { + bucket: await getProjectBucket(), + prefix: ['purpose=cache', input.scope ? `scope=${input.scope}` : undefined] + .filter(isPresent) + .join('/'), + }, +});