|
1 | 1 | import { execa } from "execa";
|
2 | 2 | import { readFile, writeFile } from "fs/promises";
|
3 | 3 | import { join } from "path";
|
| 4 | +import { findUp } from "find-up"; |
4 | 5 |
|
5 | 6 | export async function buildFramework() {
|
6 |
| - const projectDir = process.cwd(); |
7 |
| - |
8 |
| - try { |
9 |
| - console.log(`Building project in: ${projectDir}`); |
10 |
| - |
11 |
| - await execa("tsc", [], { |
12 |
| - stdio: "inherit", |
13 |
| - reject: true, |
14 |
| - cwd: projectDir |
| 7 | + const projectRoot = await findUp(async directory => { |
| 8 | + const pkgPath = join(directory, 'package.json'); |
| 9 | + const tsConfigPath = join(directory, 'tsconfig.json'); |
| 10 | + |
| 11 | + try { |
| 12 | + const [pkgContent, tsConfigContent] = await Promise.all([ |
| 13 | + readFile(pkgPath, 'utf8').catch(() => null), |
| 14 | + readFile(tsConfigPath, 'utf8').catch(() => null) |
| 15 | + ]); |
| 16 | + |
| 17 | + if (pkgContent && tsConfigContent) { |
| 18 | + const pkg = JSON.parse(pkgContent); |
| 19 | + return pkg.name === 'composer-mcp' ? directory : undefined; |
| 20 | + } |
| 21 | + } catch { |
| 22 | + return undefined; |
| 23 | + } |
15 | 24 | });
|
16 | 25 |
|
17 |
| - const distPath = join(projectDir, "dist"); |
18 |
| - const projectIndexPath = join(distPath, "index.js"); |
19 |
| - const shebang = "#!/usr/bin/env node\n"; |
20 |
| - |
21 |
| - const content = await readFile(projectIndexPath, "utf8"); |
22 |
| - if (!content.startsWith(shebang)) { |
23 |
| - await writeFile(projectIndexPath, shebang + content); |
| 26 | + if (!projectRoot) { |
| 27 | + throw new Error('Could not find target project root directory'); |
24 | 28 | }
|
25 | 29 |
|
26 |
| - console.log("Build complete!"); |
27 |
| - } catch (error) { |
28 |
| - console.error("Build failed:", error instanceof Error ? error.message : error); |
29 |
| - process.exit(1); |
30 |
| - } |
| 30 | + try { |
| 31 | + await execa("tsc", [], { |
| 32 | + stdio: "inherit", |
| 33 | + reject: true, |
| 34 | + cwd: projectRoot |
| 35 | + }); |
| 36 | + |
| 37 | + const distPath = join(projectRoot, "dist"); |
| 38 | + const projectIndexPath = join(distPath, "index.js"); |
| 39 | + const shebang = "#!/usr/bin/env node\n"; |
| 40 | + |
| 41 | + const content = await readFile(projectIndexPath, "utf8"); |
| 42 | + if (!content.startsWith(shebang)) { |
| 43 | + await writeFile(projectIndexPath, shebang + content); |
| 44 | + } |
| 45 | + } catch (error) { |
| 46 | + console.error("Build failed:", error instanceof Error ? error.message : error); |
| 47 | + process.exit(1); |
| 48 | + } |
31 | 49 | }
|
32 | 50 |
|
33 | 51 | if (import.meta.url === new URL(import.meta.url).href) {
|
34 |
| - buildFramework().catch(console.error); |
| 52 | + buildFramework().catch(console.error); |
35 | 53 | }
|
0 commit comments