@@ -3,14 +3,15 @@ import { spawnSync } from "child_process";
3
3
import { readFileSync , writeFileSync } from "fs" ;
4
4
import { join } from "path" ;
5
5
import { platform } from "os" ;
6
+ import { existsSync } from "fs" ;
6
7
7
8
export function buildFramework ( ) {
8
- runTsc ( ) ;
9
- addShebang ( ) ;
10
- console . log ( "MCP Build complete ") ;
11
- }
12
-
13
- function runTsc ( ) {
9
+ const tsconfigPath = join ( process . cwd ( ) , "tsconfig.json" ) ;
10
+ if ( ! existsSync ( tsconfigPath ) ) {
11
+ console . error ( "Error: tsconfig.json not found! ") ;
12
+ process . exit ( 1 ) ;
13
+ }
14
+ console . log ( "Found tsconfig.json" ) ;
14
15
const isWindows = platform ( ) === "win32" ;
15
16
const tscPath = join (
16
17
process . cwd ( ) ,
@@ -19,6 +20,11 @@ function runTsc() {
19
20
isWindows ? "tsc.cmd" : "tsc"
20
21
) ;
21
22
23
+ if ( ! existsSync ( tscPath ) ) {
24
+ console . error ( "Error: TypeScript compiler not found at:" , tscPath ) ;
25
+ process . exit ( 1 ) ;
26
+ }
27
+
22
28
const tsc = spawnSync ( tscPath , [ ] , {
23
29
stdio : "inherit" ,
24
30
shell : true ,
@@ -28,24 +34,48 @@ function runTsc() {
28
34
} ,
29
35
} ) ;
30
36
37
+ if ( tsc . error ) {
38
+ console . error ( "TypeScript compilation error:" , tsc . error ) ;
39
+ }
40
+ if ( tsc . stderr ) {
41
+ console . error ( "TypeScript stderr:" , tsc . stderr . toString ( ) ) ;
42
+ }
43
+
31
44
if ( tsc . status !== 0 ) {
32
45
console . error ( "TypeScript compilation failed" ) ;
33
46
process . exit ( tsc . status ?? 1 ) ;
34
47
}
35
- }
36
48
37
- function addShebang ( ) {
38
- const indexPath = join ( process . cwd ( ) , "dist" , "index.js" ) ;
39
49
try {
50
+ const distPath = join ( process . cwd ( ) , "dist" ) ;
51
+
52
+ if ( ! existsSync ( distPath ) ) {
53
+ console . error ( "Error: dist directory not found after compilation!" ) ;
54
+ process . exit ( 1 ) ;
55
+ }
56
+
57
+ const indexPath = join ( process . cwd ( ) , "dist" , "index.js" ) ;
58
+ console . log ( "Adding shebang to:" , indexPath ) ;
59
+
60
+ if ( ! existsSync ( indexPath ) ) {
61
+ console . error ( "Error: index.js not found in dist directory!" ) ;
62
+ process . exit ( 1 ) ;
63
+ }
64
+
40
65
const content = readFileSync ( indexPath , "utf8" ) ;
41
66
const shebang = "#!/usr/bin/env node\n" ;
42
67
43
68
if ( ! content . startsWith ( shebang ) ) {
44
69
writeFileSync ( indexPath , shebang + content ) ;
45
- console . log ( "Added shebang to dist/index.js" ) ;
46
70
}
47
71
} catch ( error ) {
48
- console . error ( "Error adding shebang:" , error ) ;
72
+ console . error ( "Error in shebang process :" , error ) ;
49
73
process . exit ( 1 ) ;
50
74
}
75
+
76
+ console . log ( "Build complete!" ) ;
77
+ }
78
+
79
+ if ( import . meta. url === new URL ( import . meta. url ) . href ) {
80
+ buildFramework ( ) ;
51
81
}
0 commit comments