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 <lynweklm@gmail.com>
This commit is contained in:
Francesco Benedetto 2023-03-20 07:19:18 +01:00 committed by GitHub
parent d255a0a575
commit c8bd8924e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 4 deletions

View file

@ -121,6 +121,12 @@ export class BuildCommand extends Command {
)} file, relative to cwd`, )} 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, { noDtsHeader = Option.Boolean('--no-dts-header', false, {
description: `Don't generate ${chalk.green('.d.ts')} header`, 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 let cargoArtifactName = this.cargoName
if (!cargoArtifactName) { if (!cargoArtifactName) {
if (this.bin) { if (this.bin) {
@ -617,6 +628,7 @@ export class BuildCommand extends Command {
intermediateTypeFile, intermediateTypeFile,
dtsFilePath, dtsFilePath,
this.noDtsHeader, this.noDtsHeader,
tsConstEnum,
) )
await writeJsBinding( await writeJsBinding(
binaryName, binaryName,
@ -666,6 +678,7 @@ async function processIntermediateTypeFile(
source: string, source: string,
target: string, target: string,
noDtsHeader: boolean, noDtsHeader: boolean,
tsConstEnum: boolean,
): Promise<string[]> { ): Promise<string[]> {
const idents: string[] = [] const idents: string[] = []
if (!existsSync(source)) { if (!existsSync(source)) {
@ -724,9 +737,12 @@ async function processIntermediateTypeFile(
if (!nested) { if (!nested) {
idents.push(def.name) idents.push(def.name)
} }
const enumPrefix = tsConstEnum ? ' const' : ''
dts += dts +=
indentLines(`${def.js_doc}export const enum ${def.name} {`, nest) + indentLines(
'\n' `${def.js_doc}export${enumPrefix} enum ${def.name} {`,
nest,
) + '\n'
dts += indentLines(def.def, nest + 2) + '\n' dts += indentLines(def.def, nest + 2) + '\n'
dts += indentLines(`}`, nest) + '\n' dts += indentLines(`}`, nest) + '\n'
break break

View file

@ -15,6 +15,7 @@ export function getNapiConfig(
).map(parseTriple) ).map(parseTriple)
const defaultPlatforms = const defaultPlatforms =
napi?.triples?.defaults === false ? [] : [...DefaultPlatforms] napi?.triples?.defaults === false ? [] : [...DefaultPlatforms]
const tsConstEnum: boolean = napi?.ts?.constEnum ?? true
const platforms = [...defaultPlatforms, ...additionPlatforms] const platforms = [...defaultPlatforms, ...additionPlatforms]
const releaseVersion = process.env.RELEASE_VERSION const releaseVersion = process.env.RELEASE_VERSION
const releaseVersionWithoutPrefix = releaseVersion?.startsWith('v') const releaseVersionWithoutPrefix = releaseVersion?.startsWith('v')
@ -34,5 +35,6 @@ export function getNapiConfig(
packageJsonPath, packageJsonPath,
content: pkgJson, content: pkgJson,
npmClient, npmClient,
tsConstEnum,
} }
} }

View file

@ -137,7 +137,6 @@ pub async fn tsfn_return_promise(func: ThreadsafeFunction<u32>) -> Result<u32> {
pub async fn tsfn_return_promise_timeout(func: ThreadsafeFunction<u32>) -> Result<u32> { pub async fn tsfn_return_promise_timeout(func: ThreadsafeFunction<u32>) -> Result<u32> {
use tokio::time::{self, Duration}; use tokio::time::{self, Duration};
let promise = func.call_async::<Promise<u32>>(Ok(1)).await?; let promise = func.call_async::<Promise<u32>>(Ok(1)).await?;
let sleep = time::sleep(Duration::from_millis(100));
let sleep = time::sleep(Duration::from_nanos(1)); let sleep = time::sleep(Duration::from_nanos(1));
tokio::select! { tokio::select! {
_ = sleep => { _ = sleep => {