Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.2 KB
Raw Blame
import type { ExecSyncOptions } from 'node:child_process'
import { execSync } from 'node:child_process'
import { readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

export const BUILD_CHANNELS = ['dev', 'alpha', 'rc', 'edge', 'stable'] as const
export type BuildChannel = (typeof BUILD_CHANNELS)[number]

export type BuildInfo = {
  version: string
  commit: string
  buildDate: string
  channel: BuildChannel
}

export type Env = Record<string, string | undefined>

export type GitProbe = (cmd: string) => string | null

const GIT_PROBE_OPTS: ExecSyncOptions = {
  stdio: ['ignore', 'pipe', 'ignore'],
}

export const defaultGitProbe: GitProbe = (cmd) => {
  try {
    return execSync(cmd, GIT_PROBE_OPTS).toString().trim() || null
  } catch {
    return null
  }
}

type PackageManifest = {
  difyctl?: {
    channel?: string
  }
}

export type PackageReader = () => PackageManifest

// Default reader resolves cli/package.json relative to this file so the same
// helper works whether invoked from vite.config.ts, bin/dev.js, or release.sh.
const defaultPackageReader: PackageReader = () => {
  try {
    const pkgPath = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', 'package.json')
    return JSON.parse(readFileSync(pkgPath, 'utf8')) as PackageManifest
  } catch {
    return {}
  }
}

export type ResolveOptions = {
  env?: Env
  git?: GitProbe
  now?: () => Date
  pkg?: PackageReader
}

export function resolveBuildInfo(opts: ResolveOptions = {}): BuildInfo {
  const env = opts.env ?? process.env
  const git = opts.git ?? defaultGitProbe
  const now = opts.now ?? (() => new Date())
  const pkg = (opts.pkg ?? defaultPackageReader)()

  const channel = env.DIFYCTL_CHANNEL ?? pkg.difyctl?.channel ?? 'dev'
  if (!(BUILD_CHANNELS as readonly string[]).includes(channel)) {
    throw new Error(`invalid DIFYCTL_CHANNEL: ${channel} (expected ${BUILD_CHANNELS.join(' | ')})`)
  }

  const version = env.DIFYCTL_VERSION ?? git('git describe --tags --dirty --always') ?? '0.0.0-dev'

  const commit = env.DIFYCTL_COMMIT ?? git('git rev-parse HEAD') ?? 'none'

  const buildDate = env.DIFYCTL_BUILD_DATE ?? now().toISOString()

  return { version, commit, buildDate, channel: channel as BuildChannel }
}