feat: add GitHub Actions workflow for SDK release automation

This commit is contained in:
mingholy.lmh
2025-12-01 17:54:43 +08:00
parent 50e3a6ee0a
commit 81c8b3eaec
5 changed files with 872 additions and 12 deletions

226
.github/workflows/release-sdk.yml vendored Normal file
View File

@@ -0,0 +1,226 @@
name: 'Release SDK'
on:
schedule:
# Runs every day at 1:00 AM UTC for the nightly release (offset from CLI at 0:00).
- cron: '0 1 * * *'
# Runs every Wednesday at 00:59 UTC for the preview release (offset from CLI on Tuesday).
- cron: '59 0 * * 3'
workflow_dispatch:
inputs:
version:
description: 'The version to release (e.g., v0.1.11). Required for manual patch releases.'
required: false
type: 'string'
ref:
description: 'The branch or ref (full git sha) to release from.'
required: true
type: 'string'
default: 'main'
dry_run:
description: 'Run a dry-run of the release process; no branches, npm packages or GitHub releases will be created.'
required: true
type: 'boolean'
default: true
create_nightly_release:
description: 'Auto apply the nightly release tag, input version is ignored.'
required: false
type: 'boolean'
default: false
create_preview_release:
description: 'Auto apply the preview release tag, input version is ignored.'
required: false
type: 'boolean'
default: false
force_skip_tests:
description: 'Select to skip the "Run Tests" step in testing. Prod releases should run tests'
required: false
type: 'boolean'
default: false
jobs:
release-sdk:
runs-on: 'ubuntu-latest'
environment:
name: 'production-release'
url: '${{ github.server_url }}/${{ github.repository }}/releases/tag/sdk-typescript-${{ steps.version.outputs.RELEASE_TAG }}'
if: |-
${{ github.repository == 'QwenLM/qwen-code' }}
permissions:
contents: 'write'
packages: 'write'
id-token: 'write'
issues: 'write'
outputs:
RELEASE_TAG: '${{ steps.version.outputs.RELEASE_TAG }}'
steps:
- name: 'Checkout'
uses: 'actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8' # ratchet:actions/checkout@v5
with:
ref: '${{ github.event.inputs.ref || github.sha }}'
fetch-depth: 0
- name: 'Set booleans for simplified logic'
env:
CREATE_NIGHTLY_RELEASE: '${{ github.event.inputs.create_nightly_release }}'
CREATE_PREVIEW_RELEASE: '${{ github.event.inputs.create_preview_release }}'
EVENT_NAME: '${{ github.event_name }}'
CRON: '${{ github.event.schedule }}'
DRY_RUN_INPUT: '${{ github.event.inputs.dry_run }}'
id: 'vars'
run: |-
is_nightly="false"
if [[ "${CRON}" == "0 1 * * *" || "${CREATE_NIGHTLY_RELEASE}" == "true" ]]; then
is_nightly="true"
fi
echo "is_nightly=${is_nightly}" >> "${GITHUB_OUTPUT}"
is_preview="false"
if [[ "${CRON}" == "59 0 * * 3" || "${CREATE_PREVIEW_RELEASE}" == "true" ]]; then
is_preview="true"
fi
echo "is_preview=${is_preview}" >> "${GITHUB_OUTPUT}"
is_dry_run="false"
if [[ "${DRY_RUN_INPUT}" == "true" ]]; then
is_dry_run="true"
fi
echo "is_dry_run=${is_dry_run}" >> "${GITHUB_OUTPUT}"
- name: 'Setup Node.js'
uses: 'actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020' # ratchet:actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'npm'
- name: 'Install Dependencies'
run: |-
npm ci
- name: 'Get the version'
id: 'version'
run: |
VERSION_ARGS=()
if [[ "${IS_NIGHTLY}" == "true" ]]; then
VERSION_ARGS+=(--type=nightly)
elif [[ "${IS_PREVIEW}" == "true" ]]; then
VERSION_ARGS+=(--type=preview)
if [[ -n "${MANUAL_VERSION}" ]]; then
VERSION_ARGS+=("--preview_version_override=${MANUAL_VERSION}")
fi
else
VERSION_ARGS+=(--type=stable)
if [[ -n "${MANUAL_VERSION}" ]]; then
VERSION_ARGS+=("--stable_version_override=${MANUAL_VERSION}")
fi
fi
VERSION_JSON=$(node packages/sdk-typescript/scripts/get-release-version.js "${VERSION_ARGS[@]}")
echo "RELEASE_TAG=$(echo "$VERSION_JSON" | jq -r .releaseTag)" >> "$GITHUB_OUTPUT"
echo "RELEASE_VERSION=$(echo "$VERSION_JSON" | jq -r .releaseVersion)" >> "$GITHUB_OUTPUT"
echo "NPM_TAG=$(echo "$VERSION_JSON" | jq -r .npmTag)" >> "$GITHUB_OUTPUT"
echo "PREVIOUS_RELEASE_TAG=$(echo "$VERSION_JSON" | jq -r .previousReleaseTag)" >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
IS_NIGHTLY: '${{ steps.vars.outputs.is_nightly }}'
IS_PREVIEW: '${{ steps.vars.outputs.is_preview }}'
MANUAL_VERSION: '${{ inputs.version }}'
- name: 'Run Tests'
if: |-
${{ github.event.inputs.force_skip_tests != 'true' }}
working-directory: 'packages/sdk-typescript'
run: |
npm run test:ci
env:
OPENAI_API_KEY: '${{ secrets.OPENAI_API_KEY }}'
OPENAI_BASE_URL: '${{ secrets.OPENAI_BASE_URL }}'
OPENAI_MODEL: '${{ secrets.OPENAI_MODEL }}'
- name: 'Configure Git User'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: 'Create and switch to a release branch'
id: 'release_branch'
env:
RELEASE_TAG: '${{ steps.version.outputs.RELEASE_TAG }}'
run: |-
BRANCH_NAME="release/sdk-typescript/${RELEASE_TAG}"
git switch -c "${BRANCH_NAME}"
echo "BRANCH_NAME=${BRANCH_NAME}" >> "${GITHUB_OUTPUT}"
- name: 'Update package version'
working-directory: 'packages/sdk-typescript'
env:
RELEASE_VERSION: '${{ steps.version.outputs.RELEASE_VERSION }}'
run: |-
npm version "${RELEASE_VERSION}" --no-git-tag-version --allow-same-version
- name: 'Commit and Conditionally Push package version'
env:
BRANCH_NAME: '${{ steps.release_branch.outputs.BRANCH_NAME }}'
IS_DRY_RUN: '${{ steps.vars.outputs.is_dry_run }}'
RELEASE_TAG: '${{ steps.version.outputs.RELEASE_TAG }}'
run: |-
git add packages/sdk-typescript/package.json
if git diff --staged --quiet; then
echo "No version changes to commit"
else
git commit -m "chore(release): sdk-typescript ${RELEASE_TAG}"
fi
if [[ "${IS_DRY_RUN}" == "false" ]]; then
echo "Pushing release branch to remote..."
git push --set-upstream origin "${BRANCH_NAME}" --follow-tags
else
echo "Dry run enabled. Skipping push."
fi
- name: 'Build SDK'
working-directory: 'packages/sdk-typescript'
run: |-
npm run build
- name: 'Configure npm for publishing'
uses: 'actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020' # ratchet:actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org'
scope: '@qwen-code'
- name: 'Publish @qwen-code/sdk-typescript'
working-directory: 'packages/sdk-typescript'
run: |-
npm publish --access public --tag=${{ steps.version.outputs.NPM_TAG }} ${{ steps.vars.outputs.is_dry_run == 'true' && '--dry-run' || '' }}
env:
NODE_AUTH_TOKEN: '${{ secrets.NPM_TOKEN }}'
- name: 'Create GitHub Release and Tag'
if: |-
${{ steps.vars.outputs.is_dry_run == 'false' }}
env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
RELEASE_BRANCH: '${{ steps.release_branch.outputs.BRANCH_NAME }}'
RELEASE_TAG: '${{ steps.version.outputs.RELEASE_TAG }}'
PREVIOUS_RELEASE_TAG: '${{ steps.version.outputs.PREVIOUS_RELEASE_TAG }}'
run: |-
gh release create "sdk-typescript-${RELEASE_TAG}" \
--target "$RELEASE_BRANCH" \
--title "SDK TypeScript Release ${RELEASE_TAG}" \
--notes-start-tag "sdk-typescript-${PREVIOUS_RELEASE_TAG}" \
--generate-notes
- name: 'Create Issue on Failure'
if: |-
${{ failure() }}
env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
RELEASE_TAG: '${{ steps.version.outputs.RELEASE_TAG }} || "N/A"'
DETAILS_URL: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'
run: |-
gh issue create \
--title "SDK Release Failed for ${RELEASE_TAG} on $(date +'%Y-%m-%d')" \
--body "The SDK release workflow failed. See the full run for details: ${DETAILS_URL}"

