Created
April 5, 2023 10:48
-
-
Save HananoshikaYomaru/bd635a576e6fff1a4d64c2b469b70587 to your computer and use it in GitHub Desktop.
getVercelBranchUrl.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createHash } = require("crypto"); | |
function sha256(string) { | |
return createHash("sha256").update(string).digest("hex"); | |
} | |
const firstUrlMaxLength = 74; | |
/** | |
* | |
* https://vercel.com/docs/concepts/deployments/generated-urls#truncation | |
* | |
* @param {string} projectName | |
* @param {string} branchName | |
* @param {string} scopeName | |
* @returns {string} the generated branch url | |
*/ | |
const getVercelBranchUrl = (projectName, branchName, scopeName) => { | |
const firstUrl = | |
`${projectName}-git-${branchName}-${scopeName}.vercel.app`.replace( | |
"/", | |
"-" | |
); | |
if (firstUrl.length <= firstUrlMaxLength) return firstUrl; | |
const temp = `git-${branchName}${projectName}`; | |
const hash = sha256(temp).slice(0, 6); | |
const secondPart = `-${hash}-${scopeName}`; | |
return `${firstUrl.substring( | |
0, | |
firstUrlMaxLength - ".vercel.app".length - secondPart.length | |
)}${secondPart}.vercel.app`.replace("/", "-"); | |
}; | |
module.exports = { | |
getVercelBranchUrl, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment