import { execSync } from 'node:child_process'
import { createHash } from 'node:crypto'

// 获取当前分支名 去掉空格 斜杠 换行 等特殊字符
export const getCurrentBranchName = () => {
  const branchName = execSync('git branch --show-current', {
    encoding: 'utf-8',
  })
    .trim()
    .replaceAll(' ', '_')
    .replaceAll('/', '_')
    .replaceAll('\n', '_')
  return branchName
}

// 根据分支名创建一个唯一的服务器文件夹名字
export const getDeploySlug = (branchName) => {
  return `branch-${createHash('md5').update(branchName).digest('hex').slice(0, 8)}`
}