View File

@@ -0,0 +1,284 @@
# @qwen-code/sdk-typescript
A minimum experimental TypeScript SDK for programmatic access to Qwen Code.
Feel free to submit a feature request/issue/PR.
## Installation
```bash
npm install @qwen-code/sdk-typescript
```
## Requirements
- Node.js >= 20.0.0
- [Qwen Code](https://github.com/QwenLM/qwen-code) installed and accessible in PATH
> **Note for nvm users**: If you use nvm to manage Node.js versions, the SDK may not be able to auto-detect the Qwen Code executable. You should explicitly set the `pathToQwenExecutable` option to the full path of the `qwen` binary.
## Quick Start
```typescript
import { query } from '@qwen-code/sdk-typescript';
// Single-turn query
const result = query({
prompt: 'What files are in the current directory?',
options: {
cwd: '/path/to/project',
},
});
// Iterate over messages
for await (const message of result) {
if (message.type === 'assistant') {
console.log('Assistant:', message.message.content);
} else if (message.type === 'result') {
console.log('Result:', message.result);
}
}
```
## API Reference
### `query(config)`
Creates a new query session with the Qwen Code.
#### Parameters
- `prompt`: `string | AsyncIterable<SDKUserMessage>` - The prompt to send. Use a string for single-turn queries or an async iterable for multi-turn conversations.
- `options`: `QueryOptions` - Configuration options for the query session.
#### QueryOptions
| Option | Type | Default | Description |
| ------------------------ | ---------------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cwd` | `string` | `process.cwd()` | The working directory for the query session. Determines the context in which file operations and commands are executed. |
| `model` | `string` | - | The AI model to use (e.g., `'qwen-max'`, `'qwen-plus'`, `'qwen-turbo'`). Takes precedence over `OPENAI_MODEL` and `QWEN_MODEL` environment variables. |
| `pathToQwenExecutable` | `string` | Auto-detected | Path to the Qwen Code executable. Supports multiple formats: `'qwen'` (native binary from PATH), `'/path/to/qwen'` (explicit path), `'/path/to/cli.js'` (Node.js bundle), `'node:/path/to/cli.js'` (force Node.js runtime), `'bun:/path/to/cli.js'` (force Bun runtime). If not provided, auto-detects from: `QWEN_CODE_CLI_PATH` env var, `~/.volta/bin/qwen`, `~/.npm-global/bin/qwen`, `/usr/local/bin/qwen`, `~/.local/bin/qwen`, `~/node_modules/.bin/qwen`, `~/.yarn/bin/qwen`. |
| `permissionMode` | `'default' \| 'plan' \| 'auto-edit' \| 'yolo'` | `'default'` | Permission mode controlling tool execution approval. See [Permission Modes](#permission-modes) for details. |
| `canUseTool` | `CanUseTool` | - | Custom permission handler for tool execution approval. Invoked when a tool requires confirmation. Must respond within 30 seconds or the request will be auto-denied. See [Custom Permission Handler](#custom-permission-handler). |
| `env` | `Record<string, string>` | - | Environment variables to pass to the Qwen Code process. Merged with the current process environment. |
| `mcpServers` | `Record<string, ExternalMcpServerConfig>` | - | External MCP (Model Context Protocol) servers to connect. Each server is identified by a unique name and configured with `command`, `args`, and `env`. |
| `abortController` | `AbortController` | - | Controller to cancel the query session. Call `abortController.abort()` to terminate the session and cleanup resources. |
| `debug` | `boolean` | `false` | Enable debug mode for verbose logging from the CLI process. |
| `maxSessionTurns` | `number` | `-1` (unlimited) | Maximum number of conversation turns before the session automatically terminates. A turn consists of a user message and an assistant response. |
| `coreTools` | `string[]` | - | Equivalent to `tool.core` in settings.json. If specified, only these tools will be available to the AI. Example: `['read_file', 'write_file', 'run_terminal_cmd']`. |
| `excludeTools` | `string[]` | - | Equivalent to `tool.exclude` in settings.json. Excluded tools return a permission error immediately. Takes highest priority over all other permission settings. Supports pattern matching: tool name (`'write_file'`), tool class (`'ShellTool'`), or shell command prefix (`'ShellTool(rm )'`). |
| `allowedTools` | `string[]` | - | Equivalent to `tool.allowed` in settings.json. Matching tools bypass `canUseTool` callback and execute automatically. Only applies when tool requires confirmation. Supports same pattern matching as `excludeTools`. |
| `authType` | `'openai' \| 'qwen-oauth'` | `'openai'` | Authentication type for the AI service. Using `'qwen-oauth'` in SDK is not recommended as credentials are stored in `~/.qwen` and may need periodic refresh. |
| `agents` | `SubagentConfig[]` | - | Configuration for subagents that can be invoked during the session. Subagents are specialized AI agents for specific tasks or domains. |
| `includePartialMessages` | `boolean` | `false` | When `true`, the SDK emits incomplete messages as they are being generated, allowing real-time streaming of the AI's response. |
### Timeouts
The SDK enforces the following timeouts:
| Timeout | Duration | Description |
| ------------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------- |
| Permission Callback | 30 seconds | Maximum time for `canUseTool` callback to respond. If exceeded, the tool request is auto-denied. |
| Control Request | 30 seconds | Maximum time for control operations like `initialize()`, `setModel()`, `setPermissionMode()`, and `interrupt()` to complete. |
### Message Types
The SDK provides type guards to identify different message types:
```typescript
import {
isSDKUserMessage,
isSDKAssistantMessage,
isSDKSystemMessage,
isSDKResultMessage,
isSDKPartialAssistantMessage,
} from '@qwen-code/sdk-typescript';
for await (const message of result) {
if (isSDKAssistantMessage(message)) {
// Handle assistant message
} else if (isSDKResultMessage(message)) {
// Handle result message
}
}
```
### Query Instance Methods
The `Query` instance returned by `query()` provides several methods:
```typescript
const q = query({ prompt: 'Hello', options: {} });
// Get session ID
const sessionId = q.getSessionId();
// Check if closed
const closed = q.isClosed();
// Interrupt the current operation
await q.interrupt();
// Change permission mode mid-session
await q.setPermissionMode('yolo');
// Change model mid-session
await q.setModel('qwen-max');
// Close the session
await q.close();
```
## Permission Modes
The SDK supports different permission modes for controlling tool execution:
- **`default`**: Write tools are denied unless approved via `canUseTool` callback or in `allowedTools`. Read-only tools execute without confirmation.
- **`plan`**: Blocks all write tools, instructing AI to present a plan first.
- **`auto-edit`**: Auto-approve edit tools (edit, write_file) while other tools require confirmation.
- **`yolo`**: All tools execute automatically without confirmation.
### Permission Priority Chain
1. `excludeTools` - Blocks tools completely
2. `permissionMode: 'plan'` - Blocks non-read-only tools
3. `permissionMode: 'yolo'` - Auto-approves all tools
4. `allowedTools` - Auto-approves matching tools
5. `canUseTool` callback - Custom approval logic
6. Default behavior - Auto-deny in SDK mode
## Examples
### Multi-turn Conversation
```typescript
import { query, type SDKUserMessage } from '@qwen-code/sdk-typescript';
async function* generateMessages(): AsyncIterable<SDKUserMessage> {
yield {
type: 'user',
session_id: 'my-session',
message: { role: 'user', content: 'Create a hello.txt file' },
parent_tool_use_id: null,
};
// Wait for some condition or user input
yield {
type: 'user',
session_id: 'my-session',
message: { role: 'user', content: 'Now read the file back' },
parent_tool_use_id: null,
};
}
const result = query({
prompt: generateMessages(),
options: {
permissionMode: 'auto-edit',
},
});
for await (const message of result) {
console.log(message);
}
```
### Custom Permission Handler
```typescript
import { query, type CanUseTool } from '@qwen-code/sdk-typescript';
const canUseTool: CanUseTool = async (toolName, input, { signal }) => {
// Allow all read operations
if (toolName.startsWith('read_')) {
return { behavior: 'allow', updatedInput: input };
}
// Prompt user for write operations (in a real app)
const userApproved = await promptUser(`Allow ${toolName}?`);
if (userApproved) {
return { behavior: 'allow', updatedInput: input };
}
return { behavior: 'deny', message: 'User denied the operation' };
};
const result = query({
prompt: 'Create a new file',
options: {
canUseTool,
},
});
```
### With MCP Servers
```typescript
import { query } from '@qwen-code/sdk-typescript';
const result = query({
prompt: 'Use the custom tool from my MCP server',
options: {
mcpServers: {
'my-server': {
command: 'node',
args: ['path/to/mcp-server.js'],
env: { PORT: '3000' },
},
},
},
});
```
### Abort a Query
```typescript
import { query, isAbortError } from '@qwen-code/sdk-typescript';
const abortController = new AbortController();
const result = query({
prompt: 'Long running task...',
options: {
abortController,
},
});
// Abort after 5 seconds
setTimeout(() => abortController.abort(), 5000);
try {
for await (const message of result) {
console.log(message);
}
} catch (error) {
if (isAbortError(error)) {
console.log('Query was aborted');
} else {
throw error;
}
}
```
## Error Handling
The SDK provides an `AbortError` class for handling aborted queries:
```typescript
import { AbortError, isAbortError } from '@qwen-code/sdk-typescript';
try {
// ... query operations
} catch (error) {
if (isAbortError(error)) {
// Handle abort
} else {
// Handle other errors
}
}
```
## License
Apache-2.0 - see [LICENSE](./LICENSE) for details.

View File

@@ -16,8 +16,7 @@
},
"files": [
"dist",
"README.md",
"LICENSE"
"README.md"
],
"scripts": {
"build": "node scripts/build.js",

View File

@@ -81,15 +81,13 @@ await esbuild.build({
treeShaking: true,
});
const filesToCopy = ['README.md', 'LICENSE'];
for (const file of filesToCopy) {
const sourcePath = join(rootDir, '..', '..', file);
const targetPath = join(rootDir, 'dist', file);
if (existsSync(sourcePath)) {
try {
cpSync(sourcePath, targetPath);
} catch (error) {
console.warn(`Could not copy ${file}:`, error.message);
}
// Copy LICENSE from root directory to dist
const licenseSource = join(rootDir, '..', '..', 'LICENSE');
const licenseTarget = join(rootDir, 'dist', 'LICENSE');
if (existsSync(licenseSource)) {
try {
cpSync(licenseSource, licenseTarget);
} catch (error) {
console.warn('Could not copy LICENSE:', error.message);
}
}

View File

@@ -0,0 +1,353 @@
#!/usr/bin/env node
/**
* @license
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/
import { execSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PACKAGE_NAME = '@qwen-code/sdk-typescript';
const TAG_PREFIX = 'sdk-typescript-v';
function readJson(filePath) {
return JSON.parse(readFileSync(filePath, 'utf-8'));
}
function getArgs() {
const args = {};
process.argv.slice(2).forEach((arg) => {
if (arg.startsWith('--')) {
const [key, value] = arg.substring(2).split('=');
args[key] = value === undefined ? true : value;
}
});
return args;
}
function getVersionFromNPM(distTag) {
const command = `npm view ${PACKAGE_NAME} version --tag=${distTag}`;
try {
return execSync(command).toString().trim();
} catch (error) {
console.error(
`Failed to get NPM version for dist-tag "${distTag}": ${error.message}`,
);
return '';
}
}
function getAllVersionsFromNPM() {
const command = `npm view ${PACKAGE_NAME} versions --json`;
try {
const versionsJson = execSync(command).toString().trim();
const result = JSON.parse(versionsJson);
// npm returns a string if there's only one version, array otherwise
return Array.isArray(result) ? result : [result];
} catch (error) {
console.error(`Failed to get all NPM versions: ${error.message}`);
return [];
}
}
function isVersionDeprecated(version) {
const command = `npm view ${PACKAGE_NAME}@${version} deprecated`;
try {
const output = execSync(command).toString().trim();
return output.length > 0;
} catch (error) {
console.error(
`Failed to check deprecation status for ${version}: ${error.message}`,
);
return false;
}
}
function semverCompare(a, b) {
const parseVersion = (v) => {
const [main, prerelease] = v.split('-');
const [major, minor, patch] = main.split('.').map(Number);
return { major, minor, patch, prerelease: prerelease || '' };
};
const va = parseVersion(a);
const vb = parseVersion(b);
if (va.major !== vb.major) return va.major - vb.major;
if (va.minor !== vb.minor) return va.minor - vb.minor;
if (va.patch !== vb.patch) return va.patch - vb.patch;
// Handle prerelease comparison
if (!va.prerelease && vb.prerelease) return 1; // stable > prerelease
if (va.prerelease && !vb.prerelease) return -1; // prerelease < stable
if (va.prerelease && vb.prerelease) {
return va.prerelease.localeCompare(vb.prerelease);
}
return 0;
}
function detectRollbackAndGetBaseline(npmDistTag) {
const distTagVersion = getVersionFromNPM(npmDistTag);
if (!distTagVersion) return { baseline: '', isRollback: false };
const allVersions = getAllVersionsFromNPM();
if (allVersions.length === 0)
return { baseline: distTagVersion, isRollback: false };
let matchingVersions;
if (npmDistTag === 'latest') {
matchingVersions = allVersions.filter((v) => !v.includes('-'));
} else if (npmDistTag === 'preview') {
matchingVersions = allVersions.filter((v) => v.includes('-preview'));
} else if (npmDistTag === 'nightly') {
matchingVersions = allVersions.filter((v) => v.includes('-nightly'));
} else {
return { baseline: distTagVersion, isRollback: false };
}
if (matchingVersions.length === 0)
return { baseline: distTagVersion, isRollback: false };
matchingVersions.sort((a, b) => -semverCompare(a, b));
let highestExistingVersion = '';
for (const version of matchingVersions) {
if (!isVersionDeprecated(version)) {
highestExistingVersion = version;
break;
} else {
console.error(`Ignoring deprecated version: ${version}`);
}
}
if (!highestExistingVersion) {
highestExistingVersion = distTagVersion;
}
const isRollback = semverCompare(highestExistingVersion, distTagVersion) > 0;
return {
baseline: isRollback ? highestExistingVersion : distTagVersion,
isRollback,
distTagVersion,
highestExistingVersion,
};
}
function doesVersionExist(version) {
// Check NPM
try {
const command = `npm view ${PACKAGE_NAME}@${version} version 2>/dev/null`;
const output = execSync(command).toString().trim();
if (output === version) {
console.error(`Version ${version} already exists on NPM.`);
return true;
}
} catch (_error) {
// This is expected if the version doesn't exist.
}
// Check Git tags
try {
const command = `git tag -l '${TAG_PREFIX}${version}'`;
const tagOutput = execSync(command).toString().trim();
if (tagOutput === `${TAG_PREFIX}${version}`) {
console.error(`Git tag ${TAG_PREFIX}${version} already exists.`);
return true;
}
} catch (error) {
console.error(`Failed to check git tags for conflicts: ${error.message}`);
}
// Check GitHub releases
try {
const command = `gh release view "${TAG_PREFIX}${version}" --json tagName --jq .tagName 2>/dev/null`;
const output = execSync(command).toString().trim();
if (output === `${TAG_PREFIX}${version}`) {
console.error(`GitHub release ${TAG_PREFIX}${version} already exists.`);
return true;
}
} catch (error) {
const isExpectedNotFound =
error.message.includes('release not found') ||
error.message.includes('Not Found') ||
error.message.includes('not found') ||
error.status === 1;
if (!isExpectedNotFound) {
console.error(
`Failed to check GitHub releases for conflicts: ${error.message}`,
);
}
}
return false;
}
function getAndVerifyTags(npmDistTag) {
const rollbackInfo = detectRollbackAndGetBaseline(npmDistTag);
const baselineVersion = rollbackInfo.baseline;
if (!baselineVersion) {
// First release for this dist-tag, use package.json version as baseline
const packageJson = readJson(join(__dirname, '..', 'package.json'));
return {
latestVersion: packageJson.version.split('-')[0],
latestTag: `v${packageJson.version.split('-')[0]}`,
};
}
if (rollbackInfo.isRollback) {
console.error(
`Rollback detected! NPM ${npmDistTag} tag is ${rollbackInfo.distTagVersion}, but using ${baselineVersion} as baseline for next version calculation.`,
);
}
return {
latestVersion: baselineVersion,
latestTag: `v${baselineVersion}`,
};
}
function getLatestStableReleaseTag() {
try {
const { latestTag } = getAndVerifyTags('latest');
return latestTag;
} catch (error) {
console.error(
`Failed to determine latest stable release tag: ${error.message}`,
);
return '';
}
}
function getNightlyVersion() {
const packageJson = readJson(join(__dirname, '..', 'package.json'));
const baseVersion = packageJson.version.split('-')[0];
const date = new Date().toISOString().slice(0, 10).replace(/-/g, '');
const gitShortHash = execSync('git rev-parse --short HEAD').toString().trim();
const releaseVersion = `${baseVersion}-nightly.${date}.${gitShortHash}`;
return {
releaseVersion,
npmTag: 'nightly',
};
}
function validateVersion(version, format, name) {
const versionRegex = {
'X.Y.Z': /^\d+\.\d+\.\d+$/,
'X.Y.Z-preview.N': /^\d+\.\d+\.\d+-preview\.\d+$/,
};
if (!versionRegex[format] || !versionRegex[format].test(version)) {
throw new Error(
`Invalid ${name}: ${version}. Must be in ${format} format.`,
);
}
}
function getStableVersion(args) {
let releaseVersion;
if (args.stable_version_override) {
const overrideVersion = args.stable_version_override.replace(/^v/, '');
validateVersion(overrideVersion, 'X.Y.Z', 'stable_version_override');
releaseVersion = overrideVersion;
} else {
// Try to get from preview, fallback to package.json for first release
const { latestVersion: latestPreviewVersion } = getAndVerifyTags('preview');
releaseVersion = latestPreviewVersion.replace(/-preview.*/, '');
}
return {
releaseVersion,
npmTag: 'latest',
};
}
function getPreviewVersion(args) {
let releaseVersion;
if (args.preview_version_override) {
const overrideVersion = args.preview_version_override.replace(/^v/, '');
validateVersion(
overrideVersion,
'X.Y.Z-preview.N',
'preview_version_override',
);
releaseVersion = overrideVersion;
} else {
// Try to get from nightly, fallback to package.json for first release
const { latestVersion: latestNightlyVersion } = getAndVerifyTags('nightly');
releaseVersion =
latestNightlyVersion.replace(/-nightly.*/, '') + '-preview.0';
}
return {
releaseVersion,
npmTag: 'preview',
};
}
export function getVersion(options = {}) {
const args = { ...getArgs(), ...options };
const type = args.type || 'nightly';
let versionData;
switch (type) {
case 'nightly':
versionData = getNightlyVersion();
if (doesVersionExist(versionData.releaseVersion)) {
throw new Error(
`Version conflict! Nightly version ${versionData.releaseVersion} already exists.`,
);
}
break;
case 'stable':
versionData = getStableVersion(args);
break;
case 'preview':
versionData = getPreviewVersion(args);
break;
default:
throw new Error(`Unknown release type: ${type}`);
}
// For stable and preview versions, check for existence and increment if needed.
if (type === 'stable' || type === 'preview') {
let releaseVersion = versionData.releaseVersion;
while (doesVersionExist(releaseVersion)) {
console.error(`Version ${releaseVersion} exists, incrementing.`);
if (releaseVersion.includes('-preview.')) {
const [version, prereleasePart] = releaseVersion.split('-');
const previewNumber = parseInt(prereleasePart.split('.')[1]);
releaseVersion = `${version}-preview.${previewNumber + 1}`;
} else {
const versionParts = releaseVersion.split('.');
const major = versionParts[0];
const minor = versionParts[1];
const patch = parseInt(versionParts[2]);
releaseVersion = `${major}.${minor}.${patch + 1}`;
}
}
versionData.releaseVersion = releaseVersion;
}
const result = {
releaseTag: `v${versionData.releaseVersion}`,
...versionData,
};
result.previousReleaseTag = getLatestStableReleaseTag();
return result;
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const version = JSON.stringify(getVersion(getArgs()), null, 2);
console.log(version);
}