Skip to content

Commit

Permalink
WIP: feat(bindgen): readme pipeline function interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jan 3, 2023
1 parent 4c8ae5b commit b908abf
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions src/itk-wasm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
if (err.code !== 'EEXIST') throw err
}

let readmeInterface = ''
let readmePipelines = ''

if (options.packageName) {
const packageName = options.packageName
const packageJsonPath = path.join(outputDir, 'package.json')
Expand All @@ -299,15 +302,6 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
packageJson.exports['.'].default = `./dist/${packageName}.js`
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))

const readmePath = path.join(outputDir, 'README.md')
if (!fs.existsSync(readmePath)) {
let readme = `# ${packageName}\n`
readme += `\n[![npm version](https://badge.fury.io/js/${packageName}.svg)](https://www.npmjs.com/package/${packageName})\n`
if (options.packageDescription) {
readme += `\n${options.packageDescription}\n`
}
fs.writeFileSync(readmePath, readme)
}
}

if (!forNode) {
Expand Down Expand Up @@ -384,10 +378,16 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
const moduleCamelCase = camelCase(parsedPath.name)
const modulePascalCase = `${moduleCamelCase[0].toUpperCase()}${moduleCamelCase.substring(1)}`

readmeInterface += ` ${moduleCamelCase}${nodeText},\n`
let readmeResult = ''
let readmeOptions = ''

// Result module
let resultContent = `interface ${modulePascalCase}${nodeText}Result {\n`
readmeResult += `\n\`${modulePascalCase}${nodeText}Result\` interface:\n\n| Property | Type | Description |\n| -------- | ---- | ----------- |\n`
if (!forNode) {
resultContent += ` /** WebWorker used for computation */\n webWorker: Worker | null\n\n`
readmeResult += `| webWorker | Worker | WebWorker used for computation |\n`
}

// track unique output types in this set
Expand All @@ -405,6 +405,7 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
importTypes.add(outputType)
}
resultContent += ` ${camelCase(output.name)}: ${outputType}\n\n`
readmeResult += `| ${camelCase(output.name)} | ${outputType} | ${output.description} |\n`
})

// Insert the import statement in the beginning for the file.
Expand Down Expand Up @@ -617,8 +618,17 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
indexContent += `import ${moduleCamelCase}${nodeText} from './${moduleCamelCase}${nodeText}.js'\n`
indexContent += `export { ${moduleCamelCase}${nodeText} }\n`

readmePipelines += readmeOptions
readmePipelines += readmeResult
})

if (options.packageName) {
const packageName = options.packageName
readmeInterface += `} from "${packageName}"\n\`\`\`\n`
readmeInterface += readmePipelines
}
fs.writeFileSync(path.join(srcOutputDir, `index${nodeText}.ts`), indexContent)
return readmeInterface
}


Expand All @@ -639,8 +649,37 @@ function bindgen(wasmBinaries, options) {

switch (language) {
case 'typescript': {
typescriptBindings(outputDir, buildDir, filteredWasmBinaries, options, false)
typescriptBindings(outputDir, buildDir, filteredWasmBinaries, options, true)
let readme = ''
if (options.packageName) {
const packageName = options.packageName
readme += `# ${packageName}\n`
readme += `\n[![npm version](https://badge.fury.io/js/${packageName}.svg)](https://www.npmjs.com/package/${packageName})\n`
if (options.packageDescription) {
readme += `\n${options.packageDescription}\n`
}
readme += `\n## Installation\n
\`\`\`sh
npm install ${packageName}
\`\`\`
`
}

let readmeUsage = '\n## Usage\n'
let readmeBrowserInterface = '\n### Browser interface\n\nImport desired functions from the package:\n\n```js\nimport {\n'
let readmeNodeInterface = '\n### Node interface\n\nImport desired functions from the package:\n\n```js\nimport {\n'

readmeBrowserInterface += typescriptBindings(outputDir, buildDir, filteredWasmBinaries, options, false)
readmeNodeInterface += typescriptBindings(outputDir, buildDir, filteredWasmBinaries, options, true)
if (options.packageName) {
readme += readmeUsage
readme += readmeBrowserInterface
readme += readmeNodeInterface

const readmePath = path.join(outputDir, 'README.md')
if (!fs.existsSync(readmePath)) {
fs.writeFileSync(readmePath, readme)
}
}
}
break
}
Expand Down

0 comments on commit b908abf

Please sign in to comment.