Repository navigation
Expand file tree
/
Copy pathbuild.js
More file actions
265 lines (218 loc) Β· 7.4 KB
/
Copy pathbuild.js
File metadata and controls
265 lines (218 loc) Β· 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import fs from "node:fs";
import path from "node:path";
import { build } from "esbuild";
async function buildProject() {
console.log("ποΈ Building project...");
// Ensure dist directory exists
if (fs.existsSync("./dist")) {
console.log("Cleaning dist directory...");
fs.rmSync("./dist", { recursive: true, force: true });
}
fs.mkdirSync("./dist", { recursive: true });
try {
// Build TypeScript
await build({
entryPoints: ["./src/main.ts"],
outfile: "./dist/main.js",
bundle: true,
minify: true,
format: "esm",
platform: "browser",
loader: {
".ts": "ts",
},
define: {
"process.env.NODE_ENV": '"production"',
},
});
console.log("β
TypeScript compiled successfully.");
// Copy HTML file
fs.copyFileSync("./src/index.html", "./dist/index.html");
console.log("β
HTML files copied.");
// Copy CSS file
fs.copyFileSync("./src/style.css", "./dist/style.css");
console.log("β
CSS files copied.");
// Copy any other static assets if they exist
const imagesDir = "./src/images";
if (fs.existsSync(imagesDir)) {
const targetDir = "./dist/images";
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
fs.readdirSync(imagesDir).forEach((file) => {
fs.copyFileSync(path.join(imagesDir, file), path.join(targetDir, file));
});
console.log("β
Images copied.");
}
console.log("π Build complete! Files written to ./dist");
} catch (error) {
console.error("β Build failed:", error);
process.exit(1);
}
}
async function buildMcpServer() {
console.log("ποΈ Building MCP server...");
// Ensure dist-mcp directory exists
if (fs.existsSync("./dist-mcp")) {
console.log("Cleaning dist-mcp directory...");
fs.rmSync("./dist-mcp", { recursive: true, force: true });
}
fs.mkdirSync("./dist-mcp", { recursive: true });
try {
// Build MCP server
await build({
entryPoints: ["./utils/mcp-server/index.ts"],
outfile: "./dist-mcp/index.js",
bundle: true,
minify: false, // For easier debugging
format: "esm",
platform: "node",
loader: {
".ts": "ts",
},
external: ["child_process", "fs", "path", "os", "util"],
define: {
"process.env.NODE_ENV": '"production"',
},
});
console.log("β
MCP server compiled successfully.");
// Create README for MCP server
const mcpReadme = `# Katana Foundry MCP Server
This is a Model Context Protocol (MCP) server for Foundry that provides tools
for interacting with Katana blockchain via the command line.
## Usage
To use this MCP server with Cursor, add the following to your Cursor config:
\`\`\`json
"mcpServers": {
"foundry": {
"command": "bun",
"args": [
"${path.resolve("./dist-mcp/index.js")}"
],
"env": {
"PRIVATE_KEY": "0xYourPrivateKeyHere",
"RPC_URL": "http://localhost:8545"
}
}
}
\`\`\`
The \`PRIVATE_KEY\` and \`RPC_URL\` environment variables are optional. If not provided,
the RPC URL will default to http://localhost:8545.
`;
fs.writeFileSync("./dist-mcp/README.md", mcpReadme);
console.log("β
MCP server README created.");
} catch (error) {
console.error("β MCP server build failed:", error);
process.exit(1);
}
}
async function buildExamples() {
console.log("ποΈ Building examples...");
// Ensure dist-examples directory exists
if (fs.existsSync("./dist-examples")) {
console.log("Cleaning dist-examples directory...");
fs.rmSync("./dist-examples", { recursive: true, force: true });
}
fs.mkdirSync("./dist-examples", { recursive: true });
const examplesDir = "./examples";
if (!fs.existsSync(examplesDir)) {
console.log("β Examples directory not found");
return;
}
const examples = fs
.readdirSync(examplesDir, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
if (examples.length === 0) {
console.log("β No example directories found");
return;
}
console.log(`π Found ${examples.length} examples: ${examples.join(", ")}`);
for (const example of examples) {
const examplePath = path.join(examplesDir, example);
const outputPath = path.join("./dist-examples", example);
console.log(`π¨ Building example: ${example}`);
// Create output directory for this example
fs.mkdirSync(outputPath, { recursive: true });
try {
// Check if main.ts exists
const mainTsPath = path.join(examplePath, "main.ts");
if (!fs.existsSync(mainTsPath)) {
console.log(`β οΈ Skipping ${example}: no main.ts found`);
continue;
}
// Build TypeScript for this example
await build({
entryPoints: [mainTsPath],
outfile: path.join(outputPath, "main.js"),
bundle: true,
minify: true,
format: "esm",
platform: "browser",
loader: {
".ts": "ts",
},
define: {
"process.env.NODE_ENV": '"production"',
},
});
console.log(`β
TypeScript compiled for ${example}`);
// Copy HTML file if it exists
const htmlPath = path.join(examplePath, "index.html");
if (fs.existsSync(htmlPath)) {
fs.copyFileSync(htmlPath, path.join(outputPath, "index.html"));
console.log(`β
HTML copied for ${example}`);
}
// Copy CSS file if it exists
const cssPath = path.join(examplePath, "style.css");
if (fs.existsSync(cssPath)) {
fs.copyFileSync(cssPath, path.join(outputPath, "style.css"));
console.log(`β
CSS copied for ${example}`);
}
// Copy any images directory if it exists
const imagesDir = path.join(examplePath, "images");
if (fs.existsSync(imagesDir)) {
const targetImagesDir = path.join(outputPath, "images");
fs.mkdirSync(targetImagesDir, { recursive: true });
fs.readdirSync(imagesDir).forEach((file) => {
fs.copyFileSync(
path.join(imagesDir, file),
path.join(targetImagesDir, file),
);
});
console.log(`β
Images copied for ${example}`);
}
// Create a README for this example
const exampleReadme = `# ${example.charAt(0).toUpperCase() + example.slice(1)} Example
This is a demo application showing how to use Katana blockchain features.
## Running the Demo
To run this demo:
1. Start a local HTTP server in this directory:
\`\`\`bash
npx http-server
\`\`\`
2. Open your browser to http://localhost:PORT (port will vary based on other apps running)
`;
fs.writeFileSync(path.join(outputPath, "README.md"), exampleReadme);
console.log(`β
README created for ${example}`);
} catch (error) {
console.error(`β Failed to build example ${example}:`, error);
// Continue with other examples instead of failing entirely
}
}
console.log("π Examples build complete! Files written to ./dist-examples");
console.log(
"π‘ To run an example: cd dist-examples/<example-name> && npx http-server",
);
}
// Run if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
if (process.argv.includes("--mcp-only")) {
buildMcpServer();
} else if (process.argv.includes("--examples-only")) {
buildExamples();
} else {
buildProject();
}
}
export { buildProject, buildMcpServer, buildExamples };