Skip to content

Commit

Permalink
refactor(bindgen): Make package-name and package-description required
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jan 18, 2023
1 parent 41af4f2 commit 7529612
Showing 1 changed file with 70 additions and 85 deletions.
155 changes: 70 additions & 85 deletions src/itk-wasm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,75 +287,69 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
let readmeInterface = ''
let readmePipelines = ''

if (options.packageName) {
const packageName = options.packageName
const packageJsonPath = path.join(outputDir, 'package.json')
if (!fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(bindgenResource('template.package.json')))
packageJson.name = packageName
if (options.packageDescription) {
packageJson.description = options.packageDescription
}
packageJson.module = `./dist/${packageName}.js`
packageJson.exports['.'].browser = `./dist/${packageName}.js`
packageJson.exports['.'].node = `./dist/${packageName}.node.js`
packageJson.exports['.'].default = `./dist/${packageName}.js`
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
const packageName = options.packageName
const packageJsonPath = path.join(outputDir, 'package.json')
if (!fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(bindgenResource('template.package.json')))
packageJson.name = packageName
packageJson.description = options.packageDescription
packageJson.module = `./dist/${packageName}.js`
packageJson.exports['.'].browser = `./dist/${packageName}.js`
packageJson.exports['.'].node = `./dist/${packageName}.node.js`
packageJson.exports['.'].default = `./dist/${packageName}.js`
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))

}
}

if (!forNode) {
try {
fs.mkdirSync(path.join(outputDir, 'dist', 'demo'), { recursive: true })
} catch (err) {
if (err.code !== 'EEXIST') throw err
}
if (!forNode) {
try {
fs.mkdirSync(path.join(outputDir, 'dist', 'demo'), { recursive: true })
} catch (err) {
if (err.code !== 'EEXIST') throw err
}

const npmIgnorePath = path.join(outputDir, '.npmignore')
if (!fs.existsSync(npmIgnorePath)) {
fs.copyFileSync(bindgenResource('.npmignore'), npmIgnorePath)
}
const npmIgnorePath = path.join(outputDir, '.npmignore')
if (!fs.existsSync(npmIgnorePath)) {
fs.copyFileSync(bindgenResource('.npmignore'), npmIgnorePath)
}

const docsIndexPath = path.join(outputDir, 'index.html')
if (!fs.existsSync(docsIndexPath)) {
let indexContent = fs.readFileSync(bindgenResource('index.html'), { encoding: 'utf8', flag: 'r' })
indexContent = indexContent.replaceAll('<bindgenPackageName>', packageName)
if (options.packageDescription) {
indexContent = indexContent.replaceAll('<bindgenPackageDescription>', options.packageDescription)
}
fs.writeFileSync(docsIndexPath, indexContent)
fs.copyFileSync(bindgenResource('.nojekyll'), path.join(outputDir, '.nojekll'))
}
const docsIndexPath = path.join(outputDir, 'index.html')
if (!fs.existsSync(docsIndexPath)) {
let indexContent = fs.readFileSync(bindgenResource('index.html'), { encoding: 'utf8', flag: 'r' })
indexContent = indexContent.replaceAll('<bindgenPackageName>', packageName)
indexContent = indexContent.replaceAll('<bindgenPackageDescription>', options.packageDescription)
fs.writeFileSync(docsIndexPath, indexContent)
fs.copyFileSync(bindgenResource('.nojekyll'), path.join(outputDir, '.nojekll'))
}

const logoPath = path.join(outputDir, 'dist', 'demo', 'logo.svg')
if (!fs.existsSync(logoPath)) {
fs.copyFileSync(bindgenResource('logo.svg'), logoPath)
}
const logoPath = path.join(outputDir, 'dist', 'demo', 'logo.svg')
if (!fs.existsSync(logoPath)) {
fs.copyFileSync(bindgenResource('logo.svg'), logoPath)
}

const demoStylePath = path.join(outputDir, 'dist', 'demo', 'style.css')
if (!fs.existsSync(demoStylePath)) {
fs.copyFileSync(bindgenResource('demo.css'), demoStylePath)
}
const demoStylePath = path.join(outputDir, 'dist', 'demo', 'style.css')
if (!fs.existsSync(demoStylePath)) {
fs.copyFileSync(bindgenResource('demo.css'), demoStylePath)
}

const indexPath = path.join(outputDir, 'dist', 'index.html')
if (!fs.existsSync(indexPath)) {
let indexContent = fs.readFileSync(bindgenResource('dist-index.html'), { encoding: 'utf8', flag: 'r' })
indexContent = indexContent.replaceAll('<bindgenPackageName>', packageName)
fs.writeFileSync(indexPath, indexContent)
}
const indexPath = path.join(outputDir, 'dist', 'index.html')
if (!fs.existsSync(indexPath)) {
let indexContent = fs.readFileSync(bindgenResource('dist-index.html'), { encoding: 'utf8', flag: 'r' })
indexContent = indexContent.replaceAll('<bindgenPackageName>', packageName)
fs.writeFileSync(indexPath, indexContent)
}

const demoPath = path.join(outputDir, 'dist', 'demo', 'app.js')
if (!fs.existsSync(demoPath)) {
let demoContent = fs.readFileSync(bindgenResource('demo.js'), { encoding: 'utf8', flag: 'r' })
demoContent = demoContent.replaceAll('<bindgenPackageName>', options.packageName)
demoContent = demoContent.replaceAll('<bindgenPackageNameCamelCase>', camelCase(packageName))
fs.writeFileSync(demoPath, demoContent)
}
const demoPath = path.join(outputDir, 'dist', 'demo', 'app.js')
if (!fs.existsSync(demoPath)) {
let demoContent = fs.readFileSync(bindgenResource('demo.js'), { encoding: 'utf8', flag: 'r' })
demoContent = demoContent.replaceAll('<bindgenPackageName>', options.packageName)
demoContent = demoContent.replaceAll('<bindgenPackageNameCamelCase>', camelCase(packageName))
fs.writeFileSync(demoPath, demoContent)
}

const rollupConfigPath = path.join(outputDir, 'rollup.browser.config.js')
if (!fs.existsSync(rollupConfigPath)) {
fs.copyFileSync(bindgenResource('rollup.browser.config.js'), rollupConfigPath)
}
const rollupConfigPath = path.join(outputDir, 'rollup.browser.config.js')
if (!fs.existsSync(rollupConfigPath)) {
fs.copyFileSync(bindgenResource('rollup.browser.config.js'), rollupConfigPath)
}
}

Expand Down Expand Up @@ -654,11 +648,8 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
readmePipelines += readmeResult
})

