Fix/SW-2401 share cache in prod * fix: reuse cache between prod and pre-prod * tests: add tests for generating cachePrefix * tests: remove unnecessary reset of process.env * tests: add tests for generateCacheKey * fix: make sure that we don't get invalid cacheKeys * fix: make sure that we don't get invalid cacheKeys Approved-by: Linus Flood
30 lines
754 B
TypeScript
30 lines
754 B
TypeScript
import { env } from "@/env/server"
|
|
|
|
import { getBranchPrefix } from "./getBranchPrefix"
|
|
|
|
export function getPrefix(): string {
|
|
if (process.env.NODE_ENV === "development") {
|
|
const devPrefix = process.env.USER || process.env.USERNAME || "dev"
|
|
return `${devPrefix}`
|
|
}
|
|
|
|
const branch = env.BRANCH.trim()
|
|
const gitSha = env.GIT_SHA?.trim().substring(0, 7)
|
|
|
|
if (!branch && !gitSha) {
|
|
throw new Error("Unable to getPrefix, BRANCH and GIT_SHA must be set")
|
|
}
|
|
|
|
if (!branch) {
|
|
throw new Error("Unable to getPrefix, BRANCH must be set")
|
|
}
|
|
|
|
if (!gitSha) {
|
|
throw new Error("Unable to getPrefix, GIT_SHA must be set")
|
|
}
|
|
|
|
const prefixTokens = [getBranchPrefix(branch), gitSha].filter(Boolean)
|
|
|
|
return prefixTokens.join(":")
|
|
}
|