diff --git a/src/itk-wasm-cli.js b/src/itk-wasm-cli.js index f5cba430a..349222aaa 100755 --- a/src/itk-wasm-cli.js +++ b/src/itk-wasm-cli.js @@ -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') @@ -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) { @@ -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 @@ -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. @@ -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 } @@ -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 }