From c8bd8924e220ac39cfc29cf9e0e18eee6c36a912 Mon Sep 17 00:00:00 2001 From: Francesco Benedetto Date: Mon, 20 Mar 2023 07:19:18 +0100 Subject: [PATCH] fix(cli): export non const enums when generating typedefs (#1527) * fix(cli): export non const enums when generating typedefs * Make --const-enum as a build flag --------- Co-authored-by: LongYinan --- cli/src/build.ts | 22 +++++++++++++++++++--- cli/src/consts.ts | 2 ++ examples/napi/src/threadsafe_function.rs | 1 - 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 02268fa7..052623e7 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -121,6 +121,12 @@ export class BuildCommand extends Command { )} file, relative to cwd`, }) + constEnum?: boolean = Option.Boolean('--const-enum', true, { + description: `Generate ${chalk.green( + 'const enum', + )} in .d.ts file or not, default is ${chalk.green('true')}`, + }) + noDtsHeader = Option.Boolean('--no-dts-header', false, { description: `Don't generate ${chalk.green('.d.ts')} header`, }) @@ -440,7 +446,12 @@ export class BuildCommand extends Command { }) } - const { binaryName, packageName } = getNapiConfig(this.configFileName) + const { + binaryName, + packageName, + tsConstEnum: tsConstEnumFromConfig, + } = getNapiConfig(this.configFileName) + const tsConstEnum = this.constEnum ?? tsConstEnumFromConfig let cargoArtifactName = this.cargoName if (!cargoArtifactName) { if (this.bin) { @@ -617,6 +628,7 @@ export class BuildCommand extends Command { intermediateTypeFile, dtsFilePath, this.noDtsHeader, + tsConstEnum, ) await writeJsBinding( binaryName, @@ -666,6 +678,7 @@ async function processIntermediateTypeFile( source: string, target: string, noDtsHeader: boolean, + tsConstEnum: boolean, ): Promise { const idents: string[] = [] if (!existsSync(source)) { @@ -724,9 +737,12 @@ async function processIntermediateTypeFile( if (!nested) { idents.push(def.name) } + const enumPrefix = tsConstEnum ? ' const' : '' dts += - indentLines(`${def.js_doc}export const enum ${def.name} {`, nest) + - '\n' + indentLines( + `${def.js_doc}export${enumPrefix} enum ${def.name} {`, + nest, + ) + '\n' dts += indentLines(def.def, nest + 2) + '\n' dts += indentLines(`}`, nest) + '\n' break diff --git a/cli/src/consts.ts b/cli/src/consts.ts index 646cef3c..c4d03303 100644 --- a/cli/src/consts.ts +++ b/cli/src/consts.ts @@ -15,6 +15,7 @@ export function getNapiConfig( ).map(parseTriple) const defaultPlatforms = napi?.triples?.defaults === false ? [] : [...DefaultPlatforms] + const tsConstEnum: boolean = napi?.ts?.constEnum ?? true const platforms = [...defaultPlatforms, ...additionPlatforms] const releaseVersion = process.env.RELEASE_VERSION const releaseVersionWithoutPrefix = releaseVersion?.startsWith('v') @@ -34,5 +35,6 @@ export function getNapiConfig( packageJsonPath, content: pkgJson, npmClient, + tsConstEnum, } } diff --git a/examples/napi/src/threadsafe_function.rs b/examples/napi/src/threadsafe_function.rs index f4adcf50..d54ee7b6 100644 --- a/examples/napi/src/threadsafe_function.rs +++ b/examples/napi/src/threadsafe_function.rs @@ -137,7 +137,6 @@ pub async fn tsfn_return_promise(func: ThreadsafeFunction) -> Result { pub async fn tsfn_return_promise_timeout(func: ThreadsafeFunction) -> Result { use tokio::time::{self, Duration}; let promise = func.call_async::>(Ok(1)).await?; - let sleep = time::sleep(Duration::from_millis(100)); let sleep = time::sleep(Duration::from_nanos(1)); tokio::select! { _ = sleep => {