fix(cli): add back constEnum option (#1829)

This commit is contained in:
LongYinan 2023-11-30 20:00:52 +08:00 committed by GitHub
parent 64a99eadd3
commit afa040660c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 614 additions and 17 deletions

View file

@ -159,6 +159,11 @@ const BUILD_OPTIONS: CommandSchema = {
description:
'Package name in generated js binding file. Only works with `--platform` flag',
},
{
name: 'constEnum',
type: 'boolean',
description: 'Whether generate const enum for typescript bindings',
},
{
name: 'jsBinding',
type: 'string',

View file

@ -33,6 +33,7 @@ new NapiCli().build({
| outputDir | --output-dir,-o | string | false | | Path to where all the built files would be put. Default to the crate folder |
| platform | --platform | boolean | false | | Add platform triple to the generated nodejs binding file, eg: `[name].linux-x64-gnu.node` |
| jsPackageName | --js-package-name | string | false | | Package name in generated js binding file. Only works with `--platform` flag |
| constEnum | --const-enum | boolean | false | | Whether generate const enum for typescript bindings |
| jsBinding | --js | string | false | | Path and filename of generated JS binding file. Only works with `--platform` flag. Relative to `--output_dir`. |
| noJsBinding | --no-js | boolean | false | | Whether to disable the generation JS binding file. Only works with `--platform` flag. |
| dts | --dts | string | false | | Path and filename of generated type def file. Relative to `--output_dir` |

View file

@ -81,8 +81,8 @@ export async function buildProject(options: BuildOptions) {
options.target
? parseTriple(options.target)
: process.env.CARGO_BUILD_TARGET
? parseTriple(process.env.CARGO_BUILD_TARGET)
: getSystemDefaultTarget(),
? parseTriple(process.env.CARGO_BUILD_TARGET)
: getSystemDefaultTarget(),
crateDir,
resolvePath(options.outputDir ?? crateDir),
options.targetDir ??
@ -373,8 +373,8 @@ class Builder {
process.platform === 'darwin'
? 'darwin'
: process.platform === 'win32'
? 'windows'
: 'linux'
? 'windows'
: 'linux'
Object.assign(this.envs, {
CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-android24-clang`,
CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-androideabi24-clang`,
@ -559,10 +559,10 @@ class Builder {
this.target.platform === 'darwin'
? `lib${cdyLib}.dylib`
: this.target.platform === 'win32'
? `${cdyLib}.dll`
: this.target.platform === 'wasi' || this.target.platform === 'wasm'
? `${cdyLib}.wasm`
: `lib${cdyLib}.so`
? `${cdyLib}.dll`
: this.target.platform === 'wasi' || this.target.platform === 'wasm'
? `${cdyLib}.wasm`
: `lib${cdyLib}.so`
let destName = this.config.binaryName
// add platform suffix to binary name
@ -597,6 +597,7 @@ class Builder {
const { dts, exports } = await processTypeDef(
this.envs.TYPE_DEF_TMP_PATH,
this.options.constEnum ?? true,
!this.options.noDtsHeader
? this.options.dtsHeader ?? DEFAULT_TYPE_DEF_HEADER
: '',

View file

@ -47,6 +47,10 @@ export abstract class BaseBuildCommand extends Command {
'Package name in generated js binding file. Only works with `--platform` flag',
})
constEnum?: boolean = Option.Boolean('--const-enum', {
description: 'Whether generate const enum for typescript bindings',
})
jsBinding?: string = Option.String('--js', {
description:
'Path and filename of generated JS binding file. Only works with `--platform` flag. Relative to `--output_dir`.',
@ -133,6 +137,7 @@ export abstract class BaseBuildCommand extends Command {
outputDir: this.outputDir,
platform: this.platform,
jsPackageName: this.jsPackageName,
constEnum: this.constEnum,
jsBinding: this.jsBinding,
noJsBinding: this.noJsBinding,
dts: this.dts,
@ -190,6 +195,10 @@ export interface BuildOptions {
* Package name in generated js binding file. Only works with `--platform` flag
*/
jsPackageName?: string
/**
* Whether generate const enum for typescript bindings
*/
constEnum?: boolean
/**
* Path and filename of generated JS binding file. Only works with `--platform` flag. Relative to `--output_dir`.
*/

View file

@ -616,3 +616,558 @@ Generated by [AVA](https://avajs.dev).
}␊
`
## should process type def with noConstEnum correctly
> Snapshot 1
`␊
export class ExternalObject<T> {␊
readonly '': {␊
readonly '': unique symbol␊
[K: symbol]: T␊
}␊
}␊
/**␊
* \`constructor\` option for \`struct\` requires all fields to be public,␊
* otherwise tag impl fn as constructor␊
* #[napi(constructor)]␊
*/␊
export class Animal {␊
/** Kind of animal */␊
readonly kind: Kind␊
/** This is the constructor */␊
constructor(kind: Kind, name: string)␊
/** This is a factory method */␊
static withKind(kind: Kind): Animal␊
get name(): string␊
set name(name: string)␊
get type(): Kind␊
set type(kind: Kind)␊
/**␊
* This is a␊
* multi-line comment␊
* with an emoji 🚀␊
*/␊
whoami(): string␊
/** This is static... */␊
static getDogKind(): Kind␊
/**␊
* Here are some characters and character sequences␊
* that should be escaped correctly:␊
* \\[]{}/\\:""{␊
* }␊
*/␊
returnOtherClass(): Dog␊
returnOtherClassWithCustomConstructor(): Bird␊
overrideIndividualArgOnMethod(normalTy: string, overriddenTy: {n: string}): Bird␊
}␊
export class AnimalWithDefaultConstructor {␊
name: string␊
kind: number␊
constructor(name: string, kind: number)␊
}␊
export class AnotherClassForEither {␊
constructor()␊
}␊
export class AnotherCssStyleSheet {␊
get rules(): CssRuleList␊
}␊
export type AnotherCSSStyleSheet = AnotherCssStyleSheet␊
export class Asset {␊
constructor()␊
get filePath(): number␊
}␊
export type JsAsset = Asset␊
export class Assets {␊
constructor()␊
get(id: number): JsAsset | null␊
}␊
export type JsAssets = Assets␊
export class Bird {␊
name: string␊
constructor(name: string)␊
getCount(): number␊
getNameAsync(): Promise<string>
}␊
/** Smoking test for type generation */␊
export class Blake2BHasher {␊
static withKey(key: Blake2bKey): Blake2BHasher␊
update(data: Buffer): void␊
}␊
export type Blake2bHasher = Blake2BHasher␊
export class Blake2BKey {␊
}␊
export type Blake2bKey = Blake2BKey␊
export class ClassWithFactory {␊
name: string␊
static withName(name: string): ClassWithFactory␊
setName(name: string): this␊
}␊
export class Context {␊
maybeNeed?: boolean␊
buffer: Uint8Array␊
constructor()␊
static withData(data: string): Context␊
static withBuffer(buf: Uint8Array): Context␊
method(): string␊
}␊
export class CssRuleList {␊
getRules(): Array<string>
get parentStyleSheet(): CSSStyleSheet␊
get name(): string | null␊
}␊
export type CSSRuleList = CssRuleList␊
export class CssStyleSheet {␊
constructor(name: string, rules: Array<string>)␊
get rules(): CssRuleList␊
anotherCssStyleSheet(): AnotherCssStyleSheet␊
}␊
export type CSSStyleSheet = CssStyleSheet␊
export class CustomFinalize {␊
constructor(width: number, height: number)␊
}␊
export class Dog {␊
name: string␊
constructor(name: string)␊
}␊
export class Fib {␊
[Symbol.iterator](): Iterator<number, void, number>
constructor()␊
}␊
export class Fib2 {␊
[Symbol.iterator](): Iterator<number, void, number>
static create(seed: number): Fib2␊
}␊
export class Fib3 {␊
current: number␊
next: number␊
constructor(current: number, next: number)␊
[Symbol.iterator](): Iterator<number, void, number>
}␊
export class JsClassForEither {␊
constructor()␊
}␊
export class JsRemote {␊
name(): string␊
}␊
export class JsRepo {␊
constructor(dir: string)␊
remote(): JsRemote␊
}␊
export class NinjaTurtle {␊
name: string␊
static isInstanceOf(value: unknown): boolean␊
/** Create your ninja turtle! 🐢 */␊
static newRaph(): NinjaTurtle␊
getMaskColor(): string␊
getName(): string␊
returnThis(this: this): this␊
}␊
export class NotWritableClass {␊
name: string␊
constructor(name: string)␊
setName(name: string): void␊
}␊
export class Optional {␊
static optionEnd(required: string, optional?: string | undefined | null): string␊
static optionStart(optional: string | undefined | null, required: string): string␊
static optionStartEnd(optional1: string | undefined | null, required: string, optional2?: string | undefined | null): string␊
static optionOnly(optional?: string | undefined | null): string␊
}␊
export class Width {␊
value: number␊
constructor(value: number)␊
}␊
export interface A {␊
foo: number␊
}␊
export function acceptThreadsafeFunction(func: (err: Error | null, value: number) => any): void␊
export function acceptThreadsafeFunctionFatal(func: (value: number) => any): void␊
export function acceptThreadsafeFunctionTupleArgs(func: (err: Error | null, arg0: number, arg1: boolean, arg2: string) => any): void␊
export function add(a: number, b: number): number␊
export enum ALIAS {␊
A = 0,␊
B = 1␊
}␊
export interface AliasedStruct {␊
a: ALIAS␊
b: number␊
}␊
export interface AllOptionalObject {␊
name?: string␊
age?: number␊
}␊
export function appendBuffer(buf: Buffer): Buffer␊
export function arrayBufferPassThrough(buf: Uint8Array): Promise<Uint8Array>
export function asyncMultiTwo(arg: number): Promise<number>
export function asyncPlus100(p: Promise<number>): Promise<number>
export function asyncReduceBuffer(buf: Buffer): Promise<number>
export interface B {␊
bar: number␊
}␊
export function bigintAdd(a: bigint, b: bigint): bigint␊
export function bigintFromI128(): bigint␊
export function bigintFromI64(): bigint␊
export function bigintGetU64AsString(bi: bigint): string␊
export function bufferPassThrough(buf: Buffer): Promise<Buffer>
export interface C {␊
baz: number␊
}␊
export function callbackReturnPromise<T>(functionInput: () => T | Promise<T>, callback: (err: Error | null, result: T) => void): T | Promise<T>
export function callThreadsafeFunction(callback: (...args: any[]) => any): void␊
export function captureErrorInCallback(cb1: () => void, cb2: (arg0: Error) => void): void␊
export function chronoDateAdd1Minute(input: Date): Date␊
export function chronoDateToMillis(input: Date): number␊
export function concatLatin1(s: string): string␊
export function concatStr(s: string): string␊
export function concatUtf16(s: string): string␊
export function contains(source: string, target: string): boolean␊
export function convertU32Array(input: Uint32Array): Array<number>
export function createBigInt(): bigint␊
export function createBigIntI64(): bigint␊
export function createExternal(size: number): ExternalObject<number>
export function createExternalString(content: string): ExternalObject<string>
export function createExternalTypedArray(): Uint32Array␊
export function createObj(): object␊
export function createObjectWithClassField(): ObjectFieldClassInstance␊
export function createObjWithProperty(): { value: ArrayBuffer, get getter(): number }␊
export function createSymbol(): symbol␊
/** You could break the step and for an new continuous value. */␊
export enum CustomNumEnum {␊
One = 1,␊
Two = 2,␊
Three = 3,␊
Four = 4,␊
Six = 6,␊
Eight = 8,␊
Nine = 9,␊
Ten = 10␊
}␊
export function customStatusCode(): void␊
export interface Dates {␊
start: Date␊
end?: Date␊
}␊
export function dateToNumber(input: Date): number␊
/** This is a const */␊
export const DEFAULT_COST: number␊
export function derefUint8Array(a: Uint8Array, b: Uint8ClampedArray): number␊
export function either3(input: string | number | boolean): number␊
export function either4(input: string | number | boolean | Obj): number␊
export function eitherBoolOrFunction(input: boolean | ((...args: any[]) => any)): void␊
export function eitherFromObjects(input: A | B | C): string␊
export function eitherFromOption(): JsClassForEither | undefined␊
export function eitherStringOrNumber(input: string | number): number␊
export enum Empty {␊
}␊
export function enumToI32(e: CustomNumEnum): number␊
export function fibonacci(n: number): number␊
export function fnReceivedAliased(s: AliasedStruct, e: ALIAS): void␊
export function getBuffer(): Buffer␊
export function getCwd(callback: (arg0: string) => void): void␊
export function getEmptyBuffer(): Buffer␊
export function getExternal(external: ExternalObject<number>): number␊
export function getGlobal(): typeof global␊
export function getMapping(): Record<string, number>
export function getNestedNumArr(): number[][][]␊
export function getNull(): null␊
export function getNumArr(): number[]␊
/** Gets some numbers */␊
export function getNums(): Array<number>
export function getPackageJsonName(packageJson: PackageJson): string␊
export function getStrFromObject(): void␊
export function getterFromObj(): number␊
export function getUndefined(): void␊
export function getWords(): Array<string>
/** default enum values are continuos i32s start from 0 */␊
export enum Kind {␊
/** Barks */␊
Dog = 0,␊
/** Kills birds */␊
Cat = 1,␊
/** Tasty */␊
Duck = 2␊
}␊
export function listObjKeys(obj: object): Array<string>
export function mapOption(val?: number | undefined | null): number | null␊
export function mutateExternal(external: ExternalObject<number>, newVal: number): void␊
export function mutateTypedArray(input: Float32Array): void␊
export interface Obj {␊
v: string | number␊
}␊
export interface ObjectFieldClassInstance {␊
bird: Bird␊
}␊
export interface ObjectOnlyFromJs {␊
count: number␊
callback: (err: Error | null, value: number) => any␊
}␊
export function optionEnd(callback: (arg0: string, arg1?: string | undefined | null) => void): void␊
export function optionOnly(callback: (arg0?: string | undefined | null) => void): void␊
export function optionStart(callback: (arg0: string | undefined | null, arg1: string) => void): void␊
export function optionStartEnd(callback: (arg0: string | undefined | null, arg1: string, arg2?: string | undefined | null) => void): void␊
export function overrideIndividualArgOnFunction(notOverridden: string, f: () => string, notOverridden2: number): string␊
export function overrideIndividualArgOnFunctionWithCbArg(callback: (town: string, name?: string | undefined | null) => string, notOverridden: number): object␊
/** This is an interface for package.json */␊
export interface PackageJson {␊
name: string␊
/** The version of the package */␊
version: string␊
dependencies?: Record<string, any>
devDependencies?: Record<string, any>
}␊
export function panic(): void␊
export function plusOne(this: Width): number␊
export function promiseInEither(input: number | Promise<number>): Promise<boolean>
/** napi = { version = 2, features = ["serde-json"] } */␊
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void␊
export function readFileAsync(path: string): Promise<Buffer>
export function readPackageJson(): PackageJson␊
export function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void␊
export function receiveClassOrNumber(either: number | JsClassForEither): number␊
export function receiveDifferentClass(either: JsClassForEither | AnotherClassForEither): number␊
export function receiveMutClassOrNumber(either: number | JsClassForEither): number␊
export function receiveObjectOnlyFromJs(obj: { count: number, callback: (err: Error | null, count: number) => void }): void␊
export function receiveObjectWithClassField(object: ObjectFieldClassInstance): Bird␊
export function receiveStrictObject(strictObject: StrictObject): void␊
export function receiveString(s: string): string␊
export function returnEither(input: number): string | number␊
export function returnEitherClass(input: number): number | JsClassForEither␊
export function returnJsFunction(): (...args: any[]) => any␊
export function returnNull(): null␊
export function returnUndefined(): void␊
export function returnUndefinedIfInvalid(input: boolean): boolean␊
export function returnUndefinedIfInvalidPromise(input: Promise<boolean>): Promise<boolean>
export function roundtripStr(s: string): string␊
export function runScript(script: string): unknown␊
export function setSymbolInObj(symbol: symbol): object␊
export interface StrictObject {␊
name: string␊
}␊
export function sumMapping(nums: Record<string, number>): number␊
export function sumNums(nums: Array<number>): number␊
export function testSerdeRoundtrip(data: any): any␊
export function threadsafeFunctionClosureCapture(func: (...args: any[]) => any): void␊
export function threadsafeFunctionFatalMode(cb: (...args: any[]) => any): void␊
export function threadsafeFunctionFatalModeError(cb: (...args: any[]) => any): void␊
export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void␊
export function throwError(): void␊
export function toJsObj(): object␊
export function tsfnAsyncCall(func: (...args: any[]) => any): Promise<void>
export function tsfnCallWithCallback(func: (...args: any[]) => any): void␊
export function tsRename(a: { foo: number }): string[]␊
export interface TsTypeChanged {␊
typeOverride: object␊
typeOverrideOptional?: object␊
}␊
export function validateArray(arr: Array<number>): number␊
export function validateBigint(input: bigint): bigint␊
export function validateBoolean(i: boolean): boolean␊
export function validateBuffer(b: Buffer): number␊
export function validateDate(d: Date): number␊
export function validateDateTime(d: Date): number␊
export function validateExternal(e: ExternalObject<number>): number␊
export function validateFunction(cb: () => number): number␊
export function validateHashMap(input: Record<string, number>): number␊
export function validateNull(i: null): boolean␊
export function validateNumber(i: number): number␊
export function validateOptional(input1?: string | undefined | null, input2?: boolean | undefined | null): boolean␊
export function validatePromise(p: Promise<number>): Promise<number>
export function validateString(s: string): string␊
export function validateSymbol(s: symbol): boolean␊
export function validateTypedArray(input: Uint8Array): number␊
export function validateUndefined(i: undefined): boolean␊
export function withAbortController(a: number, b: number, signal: AbortSignal): Promise<number>
export function withoutAbortController(a: number, b: number): Promise<number>
export function xxh64Alias(input: Buffer): bigint␊
export namespace xxh2 {␊
export function xxh2Plus(a: number, b: number): number␊
export function xxh3Xxh64Alias(input: Buffer): bigint␊
}␊
export namespace xxh3 {␊
/** Xxh3 class */␊
export class Xxh3 {␊
constructor()␊
/** update */␊
update(input: Buffer): void␊
digest(): bigint␊
}␊
export const ALIGNMENT: number␊
/** xxh128 function */␊
export function xxh128(input: Buffer): bigint␊
export function xxh3_64(input: Buffer): bigint␊
}␊
`

View file

@ -18,13 +18,13 @@ test('should ident string correctly', (t) => {
foo() {
a = b
}
bar = () => {
}
boz = 1
}
namespace B {
namespace C {
type D = A
@ -43,6 +43,21 @@ test('should process type def correctly', async (t) => {
'__fixtures__',
'napi_type_def',
),
true,
)
t.snapshot(dts)
})
test('should process type def with noConstEnum correctly', async (t) => {
const { dts } = await processTypeDef(
join(
fileURLToPath(import.meta.url),
'../',
'__fixtures__',
'napi_type_def',
),
false,
)
t.snapshot(dts)

View file

@ -24,6 +24,11 @@ interface UserNapiConfig {
*/
npmClient?: string
/**
* Whether generate const enum for typescript bindings
*/
constEnum?: boolean
/**
* @deprecated binaryName instead
*/
@ -114,8 +119,8 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
let targets: string[] = userNapiConfig.targets ?? []
// compatible with old config
if (userNapiConfig.package?.name) {
napiConfig.packageName = userNapiConfig.package.name
if (userNapiConfig?.name) {
napiConfig.packageName = userNapiConfig.name
}
if (!targets.length) {

View file

@ -25,7 +25,11 @@ interface TypeDefLine {
js_mod?: string
}
function prettyPrint(line: TypeDefLine, ident: number): string {
function prettyPrint(
line: TypeDefLine,
constEnum: boolean,
ident: number,
): string {
let s = line.js_doc ?? ''
switch (line.kind) {
case TypeDefKind.Interface:
@ -33,7 +37,8 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
break
case TypeDefKind.Enum:
s += `export const enum ${line.name} {\n${line.def}\n}`
const enumName = constEnum ? 'const enum' : 'enum'
s += `export ${enumName} ${line.name} {\n${line.def}\n}`
break
case TypeDefKind.Struct:
@ -52,6 +57,7 @@ function prettyPrint(line: TypeDefLine, ident: number): string {
export async function processTypeDef(
intermediateTypeFile: string,
constEnum: boolean,
header?: string,
) {
const exports: string[] = []
@ -65,7 +71,7 @@ export async function processTypeDef(
([namespace, defs]) => {
if (namespace === TOP_LEVEL_NAMESPACE) {
for (const def of defs) {
dts += prettyPrint(def, 0) + '\n\n'
dts += prettyPrint(def, constEnum, 0) + '\n\n'
switch (def.kind) {
case TypeDefKind.Const:
case TypeDefKind.Enum:
@ -85,7 +91,7 @@ export async function processTypeDef(
exports.push(namespace)
dts += `export namespace ${namespace} {\n`
for (const def of defs) {
dts += prettyPrint(def, 2) + '\n'
dts += prettyPrint(def, constEnum, 2) + '\n'
}
dts += '}\n\n'
}