Skip to content

Commit

Permalink
feat: add defineCommand interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Jul 19, 2024
1 parent 13c0a37 commit 4b2b7c9
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 63 deletions.
14 changes: 10 additions & 4 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import test from 'ava'

import { plus100 } from '../index'
import { defineCommand } from '../index'

test('sync function from native code', (t) => {
const fixture = 42
t.is(plus100(fixture), fixture + 100)
test('define command', (t) => {
const cmd = {
meta: {},
options: {},
callback: (ctx: any) => {
console.log(ctx)
},
}
t.deepEqual(defineCommand(cmd), cmd)
})
19 changes: 18 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,21 @@

/* auto-generated by NAPI-RS */

export function plus100(input: number): number
export interface Context {

}
export interface CommandMeta {
name?: string
version?: string
description?: string
hidden?: boolean
}
export interface CommandOption {

}
export interface Command {
meta: CommandMeta
options: Record<string, CommandOption>
callback: (ctx: Context) => void
}
export declare function defineCommand(options: Command): Command
138 changes: 82 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'package-template.android-arm64.node'))
localFileExisted = existsSync(join(__dirname, 'ntscl.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./package-template.android-arm64.node')
nativeBinding = require('./ntscl.android-arm64.node')
} else {
nativeBinding = require('@napi-rs/package-template-android-arm64')
nativeBinding = require('ntscl-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'package-template.android-arm-eabi.node'))
localFileExisted = existsSync(join(__dirname, 'ntscl.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./package-template.android-arm-eabi.node')
nativeBinding = require('./ntscl.android-arm-eabi.node')
} else {
nativeBinding = require('@napi-rs/package-template-android-arm-eabi')
nativeBinding = require('ntscl-android-arm-eabi')
}
} catch (e) {
loadError = e
Expand All @@ -62,36 +62,42 @@ switch (platform) {
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'package-template.win32-x64-msvc.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.win32-x64-msvc.node')
nativeBinding = require('./ntscl.win32-x64-msvc.node')
} else {
nativeBinding = require('@napi-rs/package-template-win32-x64-msvc')
nativeBinding = require('ntscl-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(join(__dirname, 'package-template.win32-ia32-msvc.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.win32-ia32-msvc.node')
nativeBinding = require('./ntscl.win32-ia32-msvc.node')
} else {
nativeBinding = require('@napi-rs/package-template-win32-ia32-msvc')
nativeBinding = require('ntscl-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'package-template.win32-arm64-msvc.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.win32-arm64-msvc.node')
nativeBinding = require('./ntscl.win32-arm64-msvc.node')
} else {
nativeBinding = require('@napi-rs/package-template-win32-arm64-msvc')
nativeBinding = require('ntscl-win32-arm64-msvc')
}
} catch (e) {
loadError = e
Expand All @@ -102,35 +108,37 @@ switch (platform) {
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'package-template.darwin-universal.node'))
localFileExisted = existsSync(join(__dirname, 'ntscl.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./package-template.darwin-universal.node')
nativeBinding = require('./ntscl.darwin-universal.node')
} else {
nativeBinding = require('@napi-rs/package-template-darwin-universal')
nativeBinding = require('ntscl-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'package-template.darwin-x64.node'))
localFileExisted = existsSync(join(__dirname, 'ntscl.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./package-template.darwin-x64.node')
nativeBinding = require('./ntscl.darwin-x64.node')
} else {
nativeBinding = require('@napi-rs/package-template-darwin-x64')
nativeBinding = require('ntscl-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'package-template.darwin-arm64.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.darwin-arm64.node')
nativeBinding = require('./ntscl.darwin-arm64.node')
} else {
nativeBinding = require('@napi-rs/package-template-darwin-arm64')
nativeBinding = require('ntscl-darwin-arm64')
}
} catch (e) {
loadError = e
Expand All @@ -144,12 +152,12 @@ switch (platform) {
if (arch !== 'x64') {
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
}
localFileExisted = existsSync(join(__dirname, 'package-template.freebsd-x64.node'))
localFileExisted = existsSync(join(__dirname, 'ntscl.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./package-template.freebsd-x64.node')
nativeBinding = require('./ntscl.freebsd-x64.node')
} else {
nativeBinding = require('@napi-rs/package-template-freebsd-x64')
nativeBinding = require('ntscl-freebsd-x64')
}
} catch (e) {
loadError = e
Expand All @@ -159,23 +167,27 @@ switch (platform) {
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-x64-musl.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-x64-musl.node')
nativeBinding = require('./ntscl.linux-x64-musl.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-x64-musl')
nativeBinding = require('ntscl-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-x64-gnu.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-x64-gnu.node')
nativeBinding = require('./ntscl.linux-x64-gnu.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-x64-gnu')
nativeBinding = require('ntscl-linux-x64-gnu')
}
} catch (e) {
loadError = e
Expand All @@ -184,23 +196,27 @@ switch (platform) {
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-arm64-musl.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-arm64-musl.node')
nativeBinding = require('./ntscl.linux-arm64-musl.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-arm64-musl')
nativeBinding = require('ntscl-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-arm64-gnu.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-arm64-gnu.node')
nativeBinding = require('./ntscl.linux-arm64-gnu.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-arm64-gnu')
nativeBinding = require('ntscl-linux-arm64-gnu')
}
} catch (e) {
loadError = e
Expand All @@ -209,23 +225,27 @@ switch (platform) {
break
case 'arm':
if (isMusl()) {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-arm-musleabihf.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-arm-musleabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-arm-musleabihf.node')
nativeBinding = require('./ntscl.linux-arm-musleabihf.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-arm-musleabihf')
nativeBinding = require('ntscl-linux-arm-musleabihf')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-arm-gnueabihf.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-arm-gnueabihf.node')
nativeBinding = require('./ntscl.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-arm-gnueabihf')
nativeBinding = require('ntscl-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
Expand All @@ -234,36 +254,42 @@ switch (platform) {
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-riscv64-musl.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-riscv64-musl.node')
nativeBinding = require('./ntscl.linux-riscv64-musl.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-riscv64-musl')
nativeBinding = require('ntscl-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(join(__dirname, 'package-template.linux-riscv64-gnu.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-riscv64-gnu.node')
nativeBinding = require('./ntscl.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-riscv64-gnu')
nativeBinding = require('ntscl-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(join(__dirname, 'package-template.linux-s390x-gnu.node'))
localFileExisted = existsSync(
join(__dirname, 'ntscl.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./package-template.linux-s390x-gnu.node')
nativeBinding = require('./ntscl.linux-s390x-gnu.node')
} else {
nativeBinding = require('@napi-rs/package-template-linux-s390x-gnu')
nativeBinding = require('ntscl-linux-s390x-gnu')
}
} catch (e) {
loadError = e
Expand All @@ -284,6 +310,6 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { plus100 } = nativeBinding
const { defineCommand } = nativeBinding

module.exports.plus100 = plus100
module.exports.defineCommand = defineCommand
29 changes: 27 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
#![deny(clippy::all)]

use std::collections::HashMap;

use napi::JsFunction;
use napi_derive::napi;

#[napi(object)]
pub struct Context {}

#[napi(object)]
pub struct CommandMeta {
pub name: Option<String>,
pub version: Option<String>,
pub description: Option<String>,
pub hidden: Option<bool>,
}

#[napi(object)]
pub struct CommandOption {}

#[napi(object)]
pub struct Command {
pub meta: CommandMeta,
pub options: HashMap<String, CommandOption>,
#[napi(ts_type = "(ctx: Context) => void")]
pub callback: JsFunction,
}

#[napi]
pub fn plus_100(input: u32) -> u32 {
input + 100
pub fn define_command(options: Command) -> Command {
options
}

0 comments on commit 4b2b7c9

Please sign in to comment.