Skip to content

Commit

Permalink
fix(bindgen): Add Option module interface types imports
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Mar 1, 2023
1 parent de7eb88 commit 8495e23
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/bindgen/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
}

// track unique output types in this set
const importTypes = new Set()
const resultsImportTypes = new Set()

interfaceJson.outputs.forEach((output) => {
if (!interfaceJsonTypeToTypeScriptType.has(output.type)) {
Expand All @@ -239,16 +239,16 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
resultContent += ` /** ${output.description} */\n`
const outputType = interfaceJsonTypeToTypeScriptType.get(output.type)
if(typesRequireImport.includes(outputType)) {
importTypes.add(outputType)
resultsImportTypes.add(outputType)
}
resultContent += ` ${camelCase(output.name)}: ${outputType}\n\n`
readmeResultTable.push([`\`${camelCase(output.name)}\``, `*${outputType}*`, output.description])
})
readmeResult += markdownTable(readmeResultTable, { align: ['c', 'c', 'l'] }) + '\n'

// Insert the import statement in the beginning for the file.
if(importTypes.size !== 0)
resultContent = `import { ${Array.from(importTypes).join(',')} } from 'itk-wasm'\n\n` + resultContent;
if(resultsImportTypes.size !== 0)
resultContent = `import { ${Array.from(resultsImportTypes).join(',')} } from 'itk-wasm'\n\n` + resultContent;

resultContent += `}\n\nexport default ${modulePascalCase}${nodeTextCamel}Result\n`
fs.writeFileSync(path.join(srcOutputDir, `${moduleKebabCase}${nodeTextKebab}-result.ts`), resultContent)
Expand All @@ -259,10 +259,15 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
// Options module
const filteredParameters = interfaceJson.parameters.filter(p => { return p.name !== 'memory-io'})
const haveParameters = !!filteredParameters.length

// track unique output types in this set
const optionsImportTypes = new Set()

if (haveParameters) {
readmeOptions += `\n**\`${modulePascalCase}${nodeTextCamel}Options\` interface:**\n\n`
const readmeOptionsTable = [ ['Property', 'Type', 'Description'], ]
let optionsContent = `interface ${modulePascalCase}Options {\n`
let optionsContent = ''
let optionsInterfaceContent = `interface ${modulePascalCase}Options {\n`
interfaceJson.parameters.forEach((parameter) => {
if (parameter.name === 'memory-io') {
// Internal
Expand All @@ -273,11 +278,18 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
console.error(`Unexpected parameter type: ${parameter.type}`)
process.exit(1)
}
optionsContent += ` /** ${parameter.description} */\n`
optionsInterfaceContent += ` /** ${parameter.description} */\n`
const parameterType = interfaceJsonTypeToTypeScriptType.get(parameter.type)
optionsContent += ` ${camelCase(parameter.name)}?: ${parameterType}\n\n`
if(typesRequireImport.includes(parameterType)) {
optionsImportTypes.add(parameterType)
}
optionsInterfaceContent += ` ${camelCase(parameter.name)}?: ${parameterType}\n\n`
readmeOptionsTable.push([`\`${camelCase(parameter.name)}\``, `*${parameterType}*`, parameter.description])
})
// Insert the import statement in the beginning for the file.
if(optionsImportTypes.size !== 0)
optionsContent += `import { ${Array.from(optionsImportTypes).join(',')} } from 'itk-wasm'\n\n`;
optionsContent += optionsInterfaceContent
optionsContent += `}\n\nexport default ${modulePascalCase}Options\n`
fs.writeFileSync(path.join(srcOutputDir, `${moduleKebabCase}-options.ts`), optionsContent)

Expand Down

0 comments on commit 8495e23

Please sign in to comment.