@treelsp/cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.js +472 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 treelsp contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import prompts from "prompts";
|
|
4
|
+
import ora from "ora";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
7
|
+
import { resolve } from "node:path";
|
|
8
|
+
import { copyFileSync, existsSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { pathToFileURL } from "node:url";
|
|
10
|
+
import { generateAstTypes, generateGrammar, generateHighlights, generateLocals, generateManifest } from "treelsp/codegen";
|
|
11
|
+
import { execSync } from "node:child_process";
|
|
12
|
+
import { build } from "esbuild";
|
|
13
|
+
import chokidar from "chokidar";
|
|
14
|
+
|
|
15
|
+
//#region src/commands/init.ts
|
|
16
|
+
async function init() {
|
|
17
|
+
console.log(pc.bold("treelsp init\n"));
|
|
18
|
+
const answers = await prompts([{
|
|
19
|
+
type: "text",
|
|
20
|
+
name: "name",
|
|
21
|
+
message: "Language name:",
|
|
22
|
+
initial: "my-lang",
|
|
23
|
+
validate: (value) => value.length > 0 || "Name is required"
|
|
24
|
+
}, {
|
|
25
|
+
type: "text",
|
|
26
|
+
name: "extension",
|
|
27
|
+
message: "File extension:",
|
|
28
|
+
initial: ".mylang",
|
|
29
|
+
validate: (value) => {
|
|
30
|
+
if (!value.startsWith(".")) return "Extension must start with a dot";
|
|
31
|
+
if (value.length < 2) return "Extension is too short";
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}]);
|
|
35
|
+
if (!answers.name || !answers.extension) {
|
|
36
|
+
console.log(pc.dim("\nCancelled"));
|
|
37
|
+
process.exit(0);
|
|
38
|
+
}
|
|
39
|
+
const { name, extension } = answers;
|
|
40
|
+
const spinner = ora("Creating project structure...").start();
|
|
41
|
+
try {
|
|
42
|
+
const projectDir = resolve(process.cwd(), name);
|
|
43
|
+
if (existsSync(projectDir)) {
|
|
44
|
+
spinner.fail(`Directory "${name}" already exists`);
|
|
45
|
+
console.log(pc.dim("\nChoose a different name or remove the existing directory"));
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
await mkdir(projectDir);
|
|
49
|
+
const packageJson = {
|
|
50
|
+
name,
|
|
51
|
+
version: "0.1.0",
|
|
52
|
+
type: "module",
|
|
53
|
+
dependencies: { treelsp: "^0.0.1" },
|
|
54
|
+
devDependencies: {
|
|
55
|
+
"@treelsp/cli": "^0.0.1",
|
|
56
|
+
typescript: "^5.7.3"
|
|
57
|
+
},
|
|
58
|
+
scripts: {
|
|
59
|
+
generate: "treelsp generate",
|
|
60
|
+
build: "treelsp build",
|
|
61
|
+
watch: "treelsp watch"
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
await writeFile(resolve(projectDir, "package.json"), JSON.stringify(packageJson, null, 2) + "\n", "utf-8");
|
|
65
|
+
const tsconfig = {
|
|
66
|
+
extends: "treelsp/tsconfig.base.json",
|
|
67
|
+
compilerOptions: { outDir: "./dist" },
|
|
68
|
+
include: ["grammar.ts"]
|
|
69
|
+
};
|
|
70
|
+
await writeFile(resolve(projectDir, "tsconfig.json"), JSON.stringify(tsconfig, null, 2) + "\n", "utf-8");
|
|
71
|
+
const capitalizedName = name.split("-").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
72
|
+
const grammarTemplate = `/**
|
|
73
|
+
* ${capitalizedName} - Language definition for treelsp
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
import { defineLanguage } from 'treelsp';
|
|
77
|
+
|
|
78
|
+
export default defineLanguage({
|
|
79
|
+
name: '${capitalizedName}',
|
|
80
|
+
fileExtensions: ['${extension}'],
|
|
81
|
+
entry: 'program',
|
|
82
|
+
word: 'identifier',
|
|
83
|
+
|
|
84
|
+
grammar: {
|
|
85
|
+
// Program is a sequence of statements
|
|
86
|
+
program: r => r.repeat(r.rule('statement')),
|
|
87
|
+
|
|
88
|
+
// Define your language's statement types
|
|
89
|
+
statement: r => r.choice(
|
|
90
|
+
r.rule('variable_decl'),
|
|
91
|
+
r.rule('expr_statement'),
|
|
92
|
+
),
|
|
93
|
+
|
|
94
|
+
// Variable declaration: let name = value;
|
|
95
|
+
variable_decl: r => r.seq(
|
|
96
|
+
'let',
|
|
97
|
+
r.field('name', r.rule('identifier')),
|
|
98
|
+
'=',
|
|
99
|
+
r.field('value', r.rule('expression')),
|
|
100
|
+
';',
|
|
101
|
+
),
|
|
102
|
+
|
|
103
|
+
// Expression statement: expr;
|
|
104
|
+
expr_statement: r => r.seq(
|
|
105
|
+
r.field('expr', r.rule('expression')),
|
|
106
|
+
';',
|
|
107
|
+
),
|
|
108
|
+
|
|
109
|
+
// Define your language's expressions
|
|
110
|
+
expression: r => r.choice(
|
|
111
|
+
r.rule('identifier'),
|
|
112
|
+
r.rule('number'),
|
|
113
|
+
),
|
|
114
|
+
|
|
115
|
+
// Tokens
|
|
116
|
+
identifier: r => r.token(/[a-zA-Z_][a-zA-Z0-9_]*/),
|
|
117
|
+
number: r => r.token(/[0-9]+/),
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
semantic: {
|
|
121
|
+
// Program creates global scope
|
|
122
|
+
program: { scope: 'global' },
|
|
123
|
+
|
|
124
|
+
// Variable declarations introduce names
|
|
125
|
+
variable_decl: {
|
|
126
|
+
declares: {
|
|
127
|
+
field: 'name',
|
|
128
|
+
scope: 'enclosing',
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
// Identifiers in expressions are references
|
|
133
|
+
identifier: {
|
|
134
|
+
references: {
|
|
135
|
+
field: 'name',
|
|
136
|
+
to: 'variable_decl',
|
|
137
|
+
onUnresolved: 'error',
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
validation: {
|
|
143
|
+
// Add your custom validation rules here
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
lsp: {
|
|
147
|
+
// Keyword completions
|
|
148
|
+
$keywords: {
|
|
149
|
+
'let': { detail: 'Declare a variable' },
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
// Hover for variables
|
|
153
|
+
variable_decl: {
|
|
154
|
+
completionKind: 'Variable',
|
|
155
|
+
symbol: {
|
|
156
|
+
kind: 'Variable',
|
|
157
|
+
label: n => n.field('name').text,
|
|
158
|
+
},
|
|
159
|
+
hover(node, ctx) {
|
|
160
|
+
const name = node.field('name').text;
|
|
161
|
+
return \`**let** \\\`\${name}\\\`\`;
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
`;
|
|
167
|
+
await writeFile(resolve(projectDir, "grammar.ts"), grammarTemplate, "utf-8");
|
|
168
|
+
const gitignore = `node_modules
|
|
169
|
+
dist
|
|
170
|
+
generated/
|
|
171
|
+
*.wasm
|
|
172
|
+
*.log
|
|
173
|
+
*.tsbuildinfo
|
|
174
|
+
.DS_Store
|
|
175
|
+
`;
|
|
176
|
+
await writeFile(resolve(projectDir, ".gitignore"), gitignore, "utf-8");
|
|
177
|
+
const readme = `# ${capitalizedName}
|
|
178
|
+
|
|
179
|
+
A language definition for treelsp.
|
|
180
|
+
|
|
181
|
+
## Getting Started
|
|
182
|
+
|
|
183
|
+
Install dependencies:
|
|
184
|
+
|
|
185
|
+
\`\`\`bash
|
|
186
|
+
npm install
|
|
187
|
+
# or: pnpm install
|
|
188
|
+
\`\`\`
|
|
189
|
+
|
|
190
|
+
Generate grammar and build parser:
|
|
191
|
+
|
|
192
|
+
\`\`\`bash
|
|
193
|
+
npm run generate # Generates grammar.js, ast.ts, server.ts
|
|
194
|
+
npm run build # Compiles to WASM
|
|
195
|
+
\`\`\`
|
|
196
|
+
|
|
197
|
+
Development workflow:
|
|
198
|
+
|
|
199
|
+
\`\`\`bash
|
|
200
|
+
npm run watch # Auto-rebuild on changes
|
|
201
|
+
\`\`\`
|
|
202
|
+
|
|
203
|
+
## Project Structure
|
|
204
|
+
|
|
205
|
+
- \`grammar.ts\` - Language definition (grammar, semantic, validation, LSP)
|
|
206
|
+
- \`generated/\` - Generated files (grammar.js, WASM, types)
|
|
207
|
+
- \`package.json\` - Project dependencies
|
|
208
|
+
|
|
209
|
+
## Documentation
|
|
210
|
+
|
|
211
|
+
- [treelsp Documentation](https://github.com/yourusername/treelsp)
|
|
212
|
+
- [Tree-sitter](https://tree-sitter.github.io/tree-sitter/)
|
|
213
|
+
|
|
214
|
+
## License
|
|
215
|
+
|
|
216
|
+
MIT
|
|
217
|
+
`;
|
|
218
|
+
await writeFile(resolve(projectDir, "README.md"), readme, "utf-8");
|
|
219
|
+
spinner.succeed("Project created!");
|
|
220
|
+
console.log(pc.dim("\nNext steps:"));
|
|
221
|
+
console.log(pc.dim(` cd ${name}`));
|
|
222
|
+
console.log(pc.dim(" npm install"));
|
|
223
|
+
console.log(pc.dim(" Edit grammar.ts to define your language"));
|
|
224
|
+
console.log(pc.dim(" npm run generate"));
|
|
225
|
+
console.log(pc.dim(" npm run build"));
|
|
226
|
+
} catch (error) {
|
|
227
|
+
spinner.fail("Failed to create project");
|
|
228
|
+
if (error instanceof Error) console.error(pc.red(`\n${error.message}`));
|
|
229
|
+
process.exit(1);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
//#endregion
|
|
234
|
+
//#region src/commands/generate.ts
|
|
235
|
+
async function generate(options) {
|
|
236
|
+
const spinner = ora("Loading grammar.ts...").start();
|
|
237
|
+
try {
|
|
238
|
+
const grammarPath = resolve(process.cwd(), "grammar.ts");
|
|
239
|
+
if (!existsSync(grammarPath)) {
|
|
240
|
+
spinner.fail("Could not find grammar.ts in current directory");
|
|
241
|
+
console.log(pc.dim("\nRun \"treelsp init\" to create a new language project"));
|
|
242
|
+
process.exit(1);
|
|
243
|
+
}
|
|
244
|
+
const grammarUrl = pathToFileURL(grammarPath).href;
|
|
245
|
+
const module = await import(grammarUrl);
|
|
246
|
+
const definition = module.default;
|
|
247
|
+
if (!definition || !definition.name || !definition.grammar) {
|
|
248
|
+
spinner.fail("Invalid language definition");
|
|
249
|
+
console.log(pc.dim("\nEnsure grammar.ts exports a valid language definition using defineLanguage()"));
|
|
250
|
+
process.exit(1);
|
|
251
|
+
}
|
|
252
|
+
spinner.text = `Generating code for ${definition.name}...`;
|
|
253
|
+
const grammarJs = generateGrammar(definition);
|
|
254
|
+
const astTypes = generateAstTypes(definition);
|
|
255
|
+
const manifest = generateManifest(definition);
|
|
256
|
+
const highlightsSCM = generateHighlights(definition);
|
|
257
|
+
const localsSCM = generateLocals(definition);
|
|
258
|
+
const genDir = resolve(process.cwd(), "generated");
|
|
259
|
+
const queriesDir = resolve(genDir, "queries");
|
|
260
|
+
await mkdir(queriesDir, { recursive: true });
|
|
261
|
+
await Promise.all([
|
|
262
|
+
writeFile(resolve(genDir, "grammar.js"), grammarJs, "utf-8"),
|
|
263
|
+
writeFile(resolve(genDir, "ast.ts"), astTypes, "utf-8"),
|
|
264
|
+
writeFile(resolve(genDir, "treelsp.json"), manifest, "utf-8"),
|
|
265
|
+
writeFile(resolve(queriesDir, "highlights.scm"), highlightsSCM, "utf-8"),
|
|
266
|
+
writeFile(resolve(queriesDir, "locals.scm"), localsSCM, "utf-8")
|
|
267
|
+
]);
|
|
268
|
+
spinner.succeed("Generated grammar.js, ast.ts, treelsp.json, queries/highlights.scm, queries/locals.scm");
|
|
269
|
+
if (!options.watch) console.log(pc.dim("\nNext step: Run \"treelsp build\" to compile grammar to WASM"));
|
|
270
|
+
} catch (error) {
|
|
271
|
+
spinner.fail("Generation failed");
|
|
272
|
+
if (error instanceof Error) if (error.message.includes("Cannot find module")) {
|
|
273
|
+
console.error(pc.red("\nFailed to load grammar.ts"));
|
|
274
|
+
console.log(pc.dim("Ensure the file exists and has no syntax errors"));
|
|
275
|
+
} else if (error.message.includes("EACCES") || error.message.includes("EPERM")) {
|
|
276
|
+
console.error(pc.red("\nPermission denied writing to generated/"));
|
|
277
|
+
console.log(pc.dim("Check file permissions in the current directory"));
|
|
278
|
+
} else console.error(pc.red(`\n${error.message}`));
|
|
279
|
+
process.exit(1);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/commands/build.ts
|
|
285
|
+
async function build$1() {
|
|
286
|
+
const spinner = ora("Checking prerequisites...").start();
|
|
287
|
+
try {
|
|
288
|
+
const grammarPath = resolve(process.cwd(), "generated", "grammar.js");
|
|
289
|
+
if (!existsSync(grammarPath)) {
|
|
290
|
+
spinner.fail("generated/grammar.js not found");
|
|
291
|
+
console.log(pc.dim("\nRun \"treelsp generate\" first to create grammar.js"));
|
|
292
|
+
process.exit(1);
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
execSync("tree-sitter --version", { stdio: "ignore" });
|
|
296
|
+
} catch {
|
|
297
|
+
spinner.fail("tree-sitter CLI not found");
|
|
298
|
+
console.log(pc.dim("\nInstall tree-sitter CLI:"));
|
|
299
|
+
console.log(pc.dim(" npm install -g tree-sitter-cli"));
|
|
300
|
+
console.log(pc.dim(" or: cargo install tree-sitter-cli"));
|
|
301
|
+
process.exit(1);
|
|
302
|
+
}
|
|
303
|
+
spinner.text = "Generating C parser...";
|
|
304
|
+
const genDir = resolve(process.cwd(), "generated");
|
|
305
|
+
const genPkgJson = resolve(genDir, "package.json");
|
|
306
|
+
const hadPkgJson = existsSync(genPkgJson);
|
|
307
|
+
if (!hadPkgJson) writeFileSync(genPkgJson, "{\"type\":\"commonjs\"}\n");
|
|
308
|
+
try {
|
|
309
|
+
execSync("tree-sitter generate generated/grammar.js", {
|
|
310
|
+
stdio: "pipe",
|
|
311
|
+
cwd: process.cwd()
|
|
312
|
+
});
|
|
313
|
+
} finally {
|
|
314
|
+
if (!hadPkgJson) rmSync(genPkgJson, { force: true });
|
|
315
|
+
}
|
|
316
|
+
spinner.text = "Compiling to WASM...";
|
|
317
|
+
execSync("tree-sitter build --wasm", {
|
|
318
|
+
stdio: "pipe",
|
|
319
|
+
cwd: process.cwd()
|
|
320
|
+
});
|
|
321
|
+
spinner.text = "Moving WASM output...";
|
|
322
|
+
const cwd = process.cwd();
|
|
323
|
+
const wasmFiles = readdirSync(cwd).filter((f) => f.startsWith("tree-sitter-") && f.endsWith(".wasm"));
|
|
324
|
+
if (wasmFiles.length === 0) {
|
|
325
|
+
spinner.fail("tree-sitter build --wasm did not produce a .wasm file");
|
|
326
|
+
process.exit(1);
|
|
327
|
+
}
|
|
328
|
+
const sourceWasm = resolve(cwd, wasmFiles[0]);
|
|
329
|
+
const destWasm = resolve(cwd, "generated", "grammar.wasm");
|
|
330
|
+
renameSync(sourceWasm, destWasm);
|
|
331
|
+
const cleanupDirs = ["src", "bindings"];
|
|
332
|
+
const cleanupFiles = [
|
|
333
|
+
"binding.gyp",
|
|
334
|
+
"Makefile",
|
|
335
|
+
"Package.swift",
|
|
336
|
+
".editorconfig"
|
|
337
|
+
];
|
|
338
|
+
for (const dir of cleanupDirs) {
|
|
339
|
+
const p = resolve(cwd, dir);
|
|
340
|
+
if (existsSync(p)) rmSync(p, {
|
|
341
|
+
recursive: true,
|
|
342
|
+
force: true
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
for (const file of cleanupFiles) {
|
|
346
|
+
const p = resolve(cwd, file);
|
|
347
|
+
if (existsSync(p)) rmSync(p, { force: true });
|
|
348
|
+
}
|
|
349
|
+
for (const f of readdirSync(cwd)) if (f.startsWith("tree-sitter-") && f.endsWith(".pc")) rmSync(resolve(cwd, f), { force: true });
|
|
350
|
+
spinner.text = "Bundling language server...";
|
|
351
|
+
const serverEntry = [
|
|
352
|
+
`import { startStdioServer } from 'treelsp/server';`,
|
|
353
|
+
`import { resolve, dirname } from 'node:path';`,
|
|
354
|
+
`import { fileURLToPath } from 'node:url';`,
|
|
355
|
+
`import definition from './grammar.ts';`,
|
|
356
|
+
``,
|
|
357
|
+
`const __dirname = dirname(fileURLToPath(import.meta.url));`,
|
|
358
|
+
`const wasmPath = resolve(__dirname, 'grammar.wasm');`,
|
|
359
|
+
``,
|
|
360
|
+
`startStdioServer({ definition, wasmPath });`
|
|
361
|
+
].join("\n");
|
|
362
|
+
const treelspServer = import.meta.resolve("treelsp/server");
|
|
363
|
+
const treelspPkg = resolve(new URL(treelspServer).pathname, "..", "..", "..");
|
|
364
|
+
const bundlePath = resolve(genDir, "server.bundle.cjs");
|
|
365
|
+
await build({
|
|
366
|
+
stdin: {
|
|
367
|
+
contents: serverEntry,
|
|
368
|
+
resolveDir: process.cwd(),
|
|
369
|
+
loader: "ts"
|
|
370
|
+
},
|
|
371
|
+
bundle: true,
|
|
372
|
+
format: "cjs",
|
|
373
|
+
platform: "node",
|
|
374
|
+
outfile: bundlePath,
|
|
375
|
+
sourcemap: true,
|
|
376
|
+
nodePaths: [resolve(treelspPkg, "node_modules")],
|
|
377
|
+
logLevel: "silent"
|
|
378
|
+
});
|
|
379
|
+
const { readFileSync } = await import("node:fs");
|
|
380
|
+
let bundleCode = readFileSync(bundlePath, "utf-8");
|
|
381
|
+
bundleCode = bundleCode.replace(/var import_meta\s*=\s*\{\s*\};/, "var import_meta = { url: require(\"url\").pathToFileURL(__filename).href };");
|
|
382
|
+
writeFileSync(bundlePath, bundleCode);
|
|
383
|
+
const treelspNodeModules = resolve(treelspPkg, "node_modules");
|
|
384
|
+
const tsWasmSrc = resolve(treelspNodeModules, "web-tree-sitter", "tree-sitter.wasm");
|
|
385
|
+
const tsWasmDest = resolve(genDir, "tree-sitter.wasm");
|
|
386
|
+
if (existsSync(tsWasmSrc) && !existsSync(tsWasmDest)) copyFileSync(tsWasmSrc, tsWasmDest);
|
|
387
|
+
spinner.succeed("Build complete");
|
|
388
|
+
console.log(pc.dim("\nGenerated files:"));
|
|
389
|
+
console.log(pc.dim(" generated/grammar.js"));
|
|
390
|
+
console.log(pc.dim(" generated/grammar.wasm"));
|
|
391
|
+
if (existsSync(resolve(genDir, "server.bundle.cjs"))) console.log(pc.dim(" generated/server.bundle.cjs"));
|
|
392
|
+
} catch (error) {
|
|
393
|
+
spinner.fail("Build failed");
|
|
394
|
+
if (error instanceof Error) {
|
|
395
|
+
const execError = error;
|
|
396
|
+
if (execError.stderr) {
|
|
397
|
+
const stderr = execError.stderr.toString();
|
|
398
|
+
console.error(pc.red("\nTree-sitter error:"));
|
|
399
|
+
console.error(pc.dim(stderr));
|
|
400
|
+
if (stderr.includes("grammar")) console.log(pc.dim("\nSuggestion: Check your grammar definition for errors"));
|
|
401
|
+
else if (stderr.includes("emcc") || stderr.includes("compiler")) {
|
|
402
|
+
console.log(pc.dim("\nSuggestion: Ensure Emscripten is installed for WASM compilation"));
|
|
403
|
+
console.log(pc.dim(" See: https://tree-sitter.github.io/tree-sitter/creating-parsers#tool-overview"));
|
|
404
|
+
}
|
|
405
|
+
} else console.error(pc.red(`\n${error.message}`));
|
|
406
|
+
}
|
|
407
|
+
process.exit(1);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
//#endregion
|
|
412
|
+
//#region src/commands/watch.ts
|
|
413
|
+
async function watch() {
|
|
414
|
+
console.log(pc.bold("treelsp watch\n"));
|
|
415
|
+
if (!existsSync("grammar.ts")) {
|
|
416
|
+
console.error(pc.red("Could not find grammar.ts in current directory"));
|
|
417
|
+
console.log(pc.dim("\nRun \"treelsp init\" to create a new language project"));
|
|
418
|
+
process.exit(1);
|
|
419
|
+
}
|
|
420
|
+
const watcher = chokidar.watch("grammar.ts", {
|
|
421
|
+
persistent: true,
|
|
422
|
+
awaitWriteFinish: {
|
|
423
|
+
stabilityThreshold: 100,
|
|
424
|
+
pollInterval: 50
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
let isBuilding = false;
|
|
428
|
+
watcher.on("change", (path) => {
|
|
429
|
+
(async () => {
|
|
430
|
+
if (isBuilding) {
|
|
431
|
+
console.log(pc.dim("Build in progress, skipping..."));
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
console.log(pc.dim(`\n${path} changed`));
|
|
435
|
+
isBuilding = true;
|
|
436
|
+
try {
|
|
437
|
+
await generate({});
|
|
438
|
+
await build$1();
|
|
439
|
+
console.log(pc.green("✓ Rebuild successful\n"));
|
|
440
|
+
} catch (_error) {
|
|
441
|
+
console.log(pc.red("✗ Rebuild failed\n"));
|
|
442
|
+
} finally {
|
|
443
|
+
isBuilding = false;
|
|
444
|
+
console.log(pc.dim("Watching for changes..."));
|
|
445
|
+
}
|
|
446
|
+
})();
|
|
447
|
+
});
|
|
448
|
+
watcher.on("error", (error) => {
|
|
449
|
+
console.error(pc.red("Watcher error:"), error instanceof Error ? error.message : String(error));
|
|
450
|
+
});
|
|
451
|
+
console.log(pc.dim("Running initial build...\n"));
|
|
452
|
+
try {
|
|
453
|
+
await generate({});
|
|
454
|
+
await build$1();
|
|
455
|
+
console.log(pc.green("✓ Initial build successful\n"));
|
|
456
|
+
} catch (_error) {
|
|
457
|
+
console.log(pc.red("✗ Initial build failed\n"));
|
|
458
|
+
}
|
|
459
|
+
console.log(pc.dim("Watching for changes..."));
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
//#endregion
|
|
463
|
+
//#region src/index.ts
|
|
464
|
+
const program = new Command();
|
|
465
|
+
program.name("treelsp").description("CLI for treelsp - LSP generator using Tree-sitter").version("0.0.1");
|
|
466
|
+
program.command("init").description("Scaffold a new language project").action(init);
|
|
467
|
+
program.command("generate").description("Generate grammar.js, AST types, and server from language definition").option("-w, --watch", "Watch for changes").action(generate);
|
|
468
|
+
program.command("build").description("Compile grammar.js to WASM using Tree-sitter CLI").action(build$1);
|
|
469
|
+
program.command("watch").description("Watch mode - re-run generate + build on changes").action(watch);
|
|
470
|
+
program.parse();
|
|
471
|
+
|
|
472
|
+
//#endregion
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@treelsp/cli",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"description": "CLI tool for treelsp - LSP generator using Tree-sitter",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"bin": {
|
|
10
|
+
"treelsp": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"keywords": [
|
|
17
|
+
"lsp",
|
|
18
|
+
"cli",
|
|
19
|
+
"tree-sitter",
|
|
20
|
+
"codegen"
|
|
21
|
+
],
|
|
22
|
+
"author": "Dhrubo Moy",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/dhrubomoy/treelsp.git",
|
|
27
|
+
"directory": "packages/cli"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/dhrubomoy/treelsp#readme",
|
|
30
|
+
"bugs": "https://github.com/dhrubomoy/treelsp/issues",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"chokidar": "^4.0.3",
|
|
33
|
+
"commander": "^12.1.0",
|
|
34
|
+
"esbuild": "^0.27.3",
|
|
35
|
+
"ora": "^8.1.1",
|
|
36
|
+
"picocolors": "^1.1.1",
|
|
37
|
+
"prompts": "^2.4.2",
|
|
38
|
+
"treelsp": "0.0.1"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/prompts": "^2.4.9",
|
|
42
|
+
"tsdown": "^0.2.17",
|
|
43
|
+
"typescript": "^5.7.3"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsdown",
|
|
47
|
+
"dev": "tsdown --watch",
|
|
48
|
+
"test": "vitest run --passWithNoTests",
|
|
49
|
+
"lint": "eslint src",
|
|
50
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
51
|
+
}
|
|
52
|
+
}
|