Skip to content

Commit cbbb568

Browse files
committed
Fix
1 parent f672d97 commit cbbb568

File tree

7 files changed

+81
-47
lines changed

7 files changed

+81
-47
lines changed

dist/index.cjs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24518,19 +24518,24 @@ async function saveManifest(manifest) {
2451824518
// src/compiler.ts
2451924519
var execAsync = (0, import_util.promisify)(import_child_process.exec);
2452024520
var AlkanesCompiler = class {
24521-
tempDir = ".labcoat";
24521+
baseDir;
24522+
constructor(customTempDir) {
24523+
this.baseDir = customTempDir || process.env.TMP_BUILD_DIR || ".labcoat";
24524+
}
2452224525
async compile(contractName, sourceCode) {
24526+
const buildId = `build_${Date.now().toString(36)}`;
24527+
const tempDir = import_path.default.join(this.baseDir, buildId);
2452324528
try {
24524-
await this.createProject(sourceCode);
24525-
const { stderr } = await execAsync(
24529+
await this.createProject(tempDir, sourceCode);
24530+
console.log(`\u{1F9F1} Building in ${tempDir}`);
24531+
const { stdout, stderr } = await execAsync(
2452624532
`cargo clean && cargo build --target=wasm32-unknown-unknown --release`,
24527-
{ cwd: this.tempDir }
24533+
{ cwd: tempDir }
2452824534
);
24529-
if (stderr) {
24530-
console.warn("Build warnings:", stderr);
24531-
}
24535+
if (stderr?.trim()) console.warn("\u26A0\uFE0F Build warnings:", stderr);
24536+
if (stdout?.trim()) console.log(stdout);
2453224537
const wasmPath = import_path.default.join(
24533-
this.tempDir,
24538+
tempDir,
2453424539
"target",
2453524540
"wasm32-unknown-unknown",
2453624541
"release",
@@ -24558,15 +24563,19 @@ var AlkanesCompiler = class {
2455824563
return { wasmBuffer, abi };
2455924564
} catch (error) {
2456024565
if (error instanceof Error) {
24566+
console.error(`\u274C Compilation failed: ${error.message}`);
2456124567
throw new Error(`Compilation failed: ${error.message}`);
2456224568
}
24569+
} finally {
24570+
await import_promises2.default.rm(tempDir, { recursive: true, force: true }).catch(() => {
24571+
});
2456324572
}
2456424573
}
24565-
async createProject(sourceCode) {
24566-
await import_promises2.default.mkdir(this.tempDir, { recursive: true });
24567-
await import_promises2.default.mkdir(import_path.default.join(this.tempDir, "src"), { recursive: true });
24568-
await import_promises2.default.writeFile(import_path.default.join(this.tempDir, "Cargo.toml"), cargoTemplate);
24569-
await import_promises2.default.writeFile(import_path.default.join(this.tempDir, "src", "lib.rs"), sourceCode);
24574+
async createProject(tempDir, sourceCode) {
24575+
await import_promises2.default.mkdir(tempDir, { recursive: true });
24576+
await import_promises2.default.mkdir(import_path.default.join(tempDir, "src"), { recursive: true });
24577+
await import_promises2.default.writeFile(import_path.default.join(tempDir, "Cargo.toml"), cargoTemplate);
24578+
await import_promises2.default.writeFile(import_path.default.join(tempDir, "src", "lib.rs"), sourceCode);
2457024579
}
2457124580
async parseABI(sourceCode) {
2457224581
const methods = [];

dist/index.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.d.cts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ interface ContractConfig {
5858
}
5959

6060
declare class AlkanesCompiler {
61-
private tempDir;
61+
private baseDir;
62+
constructor(customTempDir?: string);
6263
compile(contractName: string, sourceCode: string): Promise<{
6364
wasmBuffer: Buffer;
6465
abi: AlkanesABI;

dist/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ interface ContractConfig {
5858
}
5959

6060
declare class AlkanesCompiler {
61-
private tempDir;
61+
private baseDir;
62+
constructor(customTempDir?: string);
6263
compile(contractName: string, sourceCode: string): Promise<{
6364
wasmBuffer: Buffer;
6465
abi: AlkanesABI;

dist/index.js

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

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,47 @@ import { loadManifest, saveManifest } from "./manifest.js";
1515
const execAsync = promisify(exec);
1616

1717
export class AlkanesCompiler {
18-
private tempDir: string = ".labcoat";
18+
private baseDir: string;
19+
20+
constructor(customTempDir?: string) {
21+
// Allow override via arg or environment
22+
this.baseDir = customTempDir || process.env.TMP_BUILD_DIR || ".labcoat";
23+
}
1924

2025
async compile(
2126
contractName: string,
2227
sourceCode: string
2328
): Promise<{ wasmBuffer: Buffer; abi: AlkanesABI } | void> {
29+
// Each compile gets its own subdirectory
30+
const buildId = `build_${Date.now().toString(36)}`;
31+
const tempDir = path.join(this.baseDir, buildId);
32+
2433
try {
25-
await this.createProject(sourceCode);
34+
await this.createProject(tempDir, sourceCode);
35+
36+
console.log(`🧱 Building in ${tempDir}`);
2637

27-
const { stderr } = await execAsync(
38+
const { stdout, stderr } = await execAsync(
2839
`cargo clean && cargo build --target=wasm32-unknown-unknown --release`,
29-
{ cwd: this.tempDir }
40+
{ cwd: tempDir }
3041
);
3142

32-
if (stderr) {
33-
console.warn("Build warnings:", stderr);
34-
}
43+
if (stderr?.trim()) console.warn("⚠️ Build warnings:", stderr);
44+
if (stdout?.trim()) console.log(stdout);
3545

3646
// Read compiled WASM
3747
const wasmPath = path.join(
38-
this.tempDir,
48+
tempDir,
3949
"target",
4050
"wasm32-unknown-unknown",
4151
"release",
4252
"alkanes_contract.wasm"
4353
);
44-
const wasmBuffer = await fs.readFile(wasmPath);
4554

55+
const wasmBuffer = await fs.readFile(wasmPath);
4656
const abi = await this.parseABI(sourceCode);
4757

58+
// Output files
4859
const buildDir = "./build";
4960
await fs.mkdir(buildDir, { recursive: true });
5061

@@ -54,6 +65,7 @@ export class AlkanesCompiler {
5465
await fs.writeFile(abiPath, JSON.stringify(abi, null, 2));
5566
await fs.writeFile(wasmOutPath, await gzipWasm(wasmBuffer));
5667

68+
// Manifest update
5769
const manifest = await loadManifest();
5870
manifest[contractName] = {
5971
...(manifest[contractName] || {}),
@@ -70,23 +82,26 @@ export class AlkanesCompiler {
7082
return { wasmBuffer, abi };
7183
} catch (error) {
7284
if (error instanceof Error) {
85+
console.error(`❌ Compilation failed: ${error.message}`);
7386
throw new Error(`Compilation failed: ${error.message}`);
7487
}
88+
} finally {
89+
// Clean up temp folder
90+
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {});
7591
}
7692
}
7793

78-
private async createProject(sourceCode: string) {
79-
await fs.mkdir(this.tempDir, { recursive: true });
80-
await fs.mkdir(path.join(this.tempDir, "src"), { recursive: true });
81-
await fs.writeFile(path.join(this.tempDir, "Cargo.toml"), cargoTemplate);
82-
await fs.writeFile(path.join(this.tempDir, "src", "lib.rs"), sourceCode);
94+
private async createProject(tempDir: string, sourceCode: string) {
95+
await fs.mkdir(tempDir, { recursive: true });
96+
await fs.mkdir(path.join(tempDir, "src"), { recursive: true });
97+
await fs.writeFile(path.join(tempDir, "Cargo.toml"), cargoTemplate);
98+
await fs.writeFile(path.join(tempDir, "src", "lib.rs"), sourceCode);
8399
}
84100

85101
public async parseABI(sourceCode: string): Promise<AlkanesABI> {
86102
const methods: AlkanesMethod[] = [];
87103
const opcodes: Record<string, number> = {};
88104

89-
// Match enum variants
90105
const messageRegex =
91106
/#\[opcode\((\d+)\)\](?:\s*#\[returns\(([^)]+)\)\])?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(?:\{([^}]*)\})?/gm;
92107

@@ -115,16 +130,15 @@ export class AlkanesCompiler {
115130
opcodes[variantName] = opcodeNum;
116131
}
117132

118-
// Parse struct name(s)
119133
const structRegex = /pub\s+struct\s+(\w+)/g;
120134
const structNames: string[] = [];
121135
let structMatch: RegExpExecArray | null;
122136
while ((structMatch = structRegex.exec(sourceCode)) !== null) {
123137
structNames.push(structMatch[1]);
124138
}
139+
125140
const name = structNames.length > 0 ? structNames[0] : "UnknownContract";
126141

127-
// Parse storage pointers
128142
const storage: StorageKey[] = [];
129143
const storageRegex = /StoragePointer::from_keyword\("([^"]+)"\)/g;
130144
let storageMatch: RegExpExecArray | null;

0 commit comments

Comments
 (0)