Skip to content

Commit

Permalink
feat: initial nodejs kcl lib
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Apr 15, 2024
1 parent 6337a76 commit d05fd4c
Show file tree
Hide file tree
Showing 37 changed files with 2,408 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/nodejs-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: nodejs-test

on:
push:
branches:
- main
- master
tags:
- '*'
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
test:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
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

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

- name: Building
working-directory: nodejs
run: pnpm build

- name: Testing
working-directory: nodejs
run: pnpm test
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"]
197 changes: 197 additions & 0 deletions nodejs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# End of https://www.toptal.com/developers/gitignore/api/node

# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud

# End of https://www.toptal.com/developers/gitignore/api/macos

# Created by https://www.toptal.com/developers/gitignore/api/windows
# Edit at https://www.toptal.com/developers/gitignore?templates=windows

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/windows

#Added by cargo

/target
Cargo.lock

.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

*.node
14 changes: 14 additions & 0 deletions nodejs/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
target
Cargo.lock
.cargo
.github
npm
.eslintrc
.prettierignore
rustfmt.toml
yarn.lock
pnpm-lock.yaml
*.node
.yarn
__test__
renovate.json
1 change: 1 addition & 0 deletions nodejs/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shell-emulator=true
5 changes: 5 additions & 0 deletions nodejs/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target
generated.js
generated.d.ts
.yarn
pnpm-lock.yaml
20 changes: 20 additions & 0 deletions nodejs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
edition = "2021"
name = "kcl-lib"
version = "0.0.0"

[lib]
crate-type = ["cdylib"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi-derive = "2.12.2"
kcl-lang = {path = "../"}

[build-dependencies]
napi-build = "2.0.1"

[profile.release]
lto = true
strip = "symbols"
10 changes: 10 additions & 0 deletions nodejs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
default: build

build:
pnpm build

fmt:
pnpm format

test:
pnpm test
48 changes: 48 additions & 0 deletions nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# KCL Artifact Lib for Node.js

## Installation

```shell
npm install kcl-lib
```

## Quick Start

```javascript
import { execProgram, ExecProgramArgs } from "kcl-lib";

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

main();
```

## Developing

- Install `node.js` and `pnpm`
- Install `cargo` (for Rust code)
- Install dependencies

```shell
pnpm install
```

### Building

```shell
pnpm build
```

### Testing

```shell
pnpm test
```

### Format

```shell
pnpm format
```
8 changes: 8 additions & 0 deletions nodejs/__test__/exec_program.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import test from 'ava'

import { execProgram, ExecProgramArgs } from '../index.js'

test('execProgram', (t) => {
const result = execProgram(ExecProgramArgs(['__test__/test_data/schema.k']))
t.is(result.yamlResult, 'app:\n replicas: 2')
})
8 changes: 8 additions & 0 deletions nodejs/__test__/list_variables.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import test from 'ava'

import { listVariables, ListVariablesArgs } from '../index.js'

test('listVariables', (t) => {
const result = listVariables(ListVariablesArgs('__test__/test_data/schema.k', []))
t.is(result.variables['app'].value, 'AppConfig {replicas: 2}')
})
Loading

0 comments on commit d05fd4c

Please sign in to comment.