-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: split clarinet-sdk into web and node * ci: add-clarinet sdk ci (#1496) * docs: improve build and contributing steps in readmes * fix: address review * tests: add compilation in the pretest script * address review
- Loading branch information
1 parent
b60ee86
commit 1982441
Showing
75 changed files
with
1,949 additions
and
1,165 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
name: CI - Clarinet SDK | ||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
paths-ignore: | ||
- "**/CHANGELOG.md" | ||
push: | ||
branches: | ||
- main | ||
paths-ignore: | ||
- "**/CHANGELOG.md" | ||
|
||
# Cancel previous runs for the same workflow | ||
concurrency: | ||
group: "${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}" | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
build_and_test_sdk: | ||
name: Build and test clarinet-sdk packages | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Setup Rust toolchain | ||
run: | | ||
rustup toolchain install stable --profile minimal --component rustfmt | ||
rustup target add wasm32-unknown-unknown | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20' | ||
|
||
- name: Setup cache keys | ||
run: | | ||
echo "RUST_VERSION_HASH=$(rustc --version | sha256sum | awk '{print $1}')" >> $GITHUB_ENV | ||
echo "NODE_VERSION_HASH=$(node --version | sha256sum | awk '{print $1}')" >> $GITHUB_ENV | ||
- name: Cache cargo | ||
uses: actions/cache@v4 | ||
with: | ||
path: | | ||
~/.cargo/ | ||
~/target/release/build/ | ||
~/target/wasm32-unknown-unknown/build/ | ||
key: clarinet-sdk-cargo-${{ runner.os }}-${{ env.RUST_VERSION_HASH }}-${{ hashFiles('./Cargo.lock') }} | ||
|
||
- name: Cache npm | ||
uses: actions/cache@v4 | ||
with: | ||
path: | | ||
~/node_modules/ | ||
key: clarinet-sdk-npm-${{ runner.os }}-${{ env.NODE_VERSION_HASH }}-${{ hashFiles('./package-lock.json') }} | ||
|
||
- name: Install wasm-pack | ||
run: npm install -g wasm-pack | ||
|
||
- name: Build Wasm packages | ||
run: npm run build:wasm | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Run tests | ||
run: npm run test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pkg | ||
pkg-browser | ||
pkg-node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,50 @@ | ||
# Clarity SDK WASM | ||
|
||
This package is built with wasm-pack. | ||
It powers [@hirosystems/clarinet-sdk](https://www.npmjs.com/package/@hirosystems/clarinet-sdk). | ||
|
||
This component exposes Clarinet features to a JS interface through wasm-bindgen. | ||
It's built with wasm-pack. | ||
It powers [@hirosystems/clarinet-sdk](https://npmjs.com/package/@hirosystems/clarinet-sdk) and | ||
[@hirosystems/clarinet-sdk-browser](https://npmjs.com/package/@hirosystems/clarinet-sdk-browser). | ||
|
||
## Contributing | ||
|
||
### Build package | ||
|
||
Install [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/). | ||
|
||
In the root directory of Clarinet, run the following command to build the packages for Node.js and the browser. | ||
Under the hood, it will run `wasm-pack build` twice, once for each target. | ||
|
||
```sh | ||
npm run build:wasm | ||
``` | ||
|
||
Alternatively, it's also possible to build the packages separately. It should only be done for development purpose. | ||
|
||
**Build for node** | ||
|
||
```sh | ||
wasm-pack build --release --scope hirosystems --out-dir pkg-node --target nodejs | ||
``` | ||
|
||
**Build for the browser** | ||
|
||
```sh | ||
wasm-pack build --release --scope hirosystems --out-dir pkg-browser --target web | ||
``` | ||
|
||
### Release | ||
|
||
The package is built twice with `wasm-pack` as it can't target `node` and `web` at the same time. | ||
The following script will build for both target, it will also rename the package name for the | ||
browser build. | ||
|
||
```sh | ||
npm run build:wasm | ||
``` | ||
|
||
Once built, the packages can be released by running the following command. Note that by default we | ||
release with the beta tag. | ||
|
||
```sh | ||
wasm-pack build --release --target=nodejs --scope hirosystems | ||
npm run publish:sdk-wasm | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#!/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 | ||
*/ | ||
async function build() { | ||
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", | ||
"hirosystems", | ||
"--out-dir", | ||
"pkg-node", | ||
"--target", | ||
"nodejs", | ||
]), | ||
execCommand("wasm-pack", [ | ||
"build", | ||
"--release", | ||
"--scope", | ||
"hirosystems", | ||
"--out-dir", | ||
"pkg-browser", | ||
"--target", | ||
"web", | ||
]), | ||
]); | ||
|
||
await updatePackageName(); | ||
} | ||
|
||
/** | ||
* execCommand | ||
* @param {string} command | ||
* @param {string[]} args | ||
* @returns | ||
*/ | ||
export const execCommand = async (command, args) => { | ||
console.log(`Building ${args[5]}`); | ||
return new Promise((resolve, reject) => { | ||
const childProcess = spawn(command, args, { | ||
cwd: rootDir, | ||
}); | ||
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": "@hirosystems/clarinet-sdk-wasm"', | ||
'"name": "@hirosystems/clarinet-sdk-wasm-browser"', | ||
); | ||
await fs.writeFile(filePath, updatedData, "utf-8"); | ||
console.log("✅ Package name updated successfully."); | ||
} | ||
|
||
try { | ||
await build(); | ||
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; | ||
} |
Oops, something went wrong.