if (options.packageName) {
const packageName = options.packageName
readmeInterface += `} from "${packageName}"\n\`\`\`\n`
readmeInterface += readmePipelines
}
readmeInterface += `} from "${packageName}"\n\`\`\`\n`
readmeInterface += readmePipelines
fs.writeFileSync(path.join(srcOutputDir, `index${nodeText}.ts`), indexContent)
return readmeInterface
}
Expand All @@ -682,35 +673,29 @@ function bindgen(wasmBinaries, options) {
switch (language) {
case 'typescript': {
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
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`
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:\n\n```js\nimport {\n'
let readmeNodeInterface = '\n### Node interface\n\nImport:\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)
}
readme += readmeUsage
readme += readmeBrowserInterface
readme += readmeNodeInterface

const readmePath = path.join(outputDir, 'README.md')
if (!fs.existsSync(readmePath)) {
fs.writeFileSync(readmePath, readme)
}
}
break
Expand Down Expand Up @@ -742,8 +727,8 @@ program
program
.command('bindgen [wasmBinaries...]')
.option('-o, --output-dir <output-dir>', 'Output directory name. Defaults to the language option value.')
.option('-p, --package-name <package-name>', 'Output a package configuration files with the given packages name')
.option('-d, --package-description <package-description>', 'Description for package')
.requiredOption('-p, --package-name <package-name>', 'Output a package configuration files with the given packages name')
.requiredOption('-d, --package-description <package-description>', 'Description for package')
.addOption(new Option('-l, --language <language>', 'language to generate bindings for, defaults to "typescript"').choices(['typescript',]))
.usage('[options] [wasmBinaries...]')
.description('Generate WASM module bindings for a language')
Expand Down

0 comments on commit 7529612

Please sign in to comment.