Skip to content

Commit

Permalink
allow cross compiling for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
hxhxhx88 committed Feb 29, 2024
1 parent 2bdf134 commit 956b7a6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vars:
includes:
frontend: task/frontend.yaml
backend: task/backend.yaml
yjs: task/yjs.yaml
e2e: task/e2e.yaml
container: task/container.yaml
deploy: task/deploy.yaml
Expand Down Expand Up @@ -45,6 +46,7 @@ tasks:
build:
cmds:
- rm -f build/nutsh
- task: yjs:build
- task: frontend:build
- task: docs:build
- task: backend:build
Expand Down
32 changes: 32 additions & 0 deletions app/yjs-server/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# map Go's os and arch to Node's
os=""
if [ "$GOOS" == "linux" ]; then
os="linux"
elif [ "$GOOS" == "darwin" ]; then
os="mac"
fi

arch=""
if [ "$GOARCH" == "amd64" ]; then
arch="x64"
elif [ "$GOARCH" == "arm64" ]; then
arch="arm64"
fi

cmd_base="pkg -c pkg.json --output bin/yjs-server"
input_file="dist/yjs-server.js"

# if both os and arch are set, modify the command to include the -t option
if [[ -n "$os" && -n "$arch" ]]; then
target="node20-${os}-${arch}"
cmd="$cmd_base -t ${target} $input_file"
else
# bundle for the current platform
cmd="$cmd_base $input_file"
fi

# execute the command
echo "Executing command: $cmd"
$cmd
32 changes: 32 additions & 0 deletions app/yjs-server/pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

# map Go's os and arch to Node's
os=""
if [ "$GOOS" == "linux" ]; then
os="linux"
elif [ "$GOOS" == "darwin" ]; then
os="mac"
fi

arch=""
if [ "$GOARCH" == "amd64" ]; then
arch="x64"
elif [ "$GOARCH" == "arm64" ]; then
arch="arm64"
fi

rm -rf node_modules

# if both GOOS and GOARCH are set, modify the command to include the -t option
if [[ -n "$os" && -n "$arch" ]]; then
# install dependencies w.r.t the os and arch
echo "Cross-building for ${os}-${arch}"
npm_config_platform=${os} npm_config_arch=${arch} npm install
else
# install dependencies for the current platform
echo "Building for the current platform"
npm install
fi

# build
cross-env TS_NODE_PROJECT=\"tsconfig.webpack.json\" webpack
4 changes: 2 additions & 2 deletions app/yjs-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"scripts": {
"dev": "nodemon src/index.ts",
"lint": "eslint src/",
"build": "cross-env TS_NODE_PROJECT=\"tsconfig.webpack.json\" webpack",
"bundle": "pkg -c pkg.json --output bin/yjs-server dist/yjs-server.js"
"pack": "bash pack.sh",
"build": "bash build.sh"
},
"devDependencies": {
"@types/node": "^20.11.20",
Expand Down
18 changes: 18 additions & 0 deletions task/yjs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "3"

tasks:
start:
cmds:
- npm run dev
dir: app/yjs-server

pack:
cmds:
- npm run pack
dir: app/yjs-server

build:
deps: [pack]
cmds:
- npm run build
dir: app/yjs-server

0 comments on commit 956b7a6

Please sign in to comment.