Skip to content

Commit

Permalink
feat: add defineCommand interface (#2)
Browse files Browse the repository at this point in the history
* feat: add `defineCommand` interface

* chore: initialize changeset

* chore: bump version
  • Loading branch information
fu050409 authored Jul 19, 2024
1 parent 13c0a37 commit 2493536
Show file tree
Hide file tree
Showing 9 changed files with 1,044 additions and 68 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/small-ducks-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ntscl': patch
---

Add `defineCommand` interface
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"version": "napi version"
},
"devDependencies": {
"@changesets/cli": "^2.27.7",
"@napi-rs/cli": "^2.18.4",
"@swc-node/register": "^1.10.6",
"@swc/core": "^1.6.13",
Expand Down
Loading

0 comments on commit 2493536

Please sign in to comment.