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`,
})
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<string[]> {
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

View file

@ -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,
}
}

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> {
use tokio::time::{self, Duration};
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));
tokio::select! {
_ = sleep => {