diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 75e4b39c..beba8155 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -163,9 +163,9 @@ jobs: echo "Dry run enabled. Skipping push." fi - - name: 'Build and Prepare Packages' + - name: 'Build Bundle and Prepare Package' run: |- - npm run build:packages + npm run bundle npm run prepare:package - name: 'Configure npm for publishing' @@ -175,20 +175,10 @@ jobs: registry-url: 'https://registry.npmjs.org' scope: '@qwen-code' - - name: 'Publish @qwen-code/qwen-code-core' - run: |- - npm publish --workspace=@qwen-code/qwen-code-core --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: 'Install latest core package' - if: |- - ${{ steps.vars.outputs.is_dry_run == 'false' }} - run: 'npm install @qwen-code/qwen-code-core@${{ steps.version.outputs.RELEASE_VERSION }} --workspace=@qwen-code/qwen-code --save-exact' - - name: 'Publish @qwen-code/qwen-code' + working-directory: dist run: |- - npm publish --workspace=@qwen-code/qwen-code --access public --tag=${{ steps.version.outputs.NPM_TAG }} ${{ steps.vars.outputs.is_dry_run == 'true' && '--dry-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 }}' @@ -202,7 +192,7 @@ jobs: PREVIOUS_TAG: '${{ steps.version.outputs.PREVIOUS_TAG }}' run: |- gh release create "${RELEASE_TAG}" \ - bundle/gemini.js \ + dist/cli.js \ --target "$RELEASE_BRANCH" \ --title "Release ${RELEASE_TAG}" \ --notes-start-tag "$PREVIOUS_TAG" \ diff --git a/esbuild.config.js b/esbuild.config.js index 3f1644a6..7d38c2a7 100644 --- a/esbuild.config.js +++ b/esbuild.config.js @@ -30,16 +30,24 @@ const external = [ '@lydell/node-pty-linux-x64', '@lydell/node-pty-win32-arm64', '@lydell/node-pty-win32-x64', + 'tiktoken', ]; esbuild .build({ entryPoints: ['packages/cli/index.ts'], bundle: true, - outfile: 'bundle/gemini.js', + outfile: 'dist/cli.js', platform: 'node', format: 'esm', + target: 'node20', external, + packages: 'bundle', + inject: [path.resolve(__dirname, 'scripts/esbuild-shims.js')], + banner: { + js: `// Force strict mode and setup for ESM +"use strict";`, + }, alias: { 'is-in-ci': path.resolve( __dirname, @@ -48,17 +56,20 @@ esbuild }, define: { 'process.env.CLI_VERSION': JSON.stringify(pkg.version), - }, - banner: { - js: `import { createRequire } from 'module'; const require = createRequire(import.meta.url); globalThis.__filename = require('url').fileURLToPath(import.meta.url); globalThis.__dirname = require('path').dirname(globalThis.__filename);`, + // Make global available for compatibility + global: 'globalThis', }, loader: { '.node': 'file' }, metafile: true, write: true, + keepNames: true, }) .then(({ metafile }) => { if (process.env.DEV === 'true') { - writeFileSync('./bundle/esbuild.json', JSON.stringify(metafile, null, 2)); + writeFileSync('./dist/esbuild.json', JSON.stringify(metafile, null, 2)); } }) - .catch(() => process.exit(1)); + .catch((error) => { + console.error('esbuild build failed:', error); + process.exitCode = 1; + }); diff --git a/eslint.config.js b/eslint.config.js index e9b17327..7b4f502f 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -12,24 +12,12 @@ import prettierConfig from 'eslint-config-prettier'; import importPlugin from 'eslint-plugin-import'; import vitest from '@vitest/eslint-plugin'; import globals from 'globals'; -import licenseHeader from 'eslint-plugin-license-header'; -import path from 'node:path'; -import url from 'node:url'; - -// --- ESM way to get __dirname --- -const __filename = url.fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); -// --- --- - -// Determine the monorepo root (assuming eslint.config.js is at the root) -const projectRoot = __dirname; export default tseslint.config( { // Global ignores ignores: [ 'node_modules/*', - 'eslint.config.js', 'packages/**/dist/**', 'bundle/**', 'package/bundle/**', @@ -222,6 +210,21 @@ export default tseslint.config( '@typescript-eslint/no-require-imports': 'off', }, }, + // extra settings for core package scripts + { + files: ['packages/core/scripts/**/*.js'], + languageOptions: { + globals: { + ...globals.node, + process: 'readonly', + console: 'readonly', + }, + }, + rules: { + 'no-restricted-syntax': 'off', + '@typescript-eslint/no-require-imports': 'off', + }, + }, // Prettier config must be last prettierConfig, // extra settings for scripts that we run directly with node diff --git a/package-lock.json b/package-lock.json index 006aadf1..08a8d162 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1501,28 +1501,6 @@ "node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0" } }, - "node_modules/@joshua.litt/get-ripgrep": { - "version": "0.0.2", - "resolved": "https://registry.npmjs.org/@joshua.litt/get-ripgrep/-/get-ripgrep-0.0.2.tgz", - "integrity": "sha512-cSHA+H+HEkOXeiCxrNvGj/pgv2Y0bfp4GbH3R87zr7Vob2pDUZV3BkUL9ucHMoDFID4GteSy5z5niN/lF9QeuQ==", - "dependencies": { - "@lvce-editor/verror": "^1.6.0", - "execa": "^9.5.2", - "extract-zip": "^2.0.1", - "fs-extra": "^11.3.0", - "got": "^14.4.5", - "path-exists": "^5.0.0", - "xdg-basedir": "^5.1.0" - } - }, - "node_modules/@joshua.litt/get-ripgrep/node_modules/path-exists": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-5.0.0.tgz", - "integrity": "sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ==", - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - } - }, "node_modules/@jridgewell/gen-mapping": { "version": "0.3.8", "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.8.tgz", @@ -1720,12 +1698,6 @@ "integrity": "sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==", "license": "MIT" }, - "node_modules/@lvce-editor/verror": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/@lvce-editor/verror/-/verror-1.7.0.tgz", - "integrity": "sha512-+LGuAEIC2L7pbvkyAQVWM2Go0dAy+UWEui28g07zNtZsCBhm+gusBK8PNwLJLV5Jay+TyUYuwLIbJdjLLzqEBg==", - "license": "MIT" - }, "node_modules/@lydell/node-pty": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@lydell/node-pty/-/node-pty-1.1.0.tgz", @@ -3084,12 +3056,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@sec-ant/readable-stream": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/@sec-ant/readable-stream/-/readable-stream-0.4.1.tgz", - "integrity": "sha512-831qok9r2t8AlxLko40y2ebgSDhenenCatLVeW/uBtnHPyhHOvG0C7TvfgecV+wHzIm5KUICgzmVpWS+IMEAeg==", - "license": "MIT" - }, "node_modules/@secretlint/config-creator": { "version": "10.2.2", "resolved": "https://registry.npmjs.org/@secretlint/config-creator/-/config-creator-10.2.2.tgz", @@ -3308,42 +3274,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@sindresorhus/is": { - "version": "7.0.2", - "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-7.0.2.tgz", - "integrity": "sha512-d9xRovfKNz1SKieM0qJdO+PQonjnnIfSNWfHYnBSJ9hkjm0ZPw6HlxscDXYstp3z+7V2GOFHc+J0CYrYTjqCJw==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sindresorhus/is?sponsor=1" - } - }, - "node_modules/@sindresorhus/merge-streams": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@sindresorhus/merge-streams/-/merge-streams-4.0.0.tgz", - "integrity": "sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/@szmarczak/http-timer": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-5.0.1.tgz", - "integrity": "sha512-+PmQX0PiAYPMeVYe237LJAYvOMYW1j2rH5YROyS3b4CTVJum34HfRvKvAzozHAQG0TnHNdUfY9nCeUyRAs//cw==", - "license": "MIT", - "dependencies": { - "defer-to-connect": "^2.0.1" - }, - "engines": { - "node": ">=14.16" - } - }, "node_modules/@testing-library/dom": { "version": "10.4.1", "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.1.tgz", @@ -3679,12 +3609,6 @@ "integrity": "sha512-pUY3cKH/Nm2yYrEmDlPR1mR7yszjGx4DrwPjQ702C4/D5CwHuZTgZdIdwPkRbcuhs7BAh2L5rg3CL5cbRiGTCQ==", "license": "MIT" }, - "node_modules/@types/http-cache-semantics": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz", - "integrity": "sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA==", - "license": "MIT" - }, "node_modules/@types/http-errors": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.5.tgz", @@ -5685,33 +5609,6 @@ "node": ">=8" } }, - "node_modules/cacheable-lookup": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-7.0.0.tgz", - "integrity": "sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w==", - "license": "MIT", - "engines": { - "node": ">=14.16" - } - }, - "node_modules/cacheable-request": { - "version": "12.0.1", - "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-12.0.1.tgz", - "integrity": "sha512-Yo9wGIQUaAfIbk+qY0X4cDQgCosecfBe3V9NSyeY4qPC2SAkbCS4Xj79VP8WOzitpJUZKc/wsRCYF5ariDIwkg==", - "license": "MIT", - "dependencies": { - "@types/http-cache-semantics": "^4.0.4", - "get-stream": "^9.0.1", - "http-cache-semantics": "^4.1.1", - "keyv": "^4.5.4", - "mimic-response": "^4.0.0", - "normalize-url": "^8.0.1", - "responselike": "^3.0.0" - }, - "engines": { - "node": ">=18" - } - }, "node_modules/call-bind": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.8.tgz", @@ -6632,7 +6529,9 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz", "integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==", + "dev": true, "license": "MIT", + "optional": true, "dependencies": { "mimic-response": "^3.1.0" }, @@ -6647,7 +6546,9 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz", "integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==", + "dev": true, "license": "MIT", + "optional": true, "engines": { "node": ">=10" }, @@ -6718,15 +6619,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/defer-to-connect": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/defer-to-connect/-/defer-to-connect-2.0.1.tgz", - "integrity": "sha512-4tvttepXG1VaYGrRibk5EwJd1t4udunSOVMdLSAL6mId1ix438oPwPZMALY41FCijukO1L0twNcGsdzS7dHgDg==", - "license": "MIT", - "engines": { - "node": ">=10" - } - }, "node_modules/define-data-property": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", @@ -7805,44 +7697,6 @@ "node": ">=20.0.0" } }, - "node_modules/execa": { - "version": "9.6.0", - "resolved": "https://registry.npmjs.org/execa/-/execa-9.6.0.tgz", - "integrity": "sha512-jpWzZ1ZhwUmeWRhS7Qv3mhpOhLfwI+uAX4e5fOcXqwMR7EcJ0pj2kV1CVzHVMX/LphnKWD3LObjZCoJ71lKpHw==", - "license": "MIT", - "dependencies": { - "@sindresorhus/merge-streams": "^4.0.0", - "cross-spawn": "^7.0.6", - "figures": "^6.1.0", - "get-stream": "^9.0.0", - "human-signals": "^8.0.1", - "is-plain-obj": "^4.1.0", - "is-stream": "^4.0.1", - "npm-run-path": "^6.0.0", - "pretty-ms": "^9.2.0", - "signal-exit": "^4.1.0", - "strip-final-newline": "^4.0.0", - "yoctocolors": "^2.1.1" - }, - "engines": { - "node": "^18.19.0 || >=20.5.0" - }, - "funding": { - "url": "https://github.com/sindresorhus/execa?sponsor=1" - } - }, - "node_modules/execa/node_modules/is-stream": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-4.0.1.tgz", - "integrity": "sha512-Dnz92NInDqYckGEUJv689RbRiTSEHCQ7wOVeALbkOz999YpqT46yMRIGtSNl2iCL1waAZSx40+h59NV/EwzV/A==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/expand-template": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", @@ -8087,21 +7941,6 @@ "pend": "~1.2.0" } }, - "node_modules/figures": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/figures/-/figures-6.1.0.tgz", - "integrity": "sha512-d+l3qxjSesT4V7v2fh+QnmFnUWv9lSpjarhShNTgBOfA0ttejbQUAlHLitbjkoRiDulW0OPoQPYIGhIC8ohejg==", - "license": "MIT", - "dependencies": { - "is-unicode-supported": "^2.0.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/file-entry-cache": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz", @@ -8273,15 +8112,6 @@ "node": ">= 6" } }, - "node_modules/form-data-encoder": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/form-data-encoder/-/form-data-encoder-4.1.0.tgz", - "integrity": "sha512-G6NsmEW15s0Uw9XnCg+33H3ViYRyiM0hMrMhhqQOR8NFc5GhYrI+6I3u7OTw7b91J2g8rtvMBZJDbcGb2YUniw==", - "license": "MIT", - "engines": { - "node": ">= 18" - } - }, "node_modules/form-data/node_modules/mime-types": { "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", @@ -8331,6 +8161,7 @@ "version": "11.3.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.3.1.tgz", "integrity": "sha512-eXvGGwZ5CL17ZSwHWd3bbgk7UUpF6IFHtP57NYYakPvHOs8GDgDe5KJI36jIJzDkJ6eJjuzRA8eBQb6SkKue0g==", + "dev": true, "license": "MIT", "dependencies": { "graceful-fs": "^4.2.0", @@ -8345,6 +8176,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz", "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==", + "dev": true, "license": "MIT", "engines": { "node": ">= 10.0.0" @@ -8499,34 +8331,6 @@ "node": ">= 0.4" } }, - "node_modules/get-stream": { - "version": "9.0.1", - "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-9.0.1.tgz", - "integrity": "sha512-kVCxPF3vQM/N0B1PmoqVUqgHP+EeVjmZSQn+1oCRPxd2P21P2F19lIgbR3HBosbB1PUhOAoctJnfEn2GbN2eZA==", - "license": "MIT", - "dependencies": { - "@sec-ant/readable-stream": "^0.4.1", - "is-stream": "^4.0.1" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/get-stream/node_modules/is-stream": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-4.0.1.tgz", - "integrity": "sha512-Dnz92NInDqYckGEUJv689RbRiTSEHCQ7wOVeALbkOz999YpqT46yMRIGtSNl2iCL1waAZSx40+h59NV/EwzV/A==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/get-symbol-description": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/get-symbol-description/-/get-symbol-description-1.1.0.tgz", @@ -8807,43 +8611,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/got": { - "version": "14.4.8", - "resolved": "https://registry.npmjs.org/got/-/got-14.4.8.tgz", - "integrity": "sha512-vxwU4HuR0BIl+zcT1LYrgBjM+IJjNElOjCzs0aPgHorQyr/V6H6Y73Sn3r3FOlUffvWD+Q5jtRuGWaXkU8Jbhg==", - "license": "MIT", - "dependencies": { - "@sindresorhus/is": "^7.0.1", - "@szmarczak/http-timer": "^5.0.1", - "cacheable-lookup": "^7.0.0", - "cacheable-request": "^12.0.1", - "decompress-response": "^6.0.0", - "form-data-encoder": "^4.0.2", - "http2-wrapper": "^2.2.1", - "lowercase-keys": "^3.0.0", - "p-cancelable": "^4.0.1", - "responselike": "^3.0.0", - "type-fest": "^4.26.1" - }, - "engines": { - "node": ">=20" - }, - "funding": { - "url": "https://github.com/sindresorhus/got?sponsor=1" - } - }, - "node_modules/got/node_modules/type-fest": { - "version": "4.41.0", - "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-4.41.0.tgz", - "integrity": "sha512-TeTSQ6H5YHvpqVwBRcnLDCBnDOHWYu7IvGbHT6N8AOymcr9PJGjc1GTtiWZTYg0NCgYwvnYWEkVChQAr9bjfwA==", - "license": "(MIT OR CC0-1.0)", - "engines": { - "node": ">=16" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/graceful-fs": { "version": "4.2.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", @@ -9076,12 +8843,6 @@ "entities": "^4.4.0" } }, - "node_modules/http-cache-semantics": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.2.0.tgz", - "integrity": "sha512-dTxcvPXqPvXBQpq5dUr6mEMJX4oIEFv6bwom3FDwKRDsuIjjJGANqhBuoAn9c1RQJIdAKav33ED65E2ys+87QQ==", - "license": "BSD-2-Clause" - }, "node_modules/http-errors": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", @@ -9121,19 +8882,6 @@ "node": ">= 14" } }, - "node_modules/http2-wrapper": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/http2-wrapper/-/http2-wrapper-2.2.1.tgz", - "integrity": "sha512-V5nVw1PAOgfI3Lmeaj2Exmeg7fenjhRUgz1lPSezy1CuhPYbgQtbQj4jZfEAEMlaL+vupsvhjqCyjzob0yxsmQ==", - "license": "MIT", - "dependencies": { - "quick-lru": "^5.1.1", - "resolve-alpn": "^1.2.0" - }, - "engines": { - "node": ">=10.19.0" - } - }, "node_modules/https-proxy-agent": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", @@ -9147,15 +8895,6 @@ "node": ">= 14" } }, - "node_modules/human-signals": { - "version": "8.0.1", - "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-8.0.1.tgz", - "integrity": "sha512-eKCa6bwnJhvxj14kZk5NCPc6Hb6BdsU9DZcOnmQKSnO1VKrfV0zCvtttPZUsBvjmNDn8rpcJfpwSYnHBjc95MQ==", - "license": "Apache-2.0", - "engines": { - "node": ">=18.18.0" - } - }, "node_modules/husky": { "version": "9.1.7", "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.7.tgz", @@ -9967,18 +9706,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/is-plain-obj": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-4.1.0.tgz", - "integrity": "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==", - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/is-potential-custom-element-name": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz", @@ -10103,18 +9830,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-unicode-supported": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-2.1.0.tgz", - "integrity": "sha512-mE00Gnza5EEB3Ds0HfMyllZzbBrmLOX3vfWoj9A9PEnTfratQ/BcaJOuMhnkhjXvb2+FkY3VuHqtAGpTPmglFQ==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/is-weakmap": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/is-weakmap/-/is-weakmap-2.0.2.tgz", @@ -10392,6 +10107,7 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==", + "dev": true, "license": "MIT" }, "node_modules/json-parse-better-errors": { @@ -10448,6 +10164,7 @@ "version": "6.2.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.2.0.tgz", "integrity": "sha512-FGuPw30AdOIUTRMC2OMRtQV+jkVj2cfPqSeWXv1NEAJ1qZ5zb1X6z1mFhbfOB/iy3ssJCD+3KuZ8r8C3uVFlAg==", + "dev": true, "license": "MIT", "dependencies": { "universalify": "^2.0.0" @@ -10460,6 +10177,7 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz", "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==", + "dev": true, "license": "MIT", "engines": { "node": ">= 10.0.0" @@ -10574,6 +10292,7 @@ "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", "integrity": "sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==", + "dev": true, "license": "MIT", "dependencies": { "json-buffer": "3.0.1" @@ -11053,18 +10772,6 @@ "dev": true, "license": "MIT" }, - "node_modules/lowercase-keys": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-3.0.0.tgz", - "integrity": "sha512-ozCC6gdQ+glXOQsveKD0YsDy8DSQFjDTz4zyzEHNV5+JP5D62LmfDZ6o1cycFx9ouG940M5dE8C8CTewdj2YWQ==", - "license": "MIT", - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/lowlight": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/lowlight/-/lowlight-3.3.0.tgz", @@ -11305,18 +11012,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/mimic-response": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-4.0.0.tgz", - "integrity": "sha512-e5ISH9xMYU0DzrT+jl8q2ze9D6eWBto+I8CNpe+VI+K2J/F/k3PdkdTdz4wvGVH4NTpo+NRYTVIuMQEMMcsLqg==", - "license": "MIT", - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -11657,18 +11352,6 @@ "node": ">=0.10.0" } }, - "node_modules/normalize-url": { - "version": "8.0.2", - "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-8.0.2.tgz", - "integrity": "sha512-Ee/R3SyN4BuynXcnTaekmaVdbDAEiNrHqjQIA37mHU8G9pf7aaAD4ZX3XjBLo6rsdcxA/gtkcNYZLt30ACgynw==", - "license": "MIT", - "engines": { - "node": ">=14.16" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/npm-normalize-package-bin": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-4.0.0.tgz", @@ -11950,46 +11633,6 @@ "node": "^18.17.0 || >=20.5.0" } }, - "node_modules/npm-run-path": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-6.0.0.tgz", - "integrity": "sha512-9qny7Z9DsQU8Ou39ERsPU4OZQlSTP47ShQzuKZ6PRXpYLtIFgl/DEBYEXKlvcEa+9tHVcK8CF81Y2V72qaZhWA==", - "license": "MIT", - "dependencies": { - "path-key": "^4.0.0", - "unicorn-magic": "^0.3.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/npm-run-path/node_modules/path-key": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/path-key/-/path-key-4.0.0.tgz", - "integrity": "sha512-haREypq7xkM7ErfgIyA0z+Bj4AGKlMSdlQE2jvJo6huWD1EdkKYV+G/T4nq0YEF2vgTT8kqMFKo1uHn950r4SQ==", - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/npm-run-path/node_modules/unicorn-magic": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/unicorn-magic/-/unicorn-magic-0.3.0.tgz", - "integrity": "sha512-+QBBXBCvifc56fsbuxZQ6Sic3wqqc3WWaqxs58gvJrcOuN83HGTCwz3oS5phzU9LthRNE9VrJCFCLUgHeeFnfA==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/nth-check": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-2.1.1.tgz", @@ -12255,15 +11898,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/p-cancelable": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-4.0.1.tgz", - "integrity": "sha512-wBowNApzd45EIKdO1LaU+LrMBwAcjfPaYtVzV3lmfM3gf8Z4CHZsiIqlM8TZZ8okYvh5A1cP6gTfCRQtwUpaUg==", - "license": "MIT", - "engines": { - "node": ">=14.16" - } - }, "node_modules/p-limit": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", @@ -12375,18 +12009,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/parse-ms": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/parse-ms/-/parse-ms-4.0.0.tgz", - "integrity": "sha512-TXfryirbmq34y8QBwgqCVLi+8oA3oWx2eAnSn62ITyEhEYaWRlVZ2DvMM9eZbMs/RfxPu/PK/aBLyGj4IrqMHw==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/parse-semver": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/parse-semver/-/parse-semver-1.1.1.tgz", @@ -12773,21 +12395,6 @@ "dev": true, "license": "MIT" }, - "node_modules/pretty-ms": { - "version": "9.2.0", - "resolved": "https://registry.npmjs.org/pretty-ms/-/pretty-ms-9.2.0.tgz", - "integrity": "sha512-4yf0QO/sllf/1zbZWYnvWw3NxCQwLXKzIj0G849LSufP15BXKM0rbD2Z3wVnkMfjdn/CB0Dpp444gYAACdsplg==", - "license": "MIT", - "dependencies": { - "parse-ms": "^4.0.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", @@ -12967,18 +12574,6 @@ ], "license": "MIT" }, - "node_modules/quick-lru": { - "version": "5.1.1", - "resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-5.1.1.tgz", - "integrity": "sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==", - "license": "MIT", - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/qwen-code-vscode-ide-companion": { "resolved": "packages/vscode-ide-companion", "link": true @@ -13431,12 +13026,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/resolve-alpn": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/resolve-alpn/-/resolve-alpn-1.2.1.tgz", - "integrity": "sha512-0a1F4l73/ZFZOakJnQ3FvkJ2+gSTQWz/r2KE5OdDY0TxPm5h4GkqkWWfM47T7HsbnOtcJVEF4epCVy6u7Q3K+g==", - "license": "MIT" - }, "node_modules/resolve-from": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz", @@ -13457,21 +13046,6 @@ "url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1" } }, - "node_modules/responselike": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/responselike/-/responselike-3.0.0.tgz", - "integrity": "sha512-40yHxbNcl2+rzXvZuVkrYohathsSJlMTXKryG5y8uciHv1+xDLHQpgjG64JUO9nrEq2jGLH6IZ8BcZyw3wrweg==", - "license": "MIT", - "dependencies": { - "lowercase-keys": "^3.0.0" - }, - "engines": { - "node": ">=14.16" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/restore-cursor": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-4.0.0.tgz", @@ -14507,18 +14081,6 @@ "node": ">=4" } }, - "node_modules/strip-final-newline": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/strip-final-newline/-/strip-final-newline-4.0.0.tgz", - "integrity": "sha512-aulFJcD6YK8V1G7iRB5tigAP4TsHBZZrOV8pjV++zdUwmeV8uzbY7yn6h9MswN62adStNZFuCIx4haBnRuMDaw==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", @@ -16366,18 +15928,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/yoctocolors": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/yoctocolors/-/yoctocolors-2.1.1.tgz", - "integrity": "sha512-GQHQqAopRhwU8Kt1DDM8NjibDXHC8eoh1erhGAJPEyveY9qqVeXvVikNKrDz69sHowPMorbPUrH/mx8c50eiBQ==", - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/yoctocolors-cjs": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yoctocolors-cjs/-/yoctocolors-cjs-2.1.2.tgz", @@ -16590,9 +16140,9 @@ "packages/core": { "name": "@qwen-code/qwen-code-core", "version": "0.0.14", + "hasInstallScript": true, "dependencies": { "@google/genai": "1.16.0", - "@joshua.litt/get-ripgrep": "^0.0.2", "@modelcontextprotocol/sdk": "^1.11.0", "@opentelemetry/api": "^1.9.0", "@opentelemetry/exporter-logs-otlp-grpc": "^0.203.0", diff --git a/package.json b/package.json index 06887a46..5af5fd05 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "build:all": "npm run build && npm run build:sandbox && npm run build:vscode", "build:packages": "npm run build --workspaces", "build:sandbox": "node scripts/build_sandbox.js", - "bundle": "npm run generate && node esbuild.config.js && node scripts/copy_bundle_assets.js", + "bundle": "rm -rf dist && npm run generate && node esbuild.config.js && node scripts/copy_bundle_assets.js", "test": "npm run test --workspaces --if-present --parallel", "test:ci": "npm run test:ci --workspaces --if-present --parallel && npm run test:scripts", "test:scripts": "vitest run --config ./scripts/tests/vitest.config.ts", diff --git a/packages/cli/src/ui/commands/mcpCommand.ts b/packages/cli/src/ui/commands/mcpCommand.ts index 5b4efb42..2521e10c 100644 --- a/packages/cli/src/ui/commands/mcpCommand.ts +++ b/packages/cli/src/ui/commands/mcpCommand.ts @@ -20,6 +20,7 @@ import { MCPServerStatus, getErrorMessage, MCPOAuthTokenStorage, + MCPOAuthProvider, } from '@qwen-code/qwen-code-core'; import { appEvents, AppEvent } from '../../utils/events.js'; import { MessageType, type HistoryItemMcpStatus } from '../types.js'; @@ -93,9 +94,6 @@ const authCommand: SlashCommand = { Date.now(), ); - // Import dynamically to avoid circular dependencies - const { MCPOAuthProvider } = await import('@qwen-code/qwen-code-core'); - let oauthConfig = server.oauth; if (!oauthConfig) { oauthConfig = { enabled: false }; diff --git a/packages/core/package.json b/packages/core/package.json index 4846efbc..7bad466f 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -14,14 +14,16 @@ "format": "prettier --write .", "test": "vitest run", "test:ci": "vitest run", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "postinstall": "node scripts/postinstall.js" }, "files": [ - "dist" + "dist", + "vendor", + "scripts/postinstall.js" ], "dependencies": { "@google/genai": "1.16.0", - "@joshua.litt/get-ripgrep": "^0.0.2", "@modelcontextprotocol/sdk": "^1.11.0", "@opentelemetry/api": "^1.9.0", "@opentelemetry/exporter-logs-otlp-grpc": "^0.203.0", diff --git a/packages/core/scripts/postinstall.js b/packages/core/scripts/postinstall.js new file mode 100644 index 00000000..38f6d548 --- /dev/null +++ b/packages/core/scripts/postinstall.js @@ -0,0 +1,85 @@ +#!/usr/bin/env node + +/** + * @license + * Copyright 2025 Qwen + * SPDX-License-Identifier: Apache-2.0 + */ + +import { execSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import path from 'node:path'; +import fs from 'node:fs'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +// Get the package root directory +const packageRoot = path.join(__dirname, '..'); +const vendorDir = path.join(packageRoot, 'vendor', 'ripgrep'); + +/** + * Remove quarantine attribute and set executable permissions on macOS/Linux + */ +function setupRipgrepBinaries() { + if (!fs.existsSync(vendorDir)) { + console.log('Vendor directory not found, skipping ripgrep setup'); + return; + } + + const platform = process.platform; + const arch = process.arch; + + // Determine the binary directory based on platform and architecture + let binaryDir; + if (platform === 'darwin' || platform === 'linux') { + const archStr = arch === 'x64' || arch === 'arm64' ? arch : null; + if (archStr) { + binaryDir = path.join(vendorDir, `${archStr}-${platform}`); + } + } else if (platform === 'win32') { + // Windows doesn't need these fixes + return; + } + + if (!binaryDir || !fs.existsSync(binaryDir)) { + console.log( + `Binary directory not found for ${platform}-${arch}, skipping ripgrep setup`, + ); + return; + } + + const rgBinary = path.join(binaryDir, 'rg'); + + if (!fs.existsSync(rgBinary)) { + console.log(`Ripgrep binary not found at ${rgBinary}`); + return; + } + + try { + // Set executable permissions + fs.chmodSync(rgBinary, 0o755); + console.log(`āœ“ Set executable permissions on ${rgBinary}`); + + // On macOS, remove quarantine attribute + if (platform === 'darwin') { + try { + execSync(`xattr -d com.apple.quarantine "${rgBinary}"`, { + stdio: 'pipe', + }); + console.log(`āœ“ Removed quarantine attribute from ${rgBinary}`); + } catch (error) { + // Quarantine attribute might not exist, which is fine + if (error.message && !error.message.includes('No such xattr')) { + console.warn( + `Warning: Could not remove quarantine attribute: ${error.message}`, + ); + } + } + } + } catch (error) { + console.error(`Error setting up ripgrep binary: ${error.message}`); + } +} + +setupRipgrepBinaries(); diff --git a/packages/core/src/config/config.test.ts b/packages/core/src/config/config.test.ts index cc14d4a0..2f908f0c 100644 --- a/packages/core/src/config/config.test.ts +++ b/packages/core/src/config/config.test.ts @@ -38,7 +38,8 @@ vi.mock('fs', async (importOriginal) => { import { ShellTool } from '../tools/shell.js'; import { ReadFileTool } from '../tools/read-file.js'; import { GrepTool } from '../tools/grep.js'; -import { RipGrepTool, canUseRipgrep } from '../tools/ripGrep.js'; +import { canUseRipgrep } from '../utils/ripgrepUtils.js'; +import { RipGrepTool } from '../tools/ripGrep.js'; import { logRipgrepFallback } from '../telemetry/loggers.js'; import { RipgrepFallbackEvent } from '../telemetry/types.js'; import { ToolRegistry } from '../tools/tool-registry.js'; @@ -75,9 +76,11 @@ vi.mock('../tools/ls'); vi.mock('../tools/read-file'); vi.mock('../tools/grep.js'); vi.mock('../tools/ripGrep.js', () => ({ - canUseRipgrep: vi.fn(), RipGrepTool: class MockRipGrepTool {}, })); +vi.mock('../utils/ripgrepUtils.js', () => ({ + canUseRipgrep: vi.fn(), +})); vi.mock('../tools/glob'); vi.mock('../tools/edit'); vi.mock('../tools/shell'); diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index 4bc2b902..6ac472f1 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -49,7 +49,8 @@ import { LSTool } from '../tools/ls.js'; import { MemoryTool, setGeminiMdFilename } from '../tools/memoryTool.js'; import { ReadFileTool } from '../tools/read-file.js'; import { ReadManyFilesTool } from '../tools/read-many-files.js'; -import { canUseRipgrep, RipGrepTool } from '../tools/ripGrep.js'; +import { canUseRipgrep } from '../utils/ripgrepUtils.js'; +import { RipGrepTool } from '../tools/ripGrep.js'; import { ShellTool } from '../tools/shell.js'; import { SmartEditTool } from '../tools/smart-edit.js'; import { TaskTool } from '../tools/task.js'; diff --git a/packages/core/src/tools/ripGrep.test.ts b/packages/core/src/tools/ripGrep.test.ts index 7c47275b..62d5c8ea 100644 --- a/packages/core/src/tools/ripGrep.test.ts +++ b/packages/core/src/tools/ripGrep.test.ts @@ -14,7 +14,7 @@ import { type Mock, } from 'vitest'; import type { RipGrepToolParams } from './ripGrep.js'; -import { canUseRipgrep, RipGrepTool, ensureRgPath } from './ripGrep.js'; +import { RipGrepTool } from './ripGrep.js'; import path from 'node:path'; import fs from 'node:fs/promises'; import os, { EOL } from 'node:os'; @@ -22,24 +22,11 @@ import type { Config } from '../config/config.js'; import { createMockWorkspaceContext } from '../test-utils/mockWorkspaceContext.js'; import type { ChildProcess } from 'node:child_process'; import { spawn } from 'node:child_process'; -import { downloadRipGrep } from '@joshua.litt/get-ripgrep'; -import { fileExists } from '../utils/fileUtils.js'; +import { ensureRipgrepPath } from '../utils/ripgrepUtils.js'; -// Mock dependencies for canUseRipgrep -vi.mock('@joshua.litt/get-ripgrep', () => ({ - downloadRipGrep: vi.fn(), -})); -vi.mock('../utils/fileUtils.js', async (importOriginal) => { - const actual = await importOriginal(); - return { - ...actual, - fileExists: vi.fn(), - }; -}); -vi.mock('../config/storage.js', () => ({ - Storage: { - getGlobalBinDir: vi.fn().mockReturnValue('/mock/bin/dir'), - }, +// Mock ripgrepUtils +vi.mock('../utils/ripgrepUtils.js', () => ({ + ensureRipgrepPath: vi.fn(), })); // Mock child_process for ripgrep calls @@ -49,97 +36,6 @@ vi.mock('child_process', () => ({ const mockSpawn = vi.mocked(spawn); -describe('canUseRipgrep', () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - - it('should return true if ripgrep already exists', async () => { - (fileExists as Mock).mockResolvedValue(true); - const result = await canUseRipgrep(); - expect(result).toBe(true); - expect(fileExists).toHaveBeenCalledWith(path.join('/mock/bin/dir', 'rg')); - expect(downloadRipGrep).not.toHaveBeenCalled(); - }); - - it('should download ripgrep and return true if it does not exist initially', async () => { - (fileExists as Mock) - .mockResolvedValueOnce(false) - .mockResolvedValueOnce(true); - (downloadRipGrep as Mock).mockResolvedValue(undefined); - - const result = await canUseRipgrep(); - - expect(result).toBe(true); - expect(fileExists).toHaveBeenCalledTimes(2); - expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir'); - }); - - it('should return false if download fails and file does not exist', async () => { - (fileExists as Mock).mockResolvedValue(false); - (downloadRipGrep as Mock).mockResolvedValue(undefined); - - const result = await canUseRipgrep(); - - expect(result).toBe(false); - expect(fileExists).toHaveBeenCalledTimes(2); - expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir'); - }); - - it('should propagate errors from downloadRipGrep', async () => { - const error = new Error('Download failed'); - (fileExists as Mock).mockResolvedValue(false); - (downloadRipGrep as Mock).mockRejectedValue(error); - - await expect(canUseRipgrep()).rejects.toThrow(error); - expect(fileExists).toHaveBeenCalledTimes(1); - expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir'); - }); -}); - -describe('ensureRgPath', () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - - it('should return rg path if ripgrep already exists', async () => { - (fileExists as Mock).mockResolvedValue(true); - const rgPath = await ensureRgPath(); - expect(rgPath).toBe(path.join('/mock/bin/dir', 'rg')); - expect(fileExists).toHaveBeenCalledOnce(); - expect(downloadRipGrep).not.toHaveBeenCalled(); - }); - - it('should return rg path if ripgrep is downloaded successfully', async () => { - (fileExists as Mock) - .mockResolvedValueOnce(false) - .mockResolvedValueOnce(true); - (downloadRipGrep as Mock).mockResolvedValue(undefined); - const rgPath = await ensureRgPath(); - expect(rgPath).toBe(path.join('/mock/bin/dir', 'rg')); - expect(downloadRipGrep).toHaveBeenCalledOnce(); - expect(fileExists).toHaveBeenCalledTimes(2); - }); - - it('should throw an error if ripgrep cannot be used after download attempt', async () => { - (fileExists as Mock).mockResolvedValue(false); - (downloadRipGrep as Mock).mockResolvedValue(undefined); - await expect(ensureRgPath()).rejects.toThrow('Cannot use ripgrep.'); - expect(downloadRipGrep).toHaveBeenCalledOnce(); - expect(fileExists).toHaveBeenCalledTimes(2); - }); - - it('should propagate errors from downloadRipGrep', async () => { - const error = new Error('Download failed'); - (fileExists as Mock).mockResolvedValue(false); - (downloadRipGrep as Mock).mockRejectedValue(error); - - await expect(ensureRgPath()).rejects.toThrow(error); - expect(fileExists).toHaveBeenCalledTimes(1); - expect(downloadRipGrep).toHaveBeenCalledWith('/mock/bin/dir'); - }); -}); - // Helper function to create mock spawn implementations function createMockSpawn( options: { @@ -201,8 +97,7 @@ describe('RipGrepTool', () => { beforeEach(async () => { vi.clearAllMocks(); - (downloadRipGrep as Mock).mockResolvedValue(undefined); - (fileExists as Mock).mockResolvedValue(true); + (ensureRipgrepPath as Mock).mockResolvedValue('/mock/path/to/rg'); mockSpawn.mockClear(); tempRootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'grep-tool-root-')); grepTool = new RipGrepTool(mockConfig); @@ -551,16 +446,18 @@ describe('RipGrepTool', () => { }); it('should throw an error if ripgrep is not available', async () => { - // Make ensureRgPath throw - (fileExists as Mock).mockResolvedValue(false); - (downloadRipGrep as Mock).mockResolvedValue(undefined); + // Make ensureRipgrepBinary throw + (ensureRipgrepPath as Mock).mockRejectedValue( + new Error('Ripgrep binary not found'), + ); const params: RipGrepToolParams = { pattern: 'world' }; const invocation = grepTool.build(params); expect(await invocation.execute(abortSignal)).toStrictEqual({ - llmContent: 'Error during grep search operation: Cannot use ripgrep.', - returnDisplay: 'Error: Cannot use ripgrep.', + llmContent: + 'Error during grep search operation: Ripgrep binary not found', + returnDisplay: 'Error: Ripgrep binary not found', }); }); }); diff --git a/packages/core/src/tools/ripGrep.ts b/packages/core/src/tools/ripGrep.ts index 269fb379..cc4e4148 100644 --- a/packages/core/src/tools/ripGrep.ts +++ b/packages/core/src/tools/ripGrep.ts @@ -8,44 +8,16 @@ import fs from 'node:fs'; import path from 'node:path'; import { EOL } from 'node:os'; import { spawn } from 'node:child_process'; -import { downloadRipGrep } from '@joshua.litt/get-ripgrep'; import type { ToolInvocation, ToolResult } from './tools.js'; import { BaseDeclarativeTool, BaseToolInvocation, Kind } from './tools.js'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { makeRelative, shortenPath } from '../utils/paths.js'; import { getErrorMessage, isNodeError } from '../utils/errors.js'; import type { Config } from '../config/config.js'; -import { fileExists } from '../utils/fileUtils.js'; -import { Storage } from '../config/storage.js'; +import { ensureRipgrepPath } from '../utils/ripgrepUtils.js'; const DEFAULT_TOTAL_MAX_MATCHES = 20000; -function getRgPath(): string { - return path.join(Storage.getGlobalBinDir(), 'rg'); -} - -/** - * Checks if `rg` exists, if not then attempt to download it. - */ -export async function canUseRipgrep(): Promise { - if (await fileExists(getRgPath())) { - return true; - } - - await downloadRipGrep(Storage.getGlobalBinDir()); - return await fileExists(getRgPath()); -} - -/** - * Ensures `rg` is downloaded, or throws. - */ -export async function ensureRgPath(): Promise { - if (await canUseRipgrep()) { - return getRgPath(); - } - throw new Error('Cannot use ripgrep.'); -} - /** * Parameters for the GrepTool */ @@ -320,7 +292,7 @@ class GrepToolInvocation extends BaseToolInvocation< rgArgs.push(absolutePath); try { - const rgPath = await ensureRgPath(); + const rgPath = await ensureRipgrepPath(); const output = await new Promise((resolve, reject) => { const child = spawn(rgPath, rgArgs, { windowsHide: true, @@ -342,11 +314,7 @@ class GrepToolInvocation extends BaseToolInvocation< child.on('error', (err) => { options.signal.removeEventListener('abort', cleanup); - reject( - new Error( - `Failed to start ripgrep: ${err.message}. Please ensure @lvce-editor/ripgrep is properly installed.`, - ), - ); + reject(new Error(`Failed to start ripgrep: ${err.message}.`)); }); child.on('close', (code) => { diff --git a/packages/core/src/utils/ripgrepUtils.test.ts b/packages/core/src/utils/ripgrepUtils.test.ts new file mode 100644 index 00000000..8288cbb0 --- /dev/null +++ b/packages/core/src/utils/ripgrepUtils.test.ts @@ -0,0 +1,254 @@ +/** + * @license + * Copyright 2025 Qwen + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest'; +import { + canUseRipgrep, + ensureRipgrepPath, + getRipgrepPath, +} from './ripgrepUtils.js'; +import { fileExists } from './fileUtils.js'; +import path from 'node:path'; + +// Mock fileUtils +vi.mock('./fileUtils.js', async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + fileExists: vi.fn(), + }; +}); + +describe('ripgrepUtils', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + describe('getRipgrepPath', () => { + it('should return path with .exe extension on Windows', () => { + const originalPlatform = process.platform; + const originalArch = process.arch; + + // Mock Windows x64 + Object.defineProperty(process, 'platform', { value: 'win32' }); + Object.defineProperty(process, 'arch', { value: 'x64' }); + + const rgPath = getRipgrepPath(); + + expect(rgPath).toContain('x64-win32'); + expect(rgPath).toContain('rg.exe'); + expect(rgPath).toContain(path.join('vendor', 'ripgrep')); + + // Restore original values + Object.defineProperty(process, 'platform', { value: originalPlatform }); + Object.defineProperty(process, 'arch', { value: originalArch }); + }); + + it('should return path without .exe extension on macOS', () => { + const originalPlatform = process.platform; + const originalArch = process.arch; + + // Mock macOS arm64 + Object.defineProperty(process, 'platform', { value: 'darwin' }); + Object.defineProperty(process, 'arch', { value: 'arm64' }); + + const rgPath = getRipgrepPath(); + + expect(rgPath).toContain('arm64-darwin'); + expect(rgPath).toContain('rg'); + expect(rgPath).not.toContain('.exe'); + expect(rgPath).toContain(path.join('vendor', 'ripgrep')); + + // Restore original values + Object.defineProperty(process, 'platform', { value: originalPlatform }); + Object.defineProperty(process, 'arch', { value: originalArch }); + }); + + it('should return path without .exe extension on Linux', () => { + const originalPlatform = process.platform; + const originalArch = process.arch; + + // Mock Linux x64 + Object.defineProperty(process, 'platform', { value: 'linux' }); + Object.defineProperty(process, 'arch', { value: 'x64' }); + + const rgPath = getRipgrepPath(); + + expect(rgPath).toContain('x64-linux'); + expect(rgPath).toContain('rg'); + expect(rgPath).not.toContain('.exe'); + expect(rgPath).toContain(path.join('vendor', 'ripgrep')); + + // Restore original values + Object.defineProperty(process, 'platform', { value: originalPlatform }); + Object.defineProperty(process, 'arch', { value: originalArch }); + }); + + it('should throw error for unsupported platform', () => { + const originalPlatform = process.platform; + const originalArch = process.arch; + + // Mock unsupported platform + Object.defineProperty(process, 'platform', { value: 'freebsd' }); + Object.defineProperty(process, 'arch', { value: 'x64' }); + + expect(() => getRipgrepPath()).toThrow('Unsupported platform: freebsd'); + + // Restore original values + Object.defineProperty(process, 'platform', { value: originalPlatform }); + Object.defineProperty(process, 'arch', { value: originalArch }); + }); + + it('should throw error for unsupported architecture', () => { + const originalPlatform = process.platform; + const originalArch = process.arch; + + // Mock unsupported architecture + Object.defineProperty(process, 'platform', { value: 'darwin' }); + Object.defineProperty(process, 'arch', { value: 'ia32' }); + + expect(() => getRipgrepPath()).toThrow('Unsupported architecture: ia32'); + + // Restore original values + Object.defineProperty(process, 'platform', { value: originalPlatform }); + Object.defineProperty(process, 'arch', { value: originalArch }); + }); + + it('should handle all supported platform/arch combinations', () => { + const originalPlatform = process.platform; + const originalArch = process.arch; + + const combinations: Array<{ + platform: string; + arch: string; + expected: string; + }> = [ + { platform: 'darwin', arch: 'x64', expected: 'x64-darwin/rg' }, + { platform: 'darwin', arch: 'arm64', expected: 'arm64-darwin/rg' }, + { platform: 'linux', arch: 'x64', expected: 'x64-linux/rg' }, + { platform: 'linux', arch: 'arm64', expected: 'arm64-linux/rg' }, + { platform: 'win32', arch: 'x64', expected: 'x64-win32/rg.exe' }, + ]; + + combinations.forEach(({ platform, arch, expected }) => { + Object.defineProperty(process, 'platform', { value: platform }); + Object.defineProperty(process, 'arch', { value: arch }); + + const rgPath = getRipgrepPath(); + expect(rgPath).toContain(expected); + }); + + // Restore original values + Object.defineProperty(process, 'platform', { value: originalPlatform }); + Object.defineProperty(process, 'arch', { value: originalArch }); + }); + }); + + describe('canUseRipgrep', () => { + it('should return true if ripgrep binary exists', async () => { + (fileExists as Mock).mockResolvedValue(true); + + const result = await canUseRipgrep(); + + expect(result).toBe(true); + expect(fileExists).toHaveBeenCalledOnce(); + }); + + it('should return false if ripgrep binary does not exist', async () => { + (fileExists as Mock).mockResolvedValue(false); + + const result = await canUseRipgrep(); + + expect(result).toBe(false); + expect(fileExists).toHaveBeenCalledOnce(); + }); + + it('should return false if platform is unsupported', async () => { + const originalPlatform = process.platform; + + // Mock unsupported platform + Object.defineProperty(process, 'platform', { value: 'aix' }); + + const result = await canUseRipgrep(); + + expect(result).toBe(false); + expect(fileExists).not.toHaveBeenCalled(); + + // Restore original value + Object.defineProperty(process, 'platform', { value: originalPlatform }); + }); + + it('should return false if architecture is unsupported', async () => { + const originalArch = process.arch; + + // Mock unsupported architecture + Object.defineProperty(process, 'arch', { value: 's390x' }); + + const result = await canUseRipgrep(); + + expect(result).toBe(false); + expect(fileExists).not.toHaveBeenCalled(); + + // Restore original value + Object.defineProperty(process, 'arch', { value: originalArch }); + }); + }); + + describe('ensureRipgrepBinary', () => { + it('should return ripgrep path if binary exists', async () => { + (fileExists as Mock).mockResolvedValue(true); + + const rgPath = await ensureRipgrepPath(); + + expect(rgPath).toBeDefined(); + expect(rgPath).toContain('rg'); + expect(fileExists).toHaveBeenCalledOnce(); + expect(fileExists).toHaveBeenCalledWith(rgPath); + }); + + it('should throw error if binary does not exist', async () => { + (fileExists as Mock).mockResolvedValue(false); + + await expect(ensureRipgrepPath()).rejects.toThrow( + /Ripgrep binary not found/, + ); + await expect(ensureRipgrepPath()).rejects.toThrow(/Platform:/); + await expect(ensureRipgrepPath()).rejects.toThrow(/Architecture:/); + + expect(fileExists).toHaveBeenCalled(); + }); + + it('should throw error with correct path information', async () => { + (fileExists as Mock).mockResolvedValue(false); + + try { + await ensureRipgrepPath(); + // Should not reach here + expect(true).toBe(false); + } catch (error) { + expect(error).toBeInstanceOf(Error); + const errorMessage = (error as Error).message; + expect(errorMessage).toContain('Ripgrep binary not found at'); + expect(errorMessage).toContain(process.platform); + expect(errorMessage).toContain(process.arch); + } + }); + + it('should throw error if platform is unsupported', async () => { + const originalPlatform = process.platform; + + // Mock unsupported platform + Object.defineProperty(process, 'platform', { value: 'openbsd' }); + + await expect(ensureRipgrepPath()).rejects.toThrow( + 'Unsupported platform: openbsd', + ); + + // Restore original value + Object.defineProperty(process, 'platform', { value: originalPlatform }); + }); + }); +}); diff --git a/packages/core/src/utils/ripgrepUtils.ts b/packages/core/src/utils/ripgrepUtils.ts new file mode 100644 index 00000000..4aa579e8 --- /dev/null +++ b/packages/core/src/utils/ripgrepUtils.ts @@ -0,0 +1,114 @@ +/** + * @license + * Copyright 2025 Qwen + * SPDX-License-Identifier: Apache-2.0 + */ + +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { fileExists } from './fileUtils.js'; + +// Get the directory of the current module +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +type Platform = 'darwin' | 'linux' | 'win32'; +type Architecture = 'x64' | 'arm64'; + +/** + * Maps process.platform values to vendor directory names + */ +function getPlatformString(platform: string): Platform { + switch (platform) { + case 'darwin': + case 'linux': + case 'win32': + return platform; + default: + throw new Error(`Unsupported platform: ${platform}`); + } +} + +/** + * Maps process.arch values to vendor directory names + */ +function getArchitectureString(arch: string): Architecture { + switch (arch) { + case 'x64': + case 'arm64': + return arch; + default: + throw new Error(`Unsupported architecture: ${arch}`); + } +} + +/** + * Returns the path to the bundled ripgrep binary for the current platform + */ +export function getRipgrepPath(): string { + const platform = getPlatformString(process.platform); + const arch = getArchitectureString(process.arch); + + // Binary name includes .exe on Windows + const binaryName = platform === 'win32' ? 'rg.exe' : 'rg'; + + // Path resolution: + // When running from transpiled code: dist/src/utils/ripgrepUtils.js -> ../../../vendor/ripgrep/ + // When running from bundle: dist/index.js -> vendor/ripgrep/ + + // Detect if we're running from a bundle (single file) + // In bundle, __filename will be something like /path/to/dist/index.js + // In transpiled code, __filename will be /path/to/dist/src/utils/ripgrepUtils.js + const isBundled = !__filename.includes(path.join('src', 'utils')); + + const vendorPath = isBundled + ? path.join( + __dirname, + 'vendor', + 'ripgrep', + `${arch}-${platform}`, + binaryName, + ) + : path.join( + __dirname, + '..', + '..', + '..', + 'vendor', + 'ripgrep', + `${arch}-${platform}`, + binaryName, + ); + + return vendorPath; +} + +/** + * Checks if ripgrep binary is available + */ +export async function canUseRipgrep(): Promise { + try { + const rgPath = getRipgrepPath(); + return await fileExists(rgPath); + } catch (_error) { + // Unsupported platform/arch + return false; + } +} + +/** + * Ensures ripgrep binary exists and returns its path + * @throws Error if ripgrep binary is not available + */ +export async function ensureRipgrepPath(): Promise { + const rgPath = getRipgrepPath(); + + if (!(await fileExists(rgPath))) { + throw new Error( + `Ripgrep binary not found at ${rgPath}. ` + + `Platform: ${process.platform}, Architecture: ${process.arch}`, + ); + } + + return rgPath; +} diff --git a/packages/core/vendor/ripgrep/COPYING b/packages/core/vendor/ripgrep/COPYING new file mode 100644 index 00000000..bb9c20a0 --- /dev/null +++ b/packages/core/vendor/ripgrep/COPYING @@ -0,0 +1,3 @@ +This project is dual-licensed under the Unlicense and MIT licenses. + +You may use this code under the terms of either license. diff --git a/packages/core/vendor/ripgrep/arm64-darwin/rg b/packages/core/vendor/ripgrep/arm64-darwin/rg new file mode 100755 index 00000000..43148bb5 Binary files /dev/null and b/packages/core/vendor/ripgrep/arm64-darwin/rg differ diff --git a/packages/core/vendor/ripgrep/arm64-linux/rg b/packages/core/vendor/ripgrep/arm64-linux/rg new file mode 100755 index 00000000..ceaec002 Binary files /dev/null and b/packages/core/vendor/ripgrep/arm64-linux/rg differ diff --git a/packages/core/vendor/ripgrep/x64-darwin/rg b/packages/core/vendor/ripgrep/x64-darwin/rg new file mode 100755 index 00000000..820e3ade Binary files /dev/null and b/packages/core/vendor/ripgrep/x64-darwin/rg differ diff --git a/packages/core/vendor/ripgrep/x64-linux/rg b/packages/core/vendor/ripgrep/x64-linux/rg new file mode 100755 index 00000000..5fa29628 Binary files /dev/null and b/packages/core/vendor/ripgrep/x64-linux/rg differ diff --git a/packages/core/vendor/ripgrep/x64-win32/rg.exe b/packages/core/vendor/ripgrep/x64-win32/rg.exe new file mode 100644 index 00000000..15d129dd Binary files /dev/null and b/packages/core/vendor/ripgrep/x64-win32/rg.exe differ diff --git a/scripts/copy_bundle_assets.js b/scripts/copy_bundle_assets.js index 609f3611..1b2b5099 100644 --- a/scripts/copy_bundle_assets.js +++ b/scripts/copy_bundle_assets.js @@ -17,24 +17,74 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { copyFileSync, existsSync, mkdirSync } from 'node:fs'; +import { copyFileSync, existsSync, mkdirSync, statSync } from 'node:fs'; import { dirname, join, basename } from 'node:path'; import { fileURLToPath } from 'node:url'; import { glob } from 'glob'; +import fs from 'node:fs'; const __dirname = dirname(fileURLToPath(import.meta.url)); const root = join(__dirname, '..'); -const bundleDir = join(root, 'bundle'); +const distDir = join(root, 'dist'); +const coreVendorDir = join(root, 'packages', 'core', 'vendor'); -// Create the bundle directory if it doesn't exist -if (!existsSync(bundleDir)) { - mkdirSync(bundleDir); +// Create the dist directory if it doesn't exist +if (!existsSync(distDir)) { + mkdirSync(distDir); } -// Find and copy all .sb files from packages to the root of the bundle directory +// Find and copy all .sb files from packages to the root of the dist directory const sbFiles = glob.sync('packages/**/*.sb', { cwd: root }); for (const file of sbFiles) { - copyFileSync(join(root, file), join(bundleDir, basename(file))); + copyFileSync(join(root, file), join(distDir, basename(file))); } -console.log('Assets copied to bundle/'); +console.log('Copied sandbox profiles to dist/'); + +// Copy vendor directory (contains ripgrep binaries) +console.log('Copying vendor directory...'); +if (existsSync(coreVendorDir)) { + const destVendorDir = join(distDir, 'vendor'); + copyRecursiveSync(coreVendorDir, destVendorDir); + console.log('Copied vendor directory to dist/'); +} else { + console.warn(`Warning: Vendor directory not found at ${coreVendorDir}`); +} + +console.log('\nāœ… All bundle assets copied to dist/'); + +/** + * Recursively copy directory + */ +function copyRecursiveSync(src, dest) { + if (!existsSync(src)) { + return; + } + + const stats = statSync(src); + + if (stats.isDirectory()) { + if (!existsSync(dest)) { + mkdirSync(dest, { recursive: true }); + } + + const entries = fs.readdirSync(src); + for (const entry of entries) { + // Skip .DS_Store files + if (entry === '.DS_Store') { + continue; + } + + const srcPath = join(src, entry); + const destPath = join(dest, entry); + copyRecursiveSync(srcPath, destPath); + } + } else { + copyFileSync(src, dest); + // Preserve execute permissions for binaries + const srcStats = statSync(src); + if (srcStats.mode & 0o111) { + fs.chmodSync(dest, srcStats.mode); + } + } +} diff --git a/scripts/esbuild-shims.js b/scripts/esbuild-shims.js new file mode 100644 index 00000000..9b71a3d4 --- /dev/null +++ b/scripts/esbuild-shims.js @@ -0,0 +1,29 @@ +/** + * @license + * Copyright 2025 Qwen + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * Shims for esbuild ESM bundles to support require() calls + * This file is injected into the bundle via esbuild's inject option + */ + +import { createRequire } from 'node:module'; +import { fileURLToPath } from 'node:url'; +import { dirname } from 'node:path'; + +// Create require function for the current module and make it global +const _require = createRequire(import.meta.url); + +// Make require available globally for dynamic requires +if (typeof globalThis.require === 'undefined') { + globalThis.require = _require; +} + +// Export for esbuild injection +export const require = _require; + +// Setup __filename and __dirname for compatibility +export const __filename = fileURLToPath(import.meta.url); +export const __dirname = dirname(__filename); diff --git a/scripts/prepare-package.js b/scripts/prepare-package.js index ff1dc137..7c9865fb 100644 --- a/scripts/prepare-package.js +++ b/scripts/prepare-package.js @@ -1,51 +1,110 @@ /** * @license - * Copyright 2025 Google LLC + * Copyright 2025 Qwen * SPDX-License-Identifier: Apache-2.0 */ +/** + * Prepares the bundled CLI package for npm publishing + * This script adds publishing metadata (package.json, README, LICENSE) to dist/ + * All runtime assets (cli.js, vendor/, *.sb) are already in dist/ from the bundle step + */ + import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; +import { execSync } from 'node:child_process'; -// ES module equivalent of __dirname const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); - const rootDir = path.resolve(__dirname, '..'); -function copyFiles(packageName, filesToCopy) { - const packageDir = path.resolve(rootDir, 'packages', packageName); - if (!fs.existsSync(packageDir)) { - console.error(`Error: Package directory not found at ${packageDir}`); - process.exit(1); - } +const distDir = path.join(rootDir, 'dist'); +const cliBundlePath = path.join(distDir, 'cli.js'); +const vendorDir = path.join(distDir, 'vendor'); - console.log(`Preparing package: ${packageName}`); - for (const [source, dest] of Object.entries(filesToCopy)) { - const sourcePath = path.resolve(rootDir, source); - const destPath = path.resolve(packageDir, dest); - try { - fs.copyFileSync(sourcePath, destPath); - console.log(`Copied ${source} to packages/${packageName}/`); - } catch (err) { - console.error(`Error copying ${source}:`, err); - process.exit(1); - } +// Verify dist directory and bundle exist +if (!fs.existsSync(distDir)) { + console.error('Error: dist/ directory not found'); + console.error('Please run "npm run bundle" first'); + process.exit(1); +} + +if (!fs.existsSync(cliBundlePath)) { + console.error(`Error: Bundle not found at ${cliBundlePath}`); + console.error('Please run "npm run bundle" first'); + process.exit(1); +} + +if (!fs.existsSync(vendorDir)) { + console.error(`Error: Vendor directory not found at ${vendorDir}`); + console.error('Please run "npm run bundle" first'); + process.exit(1); +} + +// Copy README and LICENSE +console.log('Copying documentation files...'); +const filesToCopy = ['README.md', 'LICENSE']; +for (const file of filesToCopy) { + const sourcePath = path.join(rootDir, file); + const destPath = path.join(distDir, file); + if (fs.existsSync(sourcePath)) { + fs.copyFileSync(sourcePath, destPath); + console.log(`Copied ${file}`); + } else { + console.warn(`Warning: ${file} not found at ${sourcePath}`); } } -// Prepare 'core' package -copyFiles('core', { - 'README.md': 'README.md', - LICENSE: 'LICENSE', - '.npmrc': '.npmrc', -}); +// Copy package.json from root and modify it for publishing +console.log('Creating package.json for distribution...'); +const rootPackageJson = JSON.parse( + fs.readFileSync(path.join(rootDir, 'package.json'), 'utf-8'), +); +const corePackageJson = JSON.parse( + fs.readFileSync( + path.join(rootDir, 'packages', 'core', 'package.json'), + 'utf-8', + ), +); -// Prepare 'cli' package -copyFiles('cli', { - 'README.md': 'README.md', - LICENSE: 'LICENSE', -}); +const runtimeDependencies = {}; +if (corePackageJson.dependencies?.tiktoken) { + runtimeDependencies.tiktoken = corePackageJson.dependencies.tiktoken; +} -console.log('Successfully prepared all packages.'); +// Create a clean package.json for the published package +const distPackageJson = { + name: rootPackageJson.name, + version: rootPackageJson.version, + description: + rootPackageJson.description || 'Qwen Code - AI-powered coding assistant', + repository: rootPackageJson.repository, + type: 'module', + main: 'cli.js', + bin: { + qwen: 'cli.js', + }, + files: ['cli.js', 'vendor', 'README.md', 'LICENSE'], + config: rootPackageJson.config, + dependencies: runtimeDependencies, + optionalDependencies: { + '@lydell/node-pty': '1.1.0', + '@lydell/node-pty-darwin-arm64': '1.1.0', + '@lydell/node-pty-darwin-x64': '1.1.0', + '@lydell/node-pty-linux-x64': '1.1.0', + '@lydell/node-pty-win32-arm64': '1.1.0', + '@lydell/node-pty-win32-x64': '1.1.0', + 'node-pty': '^1.0.0', + }, + engines: rootPackageJson.engines, +}; + +fs.writeFileSync( + path.join(distDir, 'package.json'), + JSON.stringify(distPackageJson, null, 2) + '\n', +); + +console.log('\nāœ… Package prepared for publishing at dist/'); +console.log('\nPackage structure:'); +execSync('ls -lh dist/', { stdio: 'inherit', cwd: rootDir });