Skip to content

Commit 88351af

Browse files
authored
cre: build package (#35)
* cre: build package * pin rimraf version * fix lint error on script * wip build package * working a lot nicer now * delete useless scripts * revet change to compile-to-wasm * comments cleanup * separete repo scripts from cli scripts * add back deleted file * update liner rules * add postinstall script * add post install script and bash script to generate the necessary tarball * make package compilable to tarball * remove build types tsconfig * remove postinstall script, extend build script to add postinstall script after copying the package json
1 parent 143dcfc commit 88351af

21 files changed

+668
-94
lines changed

bin/cre-ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env node
2+
3+
import { execFileSync } from 'node:child_process'
4+
import { join } from 'node:path'
5+
import { resolve } from 'node:path'
6+
7+
function getBinaryPath() {
8+
// Lookup table for all platforms and binary distribution packages
9+
const BINARY_DISTRIBUTION_PACKAGES = {
10+
'linux-x64': '@chainlink/cre-ts-linux-x64',
11+
'linux-arm64': '@chainlink/cre-ts-linux-arm',
12+
'darwin-x64': '@chainlink/cre-ts-darwin-x64',
13+
'darwin-arm64': '@chainlink/cre-ts-darwin-arm64',
14+
'win32-x64': '@chainlink/cre-ts-windows-x64',
15+
}
16+
17+
// Windows binaries end with .exe so we need to special case them.
18+
const binaryName = process.platform === 'win32' ? 'cre-build.exe' : 'cre-build'
19+
20+
// Determine package name for this platform
21+
const platformSpecificPackageName =
22+
BINARY_DISTRIBUTION_PACKAGES[`${process.platform}-${process.arch}`]
23+
24+
try {
25+
// Resolving will fail if the optionalDependency was not installed
26+
return resolve(`node_modules/${platformSpecificPackageName}/bin/${binaryName}`)
27+
} catch (e) {
28+
return join(__dirname, binaryName)
29+
}
30+
}
31+
32+
33+
execFileSync(getBinaryPath(), process.argv.slice(2), {
34+
stdio: 'inherit',
35+
})

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"files": {
99
"ignoreUnknown": false,
10-
"includes": ["src/**/*", "scripts/**/*"]
10+
"includes": ["src/**/*", "scripts/**/*", "cli/**/*", "**/*.json"]
1111
},
1212
"formatter": {
1313
"enabled": true,

build-bins.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#!/bin/bash
3+
4+
# Platform configurations: "bun-target:platform-arch:os:cpu"
5+
platforms=(
6+
"bun-darwin-arm64:darwin-arm64:darwin:arm64"
7+
"bun-darwin-x64:darwin-x64:darwin:x64"
8+
"bun-linux-x64:linux-x64:linux:x64"
9+
"bun-linux-arm64:linux-arm64:linux:arm64"
10+
"bun-windows-x64:windows-x64:win32:x64"
11+
)
12+
13+
# Loop through each platform configuration
14+
for platform_config in "${platforms[@]}"; do
15+
# Split the configuration string
16+
IFS=':' read -r bun_target platform_arch os cpu <<< "$platform_config"
17+
18+
echo "Building for $platform_arch..."
19+
20+
# Build the binary
21+
bun build ./cli/run.ts --target="$bun_target" --compile --outfile "dist/bin/$platform_arch/bin/cre-build"
22+
23+
chmod +x "dist/bin/$platform_arch/bin/cre-build"
24+
25+
# Create package.json for this platform
26+
cat > "dist/bin/$platform_arch/package.json" << EOF
27+
{
28+
"name": "@chainlink/cre-build-$platform_arch",
29+
"version": "0.0.1",
30+
"os": ["$os"],
31+
"cpu": ["$cpu"]
32+
}
33+
EOF
34+
35+
echo "✓ Built $platform_arch binary"
36+
done
37+
38+
echo "All platform binaries built successfully!"

build-npm-tar.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sh build-pkg.sh
2+
3+
bun pm pack --destination ./dist
4+
# tar -cvzf backup.tgz ./dist
5+
6+
# Pack each platform-specific folder in dist/bin
7+
for folder in dist/bin/*/; do
8+
if [ -d "$folder" ] && [ -f "$folder/package.json" ]; then
9+
echo "Packing $folder..."
10+
(cd "$folder" && bun pm pack --destination ../..)
11+
fi
12+
done

build-pkg.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
bun run build:javy:sdk:wasm
2+
3+
bun tsc --project tsconfig.build.json
4+
5+
sh build-bins.sh
6+
7+
cp bin/* dist/bin/
8+
cp install.js dist/install.js
9+
10+
chmod +x dist/bin/cre-ts
11+
12+
bun rimraf tsconfig.types.tsbuildinfo
13+
14+
cp package.json dist/package.json
15+
16+
cd dist
17+
18+
bun pm pkg set scripts.postinstall="node ./install.js"

bun.lock

Lines changed: 92 additions & 8 deletions
Large diffs are not rendered by default.

cli/compile-cmd.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { main as compileToJs } from './compile-to-js'
2+
import { main as compileToWasm } from './compile-to-wasm-cmd'
3+
4+
export const main = async () => {
5+
await compileToJs()
6+
await compileToWasm()
7+
}

cli/compile-to-js.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fg from 'fast-glob'
2+
import { $ } from 'bun'
3+
4+
export const main = async () => {
5+
console.info('\n\n---> Compile TS source files to JS\n\n')
6+
7+
const workflowsSourcePaths = await fg('src/workflows/**/*.ts')
8+
const workflows = Array.from(workflowsSourcePaths).map((path) => `./${path}`)
9+
10+
await Bun.build({
11+
entrypoints: [...workflows],
12+
outdir: './dist/workflows',
13+
target: 'node',
14+
format: 'esm',
15+
root: './src/workflows',
16+
})
17+
18+
console.info('\n---> Bundling individual workflow files\n\n')
19+
20+
// Get all the generated JS files in dist/workflows
21+
const workflowJSFiles = await fg('dist/workflows/**/*.js')
22+
23+
for (const jsFile of workflowJSFiles) {
24+
await $`bun build ${jsFile} --bundle --outfile=${jsFile}`
25+
}
26+
27+
console.info('Bundling: Done!')
28+
}

cli/compile-to-wasm-cmd.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { execFileSync } from 'node:child_process'
2+
import path from 'node:path'
3+
import fg from 'fast-glob'
4+
5+
export const main = async () => {
6+
console.info('\n\n---> Compile JS workflows to WASM \n\n')
7+
8+
const workflowJSFiles = fg.sync('dist/workflows/**/*.js')
9+
10+
for (const jsFile of workflowJSFiles) {
11+
if (jsFile.includes('abi')) continue
12+
13+
const wasmFile = jsFile.replace(/\.js$/, '.wasm')
14+
15+
console.log(`\n\n🔨 Building WASM for: ${jsFile}`)
16+
17+
const javyBinary =
18+
process.platform === 'darwin'
19+
? './.bin/javy-arm-macos-v5.0.4'
20+
: './.bin/javy-arm-linux-v5.0.4'
21+
22+
const javyPath = path.join(process.cwd(), 'node_modules', javyBinary)
23+
24+
/**
25+
* -C wit=src/workflows/workflow.wit — points to the WIT file (definition of what will be available for the Host).
26+
* -C wit-world=workflow — specifies the WIT world name (world "workflow" which is defined in the .wit file).
27+
* -C plugin=... — uses your custom runtime (bundled javy chainlink sdk plugin)
28+
*/
29+
30+
execFileSync(
31+
javyPath,
32+
[
33+
'build',
34+
'-C',
35+
'wit=src/workflows/workflow.wit',
36+
'-C',
37+
'wit-world=workflow',
38+
'-C',
39+
'plugin=node_modules/@chainlink/cre-sdk/javy-chainlink-sdk.plugin.wasm',
40+
jsFile,
41+
'-o',
42+
wasmFile,
43+
],
44+
{
45+
stdio: 'inherit',
46+
},
47+
)
48+
}
49+
50+
console.info('Done!')
51+
}

cli/run.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bun
2+
import { main as compileCmd } from './compile-cmd'
3+
import { main as compileToJs } from './compile-to-js'
4+
import { main as compileToWasm } from './compile-to-wasm-cmd'
5+
6+
const availableScripts = {
7+
'compile-to-js': compileToJs,
8+
'compile-to-wasm': compileToWasm,
9+
compile: compileCmd,
10+
}
11+
12+
const main = async () => {
13+
const scriptName = process.argv[2]
14+
15+
if (!scriptName) {
16+
console.error('Usage: bun run.ts <script-name>')
17+
console.error('Available scripts:')
18+
Object.keys(availableScripts).forEach((script) => {
19+
console.error(` ${script}`)
20+
})
21+
process.exit(1)
22+
}
23+
24+
try {
25+
const script = availableScripts[scriptName]
26+
27+
if (!script) {
28+
console.error(`Script ${scriptName} not found`)
29+
process.exit(1)
30+
}
31+
32+
await script()
33+
} catch (error) {
34+
console.error(`Failed to load script ${scriptName}:`, error)
35+
process.exit(1)
36+
}
37+
}
38+
39+
main()

0 commit comments

Comments
 (0)