-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathbuild.mjs
More file actions
116 lines (106 loc) · 2.61 KB
/
build.mjs
File metadata and controls
116 lines (106 loc) · 2.61 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
#!/usr/bin/node
import { spawn } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
// directory of the current file
const rootDir = new URL(".", import.meta.url).pathname;
/**
* build clarinet-sdk-wasm
*/
async function build_wasm_sdk() {
console.log("Deleting pkg-node");
await rmIfExists(path.join(rootDir, "pkg-node"));
console.log("Deleting pkg-browser");
await rmIfExists(path.join(rootDir, "pkg-browser"));
await Promise.all([
execCommand("wasm-pack", [
"build",
"--release",
"--scope",
"stacks",
"--out-dir",
"pkg-node",
"--target",
"nodejs",
]),
execCommand("wasm-pack", [
"build",
"--release",
"--scope",
"stacks",
"--out-dir",
"pkg-browser",
"--target",
"web",
"--features",
"web",
]),
]);
await updatePackageName();
}
/**
* execCommand
* @param {string} command
* @param {string[]} args
* @returns
*/
export const execCommand = async (command, args, cwd = rootDir) => {
return new Promise((resolve, reject) => {
const childProcess = spawn(command, args, {
cwd,
});
childProcess.stdout.on("data", (data) => {
process.stdout.write(data.toString());
});
childProcess.stderr.on("data", (data) => {
process.stderr.write(data.toString());
});
childProcess.on("error", (error) => {
reject(error);
});
childProcess.on("exit", (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`❌ Command exited with code ${code}.`));
}
});
});
};
/**
* rmIfExists
* @param {string} dirPath
*/
async function rmIfExists(dirPath) {
try {
await fs.rm(dirPath, { recursive: true, force: true });
} catch (error) {
if (error.code !== "ENOENT") {
throw error;
}
}
}
/**
* updatePackageName
*/
async function updatePackageName() {
const filePath = path.join(rootDir, "pkg-browser/package.json");
const fileData = await fs.readFile(filePath, "utf-8");
const updatedData = fileData.replace(
'"name": "@stacks/clarinet-sdk-wasm"',
'"name": "@stacks/clarinet-sdk-wasm-browser"',
);
await fs.writeFile(filePath, updatedData, "utf-8");
console.log("✅ pkg-browser/package.json name updated");
}
try {
await build_wasm_sdk();
console.log("\n✅ Project successfully built.\n🚀 Ready to publish.");
console.log("Run the following commands to publish");
console.log("\n```");
console.log("$ npm run publish:sdk-wasm");
console.log("```\n");
} catch (error) {
console.error("❌ Error building:", error);
throw error;
}