chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

@@ -0,0 +1,20 @@
# Cross-arch keyring prebuilds for difyctl release builds.
#
# Appended onto pnpm-workspace.yaml in CI *before* the dependency install so a
# single `pnpm install` pulls the @napi-rs/keyring native bindings for every
# release target, letting `bun build --compile` embed the correct per-target
# `.node` into each standalone binary.
#
# Kept out of the committed pnpm-workspace.yaml on purpose: setting it as a
# workspace default would pull every foreign-arch prebuild into ordinary dev
# installs. pnpm only honors supportedArchitectures from pnpm config files
# (workspace yaml / .npmrc parsed as pnpm config), and only on the install that
# first populates node_modules — a second pass is a no-op.
supportedArchitectures:
os:
- linux
- darwin
- win32
cpu:
- x64
- arm64
+368
View File
@@ -0,0 +1,368 @@
#!/usr/bin/env bun
import { Buffer } from 'node:buffer'
/**
* e2e-provision.ts
*
* Standalone pre-flight script for CI parallel e2e jobs.
*
* What it does (mirrors global-setup.ts, but without vitest):
* 1. Console login → cookie + CSRF token
* 2. Mint a primary bearer token (or validate a cached/pre-set one)
* 3. Discover primary + secondary workspaces
* 4. Provision all DSL fixture apps (idempotent — reuses existing ones)
* 5. Write GITHUB_OUTPUT (token, workspace IDs, all app IDs)
* so downstream jobs can skip re-minting and re-provisioning.
*
* Usage (in CI):
* bun scripts/e2e-provision.ts
*
* Required env vars:
* DIFY_E2E_HOST, DIFY_E2E_EMAIL, DIFY_E2E_PASSWORD
*
* Optional:
* DIFY_E2E_EDITION (ee | ce, default: ee)
* DIFY_E2E_TOKEN pre-minted token — skips device-flow mint
*
* Output file:
* .provision-output.json (also written to GITHUB_OUTPUT if set)
*/
import { appendFile, readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
// ── Env ──────────────────────────────────────────────────────────────────────
const host = process.env.DIFY_E2E_HOST ?? ''
const email = process.env.DIFY_E2E_EMAIL ?? ''
const password = process.env.DIFY_E2E_PASSWORD ?? ''
const edition = (process.env.DIFY_E2E_EDITION ?? 'ee').toLowerCase() as 'ee' | 'ce'
const preToken = process.env.DIFY_E2E_TOKEN ?? ''
if (!host || !email || !password) {
console.warn('[provision] Missing required env: DIFY_E2E_HOST, DIFY_E2E_EMAIL, DIFY_E2E_PASSWORD')
process.exit(1)
}
const base = host.replace(/\/$/, '')
// ── Helpers ───────────────────────────────────────────────────────────────────
function sleep(ms: number) {
return new Promise((r) => setTimeout(r, ms))
}
async function consoleLogin(): Promise<{ cookieString: string; csrfToken: string }> {
const passwordB64 = Buffer.from(password, 'utf8').toString('base64')
const res = await fetch(`${base}/console/api/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password: passwordB64, remember_me: false }),
signal: AbortSignal.timeout(15_000),
})
if (!res.ok) throw new Error(`console/api/login failed: HTTP ${res.status}`)
const setCookies = res.headers.getSetCookie?.() ?? []
const cookiePairs = setCookies.map((c) => c.split(';')[0] ?? '')
const cookieString = cookiePairs.join('; ')
// cookie names may have __Host- prefix on HTTPS deployments
const csrfPair = cookiePairs.find((p) => p.includes('csrf_token='))
const csrfToken = csrfPair
? csrfPair.slice(csrfPair.indexOf('csrf_token=') + 'csrf_token='.length)
: ''
return { cookieString, csrfToken }
}
async function validateToken(token: string): Promise<boolean> {
try {
const res = await fetch(`${base}/openapi/v1/account/sessions`, {
headers: { Authorization: `Bearer ${token}` },
signal: AbortSignal.timeout(10_000),
})
return res.ok
} catch {
return false
}
}
async function mintToken(cookieStr: string, csrf: string, label: string): Promise<string> {
// Step 1: device code
const codeRes = await fetch(`${base}/openapi/v1/oauth/device/code`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ client_id: 'difyctl', device_label: label }),
signal: AbortSignal.timeout(15_000),
})
if (!codeRes.ok) throw new Error(`device/code failed: HTTP ${codeRes.status}`)
const { device_code, user_code } = (await codeRes.json()) as {
device_code: string
user_code: string
}
// Step 2: approve (with retry)
let approveRes: Response | undefined
for (let i = 1; i <= 5; i++) {
approveRes = await fetch(`${base}/openapi/v1/oauth/device/approve`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Cookie: cookieStr, 'X-CSRFToken': csrf },
body: JSON.stringify({ user_code }),
signal: AbortSignal.timeout(10_000),
})
if (approveRes.ok) break
if (approveRes.status !== 429 && approveRes.status < 500) break
console.warn(`[provision] device/approve HTTP ${approveRes.status}; retry ${i}/5 in ${i * 2}s`)
await sleep(i * 2_000)
}
if (!approveRes?.ok) throw new Error(`device/approve failed: HTTP ${approveRes?.status}`)
// Step 3: exchange token
const tokenRes = await fetch(`${base}/openapi/v1/oauth/device/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ device_code, client_id: 'difyctl' }),
signal: AbortSignal.timeout(10_000),
})
if (!tokenRes.ok) throw new Error(`device/token failed: HTTP ${tokenRes.status}`)
const body = (await tokenRes.json()) as { token?: string }
if (!body.token) throw new Error(`device/token missing token: ${JSON.stringify(body)}`)
return body.token
}
async function discoverWorkspaces(cookieStr: string, csrf: string) {
const res = await fetch(`${base}/console/api/workspaces`, {
headers: { Cookie: cookieStr, 'X-CSRF-Token': csrf },
signal: AbortSignal.timeout(10_000),
})
if (!res.ok) throw new Error(`list workspaces failed: HTTP ${res.status}`)
const data = (await res.json()) as { workspaces?: Array<{ id: string; name: string }> }
const all = data.workspaces ?? []
if (edition === 'ee') {
const ws0 = all.find((w) => w.name === 'auto_test0')
const ws1 = all.find((w) => w.name === 'auto_test1')
if (!ws0) throw new Error('[provision] EE: workspace "auto_test0" not found')
console.warn(`[provision] EE primary: ${ws0.name} (${ws0.id})`)
console.warn(
`[provision] EE secondary: ${ws1?.name ?? 'reuses primary'} (${ws1?.id ?? ws0.id})`,
)
return { primaryWsId: ws0.id, primaryWsName: ws0.name, secondaryWsId: ws1?.id ?? ws0.id }
}
const auto = all
.filter((w) => w.name.toLowerCase().includes('auto'))
.sort((a, b) => a.name.localeCompare(b.name))
const primary = auto[0] ?? all[0]
if (!primary) throw new Error('[provision] No workspaces found')
return {
primaryWsId: primary.id,
primaryWsName: primary.name,
secondaryWsId: auto[1]?.id ?? primary.id,
}
}
async function provisionApps(
cookieStr: string,
csrf: string,
primaryWsId: string,
secondaryWsId: string,
): Promise<Record<string, string>> {
const mkH = (extra: Record<string, string> = {}) => ({
Cookie: cookieStr,
'X-CSRF-Token': csrf,
...extra,
})
const scriptDir = dirname(fileURLToPath(import.meta.url))
const fixturesDir = join(scriptDir, '..', 'test', 'e2e', 'fixtures', 'apps')
const NEEDS_PUBLISH = new Set(['workflow', 'advanced-chat', 'agent-chat'])
const APP_SPECS: Array<[string, string, string]> = [
['echo-chat.yml', 'DIFY_E2E_CHAT_APP_ID', primaryWsId],
['echo-workflow.yml', 'DIFY_E2E_WORKFLOW_APP_ID', primaryWsId],
['file-upload.yml', 'DIFY_E2E_FILE_APP_ID', primaryWsId],
['hitl-main.yml', 'DIFY_E2E_HITL_APP_ID', primaryWsId],
['hitl-external.yml', 'DIFY_E2E_HITL_EXTERNAL_APP_ID', primaryWsId],
['hitl-single-action.yml', 'DIFY_E2E_HITL_SINGLE_ACTION_APP_ID', primaryWsId],
['hitl-multi-node.yml', 'DIFY_E2E_HITL_MULTI_NODE_APP_ID', primaryWsId],
['file-chat.yml', 'DIFY_E2E_FILE_CHAT_APP_ID', primaryWsId],
...(edition === 'ee'
? [['ws2-workflow.yml', 'DIFY_E2E_WS2_APP_ID', secondaryWsId] as [string, string, string]]
: []),
]
let currentWs = ''
const results: Record<string, string> = {}
for (const [dslFile, envVar, wsId] of APP_SPECS) {
try {
// Switch workspace if needed
if (wsId !== currentWs) {
await fetch(`${base}/console/api/workspaces/switch`, {
method: 'POST',
headers: { ...mkH(), 'Content-Type': 'application/json' },
body: JSON.stringify({ tenant_id: wsId }),
signal: AbortSignal.timeout(10_000),
})
currentWs = wsId
}
const dsl = await readFile(join(fixturesDir, dslFile), 'utf8')
const appName =
(dsl.match(/^[ \t]+name:[ \t]*(\S[^\n]*)$/m) ?? [])[1]
?.trim()
.replace(/^['"]|['"]$/g, '') ?? dslFile
const appMode = (dsl.match(/^[ \t]+mode:[ \t]*(\S+)/m) ?? [])[1] ?? ''
// Find existing or import
const searchRes = await fetch(
`${base}/console/api/apps?name=${encodeURIComponent(appName)}&limit=50&page=1`,
{ headers: mkH(), signal: AbortSignal.timeout(10_000) },
)
const searchData = (await searchRes.json()) as { data?: Array<{ id: string; name: string }> }
let appId = searchData.data?.find((a) => a.name === appName)?.id
if (appId) {
console.warn(`[provision] ${dslFile}: exists id=${appId}`)
} else {
const importRes = await fetch(`${base}/console/api/apps/imports`, {
method: 'POST',
headers: { ...mkH(), 'Content-Type': 'application/json' },
body: JSON.stringify({ mode: 'yaml-content', yaml_content: dsl }),
signal: AbortSignal.timeout(30_000),
})
const importData = (await importRes.json()) as { app_id?: string; import_id?: string }
if (importRes.status === 202 && importData.import_id) {
const confirmRes = await fetch(
`${base}/console/api/apps/imports/${importData.import_id}/confirm`,
{
method: 'POST',
headers: mkH(),
signal: AbortSignal.timeout(15_000),
},
)
const confirmData = (await confirmRes.json()) as { app_id?: string }
appId = confirmData.app_id
} else {
appId = importData.app_id
}
if (!appId) throw new Error(`import failed: ${JSON.stringify(importData)}`)
console.warn(`[provision] ${dslFile}: imported id=${appId}`)
}
// Enable API
await fetch(`${base}/console/api/apps/${appId}/api-enable`, {
method: 'POST',
headers: { ...mkH(), 'Content-Type': 'application/json' },
body: JSON.stringify({ enable_api: true }),
signal: AbortSignal.timeout(10_000),
})
// Set public
await fetch(`${base}/console/api/enterprise/webapp/app/access-mode`, {
method: 'POST',
headers: { ...mkH(), 'Content-Type': 'application/json' },
body: JSON.stringify({ appId, accessMode: 'public' }),
signal: AbortSignal.timeout(10_000),
}).catch(() => {})
// Publish workflow
if (NEEDS_PUBLISH.has(appMode)) {
await fetch(`${base}/console/api/apps/${appId}/workflows/publish`, {
method: 'POST',
headers: { ...mkH(), 'Content-Type': 'application/json' },
body: JSON.stringify({ marked_name: 'e2e-provision', marked_comment: '' }),
signal: AbortSignal.timeout(20_000),
}).catch(() => {})
}
results[envVar] = appId
} catch (err) {
console.warn(`[provision] ${dslFile} skipped: ${err}`)
}
}
return results
}
async function writeOutputs(outputs: Record<string, string>) {
const ghOutput = process.env.GITHUB_OUTPUT
const lines = `${Object.entries(outputs)
.map(([k, v]) => `${k}=${v}`)
.join('\n')}\n`
// Always write local JSON for debugging
const { writeFile } = await import('node:fs/promises')
await writeFile('.provision-output.json', `${JSON.stringify(outputs, null, 2)}\n`, 'utf8')
console.warn('[provision] Written .provision-output.json')
if (ghOutput) {
await appendFile(ghOutput, lines, 'utf8')
console.warn(`[provision] Written ${Object.keys(outputs).length} outputs to GITHUB_OUTPUT`)
}
// Also print to stdout for visibility
console.warn('\n[provision] Outputs:')
for (const [k, v] of Object.entries(outputs))
console.warn(` ${k}=${v.slice(0, 30)}${v.length > 30 ? '…' : ''}`)
}
// ── Main ─────────────────────────────────────────────────────────────────────
async function main() {
console.warn(`[provision] Host=${base} Email=${email} Edition=${edition}`)
// 1. Login
const { cookieString, csrfToken } = await consoleLogin()
console.warn('[provision] Login OK')
// 2. Token
let primaryToken = preToken
if (primaryToken && (await validateToken(primaryToken))) {
console.warn(`[provision] Using pre-set token: ${primaryToken.slice(0, 20)}…`)
} else {
if (primaryToken) console.warn('[provision] Pre-set token invalid, minting fresh…')
primaryToken = await mintToken(cookieString, csrfToken, 'e2e-provision')
console.warn(`[provision] Minted token: ${primaryToken.slice(0, 20)}…`)
}
// 3. Discover workspaces
const { primaryWsId, primaryWsName, secondaryWsId } = await discoverWorkspaces(
cookieString,
csrfToken,
)
// 4. Provision apps
const appIds = await provisionApps(cookieString, csrfToken, primaryWsId, secondaryWsId)
console.warn(`[provision] Provisioned ${Object.keys(appIds).length} apps`)
// 4b. Switch back to primaryWsId so the session ends in the correct workspace.
// provisionApps processes ws2-workflow.yml last (EE mode), leaving the server
// session in secondaryWsId. Suite jobs that share this token would then have
// their describe calls rejected with "workspace_id does not match app's workspace".
await fetch(`${base}/console/api/workspaces/switch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Cookie: cookieString,
'X-CSRF-Token': csrfToken,
},
body: JSON.stringify({ tenant_id: primaryWsId }),
signal: AbortSignal.timeout(10_000),
}).catch((err: unknown) =>
console.warn(`[provision] switch-back to primary failed (non-fatal): ${err}`),
)
console.warn(`[provision] Session workspace reset to primary: ${primaryWsId}`)
// 5. Write outputs
await writeOutputs({
DIFY_E2E_TOKEN: primaryToken,
DIFY_E2E_WORKSPACE_ID: primaryWsId,
DIFY_E2E_WORKSPACE_NAME: primaryWsName,
DIFY_E2E_WS2_ID: secondaryWsId,
...appIds,
})
}
main().catch((err) => {
console.warn('[provision] Fatal:', err)
process.exit(1)
})
@@ -0,0 +1,266 @@
import type { CommandEntry } from './generate-command-tree.js'
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vite-plus/test'
import {
buildTree,
discoverCommands,
formatModule,
generate,
pathToTokens,
tokensToIdentifier,
} from './generate-command-tree.js'
describe('pathToTokens', () => {
it('extracts tokens for nested command', () => {
expect(pathToTokens('src/commands/auth/devices/list/index.ts', 'src/commands')).toEqual([
'auth',
'devices',
'list',
])
})
it('extracts tokens for top-level command', () => {
expect(pathToTokens('src/commands/version/index.ts', 'src/commands')).toEqual(['version'])
})
it('normalizes backslashes (windows-style paths)', () => {
expect(pathToTokens('src\\commands\\auth\\login\\index.ts', 'src/commands')).toEqual([
'auth',
'login',
])
})
})
describe('tokensToIdentifier', () => {
it('pascal-cases joined tokens', () => {
expect(tokensToIdentifier(['auth', 'devices', 'list'])).toBe('AuthDevicesList')
expect(tokensToIdentifier(['version'])).toBe('Version')
expect(tokensToIdentifier(['run', 'app', 'resume'])).toBe('RunAppResume')
})
it('splits hyphenated tokens', () => {
expect(tokensToIdentifier(['agent-chat'])).toBe('AgentChat')
expect(tokensToIdentifier(['my-cmd', 'sub-thing'])).toBe('MyCmdSubThing')
})
it('prefixes underscore on reserved words', () => {
expect(tokensToIdentifier(['delete'])).toBe('_Delete')
})
})
describe('buildTree', () => {
it('assembles a nested tree from entries', () => {
const entries = [
{
tokens: ['auth', 'login'],
identifier: 'AuthLogin',
importPath: '@/commands/auth/login/index',
},
{
tokens: ['auth', 'devices', 'list'],
identifier: 'AuthDevicesList',
importPath: '@/commands/auth/devices/list/index',
},
{ tokens: ['version'], identifier: 'Version', importPath: '@/commands/version/index' },
]
const tree = buildTree(entries)
expect(tree.subcommands.get('auth')?.command).toBeUndefined()
expect(tree.subcommands.get('auth')?.subcommands.get('login')?.command).toBe('AuthLogin')
expect(
tree.subcommands.get('auth')?.subcommands.get('devices')?.subcommands.get('list')?.command,
).toBe('AuthDevicesList')
expect(tree.subcommands.get('version')?.command).toBe('Version')
})
it('supports a parent command with its own children', () => {
const entries = [
{ tokens: ['run', 'app'], identifier: 'RunApp', importPath: '@/commands/run/app/index' },
{
tokens: ['run', 'app', 'resume'],
identifier: 'RunAppResume',
importPath: '@/commands/run/app/resume/index',
},
]
const tree = buildTree(entries)
const runApp = tree.subcommands.get('run')?.subcommands.get('app')
expect(runApp?.command).toBe('RunApp')
expect(runApp?.subcommands.get('resume')?.command).toBe('RunAppResume')
})
})
describe('formatModule', () => {
it('produces a deterministic ESM file with imports + tree literal', () => {
const entries: CommandEntry[] = [
{
tokens: ['auth', 'login'],
identifier: 'AuthLogin',
importPath: '@/commands/auth/login/index',
},
{ tokens: ['version'], identifier: 'Version', importPath: '@/commands/version/index' },
{
tokens: ['auth', 'devices', 'list'],
identifier: 'AuthDevicesList',
importPath: '@/commands/auth/devices/list/index',
},
]
const tree = buildTree(entries)
const out = formatModule(entries, tree)
expect(out).toBe(
`// @generated by scripts/generate-command-tree.ts — DO NOT EDIT.
// Regenerate via \`pnpm tree:gen\`. Drift gated by \`pnpm tree:check\` in CI.
import type { CommandTree } from '@/plugins/commands/registry'
import AuthDevicesList from '@/commands/auth/devices/list/index'
import AuthLogin from '@/commands/auth/login/index'
import Version from '@/commands/version/index'
export const commandTree: CommandTree = {
auth: {
subcommands: {
devices: {
subcommands: {
list: { command: AuthDevicesList, subcommands: {} },
},
},
login: { command: AuthLogin, subcommands: {} },
},
},
version: { command: Version, subcommands: {} },
}
`,
)
})
it('emits parent-with-own-command shape', () => {
const entries: CommandEntry[] = [
{ tokens: ['run', 'app'], identifier: 'RunApp', importPath: '@/commands/run/app/index' },
{
tokens: ['run', 'app', 'resume'],
identifier: 'RunAppResume',
importPath: '@/commands/run/app/resume/index',
},
]
const tree = buildTree(entries)
const out = formatModule(entries, tree)
expect(out).toContain(`run: {
subcommands: {
app: {
command: RunApp,
subcommands: {
resume: { command: RunAppResume, subcommands: {} },
},
},
},
},`)
})
it('imports sorted alphabetically by import path', () => {
const entries: CommandEntry[] = [
{ tokens: ['version'], identifier: 'Version', importPath: '@/commands/version/index' },
{
tokens: ['auth', 'login'],
identifier: 'AuthLogin',
importPath: '@/commands/auth/login/index',
},
]
const out = formatModule(entries, buildTree(entries))
const authIdx = out.indexOf('AuthLogin')
const verIdx = out.indexOf('Version')
expect(authIdx).toBeLessThan(verIdx)
})
it('quotes hyphenated keys and leaves plain identifier keys unquoted', () => {
const entries: CommandEntry[] = [
{
tokens: ['export', 'app'],
identifier: 'ExportApp',
importPath: '@/commands/export/app/index',
},
{
tokens: ['export', 'studio-app'],
identifier: 'ExportStudioApp',
importPath: '@/commands/export/studio-app/index',
},
]
const out = formatModule(entries, buildTree(entries))
expect(out).toContain(`'studio-app': { command: ExportStudioApp, subcommands: {} },`)
expect(out).toContain(`app: { command: ExportApp, subcommands: {} },`)
expect(out).not.toContain(`'app':`)
})
})
function makeFixture(): string {
const root = mkdtempSync(join(tmpdir(), 'difyctl-codegen-'))
const commands = join(root, 'src', 'commands')
mkdirSync(join(commands, 'auth', 'login'), { recursive: true })
writeFileSync(join(commands, 'auth', 'login', 'index.ts'), 'export default class Login {}\n')
mkdirSync(join(commands, 'auth', 'devices', 'list'), { recursive: true })
writeFileSync(
join(commands, 'auth', 'devices', 'list', 'index.ts'),
'export default class DevicesList {}\n',
)
mkdirSync(join(commands, '_shared'), { recursive: true })
writeFileSync(join(commands, '_shared', 'index.ts'), 'export default class Shared {}\n')
mkdirSync(join(commands, 'version'), { recursive: true })
writeFileSync(join(commands, 'version', 'index.ts'), 'export default class Version {}\n')
return root
}
describe('discoverCommands', () => {
it('returns sorted entries, skipping _-prefixed segments', async () => {
const root = makeFixture()
const entries = await discoverCommands(join(root, 'src', 'commands'))
expect(entries.map((e) => e.tokens.join('/'))).toEqual([
'auth/devices/list',
'auth/login',
'version',
])
expect(entries.find((e) => e.tokens[0] === '_shared')).toBeUndefined()
})
it('errors on a loose .ts file under commands/', async () => {
const root = mkdtempSync(join(tmpdir(), 'difyctl-codegen-loose-'))
const commands = join(root, 'src', 'commands')
mkdirSync(commands, { recursive: true })
writeFileSync(join(commands, 'foo.ts'), 'export default class Foo {}\n')
await expect(discoverCommands(commands)).rejects.toThrow(/must live under their own folder/)
})
})
describe('generate', () => {
it('writes tree.generated.ts on default mode', async () => {
const root = makeFixture()
const commandsDir = join(root, 'src', 'commands')
const result = await generate({ commandsDir, mode: 'write' })
expect(result.mode).toBe('write')
const target = join(commandsDir, 'tree.generated.ts')
expect(existsSync(target)).toBe(true)
const content = readFileSync(target, 'utf8')
expect(content).toContain('@generated')
expect(content).toContain('import AuthLogin')
expect(content).toContain('import AuthDevicesList')
expect(content).toContain('import Version')
})
it('returns ok: true on --check when file matches', async () => {
const root = makeFixture()
const commandsDir = join(root, 'src', 'commands')
await generate({ commandsDir, mode: 'write' })
const result = await generate({ commandsDir, mode: 'check' })
if (result.mode !== 'check') throw new Error('expected check mode')
expect(result.ok).toBe(true)
})
it('returns ok: false on --check when file is stale', async () => {
const root = makeFixture()
const commandsDir = join(root, 'src', 'commands')
await generate({ commandsDir, mode: 'write' })
writeFileSync(join(commandsDir, 'tree.generated.ts'), '// stale\n')
const result = await generate({ commandsDir, mode: 'check' })
if (result.mode !== 'check') throw new Error('expected check mode')
expect(result.ok).toBe(false)
if (!result.ok) expect(result.diff).toBeDefined()
})
})
@@ -0,0 +1,309 @@
import { readdir, readFile, rename, writeFile } from 'node:fs/promises'
import { join, relative, sep } from 'node:path'
import { fileURLToPath } from 'node:url'
import { isExcludedCommandPath } from '../src/plugins/commands/command-fs.js'
const RESERVED_JS_KEYWORDS = new Set([
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'enum',
'export',
'extends',
'false',
'finally',
'for',
'function',
'if',
'import',
'in',
'instanceof',
'new',
'null',
'return',
'super',
'switch',
'this',
'throw',
'true',
'try',
'typeof',
'var',
'void',
'while',
'with',
'yield',
])
export type CommandEntry = {
readonly tokens: readonly string[]
readonly identifier: string
readonly importPath: string
}
export type TreeNode = {
command?: string
subcommands: Map<string, TreeNode>
}
export function pathToTokens(filePath: string, commandsRoot: string): string[] {
const normalized = filePath.replace(/\\/g, '/')
const root = commandsRoot.replace(/\\/g, '/').replace(/\/$/, '')
const trimmed = normalized.startsWith(`${root}/`) ? normalized.slice(root.length + 1) : normalized
const withoutIndex = trimmed.replace(/\/index\.ts$/, '')
return withoutIndex.split('/').filter((s) => s.length > 0)
}
function capitalize(part: string): string {
if (part.length === 0) return ''
return part[0]!.toUpperCase() + part.slice(1)
}
export function tokensToIdentifier(tokens: readonly string[]): string {
const id = tokens
.flatMap((t) => t.split(/[-_]/))
.map(capitalize)
.join('')
if (RESERVED_JS_KEYWORDS.has(id.toLowerCase())) return `_${id}`
return id
}
export function buildTree(entries: readonly CommandEntry[]): TreeNode {
const root: TreeNode = { subcommands: new Map() }
for (const entry of entries) {
let node = root
for (let i = 0; i < entry.tokens.length; i++) {
const tok = entry.tokens[i]!
let next = node.subcommands.get(tok)
if (!next) {
next = { subcommands: new Map() }
node.subcommands.set(tok, next)
}
node = next
}
node.command = entry.identifier
}
return root
}
const HEADER = `// @generated by scripts/generate-command-tree.ts — DO NOT EDIT.
// Regenerate via \`pnpm tree:gen\`. Drift gated by \`pnpm tree:check\` in CI.
`
function compareStrings(a: string, b: string): number {
if (a < b) return -1
if (a > b) return 1
return 0
}
function emitImports(entries: readonly CommandEntry[]): string {
const sorted = [...entries].sort((a, b) => compareStrings(a.importPath, b.importPath))
const lines = [`import type { CommandTree } from '@/plugins/commands/registry'`]
for (const e of sorted) lines.push(`import ${e.identifier} from '${e.importPath}'`)
return lines.join('\n')
}
function emitNode(node: TreeNode, indent: string): string {
const inner = `${indent} `
const keys = [...node.subcommands.keys()].sort()
const parts: string[] = []
if (node.command !== undefined) parts.push(`${inner}command: ${node.command},`)
if (keys.length === 0) {
parts.push(`${inner}subcommands: {},`)
} else {
parts.push(`${inner}subcommands: {`)
for (const key of keys) {
const child = node.subcommands.get(key)!
parts.push(emitEntry(key, child, `${inner} `))
}
parts.push(`${inner}},`)
}
return parts.join('\n')
}
function needsQuoting(key: string): boolean {
// A bare object key must be a valid JS identifier: the start class excludes digits
// (letter/_/$ only), so a leading digit fails the match and the key gets quoted.
return !/^[A-Z_$][\w$]*$/i.test(key)
}
function emitKey(key: string): string {
return needsQuoting(key) ? `'${key}'` : key
}
function emitEntry(key: string, node: TreeNode, indent: string): string {
const k = emitKey(key)
const isLeaf = node.subcommands.size === 0 && node.command !== undefined
if (isLeaf) return `${indent}${k}: { command: ${node.command}, subcommands: {} },`
return [`${indent}${k}: {`, emitNode(node, indent), `${indent}},`].join('\n')
}
export function formatModule(entries: readonly CommandEntry[], tree: TreeNode): string {
const importsBlock = emitImports(entries)
const topKeys = [...tree.subcommands.keys()].sort()
// Empty object on one line, matching the repo formatter's own convention —
// otherwise a fresh `tree:gen` and `tree:check` disagree right after a commit.
const literal =
topKeys.length === 0
? 'export const commandTree: CommandTree = {}'
: [
'export const commandTree: CommandTree = {',
...topKeys.map((key) => emitEntry(key, tree.subcommands.get(key)!, ' ')),
'}',
].join('\n')
return `${HEADER}\n${importsBlock}\n\n${literal}\n`
}
async function walk(dir: string): Promise<string[]> {
const out: string[] = []
const entries = await readdir(dir, { withFileTypes: true })
for (const e of entries) {
const full = join(dir, e.name)
if (e.isDirectory()) out.push(...(await walk(full)))
else if (e.isFile()) out.push(full)
}
return out
}
function toPosix(p: string): string {
return p.split(sep).join('/')
}
export async function discoverCommands(commandsDir: string): Promise<CommandEntry[]> {
const all = await walk(commandsDir)
const tsFiles = all.filter(
(f) => f.endsWith('.ts') && !f.endsWith('.test.ts') && !f.endsWith('.d.ts'),
)
const loose: string[] = []
for (const abs of tsFiles) {
const rel = toPosix(relative(commandsDir, abs))
if (isExcludedCommandPath(rel)) continue
if (rel === 'tree.ts' || rel === 'tree.generated.ts') continue
// Only flag files directly under commands/ (no path separator — no parent folder)
if (!rel.includes('/')) loose.push(rel)
}
if (loose.length > 0) {
const list = loose.map((p) => ` - src/commands/${p}`).join('\n')
throw new Error(
`commands must live under their own folder (see CLAUDE memory: feedback_cli_command_structure). Found:\n${list}`,
)
}
const entries: CommandEntry[] = []
for (const abs of tsFiles) {
const rel = toPosix(relative(commandsDir, abs))
if (isExcludedCommandPath(rel)) continue
if (!rel.endsWith('/index.ts')) continue
const tokens = pathToTokens(rel, '')
if (tokens.length === 0) continue
if (tokens[0]!.startsWith('-')) throw new Error(`command token cannot start with '-': ${rel}`)
entries.push({
tokens,
identifier: tokensToIdentifier(tokens),
importPath: `@/commands/${tokens.join('/')}/index`,
})
}
entries.sort((a, b) => compareStrings(a.importPath, b.importPath))
if (entries.length === 0)
throw new Error(`no commands found under ${commandsDir} — expected <verb>/index.ts folders`)
assertUniqueIdentifiers(entries)
return entries
}
function assertUniqueIdentifiers(entries: readonly CommandEntry[]): void {
const seen = new Map<string, string>()
for (const e of entries) {
const prev = seen.get(e.identifier)
if (prev !== undefined)
throw new Error(`identifier collision: ${e.identifier} from ${prev} and ${e.importPath}`)
seen.set(e.identifier, e.importPath)
}
}
export type GenerateOptions = {
readonly commandsDir: string
readonly mode: 'write' | 'check'
}
export type GenerateResult =
| { mode: 'write'; wrote: boolean; path: string }
| { mode: 'check'; ok: true; path: string }
| { mode: 'check'; ok: false; path: string; diff: string }
export async function generate(opts: GenerateOptions): Promise<GenerateResult> {
const entries = await discoverCommands(opts.commandsDir)
const tree = buildTree(entries)
const content = formatModule(entries, tree)
const target = join(opts.commandsDir, 'tree.generated.ts')
if (opts.mode === 'check') {
let onDisk = ''
try {
onDisk = await readFile(target, 'utf8')
} catch {
onDisk = ''
}
if (onDisk === content) return { mode: 'check', ok: true, path: target }
return { mode: 'check', ok: false, path: target, diff: shortDiff(onDisk, content) }
}
const tmp = `${target}.tmp-${process.pid}-${Date.now()}`
await writeFile(tmp, content, 'utf8')
await rename(tmp, target)
return { mode: 'write', wrote: true, path: target }
}
function shortDiff(a: string, b: string): string {
const aLines = a.split('\n')
const bLines = b.split('\n')
const lines: string[] = []
const max = Math.max(aLines.length, bLines.length)
for (let i = 0; i < max; i++) {
if (aLines[i] !== bLines[i]) {
if (aLines[i] !== undefined) lines.push(`- ${aLines[i]}`)
if (bLines[i] !== undefined) lines.push(`+ ${bLines[i]}`)
}
}
return lines.slice(0, 40).join('\n')
}
async function main(): Promise<void> {
const here = fileURLToPath(import.meta.url)
const commandsDir = join(here, '..', '..', 'src', 'commands')
const checkMode = process.argv.includes('--check')
const result = await generate({ commandsDir, mode: checkMode ? 'check' : 'write' })
if (result.mode === 'write') {
process.stderr.write(`tree:gen wrote ${result.path}\n`)
return
}
if (result.ok) {
process.stderr.write(`tree:check ok\n`)
return
}
process.stderr.write(
`tree:check FAILED — tree.generated.ts is stale.\nDiff (first 40 lines):\n${result.diff}\n\nRun \`pnpm tree:gen\` and commit.\n`,
)
process.exit(1)
}
const invokedDirectly =
process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1]
if (invokedDirectly) await main()
+264
View File
@@ -0,0 +1,264 @@
#!/bin/sh
# install-cli.sh — one-line difyctl installer. difyctl ships as assets on Dify
# GitHub Releases; this installs the build matching your Dify version.
#
# usage:
# curl -fsSL https://raw.githubusercontent.com/langgenius/dify/main/cli/scripts/install-cli.sh | sh
#
# env:
# DIFY_VERSION Dify release tag to install difyctl from (e.g. 1.14.2). Primary key.
# DIFYCTL_VERSION difyctl version pin (used only when DIFY_VERSION is unset).
# DIFYCTL_PREFIX install prefix (default $HOME/.local); binary -> $PREFIX/bin/difyctl
# DIFYCTL_INSTALL_DIR the bin folder itself (wins over DIFYCTL_PREFIX); binary -> $DIR/difyctl
# DIFYCTL_REPO release source repo (default langgenius/dify)
# GITHUB_TOKEN GitHub token (or GH_TOKEN) to raise the API rate limit to 5000/hour.
# requires: curl, uname, sort -V, and sha256sum or shasum.
set -eu
REPO="${DIFYCTL_REPO:-langgenius/dify}"
DIFY_VERSION="${DIFY_VERSION:-}"
DIFYCTL_VERSION="${DIFYCTL_VERSION:-}"
GH_AUTH="${GITHUB_TOKEN:-${GH_TOKEN:-}}"
# fetch_json runs in a subshell, so it reports failures via this file, not a variable.
FETCH_ERR_FILE="${TMPDIR:-/tmp}/difyctl-fetcherr.$$"
API="https://api.github.com/repos/${REPO}"
DL="https://github.com/${REPO}/releases/download"
err() { printf '%s\n' "install-cli: $*" >&2; }
die() { err "$*"; rm -f "$FETCH_ERR_FILE"; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || die "$1 is required"; }
re_escape() { printf '%s' "$1" | sed 's/[][\\.^$*+?(){}|/]/\\&/g'; }
# print_skill_hint BIN: the hint names a v2 command, so a binary without it (any v1 build) gets none.
print_skill_hint() {
"$1" install skills --help >/dev/null 2>&1 || return 0
printf '%s\n' 'AI agents: run `difyctl install skills <skills root>` after each install or upgrade; the skill ships inside the binary.'
}
# DIFYCTL_INSTALL_DIR names the bin folder itself; DIFYCTL_PREFIX the folder above it.
bin_dir_for() {
printf '%s' "${DIFYCTL_INSTALL_DIR:-${DIFYCTL_PREFIX:-${HOME}/.local}/bin}"
}
# install_binary SRC DEST: stage the copy next to DEST, then rename it over DEST, so a
# running difyctl keeps its old file and DEST is never half written.
install_binary() {
_stage="$(dirname "$2")/.difyctl.install.$$"
if cp "$1" "$_stage" && chmod 0755 "$_stage" && mv -f "$_stage" "$2"; then
return 0
fi
rm -f "$_stage"
die "cannot write $2"
}
detect_target() {
case "$(uname -s)" in
Linux*) _os=linux ;;
Darwin*) _os=darwin ;;
*) die "unsupported OS: $(uname -s) (use install.ps1 on Windows)" ;;
esac
case "$(uname -m)" in
x86_64|amd64) _arch=x64 ;;
arm64|aarch64) _arch=arm64 ;;
*) die "unsupported arch: $(uname -m)" ;;
esac
printf '%s-%s' "$_os" "$_arch"
}
# list_asset_names (reads release JSON on stdin) -> one difyctl asset name per line
list_asset_names() {
grep -oE '"name"[[:space:]]*:[[:space:]]*"difyctl-v[^"]*"' \
| sed -E 's#.*"name"[[:space:]]*:[[:space:]]*"([^"]*)".*#\1#'
}
# pick_asset TARGET (reads release JSON on stdin) -> highest-semver matching asset name
pick_asset() {
_target=$(re_escape "$1")
list_asset_names \
| grep -E -- "-${_target}(\\.exe)?\$" \
| grep -vE -- '-checksums\.txt$' \
| sort -V | tail -1
}
# asset_version ASSET_NAME TARGET -> difyctl version embedded in the name
asset_version() {
_target=$(re_escape "$2")
printf '%s' "$1" | sed -E "s#^difyctl-v(.*)-${_target}(\\.exe)?\$#\\1#"
}
# list_release_tags (reads /releases array JSON on stdin) -> tag per line, newest first
list_release_tags() {
grep -oE '"tag_name"[[:space:]]*:[[:space:]]*"[^"]*"' \
| sed -E 's#.*:[[:space:]]*"([^"]*)".*#\1#'
}
# fetch_json URL -> body on stdout, 0 on success. On failure writes the cause to
# FETCH_ERR_FILE (ratelimit:<epoch> | http:<code> | network) and returns nonzero.
# Inspects the status and rate-limit headers, so it must not use curl -f.
fetch_json() {
rm -f "$FETCH_ERR_FILE"
_hdr=$(mktemp) || return 1
_body=$(mktemp) || { rm -f "$_hdr"; return 1; }
if [ -n "$GH_AUTH" ]; then
_code=$(curl -sSL -D "$_hdr" -o "$_body" -w '%{http_code}' \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GH_AUTH}" \
"$1" 2>/dev/null) || _code=000
else
_code=$(curl -sSL -D "$_hdr" -o "$_body" -w '%{http_code}' \
-H "Accept: application/vnd.github+json" \
"$1" 2>/dev/null) || _code=000
fi
case "$_code" in
2*) cat "$_body"; rm -f "$_hdr" "$_body"; return 0 ;;
403|429)
_rem=$(awk 'tolower($1)=="x-ratelimit-remaining:"{gsub(/\r/,"",$2);v=$2} END{print v}' "$_hdr")
if [ "$_code" = "429" ] || [ "${_rem:-}" = "0" ]; then
_rst=$(awk 'tolower($1)=="x-ratelimit-reset:"{gsub(/\r/,"",$2);v=$2} END{print v}' "$_hdr")
printf 'ratelimit:%s' "$_rst" > "$FETCH_ERR_FILE"
else
printf 'http:%s' "$_code" > "$FETCH_ERR_FILE"
fi ;;
000) printf 'network' > "$FETCH_ERR_FILE" ;;
*) printf 'http:%s' "$_code" > "$FETCH_ERR_FILE" ;;
esac
rm -f "$_hdr" "$_body"
return 1
}
# rate_limit_hint [RESET_EPOCH] -> explain the GitHub API limit and how to proceed.
rate_limit_hint() {
err "GitHub API rate limit exceeded (unauthenticated requests are capped at 60/hour per IP)."
case "${1:-}" in
'' | *[!0-9]*) ;;
*)
_now=$(date +%s 2>/dev/null) || _now=""
if [ -n "$_now" ]; then
_mins=$(( ($1 - _now + 59) / 60 ))
if [ "$_mins" -gt 0 ]; then
err "The limit resets in ~${_mins} min."
fi
fi ;;
esac
err "To proceed now, authenticate to raise the limit to 5000/hour:"
err " curl -fsSL <install-url> | GITHUB_TOKEN=<token> sh"
err "Tip: pinning DIFY_VERSION makes a single API call (DIFYCTL_VERSION scans many)."
}
# True if the last fetch_json hit a rate limit — lets a subshell bail without printing.
fetch_hit_ratelimit() {
[ -f "$FETCH_ERR_FILE" ] || return 1
case "$(cat "$FETCH_ERR_FILE" 2>/dev/null || true)" in
ratelimit:*) return 0 ;;
esac
return 1
}
# Explain and exit if the last fetch_json hit a rate limit; else return. Main shell only.
maybe_ratelimit_exit() {
fetch_hit_ratelimit || return 0
_reset=$(cat "$FETCH_ERR_FILE" 2>/dev/null || true)
rm -f "$FETCH_ERR_FILE"
rate_limit_hint "${_reset#ratelimit:}"
exit 1
}
# find_release_for_difyctl WANT TARGET -> newest Dify tag whose assets host that difyctl build
find_release_for_difyctl() {
_want="$1"
_target="$2"
_raw=$(fetch_json "${API}/releases?per_page=100") \
|| { fetch_hit_ratelimit && return 1; die "failed to query ${REPO} releases (network error or GitHub API rate limit)"; }
_tags=$(printf '%s' "$_raw" | list_release_tags)
for _t in $_tags; do
_rel=$(fetch_json "${API}/releases/tags/${_t}") \
|| { fetch_hit_ratelimit && return 1; err "fetch failed for ${_t}, skipping"; continue; }
_name=$(printf '%s' "$_rel" | pick_asset "$_target")
[ -n "$_name" ] || continue
if [ "$(asset_version "$_name" "$_target")" = "$_want" ]; then
printf '%s' "$_t"
return 0
fi
done
return 1
}
resolve_release() {
_target="$1"
if [ -n "$DIFY_VERSION" ]; then
REL=$(fetch_json "${API}/releases/tags/${DIFY_VERSION}") \
|| { maybe_ratelimit_exit; die "Dify release ${DIFY_VERSION} not found"; }
DIFY_TAG="$DIFY_VERSION"
elif [ -n "$DIFYCTL_VERSION" ]; then
DIFY_TAG=$(find_release_for_difyctl "$DIFYCTL_VERSION" "$_target") \
|| { maybe_ratelimit_exit; die "difyctl ${DIFYCTL_VERSION} not found on any Dify release"; }
REL=$(fetch_json "${API}/releases/tags/${DIFY_TAG}") \
|| { maybe_ratelimit_exit; die "failed to fetch Dify release ${DIFY_TAG}"; }
else
REL=$(fetch_json "${API}/releases/latest") \
|| { maybe_ratelimit_exit; die "failed to query latest Dify release (set DIFY_VERSION to pin one)"; }
DIFY_TAG=$(printf '%s' "$REL" | list_release_tags | head -1)
[ -n "$DIFY_TAG" ] || die "could not parse a tag from the latest Dify release"
fi
}
main() {
trap 'rm -f "$FETCH_ERR_FILE"' EXIT INT TERM
need curl
need uname
sort -V /dev/null >/dev/null 2>&1 || die "sort with -V support is required (install coreutils)"
if command -v sha256sum >/dev/null 2>&1; then
HASH="sha256sum"
elif command -v shasum >/dev/null 2>&1; then
HASH="shasum -a 256"
else
die "need sha256sum or shasum"
fi
target=$(detect_target)
resolve_release "$target"
asset=$(printf '%s' "$REL" | pick_asset "$target")
[ -n "$asset" ] || die "no difyctl published for Dify ${DIFY_TAG} (target ${target}); set DIFY_VERSION to a release that has one"
version=$(asset_version "$asset" "$target")
checksums="difyctl-v${version}-checksums.txt"
base="${DL}/${DIFY_TAG}"
tmp=$(mktemp -d 2>/dev/null || mktemp -d -t difyctl-install)
trap 'rm -rf "$tmp"; rm -f "$FETCH_ERR_FILE"' EXIT INT TERM
printf 'downloading %s (Dify %s)...\n' "$asset" "$DIFY_TAG"
curl -fsSL "${base}/${asset}" -o "${tmp}/${asset}" \
|| die "download failed: ${base}/${asset}"
curl -fsSL "${base}/${checksums}" -o "${tmp}/${checksums}" \
|| die "checksum manifest download failed: ${base}/${checksums}"
_pattern=$(re_escape "$asset")
_sumline=$(grep -E -- "[[:space:]]${_pattern}\$" "${tmp}/${checksums}") || true
[ -n "$_sumline" ] || die "no checksum entry for ${asset}"
(
cd "$tmp"
printf '%s\n' "$_sumline" | $HASH -c -
) || die "checksum mismatch for ${asset}"
bin_dir=$(bin_dir_for)
mkdir -p "$bin_dir"
target_bin="${bin_dir}/difyctl"
install_binary "${tmp}/${asset}" "$target_bin"
printf '\ndifyctl v%s installed (from Dify %s): %s\n' "$version" "$DIFY_TAG" "$target_bin"
case ":${PATH}:" in
*":${bin_dir}:"*)
printf 'verify: run "difyctl version"\n'
print_skill_hint "$target_bin"
;;
*)
printf '\n%s is not on your PATH. Add this to your shell profile:\n' "$bin_dir"
printf ' export PATH="%s:$PATH"\n' "$bin_dir"
;;
esac
}
if [ "${DIFYCTL_INSTALL_LIB:-0}" != "1" ]; then
main "$@"
fi
@@ -0,0 +1,501 @@
import { execFileSync, spawnSync } from 'node:child_process'
import { chmodSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vite-plus/test'
const SCRIPT = fileURLToPath(new URL('./install-cli.sh', import.meta.url))
function pickAsset(target: string, releaseJson: string): string {
return execFileSync('sh', ['-c', `. "${SCRIPT}"; pick_asset "$1"`, 'sh', target], {
input: releaseJson,
encoding: 'utf8',
env: { ...process.env, DIFYCTL_INSTALL_LIB: '1' },
}).trim()
}
function assetVersion(name: string, target: string): string {
return execFileSync('sh', ['-c', `. "${SCRIPT}"; asset_version "$1" "$2"`, 'sh', name, target], {
encoding: 'utf8',
env: { ...process.env, DIFYCTL_INSTALL_LIB: '1' },
}).trim()
}
// Stubs the only network primitive (fetch_json) so resolution logic runs fully
// offline. Routes by URL; release bodies come from env (TAG_<tag-with-._->_>),
// the latest release from LATEST_JSON, the listing from LIST_JSON. A missing
// fixture returns 22 to mimic `curl -f` on a 4xx.
/* oxlint-disable no-template-curly-in-string -- shell parameter expansions, not JS template literals */
const FETCH_STUB = [
'fetch_json() {',
' case "$1" in',
' *"/releases/latest") [ -n "${LATEST_JSON:-}" ] || return 22; printf "%s" "$LATEST_JSON" ;;',
' *"/releases?per_page=100") [ -n "${LIST_JSON:-}" ] || return 22; printf "%s" "$LIST_JSON" ;;',
' *"/releases/tags/"*)',
' _t=${1##*/releases/tags/};',
' _k=$(printf "TAG_%s" "$_t" | tr ".-" "__");',
' eval "_v=\\${$_k:-}";',
' [ -n "$_v" ] || return 22;',
' printf "%s" "$_v" ;;',
' *) return 22 ;;',
' esac',
'}',
].join('\n')
/* oxlint-enable no-template-curly-in-string */
function runLib(
program: string,
env: Record<string, string> = {},
): { code: number; stdout: string; stderr: string } {
const full = `. "${SCRIPT}"\n${FETCH_STUB}\n${program}`
const r = spawnSync('sh', ['-c', full], {
encoding: 'utf8',
env: {
...process.env,
DIFYCTL_INSTALL_LIB: '1',
DIFY_VERSION: '',
DIFYCTL_VERSION: '',
...env,
},
})
return { code: r.status ?? 1, stdout: (r.stdout ?? '').trim(), stderr: r.stderr ?? '' }
}
// Like runLib but with a caller-supplied fetch_json stub, so we can drive the
// real rate_limit_hint / maybe_ratelimit_exit / fetch_hit_ratelimit (which the
// script defines) by writing a classified reason to FETCH_ERR_FILE.
function runLibStub(
stub: string,
program: string,
env: Record<string, string> = {},
): { code: number; stderr: string } {
const full = `. "${SCRIPT}"\n${stub}\n${program}`
const r = spawnSync('sh', ['-c', full], {
encoding: 'utf8',
env: {
...process.env,
DIFYCTL_INSTALL_LIB: '1',
DIFY_VERSION: '',
DIFYCTL_VERSION: '',
...env,
},
})
return { code: r.status ?? 1, stderr: r.stderr ?? '' }
}
// A fetch_json that always fails with the given classification, mimicking the
// real one writing to FETCH_ERR_FILE from inside a command-substitution subshell.
function failStub(reason: string): string {
return `fetch_json() { printf '%s' '${reason}' > "$FETCH_ERR_FILE"; return 1; }`
}
const REL_1142 = JSON.stringify({
tag_name: '1.14.2',
assets: [{ name: 'difyctl-v0.2.0-linux-x64' }, { name: 'difyctl-v0.2.0-checksums.txt' }],
})
const REL_1150 = JSON.stringify({
tag_name: '1.15.0',
assets: [{ name: 'difyctl-v0.3.0-linux-x64' }],
})
const LIST_NEWEST_FIRST = JSON.stringify({
releases: [{ tag_name: '1.15.0' }, { tag_name: '1.14.2' }],
})
const RELEASE = JSON.stringify({
tag_name: '1.14.2',
name: 'Dify 1.14.2',
assets: [
{ name: 'difyctl-v0.1.0-rc.1-linux-x64' },
{ name: 'difyctl-v0.2.0-linux-x64' },
{ name: 'difyctl-v0.2.0-linux-arm64' },
{ name: 'difyctl-v0.2.0-darwin-arm64' },
{ name: 'difyctl-v0.2.0-windows-x64.exe' },
{ name: 'difyctl-v0.2.0-checksums.txt' },
{ name: 'some-other-asset.zip' },
],
})
describe('install-cli pick_asset', () => {
it('picks the highest difyctl version for a linux target', () => {
expect(pickAsset('linux-x64', RELEASE)).toBe('difyctl-v0.2.0-linux-x64')
})
it('matches the windows .exe asset', () => {
expect(pickAsset('windows-x64', RELEASE)).toBe('difyctl-v0.2.0-windows-x64.exe')
})
it('matches an arm64 target exactly (no x64 bleed-through)', () => {
expect(pickAsset('darwin-arm64', RELEASE)).toBe('difyctl-v0.2.0-darwin-arm64')
})
it('excludes the checksums asset', () => {
expect(pickAsset('linux-x64', RELEASE)).not.toContain('checksums')
})
it('yields empty when no asset matches the target', () => {
expect(pickAsset('darwin-x64', RELEASE)).toBe('')
})
it('picks the highest semver when several difyctl versions are present', () => {
const many = JSON.stringify({
assets: [
{ name: 'difyctl-v0.2.0-linux-x64' },
{ name: 'difyctl-v0.10.0-linux-x64' },
{ name: 'difyctl-v0.9.0-linux-x64' },
],
})
expect(pickAsset('linux-x64', many)).toBe('difyctl-v0.10.0-linux-x64')
})
})
describe('install-cli asset_version', () => {
it('extracts the version from a posix asset name', () => {
expect(assetVersion('difyctl-v0.2.0-linux-x64', 'linux-x64')).toBe('0.2.0')
})
it('extracts the version from a windows .exe asset name', () => {
expect(assetVersion('difyctl-v0.2.0-windows-x64.exe', 'windows-x64')).toBe('0.2.0')
})
it('extracts a prerelease version', () => {
expect(assetVersion('difyctl-v0.1.0-rc.1-linux-x64', 'linux-x64')).toBe('0.1.0-rc.1')
})
})
describe('install-cli resolve_release', () => {
it('DIFY_VERSION pins the release directly', () => {
const r = runLib('resolve_release linux-x64; printf "%s" "$DIFY_TAG"', {
DIFY_VERSION: '1.14.2',
TAG_1_14_2: REL_1142,
})
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.14.2')
})
it('DIFY_VERSION that does not exist dies with a clear message', () => {
const r = runLib('resolve_release linux-x64', { DIFY_VERSION: '9.9.9' })
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('Dify release 9.9.9 not found')
})
it('blank resolves to the latest stable release', () => {
const r = runLib('resolve_release linux-x64; printf "%s" "$DIFY_TAG"', {
LATEST_JSON: REL_1150,
})
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.15.0')
})
it('blank dies when the latest query fails (no silent fallback)', () => {
const r = runLib('resolve_release linux-x64')
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('failed to query latest Dify release')
})
it('DIFYCTL_VERSION resolves to the release hosting that build', () => {
const r = runLib('resolve_release linux-x64; printf "%s" "$DIFY_TAG"', {
DIFYCTL_VERSION: '0.2.0',
LIST_JSON: LIST_NEWEST_FIRST,
TAG_1_15_0: REL_1150,
TAG_1_14_2: REL_1142,
})
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.14.2')
})
it('DIFYCTL_VERSION not hosted anywhere dies', () => {
const r = runLib('resolve_release linux-x64', {
DIFYCTL_VERSION: '9.9.9',
LIST_JSON: LIST_NEWEST_FIRST,
TAG_1_15_0: REL_1150,
TAG_1_14_2: REL_1142,
})
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('difyctl 9.9.9 not found on any Dify release')
})
})
describe('install-cli find_release_for_difyctl', () => {
it('returns the newest release whose assets host the wanted build', () => {
const r = runLib('find_release_for_difyctl 0.2.0 linux-x64', {
LIST_JSON: LIST_NEWEST_FIRST,
TAG_1_15_0: REL_1150,
TAG_1_14_2: REL_1142,
})
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.14.2')
})
it('dies (not false-negative) when the releases listing fails', () => {
const r = runLib('find_release_for_difyctl 0.2.0 linux-x64')
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('failed to query')
})
it('warns and skips a release whose fetch fails, then finds it later', () => {
const r = runLib('find_release_for_difyctl 0.2.0 linux-x64', {
LIST_JSON: LIST_NEWEST_FIRST,
TAG_1_14_2: REL_1142,
})
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.14.2')
expect(r.stderr).toContain('fetch failed for 1.15.0')
})
})
describe('install-cli rate limit', () => {
const futureReset = String(Math.floor(Date.now() / 1000) + 1800)
it('latest: reports the rate limit with reset ETA and remediation, not a generic error', () => {
const r = runLibStub(failStub(`ratelimit:${futureReset}`), 'resolve_release linux-x64')
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('rate limit exceeded')
expect(r.stderr).toContain('resets in ~')
expect(r.stderr).toContain('GITHUB_TOKEN')
expect(r.stderr).not.toContain('failed to query latest')
})
it('DIFY_VERSION: rate limit wins over the misleading "not found" message', () => {
const r = runLibStub(failStub(`ratelimit:${futureReset}`), 'resolve_release linux-x64', {
DIFY_VERSION: '1.15.0',
})
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('rate limit exceeded')
expect(r.stderr).not.toContain('not found')
})
it('DIFYCTL_VERSION: rate limit surfaces from the nested subshell, not "not found"', () => {
const r = runLibStub(failStub(`ratelimit:${futureReset}`), 'resolve_release linux-x64', {
DIFYCTL_VERSION: '0.2.0',
})
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('rate limit exceeded')
expect(r.stderr).not.toContain('not found')
})
it('omits the ETA line when the reset epoch is missing', () => {
const r = runLibStub(failStub('ratelimit:'), 'resolve_release linux-x64')
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('rate limit exceeded')
expect(r.stderr).not.toContain('resets in ~')
})
it('a non-rate-limit HTTP error falls back to the generic message (no false hint)', () => {
const r = runLibStub(failStub('http:500'), 'resolve_release linux-x64')
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('failed to query latest')
expect(r.stderr).not.toContain('rate limit exceeded')
})
})
// A stand-in for curl that honours the flags fetch_json passes (-D/-o/-w/-H) and
// fabricates a response per FAKE_MODE, so the tests exercise the REAL fetch_json
// (its curl invocation, header parsing, classification and token handling) rather
// than a stub. Header names it emits are lowercase, as HTTP/2 delivers them.
const FAKE_CURL = `#!/bin/sh
hdr=""; body=""
while [ $# -gt 0 ]; do
case "$1" in
-D) hdr="$2"; shift 2 ;;
-o) body="$2"; shift 2 ;;
-H) printf '%s\\n' "$2" >> "\${FAKE_HDR_LOG:-/dev/null}"; shift 2 ;;
-w) shift 2 ;;
*) shift ;;
esac
done
case "\${FAKE_MODE:-ok}" in
ok)
[ -n "$hdr" ] && printf 'HTTP/2 200\\r\\n\\r\\n' > "$hdr"
[ -n "$body" ] && printf '%s' "\${FAKE_BODY:-}" > "$body"
printf '200' ;;
ratelimit)
[ -n "$hdr" ] && printf 'HTTP/2 403\\r\\nx-ratelimit-remaining: 0\\r\\nx-ratelimit-reset: %s\\r\\n\\r\\n' "\${FAKE_RESET:-9999999999}" > "$hdr"
printf '403' ;;
perm403)
[ -n "$hdr" ] && printf 'HTTP/2 403\\r\\nx-ratelimit-remaining: 59\\r\\n\\r\\n' > "$hdr"
printf '403' ;;
notfound)
[ -n "$hdr" ] && printf 'HTTP/2 404\\r\\n\\r\\n' > "$hdr"
printf '404' ;;
net) exit 6 ;;
esac
`
// Drive the real fetch_json with FAKE_CURL first on PATH. Returns "OK|<body>" or
// "FAIL|<FETCH_ERR_FILE contents>", plus any -H lines the fake curl received.
function runRealFetch(
mode: string,
env: Record<string, string> = {},
): { result: string; headers: string } {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-fakecurl-'))
const hdrLog = join(dir, 'hdrlog')
writeFileSync(join(dir, 'curl'), FAKE_CURL)
chmodSync(join(dir, 'curl'), 0o755)
const program =
'if body=$(fetch_json "https://api.github.com/repos/x/releases/latest"); then printf \'OK|%s\' "$body"; else printf \'FAIL|%s\' "$(cat "$FETCH_ERR_FILE" 2>/dev/null)"; fi'
const r = spawnSync('sh', ['-c', `. "${SCRIPT}"\n${program}`], {
encoding: 'utf8',
env: {
...process.env,
PATH: `${dir}:${process.env.PATH ?? ''}`,
DIFYCTL_INSTALL_LIB: '1',
DIFY_VERSION: '',
DIFYCTL_VERSION: '',
FAKE_MODE: mode,
FAKE_HDR_LOG: hdrLog,
...env,
},
})
let headers = ''
try {
headers = readFileSync(hdrLog, 'utf8')
} catch {
/* no headers logged */
}
rmSync(dir, { recursive: true, force: true })
return { result: (r.stdout ?? '').trim(), headers }
}
describe('install-cli fetch_json (real, fake curl on PATH)', () => {
it('returns the response body on 200', () => {
expect(runRealFetch('ok', { FAKE_BODY: '{"tag_name":"1.15.0"}' }).result).toBe(
'OK|{"tag_name":"1.15.0"}',
)
})
it('classifies a 403 with x-ratelimit-remaining:0 as a rate limit and captures the reset', () => {
expect(runRealFetch('ratelimit', { FAKE_RESET: '1893456000' }).result).toBe(
'FAIL|ratelimit:1893456000',
)
})
it('classifies a 403 with tokens left as a plain http error, not a rate limit', () => {
expect(runRealFetch('perm403').result).toBe('FAIL|http:403')
})
it('classifies a 404 as an http error', () => {
expect(runRealFetch('notfound').result).toBe('FAIL|http:404')
})
it('classifies a curl transport failure as a network error', () => {
expect(runRealFetch('net').result).toBe('FAIL|network')
})
it('sends an Authorization bearer header when GITHUB_TOKEN is set', () => {
expect(runRealFetch('ok', { FAKE_BODY: '{}', GITHUB_TOKEN: 'ghp_secret' }).headers).toContain(
'Authorization: Bearer ghp_secret',
)
})
it('falls back to GH_TOKEN when GITHUB_TOKEN is unset', () => {
expect(
runRealFetch('ok', { FAKE_BODY: '{}', GITHUB_TOKEN: '', GH_TOKEN: 'gho_fallback' }).headers,
).toContain('Authorization: Bearer gho_fallback')
})
it('sends no Authorization header when neither token is set', () => {
expect(
runRealFetch('ok', { FAKE_BODY: '{}', GITHUB_TOKEN: '', GH_TOKEN: '' }).headers,
).not.toContain('Authorization')
})
})
describe('install-cli.sh install location', () => {
const where = (env: Record<string, string>) =>
runLib('bin_dir_for', { DIFYCTL_PREFIX: '', DIFYCTL_INSTALL_DIR: '', HOME: '/home/u', ...env })
.stdout
it('defaults to ~/.local/bin', () => {
expect(where({})).toBe('/home/u/.local/bin')
})
it('puts the binary under <DIFYCTL_PREFIX>/bin', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify' })).toBe('/opt/dify/bin')
})
it('takes DIFYCTL_INSTALL_DIR as the bin folder itself, over DIFYCTL_PREFIX', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify', DIFYCTL_INSTALL_DIR: '/usr/local/bin' })).toBe(
'/usr/local/bin',
)
})
})
describe('install-cli.sh install_binary', () => {
it('replaces the binary through a rename, leaving no staged file behind', () => {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-install-'))
try {
const src = join(dir, 'new')
const dest = join(dir, 'bin-difyctl')
writeFileSync(src, 'new build')
writeFileSync(dest, 'old build')
const r = runLib(`install_binary "${src}" "${dest}"`)
expect(r.code).toBe(0)
expect(readFileSync(dest, 'utf8')).toBe('new build')
expect(execFileSync('ls', ['-A', dir], { encoding: 'utf8' }).trim().split('\n')).toEqual([
'bin-difyctl',
'new',
])
// Windows filesystems have no executable bit to check.
if (process.platform !== 'win32') {
expect(
execFileSync('sh', ['-c', `test -x "${dest}" && echo yes`], { encoding: 'utf8' }),
).toBe('yes\n')
}
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
// A shared install must stay readable by other users, whatever the installer's umask.
it.skipIf(process.platform === 'win32')(
'installs a 0600 source as mode 0755 under umask 077',
() => {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-install-'))
try {
const src = join(dir, 'new')
const dest = join(dir, 'bin-difyctl')
writeFileSync(src, 'new build', { mode: 0o600 })
const r = runLib(`umask 077; install_binary "${src}" "${dest}"`)
expect(r.code).toBe(0)
expect(statSync(dest).mode & 0o777).toBe(0o755)
} finally {
rmSync(dir, { recursive: true, force: true })
}
},
)
it('dies without touching the old binary when the folder is not writable', () => {
const r = runLib('install_binary /nonexistent/src /nonexistent-dir/difyctl')
expect(r.code).not.toBe(0)
expect(r.stderr).toMatch(/cannot write \/nonexistent-dir\/difyctl/)
})
})
// A v2 binary answers `install skills --help`; a v1 one exits 1 with "unknown command".
describe.skipIf(process.platform === 'win32')('install-cli.sh print_skill_hint', () => {
const fakeBins = [
'd="$(mktemp -d)"',
'printf \'#!/bin/sh\\n[ "$*" = "install skills --help" ]\\n\' > "$d/v2"',
'printf \'#!/bin/sh\\necho "unknown command: $*" >&2\\nexit 1\\n\' > "$d/v1"',
'chmod 0755 "$d/v2" "$d/v1"',
]
it('prints the hint for a binary that has install skills', () => {
const r = runLib([...fakeBins, 'print_skill_hint "$d/v2"', 'rm -rf "$d"'].join('\n'))
expect(r.code).toBe(0)
expect(r.stdout).toMatch(/difyctl install skills <skills root>/)
})
it('prints nothing for a binary without install skills', () => {
const r = runLib([...fakeBins, 'print_skill_hint "$d/v1"', 'rm -rf "$d"'].join('\n'))
expect(r.code).toBe(0)
expect(r.stdout).toBe('')
})
it('prints nothing when the binary cannot run', () => {
const r = runLib('print_skill_hint /nonexistent/difyctl')
expect(r.code).toBe(0)
expect(r.stdout).toBe('')
})
})
+48
View File
@@ -0,0 +1,48 @@
#!/bin/sh
# install-local.sh — install difyctl from locally built standalone binaries.
# Run via: pnpm install:local (after `pnpm build:bin:local`)
#
# Consumes the raw, self-contained binaries emitted by scripts/release-build.sh
# into dist/bin (difyctl-v<ver>-<os>-<arch>). No GitHub Release needed: build on
# one machine, copy dist/bin to the tester, point this script at that directory.
set -eu
PREFIX="${DIFYCTL_PREFIX:-${HOME}/.local}"
SHARE_DIR="${PREFIX}/share/difyctl"
BIN_DIR="${PREFIX}/bin"
case "$(uname -s)" in
Linux*) os=linux ;;
Darwin*) os=darwin ;;
*) echo "unsupported OS: $(uname -s) (use install.ps1 on Windows)" >&2; exit 1 ;;
esac
case "$(uname -m)" in
x86_64|amd64) arch=x64 ;;
arm64|aarch64) arch=arm64 ;;
*) echo "unsupported arch: $(uname -m)" >&2; exit 1 ;;
esac
# Accept an optional directory path as the first argument.
# Default to the cli/dist/bin directory if not provided.
ARTIFACT_DIR="${1:-$(cd "$(dirname "$0")/../dist/bin" 2>/dev/null && pwd || true)}"
BINARY="$(ls "${ARTIFACT_DIR}"/difyctl-v*-${os}-${arch} 2>/dev/null | sort -V | tail -1)"
if [ -z "$BINARY" ]; then
echo "no binary found for ${os}-${arch} in ${ARTIFACT_DIR:-<unset>}" >&2
echo "run: pnpm build:bin:local" >&2
exit 1
fi
echo "installing from $(basename "$BINARY") ..."
rm -rf "$SHARE_DIR"
mkdir -p "${SHARE_DIR}/bin" "$BIN_DIR"
cp "$BINARY" "${SHARE_DIR}/bin/difyctl"
chmod +x "${SHARE_DIR}/bin/difyctl"
ln -sf "${SHARE_DIR}/bin/difyctl" "${BIN_DIR}/difyctl"
echo "installed: ${BIN_DIR}/difyctl"
case ":${PATH}:" in
*":${BIN_DIR}:"*) ;;
*) echo "note: add ${BIN_DIR} to your PATH" ;;
esac
+135
View File
@@ -0,0 +1,135 @@
# install-r2.ps1 — one-line difyctl installer (Windows) from Cloudflare R2.
# Usage: $env:DIFYCTL_R2_BASE='<BASE>'; irm https://raw.githubusercontent.com/langgenius/dify/main/cli/scripts/install-r2.ps1 | iex
# Env: DIFYCTL_R2_BASE (required), DIFYCTL_CHANNEL (default edge),
# DIFYCTL_INSTALL_DIR (default %LOCALAPPDATA%\difyctl\bin; binary written here as difyctl.exe),
# DIFYCTL_PREFIX (the folder above the bin folder; <prefix>\bin is used when DIFYCTL_INSTALL_DIR is unset),
# DIFYCTL_VERSION (pin exact published version),
# DIFYCTL_COMMIT (pin by git commit, short or full sha; via index.json),
# DIFYCTL_R2_PREFIX (default difyctl; key root for pointer JSONs),
# DIFYCTL_R2_BIN_PREFIX (default <prefix>/bin; key root for binaries).
# With no pin the channel pointer (latest) is installed.
$ErrorActionPreference = 'Stop'
function Get-TargetField($manifest, [string]$target, [string]$field) {
$t = $manifest.targets.$target
if (-not $t) { return $null }
return $t.$field
}
# First build in index.json matching by exact version or commit prefix.
function Resolve-IndexBuild($index, [string]$kind, [string]$want) {
foreach ($b in $index.builds) {
$sel = if ($kind -eq 'commit') { $b.commit } else { $b.version }
$hit = if ($kind -eq 'commit') { $sel.StartsWith($want) } else { $sel -eq $want }
if ($hit) { return $b }
}
return $null
}
# Parse a checksums.txt ("<sha> <asset>") for the line whose asset matches the
# target; returns @{ Sha; Asset } or $null.
function Get-ChecksumTarget([string]$text, [string]$target) {
foreach ($line in ($text -split "`n")) {
$line = $line.Trim()
if ($line -match "\sdifyctl-v.*-$target(\.exe)?$") {
$parts = $line -split '\s+'
return @{ Sha = $parts[0]; Asset = $parts[-1] }
}
}
return $null
}
# DIFYCTL_INSTALL_DIR names the bin folder itself; DIFYCTL_PREFIX the folder above it.
function Resolve-BinDir {
if ($env:DIFYCTL_INSTALL_DIR) { return $env:DIFYCTL_INSTALL_DIR }
$prefix = if ($env:DIFYCTL_PREFIX) { $env:DIFYCTL_PREFIX } else { Join-Path $env:LOCALAPPDATA 'difyctl' }
return Join-Path $prefix 'bin'
}
# The user PATH alone: $env:PATH also holds the machine PATH, which must not be copied into it.
function Get-PathHint([string]$Dir) {
return " [Environment]::SetEnvironmentVariable('PATH', `"$Dir;`" + [Environment]::GetEnvironmentVariable('PATH', 'User'), 'User')"
}
# The hint names a v2 command, so a binary without it (any v1 build) gets none. Under
# Windows PowerShell 5.1 a redirected stderr line throws when errors stop, hence the catch.
function Write-SkillHint([string]$Bin) {
if (-not (Test-Path -LiteralPath $Bin -PathType Leaf)) { return }
try { & $Bin install skills --help *> $null } catch { return }
if ($LASTEXITCODE -ne 0) { return }
Write-Host 'AI agents: run `difyctl install skills <skills root>` after each install or upgrade; the skill ships inside the binary.'
}
# Download, sha256-verify, place. Returns nothing; throws on a missing or wrong checksum.
function Install-DifyctlBinary([string]$dlUrl, [string]$sha, [string]$version, [string]$channel, [string]$installDir) {
if (-not $sha) { throw "no checksum published for $dlUrl; not installing an unverified binary" }
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ('difyctl-' + [guid]::NewGuid().ToString('N') + '.exe')
try {
Invoke-WebRequest -Uri $dlUrl -OutFile $tmp
$actual = (Get-FileHash -Path $tmp -Algorithm SHA256).Hash.ToLower()
if ($actual -ne $sha.ToLower()) { throw "checksum mismatch for $dlUrl" }
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
$dest = Join-Path $installDir 'difyctl.exe'
try { Move-Item -Path $tmp -Destination $dest -Force }
catch { throw "cannot replace $dest — close any running difyctl and re-run." }
}
finally {
Remove-Item -Path $tmp -Force -ErrorAction SilentlyContinue
}
Write-Host "difyctl $version (channel $channel) installed: $dest"
if (($env:PATH -split ';') -notcontains $installDir) {
Write-Host "$installDir is not on your PATH. Add it with:"
Write-Host (Get-PathHint $installDir)
}
else {
Write-SkillHint $dest
}
}
function Install-DifyctlR2 {
$base = $env:DIFYCTL_R2_BASE
if (-not $base) { throw "set DIFYCTL_R2_BASE to the R2 public base (e.g. https://pub-….r2.dev)" }
$base = $base.TrimEnd('/')
$channel = if ($env:DIFYCTL_CHANNEL) { $env:DIFYCTL_CHANNEL } else { 'edge' }
$prefix = if ($env:DIFYCTL_R2_PREFIX) { $env:DIFYCTL_R2_PREFIX } else { 'difyctl' }
$binPrefix = if ($env:DIFYCTL_R2_BIN_PREFIX) { $env:DIFYCTL_R2_BIN_PREFIX } else { "$prefix/bin" }
$installDir = Resolve-BinDir
$target = 'windows-x64'
if ($env:DIFYCTL_VERSION -or $env:DIFYCTL_COMMIT) {
$iurl = "$base/$prefix/$channel/index.json"
try { $index = Invoke-RestMethod -Uri $iurl }
catch { throw "R2 unavailable fetching $iurl; retry." }
$build = if ($env:DIFYCTL_VERSION) { Resolve-IndexBuild $index 'version' $env:DIFYCTL_VERSION }
else { Resolve-IndexBuild $index 'commit' $env:DIFYCTL_COMMIT }
$pin = if ($env:DIFYCTL_VERSION) { $env:DIFYCTL_VERSION } else { $env:DIFYCTL_COMMIT }
if (-not $build) { throw "no build matching $pin in channel $channel" }
$version = $build.version
$vbase = "$base/$binPrefix/$channel/$($build.dir)"
try { $cf = (Invoke-WebRequest -Uri "$vbase/difyctl-v$version-checksums.txt").Content }
catch { throw "checksums missing for $version (channel $channel)" }
$ct = Get-ChecksumTarget $cf $target
if (-not $ct) { throw "no build for $target at $version" }
Install-DifyctlBinary "$vbase/$($ct.Asset)" $ct.Sha $version $channel $installDir
return
}
$murl = "$base/$prefix/$channel/manifest.json"
try { $manifest = Invoke-RestMethod -Uri $murl }
catch {
if ($_.Exception.Response.StatusCode.value__ -eq 404) {
throw "channel '$channel' not published to R2. For rc/stable use install.ps1 (GitHub)."
}
throw "R2 unavailable fetching $murl; retry."
}
if ($manifest.channel -ne $channel) { throw "manifest channel '$($manifest.channel)' != requested '$channel'" }
$asset = Get-TargetField $manifest $target 'asset'
$sha = Get-TargetField $manifest $target 'sha256'
if (-not $asset) { throw "no build for $target in channel $channel" }
Install-DifyctlBinary "$($manifest.baseUrl)/$asset" $sha $manifest.version $channel $installDir
}
if ($env:DIFYCTL_INSTALL_LIB -ne '1') { Install-DifyctlR2 }
@@ -0,0 +1,139 @@
import { spawnSync } from 'node:child_process'
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it, vi } from 'vite-plus/test'
const SCRIPT = fileURLToPath(new URL('./install-r2.ps1', import.meta.url))
const hasPwsh = spawnSync('pwsh', ['-v'], { encoding: 'utf8' }).status === 0
// Each test starts pwsh; cold starts on a busy CI runner can exceed the default 5 seconds.
if (hasPwsh) vi.setConfig({ testTimeout: 15_000 })
const d = hasPwsh ? describe : describe.skip
const MANIFEST = JSON.stringify({
schema: 1,
name: 'difyctl',
channel: 'edge',
version: '0.1.0-edge.2fd7b82',
baseUrl: 'https://pub.example.r2.dev/difyctl/edge/0.1.0-edge.2fd7b82',
targets: {
'windows-x64': { asset: 'difyctl-v0.1.0-edge.2fd7b82-windows-x64.exe', sha256: 'deadbeef' },
},
})
function pwsh(
program: string,
env: Record<string, string> = {},
): { code: number; stdout: string; stderr: string } {
const full = `. '${SCRIPT}'\n${program}`
const r = spawnSync('pwsh', ['-NoProfile', '-Command', full], {
encoding: 'utf8',
env: { ...process.env, DIFYCTL_INSTALL_LIB: '1', ...env },
})
return {
code: r.status ?? 1,
stdout: (r.stdout ?? '').replace(/\r\n/g, '\n').trim(),
stderr: r.stderr ?? '',
}
}
d('install-r2.ps1', () => {
it('parses a target asset + sha from the manifest', () => {
const prog =
`$m = ConvertFrom-Json @'\n${MANIFEST}\n'@\n` +
`Write-Output (Get-TargetField $m 'windows-x64' 'asset')\n` +
`Write-Output (Get-TargetField $m 'windows-x64' 'sha256')`
const { stdout } = pwsh(prog)
expect(stdout).toBe('difyctl-v0.1.0-edge.2fd7b82-windows-x64.exe\ndeadbeef')
})
it('errors when DIFYCTL_R2_BASE is unset', () => {
const r = spawnSync('pwsh', ['-NoProfile', '-File', SCRIPT], {
encoding: 'utf8',
env: { ...process.env, DIFYCTL_R2_BASE: '' },
})
if (hasPwsh) {
expect(r.status).not.toBe(0)
expect(r.stderr + r.stdout).toMatch(/DIFYCTL_R2_BASE/)
}
})
})
d('install-r2.ps1 install location, PATH hint and download', () => {
const where = (env: Record<string, string>) =>
pwsh('Write-Output (Resolve-BinDir)', {
DIFYCTL_PREFIX: '',
DIFYCTL_INSTALL_DIR: '',
LOCALAPPDATA: '/lad',
...env,
}).stdout
it('defaults to %LOCALAPPDATA%\\difyctl\\bin', () => {
expect(where({})).toBe(join('/lad', 'difyctl', 'bin'))
})
it('accepts DIFYCTL_PREFIX like the GitHub installer', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify' })).toBe(join('/opt/dify', 'bin'))
})
it('keeps DIFYCTL_INSTALL_DIR as the bin folder itself, over DIFYCTL_PREFIX', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify', DIFYCTL_INSTALL_DIR: '/custom/bin' })).toBe(
'/custom/bin',
)
})
it('the PATH command adds to the user PATH, not the whole session PATH', () => {
const hint = pwsh(`Write-Output (Get-PathHint 'C:\\dify\\bin')`).stdout
expect(hint).toContain(
`"C:\\dify\\bin;" + [Environment]::GetEnvironmentVariable('PATH', 'User')`,
)
expect(hint).not.toContain('$env:PATH')
})
it('stops before downloading when the checksum is missing', () => {
const r = pwsh(
"function Invoke-WebRequest { throw 'must not download' }\n" +
"try { Install-DifyctlBinary 'https://x/difyctl.exe' '' '1.0.0' 'edge' '/tmp/none' } " +
'catch { Write-Output $_.Exception.Message }',
)
expect(r.stdout).toMatch(/^no checksum published for https:\/\/x\/difyctl\.exe/)
})
it('downloads to a unique temp file, not one named after the asset', () => {
const r = pwsh(
'function Invoke-WebRequest { param($Uri, $OutFile) Write-Output "OUT=$OutFile"; Set-Content -Path $OutFile -Value x }\n' +
"try { Install-DifyctlBinary 'https://x/difyctl.exe' 'deadbeef' '1.0.0' 'edge' '/tmp/none' } " +
'catch { Write-Output $_.Exception.Message }',
)
expect(r.stdout).toMatch(/OUT=.*difyctl-[0-9a-f]{32}\.exe\n/)
expect(r.stdout).toMatch(/checksum mismatch/)
})
})
// A v2 binary answers `install skills --help`; a v1 one exits 1 with "unknown command".
// The fakes are shell scripts, so this runs where pwsh runs outside Windows.
describe.skipIf(!hasPwsh || process.platform === 'win32')('install-r2.ps1 Write-SkillHint', () => {
const hint = (bin: string) => pwsh(`Write-SkillHint '${bin}'`)
it('prints the hint for a binary that has install skills, and stays quiet otherwise', () => {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-hint-'))
try {
const v2 = join(dir, 'v2')
const v1 = join(dir, 'v1')
writeFileSync(v2, '#!/bin/sh\n[ "$*" = "install skills --help" ]\n', { mode: 0o755 })
writeFileSync(v1, '#!/bin/sh\necho "unknown command: $*" >&2\nexit 1\n', { mode: 0o755 })
expect(hint(v2)).toMatchObject({
code: 0,
stdout: expect.stringContaining('difyctl install skills <skills root>'),
})
expect(hint(v1)).toMatchObject({ code: 0, stdout: '' })
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('prints nothing when the binary cannot run', () => {
expect(hint('/nonexistent/difyctl')).toMatchObject({ code: 0, stdout: '' })
})
})
+185
View File
@@ -0,0 +1,185 @@
#!/bin/sh
# install-r2.sh — one-line difyctl installer from Cloudflare R2.
# Reads a per-channel pointer manifest, sha256-verifies, installs to PATH.
# Usage: curl -fsSL https://raw.githubusercontent.com/langgenius/dify/main/cli/scripts/install-r2.sh | DIFYCTL_R2_BASE=<BASE> sh
# Env:
# DIFYCTL_R2_BASE (required) R2 public base, e.g. https://pub-….r2.dev
# DIFYCTL_CHANNEL (default edge)
# DIFYCTL_INSTALL_DIR (default $HOME/.local/bin) directory the binary is written to as <dir>/difyctl
# DIFYCTL_PREFIX the folder above the bin folder; <prefix>/bin is used when DIFYCTL_INSTALL_DIR is unset
# DIFYCTL_VERSION pin an exact published version (e.g. 0.1.0-edge.ce4af868)
# DIFYCTL_COMMIT pin by git commit (short or full sha); resolved via index.json
# DIFYCTL_R2_PREFIX (default difyctl) key root for pointer JSONs
# DIFYCTL_R2_BIN_PREFIX (default <prefix>/bin) key root for binaries
# With no pin the channel pointer (latest) is installed. A pin resolves through
# <prefix>/<channel>/index.json -> the build's immutable dir under the bin prefix.
set -eu
# --- library functions (sourced for tests when DIFYCTL_INSTALL_LIB=1) ---
tmp_m="$(mktemp 2>/dev/null || echo /tmp/difyctl-manifest.$$)"
trap 'rm -f "$tmp_m" "${tmp_c:-}" "${tmp_b:-}"' EXIT INT TERM
err() { printf '%s\n' "install-r2: $*" >&2; }
die() { err "$*"; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || die "$1 is required"; }
# print_skill_hint BIN: the hint names a v2 command, so a binary without it (any v1 build) gets none.
print_skill_hint() {
"$1" install skills --help >/dev/null 2>&1 || return 0
printf '%s\n' 'AI agents: run `difyctl install skills <skills root>` after each install or upgrade; the skill ships inside the binary.'
}
# DIFYCTL_INSTALL_DIR names the bin folder itself; DIFYCTL_PREFIX the folder above it.
bin_dir_for() {
printf '%s' "${DIFYCTL_INSTALL_DIR:-${DIFYCTL_PREFIX:-${HOME}/.local}/bin}"
}
# install_binary SRC DEST: stage the copy next to DEST, then rename it over DEST, so a
# running difyctl keeps its old file and DEST is never half written.
install_binary() {
_stage="$(dirname "$2")/.difyctl.install.$$"
if cp "$1" "$_stage" && chmod 0755 "$_stage" && mv -f "$_stage" "$2"; then
return 0
fi
rm -f "$_stage"
die "cannot write $2"
}
detect_target() {
case "$(uname -s)" in
Linux*) _os=linux ;;
Darwin*) _os=darwin ;;
*) die "unsupported OS: $(uname -s) (use install-r2.ps1 on Windows)" ;;
esac
case "$(uname -m)" in
x86_64|amd64) _arch=x64 ;;
arm64|aarch64) _arch=arm64 ;;
*) die "unsupported arch: $(uname -m)" ;;
esac
printf '%s-%s' "$_os" "$_arch"
}
# grep/sed (no jq). Correct only because release-r2-edge.mjs renders one key per line.
# manifest_str <file> <key> -> value of a top-level string field
manifest_str() {
grep "\"$2\"[[:space:]]*:" "$1" | head -1 \
| sed -E "s/.*\"$2\"[[:space:]]*:[[:space:]]*\"([^\"]+)\".*/\\1/"
}
# manifest_target_field <file> <target-id> <asset|sha256> -> value on that target's line
manifest_target_field() {
grep "\"$2\"[[:space:]]*:" "$1" | head -1 \
| sed -E "s/.*\"$3\"[[:space:]]*:[[:space:]]*\"([^\"]+)\".*/\\1/"
}
# Resolve a pinned build from index.json (no jq). Correct only because
# release-r2-edge.mjs renders each build's fields one per line, dir last.
# Prints "<version>\t<dir>" of the first match, nothing if none.
# index_resolve <index-file> <kind:version|commit> <value> (commit = prefix match)
index_resolve() {
awk -v kind="$2" -v want="$3" '
function val(s) { sub(/^[^:]*:[[:space:]]*"/, "", s); sub(/".*$/, "", s); return s }
/"version"[[:space:]]*:/ { v = val($0) }
/"commit"[[:space:]]*:/ { c = val($0) }
/"dir"[[:space:]]*:/ {
d = val($0)
sel = (kind == "commit") ? c : v
if (kind == "commit") { if (index(sel, want) == 1) { print v "\t" d; exit } }
else if (sel == want) { print v "\t" d; exit }
}
' "$1"
}
# checksums_target <checksums-file> <target-id> -> "<sha256>\t<asset>"
# checksums lines are "<sha> <asset>"; match asset ending -<target> or -<target>.exe.
checksums_target() {
grep -E "[[:space:]]difyctl-v.*-$2(\.exe)?\$" "$1" | head -1 \
| awk '{ print $1 "\t" $NF }'
}
sha256_check() {
# $1 = file, $2 = expected hex
if command -v sha256sum >/dev/null 2>&1; then _a="$(sha256sum "$1" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then _a="$(shasum -a 256 "$1" | awk '{print $1}')"
else die "no sha256 tool (need sha256sum or shasum)"; fi
[ "$_a" = "$2" ] || die "checksum mismatch for $1"
}
# fetch_verify_install <download-url> <expected-sha> <version> <channel>
fetch_verify_install() {
tmp_b="$(mktemp)"
# NOTE: no --compressed — must hash the raw bytes
curl -fsSL "$1" -o "$tmp_b" || die "download failed: $1"
sha256_check "$tmp_b" "$2"
mkdir -p "$install_dir"
install_binary "$tmp_b" "${install_dir}/difyctl"
printf 'difyctl %s (channel %s) installed: %s\n' "$3" "$4" "${install_dir}/difyctl"
case ":${PATH}:" in
*":${install_dir}:"*) print_skill_hint "${install_dir}/difyctl" ;;
*) printf 'note: add %s to your PATH\n' "$install_dir" ;;
esac
}
# Resolve a pinned build into download url + sha. Sets: version, dl_url, dl_sha.
resolve_pinned() {
iurl="${base}/${prefix}/${channel}/index.json"
curl -fsSL "$iurl" -o "$tmp_m" || die "R2 unavailable fetching ${iurl}; retry."
if [ -n "${DIFYCTL_VERSION:-}" ]; then res="$(index_resolve "$tmp_m" version "$DIFYCTL_VERSION")"
else res="$(index_resolve "$tmp_m" commit "$DIFYCTL_COMMIT")"; fi
[ -n "$res" ] || die "no build matching ${DIFYCTL_VERSION:-$DIFYCTL_COMMIT} in channel ${channel}"
version="$(printf '%s' "$res" | cut -f1)"
dir="$(printf '%s' "$res" | cut -f2)"
vbase="${base}/${bin_prefix}/${channel}/${dir}"
tmp_c="$(mktemp)"
curl -fsSL "${vbase}/difyctl-v${version}-checksums.txt" -o "$tmp_c" \
|| die "checksums missing for ${version} (channel ${channel})"
line="$(checksums_target "$tmp_c" "$target")"
[ -n "$line" ] || die "no build for ${target} at ${version}"
dl_sha="$(printf '%s' "$line" | cut -f1)"
dl_url="${vbase}/$(printf '%s' "$line" | cut -f2)"
}
# Resolve the channel pointer (latest) into download url + sha. Sets the same.
resolve_pointer() {
murl="${base}/${prefix}/${channel}/manifest.json"
_code="$(curl -fsS -o "$tmp_m" -w '%{http_code}' "$murl" 2>/dev/null || true)"
if [ ! -s "$tmp_m" ]; then
case "$_code" in
404) die "channel '${channel}' not published to R2. For rc/stable use the GitHub installer (install-cli.sh)." ;;
*) die "R2 unavailable (HTTP ${_code:-?}) fetching ${murl}; retry." ;;
esac
fi
mchannel="$(manifest_str "$tmp_m" channel)"
[ "$mchannel" = "$channel" ] || die "manifest channel '${mchannel}' != requested '${channel}'"
version="$(manifest_str "$tmp_m" version)"
baseUrl="$(manifest_str "$tmp_m" baseUrl)"
asset="$(manifest_target_field "$tmp_m" "$target" asset)"
dl_sha="$(manifest_target_field "$tmp_m" "$target" sha256)"
[ -n "$asset" ] && [ -n "$dl_sha" ] || die "no build for ${target} in channel ${channel}"
dl_url="${baseUrl}/${asset}"
}
install_main() {
need curl
[ -n "${DIFYCTL_R2_BASE:-}" ] || die "set DIFYCTL_R2_BASE to the R2 public base (e.g. https://pub-….r2.dev)"
base="${DIFYCTL_R2_BASE%/}"
channel="${DIFYCTL_CHANNEL:-edge}"
prefix="${DIFYCTL_R2_PREFIX:-difyctl}"
bin_prefix="${DIFYCTL_R2_BIN_PREFIX:-${prefix}/bin}"
install_dir="$(bin_dir_for)"
target="$(detect_target)"
if [ -n "${DIFYCTL_VERSION:-}" ] || [ -n "${DIFYCTL_COMMIT:-}" ]; then
resolve_pinned
else
resolve_pointer
fi
fetch_verify_install "$dl_url" "$dl_sha" "$version" "$channel"
}
if [ "${DIFYCTL_INSTALL_LIB:-0}" != "1" ]; then
install_main
fi
@@ -0,0 +1,222 @@
import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vite-plus/test'
const SCRIPT = fileURLToPath(new URL('./install-r2.sh', import.meta.url))
const MANIFEST = [
'{',
' "schema": 1,',
' "name": "difyctl",',
' "channel": "edge",',
' "version": "0.1.0-edge.2fd7b82",',
' "commit": "abc1234",',
' "buildDate": "2026-06-14T12:00:00Z",',
' "baseUrl": "https://pub.example.r2.dev/difyctl/edge/0.1.0-edge.2fd7b82",',
' "targets": {',
' "linux-x64": { "asset": "difyctl-v0.1.0-edge.2fd7b82-linux-x64", "sha256": "deadbeef" },',
' "darwin-arm64": { "asset": "difyctl-v0.1.0-edge.2fd7b82-darwin-arm64", "sha256": "cafef00d" }',
' }',
'}',
].join('\n')
const INDEX = [
'{',
' "schema": 1,',
' "channel": "edge",',
' "updated": "2026-06-15T00:00:00Z",',
' "builds": [',
' {',
' "version": "0.1.0-edge.ce4af868",',
' "commit": "ce4af868d653f405070fabb3be3303430cc030ad",',
' "buildDate": "2026-06-15T00:00:00Z",',
' "dir": "0.1.0-edge.ce4af868"',
' },',
' {',
' "version": "0.1.0-edge.aaaa111",',
' "commit": "aaaa111bbbbcccc000011112222333344445555",',
' "buildDate": "2026-06-14T00:00:00Z",',
' "dir": "0.1.0-edge.aaaa111"',
' }',
' ]',
'}',
].join('\n')
const CHECKSUMS = [
'deadbeef difyctl-v0.1.0-edge.ce4af868-linux-x64',
'cafef00d difyctl-v0.1.0-edge.ce4af868-darwin-arm64',
'beadc0de difyctl-v0.1.0-edge.ce4af868-windows-x64.exe',
].join('\n')
function lib(
program: string,
env: Record<string, string> = {},
): { code: number; stdout: string; stderr: string } {
const full = `. "${SCRIPT}"\n${program}`
const r = spawnSync('sh', ['-c', full], {
encoding: 'utf8',
env: { ...process.env, DIFYCTL_INSTALL_LIB: '1', ...env },
})
return { code: r.status ?? 1, stdout: (r.stdout ?? '').trim(), stderr: r.stderr ?? '' }
}
describe('install-r2 manifest parsing', () => {
// install-r2.sh is POSIX-only; under git-bash on Windows `uname -s` is MINGW*,
// so detect_target intentionally dies (Windows installs go through install-r2.ps1).
it.skipIf(process.platform === 'win32')('detect_target maps to one of the 5 ids', () => {
const { stdout } = lib('detect_target')
expect(['linux-x64', 'linux-arm64', 'darwin-x64', 'darwin-arm64', 'windows-x64']).toContain(
stdout,
)
})
it('manifest_str reads a top-level string field', () => {
const { stdout } = lib(
`printf '%s' '${MANIFEST}' > "$tmp_m"; manifest_str "$tmp_m" channel`,
{},
)
expect(stdout).toBe('edge')
})
it('manifest_target_field extracts per-target values from a single line', () => {
const prog =
`printf '%s' '${MANIFEST}' > "$tmp_m"\n` +
'manifest_target_field "$tmp_m" darwin-arm64 asset\n' +
'manifest_target_field "$tmp_m" darwin-arm64 sha256'
const { stdout } = lib(prog)
expect(stdout).toBe('difyctl-v0.1.0-edge.2fd7b82-darwin-arm64\ncafef00d')
})
it('requires DIFYCTL_R2_BASE when run as the installer (not lib)', () => {
const r = spawnSync('sh', [SCRIPT], {
encoding: 'utf8',
env: { ...process.env, DIFYCTL_R2_BASE: '' },
})
expect(r.status).not.toBe(0)
expect(r.stderr).toMatch(/DIFYCTL_R2_BASE/)
})
it('sha256_check aborts on a checksum mismatch', () => {
const r = lib('f="$(mktemp)"; printf \'hello\' > "$f"; sha256_check "$f" deadbeef')
expect(r.code).not.toBe(0)
expect(r.stderr).toMatch(/checksum mismatch/)
})
it('sha256_check passes on the correct hash', () => {
// sha256('hello') = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
const r = lib(
'f="$(mktemp)"; printf \'hello\' > "$f"; sha256_check "$f" 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 && echo OK',
)
expect(r.stdout).toBe('OK')
})
})
describe('install-r2 version/commit pin', () => {
it('index_resolve matches a build by exact version', () => {
const r = lib(
`printf '%s' '${INDEX}' > "$tmp_m"; index_resolve "$tmp_m" version 0.1.0-edge.aaaa111`,
)
expect(r.stdout).toBe('0.1.0-edge.aaaa111\t0.1.0-edge.aaaa111')
})
it('index_resolve matches a build by commit prefix', () => {
const r = lib(`printf '%s' '${INDEX}' > "$tmp_m"; index_resolve "$tmp_m" commit ce4af868`)
expect(r.stdout).toBe('0.1.0-edge.ce4af868\t0.1.0-edge.ce4af868')
})
it('index_resolve matches the full 40-char commit too', () => {
const r = lib(
`printf '%s' '${INDEX}' > "$tmp_m"; index_resolve "$tmp_m" commit aaaa111bbbbcccc000011112222333344445555`,
)
expect(r.stdout).toBe('0.1.0-edge.aaaa111\t0.1.0-edge.aaaa111')
})
it('index_resolve prints nothing when no build matches', () => {
const r = lib(`printf '%s' '${INDEX}' > "$tmp_m"; index_resolve "$tmp_m" commit ffffff`)
expect(r.stdout).toBe('')
})
it('checksums_target extracts sha and asset for a posix target', () => {
const r = lib(`printf '%s' '${CHECKSUMS}' > "$tmp_m"; checksums_target "$tmp_m" darwin-arm64`)
expect(r.stdout).toBe('cafef00d\tdifyctl-v0.1.0-edge.ce4af868-darwin-arm64')
})
it('checksums_target does not bleed x64 into arm64', () => {
const r = lib(`printf '%s' '${CHECKSUMS}' > "$tmp_m"; checksums_target "$tmp_m" linux-x64`)
expect(r.stdout).toBe('deadbeef\tdifyctl-v0.1.0-edge.ce4af868-linux-x64')
})
})
describe('install-r2.sh install location', () => {
const where = (env: Record<string, string>) =>
lib('bin_dir_for', { DIFYCTL_PREFIX: '', DIFYCTL_INSTALL_DIR: '', HOME: '/home/u', ...env })
.stdout
it('defaults to ~/.local/bin', () => {
expect(where({})).toBe('/home/u/.local/bin')
})
it('accepts DIFYCTL_PREFIX like the GitHub installer', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify' })).toBe('/opt/dify/bin')
})
it('keeps DIFYCTL_INSTALL_DIR as the bin folder itself, over DIFYCTL_PREFIX', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify', DIFYCTL_INSTALL_DIR: '/usr/local/bin' })).toBe(
'/usr/local/bin',
)
})
})
it.skipIf(process.platform === 'win32')(
'install-r2.sh points Windows users at install-r2.ps1',
() => {
const r = lib('uname() { printf MINGW64_NT; }; detect_target')
expect(r.code).not.toBe(0)
expect(r.stderr).toMatch(/use install-r2\.ps1 on Windows/)
},
)
describe.skipIf(process.platform === 'win32')('install-r2.sh install_binary', () => {
it('installs a 0600 temp file as mode 0755 next to and then over the target', () => {
const r = lib(
[
'd="$(mktemp -d)"',
'src="$(mktemp)"; chmod 0600 "$src"; printf new > "$src"',
'printf old > "$d/difyctl"',
'install_binary "$src" "$d/difyctl"',
'ls -l "$d/difyctl" | cut -c1-10; cat "$d/difyctl"; echo; ls -A "$d"',
'rm -rf "$d" "$src"',
].join('\n'),
)
expect(r.code).toBe(0)
expect(r.stdout.split('\n')).toEqual(['-rwxr-xr-x', 'new', 'difyctl'])
})
})
// A v2 binary answers `install skills --help`; a v1 one exits 1 with "unknown command".
describe.skipIf(process.platform === 'win32')('install-r2.sh print_skill_hint', () => {
const fakeBins = [
'd="$(mktemp -d)"',
'printf \'#!/bin/sh\\n[ "$*" = "install skills --help" ]\\n\' > "$d/v2"',
'printf \'#!/bin/sh\\necho "unknown command: $*" >&2\\nexit 1\\n\' > "$d/v1"',
'chmod 0755 "$d/v2" "$d/v1"',
]
it('prints the hint for a binary that has install skills', () => {
const r = lib([...fakeBins, 'print_skill_hint "$d/v2"', 'rm -rf "$d"'].join('\n'))
expect(r.code).toBe(0)
expect(r.stdout).toMatch(/difyctl install skills <skills root>/)
})
it('prints nothing for a binary without install skills', () => {
const r = lib([...fakeBins, 'print_skill_hint "$d/v1"', 'rm -rf "$d"'].join('\n'))
expect(r.code).toBe(0)
expect(r.stdout).toBe('')
})
it('prints nothing when the binary cannot run', () => {
const r = lib('print_skill_hint /nonexistent/difyctl')
expect(r.code).toBe(0)
expect(r.stdout).toBe('')
})
})
+198
View File
@@ -0,0 +1,198 @@
# install.ps1 — one-line difyctl installer for Windows. difyctl ships as assets
# on Dify GitHub Releases; this installs the build matching your Dify version.
#
# usage:
# irm https://raw.githubusercontent.com/langgenius/dify/main/cli/scripts/install.ps1 | iex
#
# env:
# DIFY_VERSION Dify release tag to install difyctl from (e.g. 1.14.2). Primary key.
# DIFYCTL_VERSION difyctl version pin (used only when DIFY_VERSION is unset).
# DIFYCTL_PREFIX install prefix (default $env:LOCALAPPDATA\difyctl); binary -> <prefix>\bin\difyctl.exe
# DIFYCTL_INSTALL_DIR the bin folder itself (wins over DIFYCTL_PREFIX); binary -> <dir>\difyctl.exe
# DIFYCTL_REPO release source repo (default langgenius/dify)
# GITHUB_TOKEN GitHub token (or GH_TOKEN) sent as a bearer to raise the API
# rate limit from 60 to 5000 requests/hour (useful in CI).
$ErrorActionPreference = 'Stop'
$repo = if ($env:DIFYCTL_REPO) { $env:DIFYCTL_REPO } else { 'langgenius/dify' }
$difyVersion = $env:DIFY_VERSION
$difyctlVersion = $env:DIFYCTL_VERSION
$target = 'windows-x64'
$apiBase = "https://api.github.com/repos/$repo"
$dlBase = "https://github.com/$repo/releases/download"
$headers = @{ Accept = 'application/vnd.github+json' }
$ghToken = if ($env:GITHUB_TOKEN) { $env:GITHUB_TOKEN } elseif ($env:GH_TOKEN) { $env:GH_TOKEN } else { $null }
if ($ghToken) { $headers.Authorization = "Bearer $ghToken" }
# Read a response header, coping with both PS7 (TryGetValues) and PS5.1 (indexer).
function Get-HeaderValue($Response, [string]$Name) {
if (-not $Response -or -not $Response.Headers) { return $null }
try {
$vals = $null
if ($Response.Headers.TryGetValues($Name, [ref]$vals)) { return ($vals | Select-Object -First 1) }
return $null
}
catch {
try { return $Response.Headers[$Name] } catch { return $null }
}
}
# $null unless the error is a GitHub rate limit; else an object with the reset epoch.
function Get-RateLimitInfo($ErrorRecord) {
$resp = $null
try { $resp = $ErrorRecord.Exception.Response } catch { return $null }
if (-not $resp) { return $null }
$status = 0; try { $status = [int]$resp.StatusCode } catch {}
if ($status -ne 403 -and $status -ne 429) { return $null }
$remaining = Get-HeaderValue $resp 'x-ratelimit-remaining'
if ($status -eq 429 -or $remaining -eq '0') {
return [pscustomobject]@{ Reset = (Get-HeaderValue $resp 'x-ratelimit-reset') }
}
return $null
}
function Write-RateLimitHint($ResetEpoch) {
[Console]::Error.WriteLine('GitHub API rate limit exceeded (unauthenticated requests are capped at 60/hour per IP).')
if ("$ResetEpoch" -match '^\d+$') {
$mins = [int][math]::Ceiling(([long]$ResetEpoch - [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()) / 60)
if ($mins -gt 0) { [Console]::Error.WriteLine("The limit resets in ~$mins min.") }
}
[Console]::Error.WriteLine('To proceed now, authenticate to raise the limit to 5000/hour:')
[Console]::Error.WriteLine(' $env:GITHUB_TOKEN = "<token>"; irm <install-url> | iex')
[Console]::Error.WriteLine('Tip: pinning DIFY_VERSION makes a single API call (DIFYCTL_VERSION scans many).')
}
# Print the hint and exit if rate-limited; else return so the caller can throw.
function Stop-IfRateLimited($ErrorRecord) {
$info = Get-RateLimitInfo $ErrorRecord
if (-not $info) { return }
Write-RateLimitHint $info.Reset
exit 1
}
function Get-AssetSemver([string]$Name) {
if ($Name -notmatch '^difyctl-v(.+?)-windows-x64\.exe$') { return $null }
$v = $Matches[1]
$core = (($v -split '\+')[0] -split '-')[0]
if ($core -notmatch '^\d+\.\d+\.\d+$') { return $null }
$rc = if ($v -match '-rc\.(\d+)') { [int]$Matches[1] } else { [int]::MaxValue }
return [pscustomobject]@{ Name = $Name; Version = $v; Core = [version]$core; Rc = $rc }
}
function Select-Asset([object]$Release) {
$Release.assets |
ForEach-Object { Get-AssetSemver $_.name } |
Where-Object { $_ } |
Sort-Object Core, Rc |
Select-Object -Last 1
}
function Find-ReleaseForDifyctl([string]$Want) {
try { $releases = Invoke-RestMethod -Uri "$apiBase/releases?per_page=100" -Headers $headers }
catch { Stop-IfRateLimited $_; throw }
foreach ($rel in $releases) {
$asset = Select-Asset $rel
if ($asset -and $asset.Version -eq $Want) { return $rel }
}
return $null
}
function Resolve-Release {
if ($difyVersion) {
try { return Invoke-RestMethod -Uri "$apiBase/releases/tags/$difyVersion" -Headers $headers }
catch { Stop-IfRateLimited $_; throw "Dify release $difyVersion not found: $_" }
}
elseif ($difyctlVersion) {
$release = Find-ReleaseForDifyctl $difyctlVersion
if (-not $release) { throw "difyctl $difyctlVersion not found on any Dify release" }
return $release
}
else {
try { return Invoke-RestMethod -Uri "$apiBase/releases/latest" -Headers $headers }
catch { Stop-IfRateLimited $_; throw "failed to query latest Dify release (set DIFY_VERSION to pin one): $_" }
}
}
# DIFYCTL_INSTALL_DIR names the bin folder itself; DIFYCTL_PREFIX the folder above it.
function Resolve-BinDir {
if ($env:DIFYCTL_INSTALL_DIR) { return $env:DIFYCTL_INSTALL_DIR }
$prefix = if ($env:DIFYCTL_PREFIX) { $env:DIFYCTL_PREFIX } else { Join-Path $env:LOCALAPPDATA 'difyctl' }
return Join-Path $prefix 'bin'
}
# The user PATH alone: $env:PATH also holds the machine PATH, which must not be copied into it.
function Get-PathHint([string]$Dir) {
return " [Environment]::SetEnvironmentVariable('PATH', `"$Dir;`" + [Environment]::GetEnvironmentVariable('PATH', 'User'), 'User')"
}
# The hint names a v2 command, so a binary without it (any v1 build) gets none. Under
# Windows PowerShell 5.1 a redirected stderr line throws when errors stop, hence the catch.
function Write-SkillHint([string]$Bin) {
if (-not (Test-Path -LiteralPath $Bin -PathType Leaf)) { return }
try { & $Bin install skills --help *> $null } catch { return }
if ($LASTEXITCODE -ne 0) { return }
Write-Host 'AI agents: run `difyctl install skills <skills root>` after each install or upgrade; the skill ships inside the binary.'
}
function Invoke-Main {
$release = Resolve-Release
$difyTag = $release.tag_name
$asset = Select-Asset $release
if (-not $asset) { throw "no difyctl published for Dify $difyTag (target $target); set DIFY_VERSION to a release that has one" }
$assetName = $asset.Name
$ver = $asset.Version
$checksums = "difyctl-v$ver-checksums.txt"
$base = "$dlBase/$difyTag"
$tmp = Join-Path $env:TEMP ("difyctl-" + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $tmp -Force | Out-Null
try {
Write-Host "downloading $assetName (Dify $difyTag)..."
$assetPath = Join-Path $tmp $assetName
$sumsPath = Join-Path $tmp $checksums
Invoke-WebRequest -Uri "$base/$assetName" -OutFile $assetPath
Invoke-WebRequest -Uri "$base/$checksums" -OutFile $sumsPath
$expected = (Get-Content $sumsPath |
Where-Object { $_ -match '\s' + [regex]::Escape($assetName) + '$' } |
ForEach-Object { ($_ -split '\s+')[0] } |
Select-Object -First 1)
if (-not $expected) { throw "no checksum entry for $assetName" }
$actual = (Get-FileHash -Path $assetPath -Algorithm SHA256).Hash.ToLower()
if ($actual -ne $expected.ToLower()) { throw "checksum mismatch for $assetName" }
$binDir = Resolve-BinDir
New-Item -ItemType Directory -Path $binDir -Force | Out-Null
$targetBin = Join-Path $binDir 'difyctl.exe'
# Stage next to the target, then rename over it, so difyctl.exe is never half written.
$stage = Join-Path $binDir ('.difyctl.install.' + [guid]::NewGuid().ToString('N') + '.exe')
try {
Copy-Item -Path $assetPath -Destination $stage -Force
Move-Item -Path $stage -Destination $targetBin -Force
}
catch {
Remove-Item -Path $stage -Force -ErrorAction SilentlyContinue
throw "cannot replace $targetBin — close any running difyctl and re-run."
}
Write-Host ""
Write-Host "difyctl v$ver installed (from Dify $difyTag): $targetBin"
if (($env:PATH -split ';') -notcontains $binDir) {
Write-Host ""
Write-Host "$binDir is not on your PATH. Add it with:"
Write-Host (Get-PathHint $binDir)
}
else {
Write-Host 'verify: run "difyctl version"'
Write-SkillHint $targetBin
}
}
finally {
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
}
}
if ($env:DIFYCTL_INSTALL_LIB -ne '1') { Invoke-Main }
@@ -0,0 +1,363 @@
import { spawnSync } from 'node:child_process'
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it, vi } from 'vite-plus/test'
const SCRIPT = fileURLToPath(new URL('./install.ps1', import.meta.url))
function hasPwsh(): boolean {
const r = spawnSync(
'pwsh',
['-NoProfile', '-NonInteractive', '-Command', '$PSVersionTable.PSVersion.Major'],
{
encoding: 'utf8',
},
)
return r.status === 0
}
const PWSH = hasPwsh()
// Each test starts pwsh; cold starts on a busy CI runner can exceed the default 5 seconds.
if (PWSH) vi.setConfig({ testTimeout: 15_000 })
const STUB = [
'function Invoke-RestMethod {',
' param([string]$Uri, $Headers)',
" if ($Uri -like '*/releases/latest') {",
" if (-not $env:HX_LATEST) { throw 'mock 404' }",
' return ($env:HX_LATEST | ConvertFrom-Json)',
' }',
" elseif ($Uri -like '*/releases?per_page=100') {",
" if (-not $env:HX_LIST) { throw 'mock 404' }",
' return ($env:HX_LIST | ConvertFrom-Json)',
' }',
" elseif ($Uri -like '*/releases/tags/*') {",
" $t = $Uri -replace '.*/releases/tags/', ''",
" $k = 'HX_TAG_' + ($t -replace '[.\\-]', '_')",
' $v = [Environment]::GetEnvironmentVariable($k)',
" if (-not $v) { throw 'mock 404' }",
' return ($v | ConvertFrom-Json)',
' }',
' throw "unexpected uri $Uri"',
'}',
].join('\n')
type Run = { code: number; stdout: string; stderr: string }
function runPwsh(body: string, env: Record<string, string> = {}): Run {
const script = `$ErrorActionPreference='Stop'\n${STUB}\n. '${SCRIPT}'\n${body}`
const r = spawnSync('pwsh', ['-NoProfile', '-NonInteractive', '-Command', script], {
encoding: 'utf8',
env: {
...process.env,
DIFYCTL_INSTALL_LIB: '1',
DIFY_VERSION: '',
DIFYCTL_VERSION: '',
LOCALAPPDATA: process.env.LOCALAPPDATA || '/tmp',
TEMP: process.env.TEMP || '/tmp',
...env,
},
})
return { code: r.status ?? 1, stdout: (r.stdout ?? '').trim(), stderr: r.stderr ?? '' }
}
const REL_1142 = JSON.stringify({
tag_name: '1.14.2',
assets: [{ name: 'difyctl-v0.2.0-windows-x64.exe' }],
})
const REL_1150 = JSON.stringify({
tag_name: '1.15.0',
assets: [{ name: 'difyctl-v0.3.0-windows-x64.exe' }],
})
const LIST_NEWEST_FIRST = JSON.stringify([
{ tag_name: '1.15.0', assets: [{ name: 'difyctl-v0.3.0-windows-x64.exe' }] },
{ tag_name: '1.14.2', assets: [{ name: 'difyctl-v0.2.0-windows-x64.exe' }] },
])
describe.skipIf(!PWSH)('install.ps1 Get-AssetSemver', () => {
it('extracts the version from a windows .exe asset name', () => {
const r = runPwsh("(Get-AssetSemver 'difyctl-v0.2.0-windows-x64.exe').Version")
expect(r.code).toBe(0)
expect(r.stdout).toBe('0.2.0')
})
it('extracts a prerelease version and its rc number', () => {
const r = runPwsh(
'$a = Get-AssetSemver \'difyctl-v0.1.0-rc.1-windows-x64.exe\'; "$($a.Version) $($a.Rc)"',
)
expect(r.code).toBe(0)
expect(r.stdout).toBe('0.1.0-rc.1 1')
})
it('rejects a non-windows asset (returns null)', () => {
const r = runPwsh(
"if ($null -eq (Get-AssetSemver 'difyctl-v0.2.0-linux-x64')) { 'NULL' } else { 'OBJ' }",
)
expect(r.code).toBe(0)
expect(r.stdout).toBe('NULL')
})
it('rejects a malformed core version (returns null)', () => {
const r = runPwsh(
"if ($null -eq (Get-AssetSemver 'difyctl-vx.y.z-windows-x64.exe')) { 'NULL' } else { 'OBJ' }",
)
expect(r.code).toBe(0)
expect(r.stdout).toBe('NULL')
})
})
describe.skipIf(!PWSH)('install.ps1 Select-Asset', () => {
it('picks the highest semver among several windows builds', () => {
const rel = JSON.stringify({
assets: [
{ name: 'difyctl-v0.2.0-windows-x64.exe' },
{ name: 'difyctl-v0.10.0-windows-x64.exe' },
{ name: 'difyctl-v0.9.0-windows-x64.exe' },
],
})
const r = runPwsh(`(Select-Asset ('${rel}' | ConvertFrom-Json)).Version`)
expect(r.code).toBe(0)
expect(r.stdout).toBe('0.10.0')
})
it('prefers the stable release over an rc of the same core', () => {
const rel = JSON.stringify({
assets: [
{ name: 'difyctl-v0.2.0-rc.1-windows-x64.exe' },
{ name: 'difyctl-v0.2.0-windows-x64.exe' },
],
})
const r = runPwsh(`(Select-Asset ('${rel}' | ConvertFrom-Json)).Version`)
expect(r.code).toBe(0)
expect(r.stdout).toBe('0.2.0')
})
it('ignores checksums and non-windows assets', () => {
const rel = JSON.stringify({
assets: [
{ name: 'difyctl-v0.2.0-linux-x64' },
{ name: 'difyctl-v0.2.0-checksums.txt' },
{ name: 'difyctl-v0.2.0-windows-x64.exe' },
{ name: 'some-other-asset.zip' },
],
})
const r = runPwsh(`(Select-Asset ('${rel}' | ConvertFrom-Json)).Name`)
expect(r.code).toBe(0)
expect(r.stdout).toBe('difyctl-v0.2.0-windows-x64.exe')
})
it('yields null when no windows asset is present', () => {
const rel = JSON.stringify({ assets: [{ name: 'difyctl-v0.2.0-linux-x64' }] })
const r = runPwsh(
`if ($null -eq (Select-Asset ('${rel}' | ConvertFrom-Json))) { 'NULL' } else { 'OBJ' }`,
)
expect(r.code).toBe(0)
expect(r.stdout).toBe('NULL')
})
})
describe.skipIf(!PWSH)('install.ps1 Resolve-Release', () => {
it('DIFY_VERSION pins the release directly', () => {
const r = runPwsh('(Resolve-Release).tag_name', {
DIFY_VERSION: '1.14.2',
HX_TAG_1_14_2: REL_1142,
})
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.14.2')
})
it('DIFY_VERSION that does not exist throws a clear message', () => {
const r = runPwsh('(Resolve-Release).tag_name', { DIFY_VERSION: '9.9.9' })
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('Dify release 9.9.9 not found')
})
it('blank resolves to the latest release', () => {
const r = runPwsh('(Resolve-Release).tag_name', { HX_LATEST: REL_1150 })
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.15.0')
})
it('blank throws when the latest query fails (no silent fallback)', () => {
const r = runPwsh('(Resolve-Release).tag_name')
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('failed to query latest Dify release')
})
it('DIFYCTL_VERSION resolves to the release hosting that build', () => {
const r = runPwsh('(Resolve-Release).tag_name', {
DIFYCTL_VERSION: '0.2.0',
HX_LIST: LIST_NEWEST_FIRST,
})
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.14.2')
})
it('DIFYCTL_VERSION not hosted anywhere throws', () => {
const r = runPwsh('(Resolve-Release).tag_name', {
DIFYCTL_VERSION: '9.9.9',
HX_LIST: LIST_NEWEST_FIRST,
})
expect(r.code).not.toBe(0)
expect(r.stderr).toContain('difyctl 9.9.9 not found on any Dify release')
})
})
describe.skipIf(!PWSH)('install.ps1 Find-ReleaseForDifyctl', () => {
it('returns the newest release whose assets host the wanted build', () => {
const r = runPwsh("(Find-ReleaseForDifyctl '0.2.0').tag_name", { HX_LIST: LIST_NEWEST_FIRST })
expect(r.code).toBe(0)
expect(r.stdout).toBe('1.14.2')
})
it('returns nothing when no release hosts the wanted build', () => {
const r = runPwsh(
"$x = Find-ReleaseForDifyctl '9.9.9'; if ($null -eq $x) { 'NULL' } else { $x.tag_name }",
{ HX_LIST: LIST_NEWEST_FIRST },
)
expect(r.code).toBe(0)
expect(r.stdout).toBe('NULL')
})
})
// Build a fake ErrorRecord whose Exception.Response is a real HttpResponseMessage
// carrying the given status + headers, matching what Get-RateLimitInfo inspects.
function fakeErr(status: number, headers: Record<string, string>): string {
const adds = Object.entries(headers)
.map(([k, v]) => `$resp.Headers.TryAddWithoutValidation('${k}','${v}') | Out-Null`)
.join('\n')
return [
`$resp = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]${status})`,
adds,
'$err = [pscustomobject]@{ Exception = [pscustomobject]@{ Response = $resp } }',
].join('\n')
}
const futureReset = String(Math.floor(Date.now() / 1000) + 1800)
describe.skipIf(!PWSH)('install.ps1 rate limit', () => {
it('classifies a 403 with x-ratelimit-remaining:0 as rate-limited, returning the reset', () => {
const r =
runPwsh(`${fakeErr(403, { 'x-ratelimit-remaining': '0', 'x-ratelimit-reset': futureReset })}
$i = Get-RateLimitInfo $err; if ($null -eq $i) { 'NULL' } else { $i.Reset }`)
expect(r.code).toBe(0)
expect(r.stdout).toBe(futureReset)
})
it('does not classify a 403 with remaining tokens as rate-limited', () => {
const r = runPwsh(`${fakeErr(403, { 'x-ratelimit-remaining': '59' })}
$i = Get-RateLimitInfo $err; if ($null -eq $i) { 'NULL' } else { 'LIMITED' }`)
expect(r.code).toBe(0)
expect(r.stdout).toBe('NULL')
})
it('always treats a 429 as rate-limited', () => {
const r = runPwsh(`${fakeErr(429, {})}
$i = Get-RateLimitInfo $err; if ($null -eq $i) { 'NULL' } else { 'LIMITED' }`)
expect(r.code).toBe(0)
expect(r.stdout).toBe('LIMITED')
})
it('returns null for an error without a response (e.g. a plain string throw)', () => {
const r = runPwsh(
"$err = [pscustomobject]@{ Exception = [pscustomobject]@{} }; if ($null -eq (Get-RateLimitInfo $err)) { 'NULL' } else { 'OBJ' }",
)
expect(r.code).toBe(0)
expect(r.stdout).toBe('NULL')
})
it('Write-RateLimitHint prints cause, ETA, and remediation to stderr', () => {
const r = runPwsh(`Write-RateLimitHint '${futureReset}'`)
expect(r.code).toBe(0)
expect(r.stderr).toContain('rate limit exceeded')
expect(r.stderr).toContain('resets in ~')
expect(r.stderr).toContain('GITHUB_TOKEN')
})
it('Write-RateLimitHint omits the ETA line when the reset epoch is missing', () => {
const r = runPwsh("Write-RateLimitHint ''")
expect(r.code).toBe(0)
expect(r.stderr).toContain('rate limit exceeded')
expect(r.stderr).not.toContain('resets in ~')
})
it('sends an Authorization header when GITHUB_TOKEN is set', () => {
const r = runPwsh('$headers.Authorization', { GITHUB_TOKEN: 'ghp_secret123' })
expect(r.code).toBe(0)
expect(r.stdout).toBe('Bearer ghp_secret123')
})
it('Resolve-Release surfaces the rate-limit hint and exits, not "not found"', () => {
const stub = `function Invoke-RestMethod { throw [Microsoft.PowerShell.Commands.HttpResponseException]::new('rate limited', $script:rlResp) }`
// The stub bypasses Invoke-RestMethod's module autoload, which provides HttpResponseException.
const setup = `Import-Module Microsoft.PowerShell.Utility
$script:rlResp = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]403)
$script:rlResp.Headers.TryAddWithoutValidation('x-ratelimit-remaining','0') | Out-Null
$script:rlResp.Headers.TryAddWithoutValidation('x-ratelimit-reset','${futureReset}') | Out-Null`
const r = runPwsh(`${setup}\n${stub}\nResolve-Release`, { DIFY_VERSION: '1.15.0' })
expect(r.code).toBe(1)
expect(r.stderr).toContain('rate limit exceeded')
expect(r.stderr).not.toContain('not found')
})
})
describe.skipIf(!PWSH)('install.ps1 install location and PATH hint', () => {
const where = (env: Record<string, string>) =>
runPwsh('Write-Output (Resolve-BinDir)', {
DIFYCTL_PREFIX: '',
DIFYCTL_INSTALL_DIR: '',
LOCALAPPDATA: '/lad',
...env,
}).stdout
it('defaults to %LOCALAPPDATA%\\difyctl\\bin', () => {
expect(where({})).toBe(join('/lad', 'difyctl', 'bin'))
})
it('puts the binary under <DIFYCTL_PREFIX>\\bin', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify' })).toBe(join('/opt/dify', 'bin'))
})
it('takes DIFYCTL_INSTALL_DIR as the bin folder itself, over DIFYCTL_PREFIX', () => {
expect(where({ DIFYCTL_PREFIX: '/opt/dify', DIFYCTL_INSTALL_DIR: '/custom/bin' })).toBe(
'/custom/bin',
)
})
it('the PATH command adds to the user PATH, not the whole session PATH', () => {
const hint = runPwsh(`Write-Output (Get-PathHint 'C:\\dify\\bin')`).stdout
expect(hint).toContain(
`"C:\\dify\\bin;" + [Environment]::GetEnvironmentVariable('PATH', 'User')`,
)
expect(hint).not.toContain('$env:PATH')
})
})
// A v2 binary answers `install skills --help`; a v1 one exits 1 with "unknown command".
// The fakes are shell scripts, so this runs where pwsh runs outside Windows.
describe.skipIf(!PWSH || process.platform === 'win32')('install.ps1 Write-SkillHint', () => {
const hint = (bin: string) => runPwsh(`Write-SkillHint '${bin}'`)
it('prints the hint for a binary that has install skills, and stays quiet otherwise', () => {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-hint-'))
try {
const v2 = join(dir, 'v2')
const v1 = join(dir, 'v1')
writeFileSync(v2, '#!/bin/sh\n[ "$*" = "install skills --help" ]\n', { mode: 0o755 })
writeFileSync(v1, '#!/bin/sh\necho "unknown command: $*" >&2\nexit 1\n', { mode: 0o755 })
expect(hint(v2)).toMatchObject({
code: 0,
stdout: expect.stringContaining('difyctl install skills <skills root>'),
})
expect(hint(v1)).toMatchObject({ code: 0, stdout: '' })
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
it('prints nothing when the binary cannot run', () => {
expect(hint('/nonexistent/difyctl')).toMatchObject({ code: 0, stdout: '' })
})
})
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# scripts/lib/common.sh — shared shell helpers for cli/ scripts.
[[ -n "${DIFYCTL_LIB_COMMON_SH:-}" ]] && return 0
readonly DIFYCTL_LIB_COMMON_SH=1
log::info() { printf '\033[36m[info]\033[0m %s\n' "$*" >&2; }
log::warn() { printf '\033[33m[warn]\033[0m %s\n' "$*" >&2; }
log::err() { printf '\033[31m[err ]\033[0m %s\n' "$*" >&2; }
die() { log::err "$*"; exit 1; }
# Resolve the cli/ directory (parent of scripts/).
cli::root() {
local dir
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
printf '%s' "$dir"
}
require() {
command -v "$1" >/dev/null 2>&1 || die "missing dependency: $1${2:+ — $2}"
}
@@ -0,0 +1,77 @@
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 }
}
@@ -0,0 +1,9 @@
import { resolveBuildInfo } from './lib/resolve-buildinfo.js'
const info = resolveBuildInfo()
process.stdout.write(
`version: ${info.version}\n` +
`commit: ${info.commit}\n` +
`built: ${info.buildDate}\n` +
`channel: ${info.channel}\n`,
)
+80
View File
@@ -0,0 +1,80 @@
#!/usr/bin/env bash
# scripts/release-build.sh — cross-compile difyctl to standalone binaries
# for every release target via `bun build --compile`.
#
# Bun consumes bin/run.ts (which imports from src/) directly — no `pnpm build`
# / dist/ step needed.
#
# Prereqs:
# - All @napi-rs/keyring native variants present in node_modules. Append
# cli/scripts/cross-arch.pnpm.yaml onto pnpm-workspace.yaml before the
# install that first populates node_modules so pnpm fetches every
# foreign-arch prebuild (the CLI Release workflow does this).
#
# Env (all optional; defaults derived from cli/package.json + git):
# CLI_VERSION — package.json `version`
# DIFYCTL_CHANNEL — package.json `difyctl.channel`
# DIFYCTL_COMMIT — `git rev-parse HEAD` (or "unknown")
# DIFYCTL_BUILD_DATE — current UTC time
#
# Output: dist/bin/difyctl-v<CLI_VERSION>-<target>[.exe]
set -euo pipefail
_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/common.sh
source "${_dir}/lib/common.sh"
require bun
cli_root="$(cli::root)"
entry="${cli_root}/bin/run.ts"
out_dir="${cli_root}/dist/bin"
read_pkg() { node -p "require('${cli_root}/package.json').$1" 2>/dev/null; }
naming() { node "${_dir}/release-naming.mjs" "$@"; }
CLI_VERSION="${CLI_VERSION:-$(read_pkg version)}"
DIFYCTL_CHANNEL="${DIFYCTL_CHANNEL:-$(read_pkg difyctl.channel)}"
DIFYCTL_COMMIT="${DIFYCTL_COMMIT:-$(git -C "$cli_root" rev-parse HEAD 2>/dev/null || echo unknown)}"
DIFYCTL_BUILD_DATE="${DIFYCTL_BUILD_DATE:-$(date -u +%Y-%m-%dT%H:%M:%SZ)}"
[[ "$CLI_VERSION" != "undefined" ]] || die "CLI_VERSION could not be derived from package.json"
repo_root="$(cd "${cli_root}/.." && pwd)"
skill_dir="${repo_root}/skills/difyctl"
[[ -f "${skill_dir}/SKILL.md" ]] || die "skill not found: ${skill_dir}"
[[ -f "$entry" ]] || die "entry not found: $entry"
rm -rf "$out_dir"
mkdir -p "$out_dir"
# Build-info globals (referenced as bare identifiers in src/version/info.ts).
# Each value must be a JS expression — wrap strings as JSON-quoted strings.
defines=(
"--define" "__DIFYCTL_VERSION__=\"${CLI_VERSION}\""
"--define" "__DIFYCTL_CHANNEL__=\"${DIFYCTL_CHANNEL}\""
"--define" "__DIFYCTL_COMMIT__=\"${DIFYCTL_COMMIT}\""
"--define" "__DIFYCTL_BUILD_DATE__=\"${DIFYCTL_BUILD_DATE}\""
"--define" "__DIFYCTL_COMPILED__=true"
)
# Targets and asset names come from cli/package.json `difyctl.release` via
# release-naming.mjs (single source of truth). Each line: bunTarget<TAB>id<TAB>exe.
while IFS=$'\t' read -r bun_target asset_target _exe; do
out="${out_dir}/$(naming asset "$CLI_VERSION" "$asset_target")"
log::info "compiling ${asset_target} -> $(basename "$out")..."
# Run from the repo root: --asset paths are embedded relative to the cwd, which gives
# the skill its difyctl/ prefix.
(cd "$repo_root" && bun build "$entry" \
--asset "$skill_dir" \
--target="$bun_target" \
--compile \
--minify \
"${defines[@]}" \
--outfile="$out") >/dev/null
done < <(naming targets)
log::info "built $(find "$out_dir" -type f | wc -l | tr -d ' ') binaries:"
ls -lh "$out_dir" >&2
@@ -0,0 +1,211 @@
#!/usr/bin/env node
// release-naming.mjs — single source of truth for difyctl release artifact
// names and version/channel rules. Reads DATA from cli/package.json
// `difyctl.release` (plus `version` and `difyctl.channel`) and owns the name
// FORMAT and the per-channel version form. Producer scripts call this;
// `validate` is the release gate.
//
// Subcommands:
// tag <version> -> <tagPrefix><version>
// asset <version> <id> -> <tagPrefix><version>-<id>[.exe]
// checksums <version> -> <tagPrefix><version><checksumsSuffix>
// tag-prefix -> <tagPrefix>
// targets -> one line per target: "<bunTarget>\t<id>\t<0|1 exe>"
// channels -> one channel name per line
// prerelease <channel> -> "true" | "false"
// github-env -> key=value lines (all fields CI needs) for $GITHUB_ENV
// edge-version <sha> -> <package version core>-edge.<sha>
// validate -> exit 1 if difyctl.release, version, or channel is malformed
// validate-version <version> <channel>
// -> exit 1 unless version matches the channel's form
import { readFileSync, realpathSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
const BUN_TARGET_RE = /^bun-(linux|darwin|windows)-(x64|arm64)$/
const SEMVER_CORE_RE = /^\d+\.\d+\.\d+$/
// Add channels here: { name, prerelease, versionForm }.
const CHANNELS = [
{ name: 'stable', prerelease: false, versionForm: /^\d+\.\d+\.\d+(\+[0-9A-Z.-]+)?$/i },
{ name: 'alpha', prerelease: true, versionForm: /^\d+\.\d+\.\d+-alpha(\.\d+)?$/ },
{ name: 'rc', prerelease: true, versionForm: /^\d+\.\d+\.\d+-rc\.\d+$/ },
{ name: 'edge', prerelease: true, versionForm: /^\d+\.\d+\.\d+-edge\.[0-9a-f]{7,40}$/ },
]
const channelByName = (name) => CHANNELS.find((c) => c.name === name)
const channelNames = () => CHANNELS.map((c) => c.name).join(', ')
function versionCore(v) {
return String(v).replace(/^v/, '').replace(/\+.*$/, '').split('-')[0]
}
function edgeVersion(sha) {
if (!/^[0-9a-f]{7,40}$/.test(sha ?? ''))
die('edge-version requires a git short sha (7-40 hex chars)')
const { version } = loadPkg()
const core = versionCore(version)
if (!SEMVER_CORE_RE.test(core)) die(`cannot derive edge base from version: ${version}`)
return `${core}-edge.${sha}`
}
// Returns a problem string if `version` cannot be resolved under `channel`, else
// null. Shared by validateVersionForChannel (die-now) and validateVersionChannel
// (collect for the `validate` gate).
function channelVersionProblem(version, channel) {
if (typeof version !== 'string' || version.length === 0)
return 'version must be a non-empty string'
const ch = channelByName(channel)
if (!ch) return `unknown channel: ${channel} (expected one of: ${channelNames()})`
if (!ch.versionForm.test(version))
return `version ${version} does not match the ${channel} channel form`
return null
}
function validateVersionForChannel(version, channelName) {
const problem = channelVersionProblem(version, channelName)
if (problem) die(problem)
return `valid: ${version} is a ${channelName} version`
}
function die(msg) {
process.stderr.write(`release-naming: ${msg}\n`)
process.exit(1)
}
// Tests point this at a fixture manifest so their assertions stay fixed while
// the real version moves with every release. The name is mirrored in
// test/fixtures/pkg-manifest.ts rather than imported from here, because this
// file's shebang breaks the Windows test runner.
const PKG_PATH_ENV = 'DIFYCTL_PKG_PATH'
function loadPkg() {
const pkgPath = process.env[PKG_PATH_ENV] || new URL('../package.json', import.meta.url)
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'))
if (!pkg.difyctl?.release) die('cli/package.json missing difyctl.release')
return {
version: pkg.version,
channel: pkg.difyctl.channel,
release: pkg.difyctl.release,
}
}
// Emits key=value lines for $GITHUB_ENV.
function githubEnv() {
const { version, channel, release } = loadPkg()
const fields = {
version,
channel,
prerelease: channelByName(channel)?.prerelease ?? false,
tagPrefix: release.tagPrefix,
difyctlTag: `${release.tagPrefix}${version}`,
}
return Object.entries(fields)
.map(([k, v]) => `${k}=${v}`)
.join('\n')
}
function requireVersion(version) {
if (!version) die('version argument is required')
return version
}
function assetName(release, version, id) {
const target = release.targets.find((t) => t.id === id)
if (!target) die(`unknown target id: ${id}`)
const suffix = target.exe ? '.exe' : ''
return `${release.tagPrefix}${version}-${id}${suffix}`
}
function validateRelease(release) {
const problems = []
const str = (v) => typeof v === 'string' && v.length > 0
if (!str(release.tagPrefix)) problems.push('tagPrefix must be a non-empty string')
if (!str(release.binName)) problems.push('binName must be a non-empty string')
if (!str(release.checksumsSuffix)) problems.push('checksumsSuffix must be a non-empty string')
if (!Array.isArray(release.targets) || release.targets.length === 0) {
problems.push('targets must be a non-empty array')
return problems
}
const seen = new Set()
for (const t of release.targets) {
const label = t?.id ?? JSON.stringify(t)
if (!str(t?.id)) problems.push(`target ${label}: id must be a non-empty string`)
else if (seen.has(t.id)) problems.push(`duplicate target id: ${t.id}`)
else seen.add(t.id)
if (!str(t?.bunTarget) || !BUN_TARGET_RE.test(t.bunTarget))
problems.push(`target ${label}: bunTarget must match ${BUN_TARGET_RE}`)
if (typeof t?.exe !== 'boolean') problems.push(`target ${label}: exe must be a boolean`)
else if (str(t?.bunTarget) && t.exe !== t.bunTarget.startsWith('bun-windows-'))
problems.push(`target ${label}: exe must be true iff bunTarget is bun-windows-*`)
}
return problems
}
function validateVersionChannel(version, channel) {
const problem = channelVersionProblem(version, channel)
return problem ? [problem] : []
}
function main(argv) {
const [cmd, ...rest] = argv
switch (cmd) {
case 'tag':
return `${loadPkg().release.tagPrefix}${requireVersion(rest[0])}`
case 'asset':
return assetName(
loadPkg().release,
requireVersion(rest[0]),
rest[1] ?? die('target id is required'),
)
case 'checksums': {
const { release } = loadPkg()
return `${release.tagPrefix}${requireVersion(rest[0])}${release.checksumsSuffix}`
}
case 'tag-prefix':
return loadPkg().release.tagPrefix
case 'targets':
return loadPkg()
.release.targets.map((t) => `${t.bunTarget}\t${t.id}\t${t.exe ? 1 : 0}`)
.join('\n')
case 'channels':
return CHANNELS.map((c) => c.name).join('\n')
case 'github-env':
return githubEnv()
case 'prerelease': {
const ch = channelByName(rest[0] ?? die('channel argument is required'))
if (!ch) die(`unknown channel: ${rest[0]} (expected one of: ${channelNames()})`)
return String(ch.prerelease)
}
case 'validate': {
const { version, channel, release } = loadPkg()
const problems = [...validateRelease(release), ...validateVersionChannel(version, channel)]
if (problems.length > 0)
die(`invalid difyctl release config:\n - ${problems.join('\n - ')}`)
return `difyctl release valid: version=${version} channel=${channel} targets=${release.targets.length}`
}
case 'edge-version':
return edgeVersion(rest[0])
case 'validate-version':
return validateVersionForChannel(
requireVersion(rest[0]),
rest[1] ?? die('channel argument is required'),
)
default:
die(`unknown subcommand: ${cmd ?? '(none)'}`)
}
}
const invokedDirectly =
process.argv[1] && realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)
if (invokedDirectly) process.stdout.write(`${main(process.argv.slice(2))}\n`)
export {
assetName,
channelByName,
CHANNELS,
edgeVersion,
loadPkg,
validateVersionForChannel,
versionCore,
}
@@ -0,0 +1,146 @@
import type { PkgManifestOverrides } from '../test/fixtures/pkg-manifest'
import { execFileSync } from 'node:child_process'
import { readFileSync, writeFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vite-plus/test'
import {
FIXTURE_CHANNEL,
FIXTURE_TAG_PREFIX,
FIXTURE_VERSION,
pkgManifestEnv,
} from '../test/fixtures/pkg-manifest'
const SCRIPT = fileURLToPath(new URL('./release-naming.mjs', import.meta.url))
const PKG_ENV = pkgManifestEnv()
type RunResult = { code: number; stdout: string; stderr: string }
function exec(args: string[], pkgEnv: Record<string, string>): RunResult {
try {
const stdout = execFileSync('node', [SCRIPT, ...args], {
encoding: 'utf8',
env: { ...process.env, ...pkgEnv },
})
return { code: 0, stdout, stderr: '' }
} catch (e) {
const err = e as { status?: number; stdout?: string; stderr?: string }
return { code: err.status ?? 1, stdout: err.stdout ?? '', stderr: err.stderr ?? '' }
}
}
function run(args: string[]): RunResult {
return exec(args, PKG_ENV)
}
function runWith(overrides: PkgManifestOverrides, args: string[]): RunResult {
return exec(args, pkgManifestEnv(overrides))
}
type FixtureManifest = {
version?: string
difyctl: { channel?: string }
}
function runOnManifest(mutate: (manifest: FixtureManifest) => void, args: string[]): RunResult {
const pkgEnv = pkgManifestEnv()
const [pkgPath] = Object.values(pkgEnv)
if (!pkgPath) throw new Error('pkgManifestEnv returned no manifest path')
const manifest = JSON.parse(readFileSync(pkgPath, 'utf8')) as FixtureManifest
mutate(manifest)
writeFileSync(pkgPath, JSON.stringify(manifest))
return exec(args, pkgEnv)
}
function parseKeyValues(stdout: string): Record<string, string> {
return Object.fromEntries(
stdout
.split('\n')
.filter(Boolean)
.map((line) => [line.slice(0, line.indexOf('=')), line.slice(line.indexOf('=') + 1)]),
)
}
describe('release-naming github-env', () => {
it('emits every manifest field for $GITHUB_ENV, plus a composed difyctlTag', () => {
const fields = parseKeyValues(run(['github-env']).stdout)
expect(fields).toEqual({
version: FIXTURE_VERSION,
channel: FIXTURE_CHANNEL,
prerelease: 'false',
tagPrefix: FIXTURE_TAG_PREFIX,
difyctlTag: `${FIXTURE_TAG_PREFIX}${FIXTURE_VERSION}`,
})
})
})
describe('release-naming edge channel', () => {
it('edge-version derives <version core>-edge.<sha> from the package version', () => {
expect(run(['edge-version', '2fd7b82']).stdout.trim()).toBe(`${FIXTURE_VERSION}-edge.2fd7b82`)
})
it('edge-version accepts a 40-char sha', () => {
const sha = '2fd7b829e1f0aaaabbbbccccddddeeeeffff0000'
expect(run(['edge-version', sha]).stdout.trim()).toBe(`${FIXTURE_VERSION}-edge.${sha}`)
})
it('edge-version rejects a non-hex sha', () => {
expect(run(['edge-version', 'nothex!']).code).not.toBe(0)
})
it('edge-version fails when the manifest carries no version', () => {
const { code, stderr } = runOnManifest(
(m) => {
delete m.version
},
['edge-version', '2fd7b82'],
)
expect(code).not.toBe(0)
expect(stderr).toContain('cannot derive edge base from version')
})
it('validate-version rejects an rc string under the edge channel', () => {
expect(run(['validate-version', '0.1.0-rc.1', 'edge']).code).not.toBe(0)
})
})
describe('release-naming validate channel', () => {
const validateChannel = (channel: string) => runWith({ channel }, ['validate'])
it.each<[string, string]>([
['stable', FIXTURE_VERSION],
['alpha', `${FIXTURE_VERSION}-alpha`],
['rc', `${FIXTURE_VERSION}-rc.1`],
['edge', `${FIXTURE_VERSION}-edge.2fd7b82`],
])('accepts the %s channel with a version in its form', (channel, version) => {
expect(runWith({ channel, version }, ['validate']).code).toBe(0)
})
it('rejects a typo of a real channel and names it', () => {
const { code, stderr } = validateChannel('stabel')
expect(code).not.toBe(0)
expect(stderr).toContain('unknown channel: stabel')
})
it('rejects a manifest with no channel at all', () => {
const { code, stderr } = runOnManifest(
(m) => {
delete m.difyctl.channel
},
['validate'],
)
expect(code).not.toBe(0)
expect(stderr).toContain('unknown channel')
})
})
describe('release-naming validate-version', () => {
it('accepts build metadata on a stable version', () => {
expect(run(['validate-version', '1.16.1+r2', 'stable']).code).toBe(0)
})
it('accepts an alpha version under the alpha channel, with or without a counter', () => {
expect(run(['validate-version', '1.16.1-alpha', 'alpha']).code).toBe(0)
expect(run(['validate-version', '1.16.1-alpha.2', 'alpha']).code).toBe(0)
})
})
@@ -0,0 +1,130 @@
#!/usr/bin/env node
// release-r2-edge.mjs — edge/R2 release metadata generator. Two subcommands:
// manifest -> the per-channel pointer manifest.json (the installer reads this)
// index -> the per-channel build-history ledger index.json
import { existsSync, readFileSync } from 'node:fs'
import { assetName, loadPkg, validateVersionForChannel } from './release-naming.mjs'
function die(msg) {
process.stderr.write(`release-r2-edge: ${msg}\n`)
process.exit(1)
}
function parseArgs(argv) {
const out = {}
for (let i = 0; i < argv.length; i += 2) {
const key = argv[i]?.replace(/^--/, '')
const val = argv[i + 1]
if (!key || val === undefined || val.startsWith('--'))
die(`malformed argument near ${argv[i]} (expected --key value)`)
out[key] = val
}
return out
}
function requireArgs(args, keys) {
for (const k of keys) {
if (!args[k]) die(`missing --${k}`)
}
}
// checksums lines are "<sha256> <assetName>"
function shaMap(checksumsPath) {
const map = new Map()
for (const line of readFileSync(checksumsPath, 'utf8').split('\n')) {
const m = line.match(/^([0-9a-f]{64})\s+(\S+)$/i)
if (m) map.set(m[2], m[1])
}
return map
}
function emitManifest(args) {
requireArgs(args, ['channel', 'version', 'commit', 'build-date', 'base-url', 'checksums'])
validateVersionForChannel(args.version, args.channel)
const { release } = loadPkg()
const shas = shaMap(args.checksums)
const targetLines = release.targets
.map((t) => {
const asset = assetName(release, args.version, t.id)
const sha = shas.get(asset)
if (!sha) die(`no sha256 for ${asset} in ${args.checksums}`)
// one target per line: install-r2.sh grep/sed depends on this layout
return ` ${JSON.stringify(t.id)}: { "asset": ${JSON.stringify(asset)}, "sha256": ${JSON.stringify(sha)} }`
})
.join(',\n')
const head = {
schema: 1,
name: release.binName,
channel: args.channel,
version: args.version,
commit: args.commit,
buildDate: args['build-date'],
baseUrl: args['base-url'],
}
const headLines = Object.entries(head)
.map(([k, v]) => ` ${JSON.stringify(k)}: ${JSON.stringify(v)}`)
.join(',\n')
process.stdout.write(`{\n${headLines},\n "targets": {\n${targetLines}\n }\n}\n`)
}
// Newline-delimited dir names of binaries that still exist in R2. Absent file =
// no reconciliation (caller could not list); empty file = no survivors.
function loadExistingDirs(path) {
if (!path || !existsSync(path)) return null
const set = new Set()
for (const line of readFileSync(path, 'utf8').split('\n')) {
const d = line.trim()
if (d) set.add(d)
}
return set
}
function emitIndex(args) {
requireArgs(args, ['current', 'channel', 'version', 'commit', 'build-date'])
// empty / "-" / missing = no ledger yet (first publish)
let current = { schema: 1, channel: args.channel, builds: [] }
if (args.current !== '-' && existsSync(args.current)) {
const raw = readFileSync(args.current, 'utf8').trim()
if (raw && raw !== '-') {
try {
current = JSON.parse(raw)
} catch {
die(`current index at ${args.current} is not valid JSON`)
}
}
}
const entry = {
version: args.version,
commit: args.commit,
buildDate: args['build-date'],
dir: args.version,
}
const kept = (current.builds ?? []).filter((b) => b.version !== entry.version)
let builds = [entry, ...kept]
// Reconcile to binaries that still exist in R2: lifecycle/TTL on the bin prefix
// is the only deletion mechanism, so the ledger never advertises a build whose
// binary is gone. The new build is always kept (just uploaded). No count cap.
const existing = loadExistingDirs(args['existing-dirs'])
if (existing) builds = builds.filter((b) => b.dir === entry.dir || existing.has(b.dir))
const index = { schema: 1, channel: args.channel, updated: args['build-date'], builds }
process.stdout.write(`${JSON.stringify(index, null, 2)}\n`)
}
const [cmd, ...rest] = process.argv.slice(2)
const args = parseArgs(rest)
switch (cmd) {
case 'manifest':
emitManifest(args)
break
case 'index':
emitIndex(args)
break
default:
die(`unknown subcommand: ${cmd ?? '(none)'} (expected: manifest | index)`)
}
@@ -0,0 +1,311 @@
import { execFileSync, spawnSync } from 'node:child_process'
import { mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vite-plus/test'
import { FIXTURE_TARGET_IDS, pkgManifestEnv } from '../test/fixtures/pkg-manifest'
const SCRIPT = fileURLToPath(new URL('./release-r2-edge.mjs', import.meta.url))
const PKG_ENV = pkgManifestEnv()
function run(args: string[]): { code: number; stdout: string; stderr: string } {
try {
return {
code: 0,
stdout: execFileSync('node', [SCRIPT, ...args], {
encoding: 'utf8',
env: { ...process.env, ...PKG_ENV },
}),
stderr: '',
}
} catch (e) {
const err = e as { status?: number; stdout?: string; stderr?: string }
return { code: err.status ?? 1, stdout: err.stdout ?? '', stderr: err.stderr ?? '' }
}
}
// ---- manifest ----
function writeChecksums(version: string): string {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-manifest-'))
const lines = FIXTURE_TARGET_IDS.map((id, i) => {
const exe = id === 'windows-x64' ? '.exe' : ''
const sha = String(i).repeat(64)
return `${sha} difyctl-v${version}-${id}${exe}`
})
const file = join(dir, `difyctl-v${version}-checksums.txt`)
writeFileSync(file, `${lines.join('\n')}\n`)
return file
}
const VERSION = '0.1.0-edge.2fd7b82'
const BASE_URL = 'https://example.r2.dev/difyctl/edge/0.1.0-edge.2fd7b82'
type ManifestJson = {
schema: number
name: string
channel: string
version: string
commit: string
buildDate: string
baseUrl: string
targets: Record<(typeof FIXTURE_TARGET_IDS)[number], { asset: string; sha256: string }>
}
type IndexBuild = {
version: string
commit: string
buildDate: string
dir: string
}
type IndexJson = {
schema: number
channel: string
updated: string
builds: IndexBuild[]
}
function buildManifest(version = VERSION): {
code: number
json: ManifestJson
stdout: string
stderr: string
} {
const checksums = writeChecksums(version)
const r = run([
'manifest',
'--channel',
'edge',
'--version',
version,
'--commit',
'abc1234',
'--build-date',
'2026-06-14T12:00:00Z',
'--base-url',
BASE_URL,
'--checksums',
checksums,
])
return {
code: r.code,
json: (r.code === 0 ? JSON.parse(r.stdout) : null) as ManifestJson,
stdout: r.stdout,
stderr: r.stderr,
}
}
describe('release-r2-edge manifest', () => {
it('emits the core pointer fields', () => {
const { json } = buildManifest()
expect(json.schema).toBe(1)
expect(json.name).toBe('difyctl')
expect(json.channel).toBe('edge')
expect(json.version).toBe(VERSION)
expect(json.commit).toBe('abc1234')
expect(json.buildDate).toBe('2026-06-14T12:00:00Z')
expect(json.baseUrl).toBe(BASE_URL)
})
it('lists all 5 targets with asset name + sha256 from the checksums file', () => {
const { json } = buildManifest()
expect(Object.keys(json.targets).sort()).toEqual([
'darwin-arm64',
'darwin-x64',
'linux-arm64',
'linux-x64',
'windows-x64',
])
expect(json.targets['linux-x64'].asset).toBe(`difyctl-v${VERSION}-linux-x64`)
expect(json.targets['windows-x64'].asset).toBe(`difyctl-v${VERSION}-windows-x64.exe`)
expect(json.targets['linux-x64'].sha256).toMatch(/^\d{64}$/)
})
it('renders each target on a single line (installer greps it)', () => {
const { stdout } = buildManifest()
expect(stdout).toMatch(/^ {4}"linux-x64": \{ "asset": ".*", "sha256": ".*" \}/m)
})
it('rejects a version that does not match the channel form', () => {
const { code } = buildManifest('0.1.0-rc.1')
expect(code).not.toBe(0)
})
it('dies when a target sha is missing from the checksums file', () => {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-manifest-'))
const file = join(dir, `difyctl-v${VERSION}-checksums.txt`)
writeFileSync(file, `${'0'.repeat(64)} difyctl-v${VERSION}-linux-x64\n`) // only 1 of 5
const r = run([
'manifest',
'--channel',
'edge',
'--version',
VERSION,
'--commit',
'abc1234',
'--build-date',
'2026-06-14T12:00:00Z',
'--base-url',
BASE_URL,
'--checksums',
file,
])
expect(r.code).not.toBe(0)
})
it('rejects a malformed dropped-value argument (no silent misparse)', () => {
// --version has no value; --commit must NOT be swallowed as the version
const r = run([
'manifest',
'--channel',
'edge',
'--version',
'--commit',
'abc1234',
'--build-date',
'2026-06-14T12:00:00Z',
'--base-url',
'https://x',
'--checksums',
'/nonexistent',
])
expect(r.code).not.toBe(0)
})
})
// ---- index ----
function runIndex(
currentContent: string | null,
build: Omit<IndexBuild, 'dir'>,
existingDirs?: string[],
) {
let currentArg = '-'
if (currentContent !== null) {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-index-'))
currentArg = join(dir, 'index.json')
writeFileSync(currentArg, currentContent)
}
const extra: string[] = []
if (existingDirs !== undefined) {
const dir = mkdtempSync(join(tmpdir(), 'difyctl-existing-'))
const f = join(dir, 'existing.txt')
writeFileSync(f, `${existingDirs.join('\n')}\n`)
extra.push('--existing-dirs', f)
}
const r = spawnSync(
'node',
[
SCRIPT,
'index',
'--current',
currentArg,
'--channel',
'edge',
'--version',
build.version,
'--commit',
build.commit,
'--build-date',
build.buildDate,
...extra,
],
{ encoding: 'utf8' },
)
return {
code: r.status ?? 1,
index: (r.status === 0 ? JSON.parse(r.stdout) : null) as IndexJson,
}
}
const B1 = { version: '0.1.0-edge.aaaaaaa', commit: 'aaaaaaa', buildDate: '2026-06-14T09:00:00Z' }
const B2 = { version: '0.1.0-edge.bbbbbbb', commit: 'bbbbbbb', buildDate: '2026-06-14T10:00:00Z' }
describe('release-r2-edge index', () => {
it('creates a fresh index from a missing current (arg "-")', () => {
const { index } = runIndex(null, B1)
expect(index.schema).toBe(1)
expect(index.channel).toBe('edge')
expect(index.builds).toHaveLength(1)
expect(index.builds[0]).toMatchObject({
version: B1.version,
commit: B1.commit,
dir: B1.version,
})
})
it('treats an empty current file as fresh (first publish, curl wrote nothing)', () => {
const { code, index } = runIndex('', B1)
expect(code).toBe(0)
expect(index.builds).toHaveLength(1)
})
it('treats a "-"-content current file as fresh (curl 404 fallback)', () => {
const { code, index } = runIndex('-\n', B1)
expect(code).toBe(0)
expect(index.builds).toHaveLength(1)
})
it('prepends the new build (publish order; newest at [0])', () => {
const current = JSON.stringify({
schema: 1,
channel: 'edge',
builds: [
{ version: B1.version, commit: B1.commit, buildDate: B1.buildDate, dir: B1.version },
],
})
const { index } = runIndex(current, B2)
expect(index.builds.map((b) => b.version)).toEqual([B2.version, B1.version])
})
it('dedups a re-cut of the same version (no duplicate, moves to top)', () => {
const current = JSON.stringify({
schema: 1,
channel: 'edge',
builds: [
{ version: B2.version, commit: B2.commit, buildDate: B2.buildDate, dir: B2.version },
{ version: B1.version, commit: B1.commit, buildDate: B1.buildDate, dir: B1.version },
],
})
const { index } = runIndex(current, B1) // re-cut B1
expect(index.builds.map((b) => b.version)).toEqual([B1.version, B2.version])
})
it('reconciles to surviving binary dirs (drops a build whose binary expired)', () => {
const current = JSON.stringify({
schema: 1,
channel: 'edge',
builds: [
{ version: B1.version, commit: B1.commit, buildDate: B1.buildDate, dir: B1.version },
],
})
// B1's binary is gone (not in existing); the new B2 is always kept.
const { index } = runIndex(current, B2, [B2.version])
expect(index.builds.map((b) => b.version)).toEqual([B2.version])
})
it('keeps the new build even when it is absent from the existing-dirs list', () => {
const { index } = runIndex(null, B1, []) // empty survivors, fresh ledger
expect(index.builds.map((b) => b.version)).toEqual([B1.version])
})
it('does not reconcile when no --existing-dirs is given (list unavailable)', () => {
const current = JSON.stringify({
schema: 1,
channel: 'edge',
builds: [
{ version: B1.version, commit: B1.commit, buildDate: B1.buildDate, dir: B1.version },
],
})
const { index } = runIndex(current, B2) // no existing-dirs → keep all
expect(index.builds.map((b) => b.version)).toEqual([B2.version, B1.version])
})
it('dies on a non-empty current file that is not valid JSON', () => {
const { code } = runIndex('{not json', B1)
expect(code).not.toBe(0)
})
})
+97
View File
@@ -0,0 +1,97 @@
#!/usr/bin/env bash
# release-r2-publish.sh — direct-publish difyctl binaries + manifest + index to R2.
# Strict order so the pointer never references missing bytes:
# sync binaries -> HEAD-verify -> list survivors -> put index.json -> put manifest.json.
# Binaries live under the bin prefix and expire via an R2 lifecycle (TTL) rule on
# that prefix; the ledger is reconciled to surviving dirs, not pruned here. The
# pointer JSONs (manifest.json/index.json) live under the (non-expiring) prefix.
# Installers (install-r2.sh/.ps1) are served from the GitHub repo, not R2.
# Usage: release-r2-publish.sh <channel> <version>
# Env: DIFYCTL_R2_S3_ENDPOINT DIFYCTL_R2_BUCKET DIFYCTL_R2_PUBLIC_BASE (+ AWS creds, AWS_REQUEST_CHECKSUM_CALCULATION).
# DIFYCTL_R2_PREFIX (default difyctl) key root for pointer JSONs
# DIFYCTL_R2_BIN_PREFIX (default <prefix>/bin) key root for binaries (TTL target)
set -euo pipefail
_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DIST_DIR="${DIST_DIR:-${_dir}/../dist/bin}"
aws_s3() { aws --endpoint-url "$DIFYCTL_R2_S3_ENDPOINT" "$@"; }
publish_main() {
local channel="$1" version="$2"
local prefix="${DIFYCTL_R2_PREFIX:-difyctl}"
local bin_prefix="${DIFYCTL_R2_BIN_PREFIX:-${prefix}/bin}"
local key="${bin_prefix}/${channel}/${version}"
local pointer_prefix="${prefix}/${channel}"
local base_url="${DIFYCTL_R2_PUBLIC_BASE}/${key}"
local current="" new_index="" existing_dirs="" manifest=""
trap 'rm -f "$current" "$new_index" "$existing_dirs" "$manifest"' RETURN
# 1. binaries (+ checksums) — immutable
aws_s3 s3 sync "$DIST_DIR" "s3://${DIFYCTL_R2_BUCKET}/${key}/" \
--content-type application/octet-stream \
--cache-control "public, max-age=31536000, immutable"
# 2. HEAD-verify each expected target asset exists on R2. Capture targets into
# a variable first; a process-substitution producer failure bypasses set -e.
local targets
targets="$(node "${_dir}/release-naming.mjs" targets)"
[ -n "$targets" ] || { echo "release-r2-publish: no release targets resolved" >&2; exit 1; }
local verified=0 _bun id _exe asset
while IFS=$'\t' read -r _bun id _exe; do
asset="$(node "${_dir}/release-naming.mjs" asset "$version" "$id")"
aws_s3 s3api head-object --bucket "$DIFYCTL_R2_BUCKET" --key "${key}/${asset}" >/dev/null
verified=$((verified + 1))
done <<< "$targets"
[ "$verified" -gt 0 ] || { echo "release-r2-publish: verified zero targets" >&2; exit 1; }
# 3. survivors: binary dirs still present under the bin prefix (lifecycle/TTL may
# have deleted old ones). aws CLI auto-paginates list-objects-v2, so --query
# aggregates CommonPrefixes across all pages. On a list failure, skip
# reconciliation (keep the ledger as-is) rather than wrongly drop builds.
existing_dirs="$(mktemp)"
local listed=0 raw
raw="$(mktemp)"
if aws_s3 s3api list-objects-v2 --bucket "$DIFYCTL_R2_BUCKET" \
--prefix "${bin_prefix}/${channel}/" --delimiter / \
--query 'CommonPrefixes[].Prefix' --output text > "$raw" 2>/dev/null; then
# text rows are "<bin_prefix>/<channel>/<dir>/"; emit just <dir>. "None" = empty.
tr '\t' '\n' < "$raw" | awk -F/ 'NF>1 && $0 != "None" { print $(NF-1) }' > "$existing_dirs"
listed=1
fi
rm -f "$raw"
# 4. index.json: fetch current, prepend, reconcile to survivors.
current="$(mktemp)"
curl -fsSL "${DIFYCTL_R2_PUBLIC_BASE}/${pointer_prefix}/index.json" -o "$current" 2>/dev/null || echo '-' > "$current"
new_index="$(mktemp)"
if [ "$listed" = "1" ]; then
emit_index "$current" "$channel" "$version" --existing-dirs "$existing_dirs" >"$new_index"
else
emit_index "$current" "$channel" "$version" >"$new_index"
fi
aws_s3 s3 cp "$new_index" "s3://${DIFYCTL_R2_BUCKET}/${pointer_prefix}/index.json" \
--content-type application/json --cache-control "public, max-age=60, must-revalidate"
# 5. manifest.json — pointer; written last so it never references missing binaries
manifest="$(mktemp)"
node "${_dir}/release-r2-edge.mjs" manifest --channel "$channel" --version "$version" \
--commit "${DIFYCTL_COMMIT:-unknown}" --build-date "${DIFYCTL_BUILD_DATE:-unknown}" \
--base-url "$base_url" --checksums "${DIST_DIR}/difyctl-v${version}-checksums.txt" >"$manifest"
aws_s3 s3 cp "$manifest" "s3://${DIFYCTL_R2_BUCKET}/${pointer_prefix}/manifest.json" \
--content-type application/json --cache-control "public, max-age=60, must-revalidate"
}
# emit_index <current> <channel> <version> [extra args...] — wrapper so the
# survivor flag can be omitted without tripping `set -u` on empty arrays (bash 3.2).
emit_index() {
local current="$1" channel="$2" version="$3"; shift 3
node "${_dir}/release-r2-edge.mjs" index --current "$current" --channel "$channel" \
--version "$version" --commit "${DIFYCTL_COMMIT:-unknown}" --build-date "${DIFYCTL_BUILD_DATE:-unknown}" "$@"
}
if [ "${RELEASE_PUBLISH_LIB:-0}" != "1" ]; then
[ "$#" -eq 2 ] || { echo "usage: release-r2-publish.sh <channel> <version>" >&2; exit 2; }
publish_main "$1" "$2"
fi
@@ -0,0 +1,91 @@
import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vite-plus/test'
const SCRIPT = fileURLToPath(new URL('./release-r2-publish.sh', import.meta.url))
// Stub `aws` + `curl` + `node` as shell functions that just log action verbs to
// $ORDER_LOG, then run the publish `main` and assert the order of operations.
function runPublish(): { code: number; order: string[]; stderr: string } {
const stub = [
'ORDER_LOG="$(mktemp)"',
'aws() {',
' case "$*" in',
' *"list-objects-v2"*) echo list-survivors >>"$ORDER_LOG" ;;',
' *" cp "*"/index.json"*) echo put-index >>"$ORDER_LOG" ;;',
' *" cp "*"/manifest.json"*) echo put-manifest >>"$ORDER_LOG" ;;',
' *" sync "*) echo sync-binaries >>"$ORDER_LOG" ;;',
' *"head-object"*) echo head-verify >>"$ORDER_LOG" ;;',
' *) : ;;',
' esac',
'}',
'curl() { echo "{}"; }',
'node() {',
' case "$*" in',
' *release-naming.mjs*targets*)',
" printf 'bun-linux-x64\\tlinux-x64\\t0\\nbun-linux-arm64\\tlinux-arm64\\t0\\nbun-darwin-x64\\tdarwin-x64\\t0\\nbun-darwin-arm64\\tdarwin-arm64\\t0\\nbun-windows-x64\\twindows-x64\\t1\\n' ;;",
" *release-naming.mjs*' asset '*) printf 'difyctl-vX\\n' ;;",
" *release-r2-edge.mjs*' index '*) echo '{}' ;;",
" *release-r2-edge.mjs*' manifest '*) echo '{}' ;;",
' *) : ;;',
' esac',
'}',
].join('\n')
const program = [
stub,
`. "${SCRIPT}"`,
'publish_main edge 0.1.0-edge.2fd7b82',
'cat "$ORDER_LOG"',
].join('\n')
// bash, NOT sh: the script uses BASH_SOURCE + process substitution.
const r = spawnSync('bash', ['-c', program], {
encoding: 'utf8',
env: {
...process.env,
RELEASE_PUBLISH_LIB: '1',
DIFYCTL_R2_S3_ENDPOINT: 'https://endpoint.example',
DIFYCTL_R2_BUCKET: 'cli-dev',
DIFYCTL_R2_PUBLIC_BASE: 'https://pub.example.r2.dev',
DIST_DIR: '/tmp',
},
})
return {
code: r.status ?? 1,
order: (r.stdout ?? '').trim().split('\n').filter(Boolean),
stderr: r.stderr ?? '',
}
}
describe('release-r2-publish order', () => {
it('uploads binaries, verifies, lists survivors, then index, then manifest', () => {
const { code, order } = runPublish()
expect(code).toBe(0)
expect(order.indexOf('sync-binaries')).toBeLessThan(order.indexOf('head-verify'))
expect(order.indexOf('head-verify')).toBeLessThan(order.indexOf('list-survivors'))
expect(order.indexOf('list-survivors')).toBeLessThan(order.indexOf('put-index'))
expect(order.indexOf('put-index')).toBeLessThan(order.indexOf('put-manifest'))
// pointer is never pruned here — deletion is owned by the R2 lifecycle rule
expect(order).not.toContain('prune')
})
it('exits non-zero when no targets resolve (head-verify safety gate)', () => {
const stub = [
'aws() { :; }',
'curl() { echo "{}"; }',
'node() { case "$*" in *release-naming.mjs*targets*) : ;; *) echo "{}" ;; esac; }',
].join('\n')
const program = [stub, `. "${SCRIPT}"`, 'publish_main edge 0.1.0-edge.2fd7b82'].join('\n')
const r = spawnSync('bash', ['-c', program], {
encoding: 'utf8',
env: {
...process.env,
RELEASE_PUBLISH_LIB: '1',
DIFYCTL_R2_S3_ENDPOINT: 'https://endpoint.example',
DIFYCTL_R2_BUCKET: 'cli-dev',
DIFYCTL_R2_PUBLIC_BASE: 'https://pub.example.r2.dev',
DIST_DIR: '/tmp',
},
})
expect(r.status).not.toBe(0)
})
})
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
# scripts/release-smoke.sh <bin> — run a built binary on this runner and check that
# `install skills` writes exactly the skill tree in skills/difyctl.
set -euo pipefail
_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/common.sh
source "${_dir}/lib/common.sh"
bin="${1:?usage: release-smoke.sh <bin>}"
skill_dir="$(cd "$(cli::root)/.." && pwd)/skills/difyctl"
out="$(mktemp -d)"
trap 'rm -rf "$out"' EXIT
"$bin" version
"$bin" install skills "$out"
diff -r "${out}/difyctl" "$skill_dir"
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
# scripts/release-write-checksums.sh — write sha256 manifest for release binaries.
#
# Required env: CLI_VERSION (e.g. 0.1.0-rc.1). Output:
# cli/dist/bin/difyctl-v<CLI_VERSION>-checksums.txt
set -euo pipefail
_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/common.sh
source "${_dir}/lib/common.sh"
naming() { node "${_dir}/release-naming.mjs" "$@"; }
CLI_VERSION="${CLI_VERSION:-$(node -p "require('$(cli::root)/package.json').version")}"
[[ -n "$CLI_VERSION" && "$CLI_VERSION" != "undefined" ]] || die "CLI_VERSION could not be derived from package.json"
cd "$(cli::root)/dist/bin"
manifest="$(naming checksums "$CLI_VERSION")"
asset_prefix="$(naming tag-prefix)${CLI_VERSION}-"
: > "$manifest"
if command -v sha256sum >/dev/null 2>&1; then
hash_cmd="sha256sum"
elif command -v shasum >/dev/null 2>&1; then
hash_cmd="shasum -a 256"
else
die "no sha256 hasher found (need sha256sum or shasum)"
fi
found=0
for bin in "${asset_prefix}"*; do
[[ -f "$bin" ]] || continue
[[ "$bin" == "$manifest" ]] && continue
$hash_cmd "$bin" >> "$manifest"
found=$((found + 1))
done
[[ "$found" -gt 0 ]] || die "no binaries matching ${asset_prefix}* in dist/bin/"
log::info "wrote ${manifest} (${found} entries)"
+72
View File
@@ -0,0 +1,72 @@
#!/usr/bin/env -S bun
// scripts/run-smoke.ts — five checks against a running Dify server (or mock),
// through `bun bin/dev.js`. DIFY_SERVER, DIFY_TOKEN and DIFY_CONFIG_DIR are
// read from the environment and passed through unchanged.
import { execFileSync, spawnSync } from 'node:child_process'
import { ExitCode } from '../src/errors/codes.js'
type Check = { name: string; run: () => void }
function cli(args: string[]): string {
return execFileSync('bun', ['bin/dev.js', ...args], { encoding: 'utf8' })
}
function cliExitCode(args: string[]): number {
return spawnSync('bun', ['bin/dev.js', ...args], { encoding: 'utf8' }).status ?? 1
}
function expectExit(args: string[], expected: number): void {
const code = cliExitCode(args)
if (code !== expected)
throw new Error(`expected exit ${expected} for "${args.join(' ')}", got ${code}`)
}
const checks: Check[] = [
{
name: 'version prints .client.version',
run: () => {
const body = JSON.parse(cli(['version']))
if (typeof body.client?.version !== 'string') throw new Error('no .client.version')
},
},
{
name: 'help get.console_app carries .input.properties.workspace_id',
run: () => {
const body = JSON.parse(cli(['help', 'get.console_app', '--json']))
if (body.input?.properties?.workspace_id === undefined)
throw new Error('no .input.properties.workspace_id')
},
},
{
name: 'get console_app returns .data',
run: () => {
const body = JSON.parse(cli(['get', 'console_app', '--input', '{"limit":1}']))
if (body.data === undefined) throw new Error('no .data')
},
},
{
name: '--stream on an object-kind op exits 2 (usage)',
run: () =>
expectExit(
['describe', 'console_app', '--input', '{"app_id":"x"}', '--stream'],
ExitCode.Usage,
),
},
{
name: 'a missing required input exits 2 (usage)',
run: () => expectExit(['describe', 'console_app', '--input', '{}'], ExitCode.Usage),
},
]
let failed = 0
for (const c of checks) {
try {
c.run()
console.log(`[x] ${c.name}`)
} catch (err) {
failed++
console.log(`[ ] ${c.name} — ${(err as Error).message}`)
}
}
console.log(`\n${checks.length - failed}/${checks.length} checks passed`)
process.exit(failed > 0 ? 1 : 0)
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
# uninstall-local.sh — remove a locally installed difyctl.
# Run via: pnpm uninstall:local
set -eu
PREFIX="${DIFYCTL_PREFIX:-${HOME}/.local}"
SHARE_DIR="${PREFIX}/share/difyctl"
BIN_LINK="${PREFIX}/bin/difyctl"
rm -rf "$SHARE_DIR"
rm -f "$BIN_LINK"
echo "removed ${SHARE_DIR} and ${BIN_LINK}"