Skip to content

Commit 185476a

Browse files
committed
chore: add CLI build configuration and dependencies
1 parent 8059b29 commit 185476a

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

esbuild.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require("path")
44

55
const production = process.argv.includes("--production")
66
const watch = process.argv.includes("--watch")
7+
const cli = process.argv.includes("--cli")
78

89
/**
910
* @type {import('esbuild').Plugin}
@@ -69,7 +70,6 @@ const extensionConfig = {
6970
logLevel: "silent",
7071
plugins: [
7172
copyWasmFiles,
72-
/* add to the end of plugins array */
7373
esbuildProblemMatcherPlugin,
7474
],
7575
entryPoints: ["src/extension.ts"],
@@ -80,13 +80,47 @@ const extensionConfig = {
8080
external: ["vscode"],
8181
}
8282

83+
const cliConfig = {
84+
bundle: true,
85+
minify: production,
86+
sourcemap: !production,
87+
logLevel: "silent",
88+
plugins: [esbuildProblemMatcherPlugin],
89+
entryPoints: ["src/cli.ts"],
90+
format: "cjs",
91+
sourcesContent: false,
92+
platform: "node",
93+
outfile: "dist/cli.js",
94+
external: ["vscode"], // Still mark vscode as external to avoid build errors from other files
95+
define: {
96+
'process.env.VSCODE': 'undefined' // Define vscode as undefined for CLI build
97+
}
98+
}
99+
83100
async function main() {
84-
const extensionCtx = await esbuild.context(extensionConfig)
85-
if (watch) {
86-
await extensionCtx.watch()
101+
if (cli) {
102+
const cliCtx = await esbuild.context(cliConfig)
103+
if (watch) {
104+
await cliCtx.watch()
105+
} else {
106+
await cliCtx.rebuild()
107+
await cliCtx.dispose()
108+
// Add shebang and make executable after build
109+
const cliPath = path.join(__dirname, 'dist/cli.js')
110+
const content = fs.readFileSync(cliPath, 'utf8')
111+
// Remove any existing shebang line
112+
const contentWithoutShebang = content.replace(/^#!.*\n/, '');
113+
fs.writeFileSync(cliPath, `#!/usr/bin/env node\n${contentWithoutShebang}`);
114+
fs.chmodSync(cliPath, '755')
115+
}
87116
} else {
88-
await extensionCtx.rebuild()
89-
await extensionCtx.dispose()
117+
const extensionCtx = await esbuild.context(extensionConfig)
118+
if (watch) {
119+
await extensionCtx.watch()
120+
} else {
121+
await extensionCtx.rebuild()
122+
await extensionCtx.dispose()
123+
}
90124
}
91125
}
92126

package-lock.json

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
"onStartupFinished"
4848
],
4949
"main": "./dist/extension.js",
50+
"bin": {
51+
"cline": "./dist/cli.js"
52+
},
5053
"contributes": {
5154
"viewsContainers": {
5255
"activitybar": [
@@ -163,7 +166,7 @@
163166
"description": "The vendor of the language model (e.g. copilot)"
164167
},
165168
"family": {
166-
"type": "string",
169+
"type": "string",
167170
"description": "The family of the language model (e.g. gpt-4)"
168171
}
169172
},
@@ -175,6 +178,7 @@
175178
"scripts": {
176179
"build": "npm run build:webview && npm run vsix",
177180
"build:webview": "cd webview-ui && npm run build",
181+
"build:cli": "node esbuild.js --cli",
178182
"changeset": "changeset",
179183
"check-types": "tsc --noEmit",
180184
"compile": "npm run check-types && npm run lint && node esbuild.js",
@@ -196,15 +200,17 @@
196200
"watch": "npm-run-all -p watch:*",
197201
"watch:esbuild": "node esbuild.js --watch",
198202
"watch:tsc": "tsc --noEmit --watch --project tsconfig.json",
199-
"watch-tests": "tsc -p . -w --outDir out"
203+
"watch-tests": "tsc -p . -w --outDir out",
204+
"cli": "node dist/cli.js"
200205
},
201206
"devDependencies": {
202207
"@changesets/cli": "^2.27.10",
203208
"@changesets/types": "^6.0.0",
204209
"@types/diff": "^5.2.1",
205210
"@types/jest": "^29.5.14",
206211
"@types/mocha": "^10.0.7",
207-
"@types/node": "20.x",
212+
"@types/node": "^20.17.13",
213+
"@types/vscode": "^1.96.0",
208214
"@typescript-eslint/eslint-plugin": "^7.14.1",
209215
"@typescript-eslint/parser": "^7.11.0",
210216
"@vscode/test-cli": "^0.0.9",
@@ -230,7 +236,6 @@
230236
"@types/clone-deep": "^4.0.4",
231237
"@types/pdf-parse": "^1.1.4",
232238
"@types/turndown": "^5.0.5",
233-
"@types/vscode": "^1.95.0",
234239
"@vscode/codicons": "^0.0.36",
235240
"axios": "^1.7.4",
236241
"cheerio": "^1.0.0",

0 commit comments

Comments
 (0)