Skip to content

Commit

Permalink
Merge pull request #57 from kcl-lang/nodejs-kcl-lib
Browse files Browse the repository at this point in the history
feat: initial nodejs kcl lib
  • Loading branch information
Peefy committed Apr 15, 2024
2 parents 3f2bed3 + 760649e commit 0b635f0
Show file tree
Hide file tree
Showing 38 changed files with 2,661 additions and 0 deletions.
293 changes: 293 additions & 0 deletions .github/workflows/nodejs-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
name: nodejs-test

env:
DEBUG: napi:*
MACOSX_DEPLOYMENT_TARGET: '10.13'

on:
push:
branches:
- main
tags:
- '*'
pull_request:
branches:
- main
paths:
- "nodejs/**"
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

# Notes: this defaults only apply on run tasks.
defaults:
run:
working-directory: "nodejs"

steps:
- uses: actions/checkout@v4
- name: Install rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: 1.74
override: true
components: clippy, rustfmt

- uses: pnpm/action-setup@v2
with:
version: 8

- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '18'
cache: pnpm
cache-dependency-path: "nodejs/pnpm-lock.yaml"

- name: Corepack
run: corepack enable

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Check format
run: pnpm exec prettier --check .

- name: Check diff
run: git diff --exit-code

- name: Build
run: pnpm build

- name: Test
run: cargo test --no-fail-fast

linux:
runs-on: ubuntu-latest
if: "startsWith(github.ref, 'refs/tags/')"
strategy:
matrix:
settings:
- target: x86_64-unknown-linux-gnu
build: |
docker run \
-v .:/build \
-e NAPI_TARGET=x86_64-unknown-linux-gnu \
-w /build/nodejs \
ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian \
bash -c "pnpm build"
cd nodejs
# change owner to current user
sudo chown -R 1001:121 *.node
- target: aarch64-unknown-linux-gnu
build: |
docker run \
-v .:/build \
-e NAPI_TARGET=aarch64-unknown-linux-gnu \
-w /build/nodejs \
ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64 \
bash -c "set -e &&
rustup target add aarch64-unknown-linux-gnu &&
pnpm build --target aarch64-unknown-linux-gnu &&
aarch64-unknown-linux-gnu-strip *.node"
cd nodejs
# change owner to current user
sudo chown -R 1001:121 *.node
- target: aarch64-unknown-linux-musl
build: |
docker run \
-v .:/build \
-e NAPI_TARGET=aarch64-unknown-linux-musl \
-w /build/nodejs \
ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine \
bash -c "set -e &&
rustup target add aarch64-unknown-linux-musl &&
pnpm build --target aarch64-unknown-linux-musl &&
/aarch64-linux-musl-cross/bin/aarch64-linux-musl-strip *.node"
cd nodejs
# change owner to current user
sudo chown -R 1001:121 *.node
# Notes: this defaults only apply on run tasks.
defaults:
run:
working-directory: "nodejs"

name: linux - ${{ matrix.settings.target }}

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '18'
cache: pnpm
cache-dependency-path: "nodejs/pnpm-lock.yaml"
- name: Corepack
run: corepack enable
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
shell: bash
working-directory: .
run: ${{ matrix.settings.build }}
- uses: actions/upload-artifact@v3
with:
name: bindings-linux-${{ matrix.settings.target }}
path: nodejs/*.node

windows:
runs-on: windows-latest
if: "startsWith(github.ref, 'refs/tags/')"
strategy:
matrix:
settings:
- target: x86_64-pc-windows-msvc
build: pnpm build
- target: aarch64-pc-windows-msvc
build: |
rustup target add aarch64-pc-windows-msvc;
NAPI_TARGET=aarch64-pc-windows-msvc pnpm build
# Notes: this defaults only apply on run tasks.
defaults:
run:
working-directory: "nodejs"

name: windows - ${{ matrix.settings.target }}

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '18'
cache: pnpm
cache-dependency-path: "nodejs/pnpm-lock.yaml"
- name: Corepack
run: corepack enable
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
shell: bash
run: ${{ matrix.settings.build }}
- uses: actions/upload-artifact@v3
with:
name: bindings-windows-${{ matrix.settings.target }}
path: nodejs/*.node

macos:
runs-on: macos-latest
if: "startsWith(github.ref, 'refs/tags/')"
strategy:
matrix:
settings:
- target: x86_64-apple-darwin
build: |
pnpm build
strip -x *.node
- target: aarch64-apple-darwin
build: |
sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*;
export CC=$(xcrun -f clang);
export CXX=$(xcrun -f clang++);
SYSROOT=$(xcrun --sdk macosx --show-sdk-path);
export CFLAGS="-isysroot $SYSROOT -isystem $SYSROOT";
rustup target add aarch64-apple-darwin;
export NAPI_TARGET=aarch64-apple-darwin;
pnpm build
strip -x *.node
# Notes: this defaults only apply on run tasks.
defaults:
run:
working-directory: "nodejs"

name: macos - ${{ matrix.settings.target }}

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '18'
cache: pnpm
cache-dependency-path: "nodejs/pnpm-lock.yaml"
- name: Corepack
run: corepack enable
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Build
run: ${{ matrix.settings.build }}
shell: bash
- uses: actions/upload-artifact@v3
with:
name: bindings-macos-${{ matrix.settings.target }}
path: nodejs/*.node

release:
name: Release
runs-on: ubuntu-latest
needs: [macos, linux, windows]
permissions:
id-token: write

# Notes: this defaults only apply on run tasks.
defaults:
run:
working-directory: "nodejs"

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: "18"
cache: pnpm
cache-dependency-path: "nodejs/pnpm-lock.yaml"
- name: Corepack
run: corepack enable
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Download all artifacts
uses: actions/download-artifact@v3
with:
path: nodejs/artifacts
- name: Move artifacts
run: pnpm exec napi artifacts

- name: List packages
run: ls -R ./npm
shell: bash

- name: Publish Dry Run
if: "startsWith(github.ref, 'refs/tags/') && contains(github.ref, '-')"
# Since this command will not exit with non-zero code when file missing,
# we need to check the output manually.
run: |
npm publish --access public --provenance --dry-run
- name: Publish
if: "startsWith(github.ref, 'refs/tags/') && !contains(github.ref, '-')"
run: |
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
npm publish --access public --provenance
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 4 additions & 0 deletions .github/workflows/python-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ on:
tags:
- '*'
pull_request:
branches:
- main
paths:
- "python/**"
workflow_dispatch:

permissions:
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ result = api.exec_program(args)
print(result.yaml_result)
```

## Node.js

```ts
import { execProgram, ExecProgramArgs } from 'kcl-lib'

function main() {
const result = execProgram(ExecProgramArgs(['__test__/test_data/schema.k']))
console.log(result.yamlResult)
}

main();
```

## License

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fkcl-lang%2Flib.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fkcl-lang%2Flib?ref=badge_large)
5 changes: 5 additions & 0 deletions nodejs/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"
rustflags = ["-C", "target-feature=-crt-static"]
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
Loading

0 comments on commit 0b635f0

Please sign in to comment.