refactor(sdk,vscode): extract CLI bundling to dedicated script

- Move CLI bundling logic from build.js to bundle-cli.js in SDK package
- Add bundle:cli script to SDK package.json lifecycle hooks
- Remove redundant Build and Bundle step from workflow (prepackage handles it)
- Add repo build step to prepackage.js to ensure workspace artifacts exist
- Fix Windows workspace symlink issue by running npm install from tmpdir
- Remove lint/typecheck from prepackage (handled elsewhere in CI)
This commit is contained in:
tanzhenxin 2026-01-24 11:35:33 +08:00
parent 510610c575
commit 3d6fe23c3b
5 changed files with 103 additions and 46 deletions

View file

@ -91,35 +91,3 @@ if (existsSync(licenseSource)) {
console.warn('Could not copy LICENSE:', error.message);
}
}
console.log('Bundling CLI into SDK package...');
const repoRoot = join(rootDir, '..', '..');
const rootDistDir = join(repoRoot, 'dist');
if (!existsSync(rootDistDir) || !existsSync(join(rootDistDir, 'cli.js'))) {
console.log('Building CLI bundle...');
try {
execSync('npm run bundle', { stdio: 'inherit', cwd: repoRoot });
} catch (error) {
console.error('Failed to build CLI bundle:', error.message);
throw error;
}
}
const cliDistDir = join(rootDir, 'dist', 'cli');
mkdirSync(cliDistDir, { recursive: true });
console.log('Copying CLI bundle...');
cpSync(join(rootDistDir, 'cli.js'), join(cliDistDir, 'cli.js'));
const vendorSource = join(rootDistDir, 'vendor');
if (existsSync(vendorSource)) {
cpSync(vendorSource, join(cliDistDir, 'vendor'), { recursive: true });
}
const localesSource = join(rootDistDir, 'locales');
if (existsSync(localesSource)) {
cpSync(localesSource, join(cliDistDir, 'locales'), { recursive: true });
}
console.log('CLI bundle copied successfully to SDK package');

View file

@ -0,0 +1,83 @@
/**
* @license
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Bundles/copies the Qwen Code CLI into the SDK package dist/ so consumers
* don't need a separate CLI install.
*
* This is intentionally NOT part of the SDK "build" step; it is a packaging
* concern (run via npm lifecycle hooks like prepack/prepublishOnly).
*/
import { spawnSync } from 'node:child_process';
import { cpSync, existsSync, mkdirSync, rmSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const sdkRoot = join(__dirname, '..');
const repoRoot = join(sdkRoot, '..', '..');
function run(cmd, args, opts = {}) {
const res = spawnSync(cmd, args, {
stdio: 'inherit',
shell: process.platform === 'win32',
...opts,
});
if (res.error) throw res.error;
if (typeof res.status === 'number' && res.status !== 0) {
throw new Error(
`Command failed (${res.status}): ${cmd} ${args.map((a) => JSON.stringify(a)).join(' ')}`,
);
}
}
function ensureRootBundle() {
const rootDistDir = join(repoRoot, 'dist');
const rootCliJs = join(rootDistDir, 'cli.js');
if (existsSync(rootCliJs)) return;
console.log(
'[sdk prepack] Root CLI bundle missing; running `npm run bundle`',
);
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
run(npm, ['run', 'bundle'], { cwd: repoRoot });
}
function main() {
ensureRootBundle();
const rootDistDir = join(repoRoot, 'dist');
const rootCliJs = join(rootDistDir, 'cli.js');
const cliDistDir = join(sdkRoot, 'dist', 'cli');
if (!existsSync(join(sdkRoot, 'dist'))) {
throw new Error(
'[sdk prepack] SDK dist/ not found. Run `npm run build` in packages/sdk-typescript first.',
);
}
rmSync(cliDistDir, { recursive: true, force: true });
mkdirSync(cliDistDir, { recursive: true });
console.log('[sdk prepack] Copying CLI bundle into SDK dist/...');
cpSync(rootCliJs, join(cliDistDir, 'cli.js'));
const vendorSource = join(rootDistDir, 'vendor');
if (existsSync(vendorSource)) {
cpSync(vendorSource, join(cliDistDir, 'vendor'), { recursive: true });
}
const localesSource = join(rootDistDir, 'locales');
if (existsSync(localesSource)) {
cpSync(localesSource, join(cliDistDir, 'locales'), { recursive: true });
}
console.log('[sdk prepack] CLI bundle copied successfully');
}
main();