11import fs from 'fs-extra'
22import path from 'path'
3+ import { fileURLToPath } from 'url'
34
45import wasmBinaryInterfaceJson from '../wasm-binary-interface-json.js'
56import camelCase from '../camel-case.js'
@@ -19,10 +20,22 @@ import inputArrayCheck from '../input-array-check.js'
1920
2021// Array of types that will require an import from itk-wasm
2122function bindgenResource ( filePath ) {
22- return path . join ( path . dirname ( import . meta. url . substring ( 7 ) ) , 'resources' , filePath )
23+ const currentScriptPath = path . dirname ( fileURLToPath ( import . meta. url ) )
24+ const resourcesDir = path . join ( currentScriptPath , 'resources' )
25+ const resourceFilePath = path . join (
26+ resourcesDir ,
27+ filePath . split ( '/' ) . join ( path . sep )
28+ )
29+ return resourceFilePath
2330}
2431
25- function typescriptBindings ( outputDir , buildDir , wasmBinaries , options , forNode = false ) {
32+ function typescriptBindings (
33+ outputDir ,
34+ buildDir ,
35+ wasmBinaries ,
36+ options ,
37+ forNode = false
38+ ) {
2639 // index module
2740 let indexContent = ''
2841 let indexCommonContent = ''
@@ -48,20 +61,22 @@ function typescriptBindings (outputDir, buildDir, wasmBinaries, options, forNode
4861 const bundleName = packageToBundleName ( packageName )
4962 const packageJsonPath = path . join ( outputDir , 'package.json' )
5063 if ( ! fs . existsSync ( packageJsonPath ) ) {
51- const packageJson = JSON . parse ( fs . readFileSync ( bindgenResource ( 'template.package.json' ) ) )
64+ const packageJson = JSON . parse (
65+ fs . readFileSync ( bindgenResource ( 'template.package.json' ) )
66+ )
5267 packageJson . name = packageName
5368 packageJson . description = options . packageDescription
5469 packageJson . module = `./dist/index.js`
5570 packageJson . exports [ '.' ] . browser = `./dist/index.js`
5671 packageJson . exports [ '.' ] . node = `./dist/index-node.js`
5772 packageJson . exports [ '.' ] . default = `./dist/index-all.js`
5873 if ( options . repository ) {
59- packageJson . repository = { ' type' : 'git' , ' url' : options . repository }
74+ packageJson . repository = { type : 'git' , url : options . repository }
6075 }
6176 if ( options . packageVersion ) {
6277 packageJson . version = options . packageVersion
6378 } else {
64- packageJson . version = " 0.1.0"
79+ packageJson . version = ' 0.1.0'
6580 }
6681 fs . writeFileSync ( packageJsonPath , JSON . stringify ( packageJson , null , 2 ) )
6782 } else {
@@ -78,7 +93,13 @@ function typescriptBindings (outputDir, buildDir, wasmBinaries, options, forNode
7893 indexContent += "export * from './default-web-worker.js'\n"
7994 }
8095
81- writeSupportFiles ( outputDir , forNode , bindgenResource , packageName , options . packageDescription )
96+ writeSupportFiles (
97+ outputDir ,
98+ forNode ,
99+ bindgenResource ,
100+ packageName ,
101+ options . packageDescription
102+ )
82103
83104 let firstFunctionName = null
84105 wasmBinaries . forEach ( ( wasmBinaryName ) => {
@@ -92,12 +113,31 @@ function typescriptBindings (outputDir, buildDir, wasmBinaries, options, forNode
92113 } catch ( err ) {
93114 if ( err . code !== 'EEXIST' ) throw err
94115 }
95- fs . copyFileSync ( wasmBinaryRelativePath , path . join ( distPipelinesDir , path . basename ( wasmBinaryRelativePath ) ) )
96- fs . copyFileSync ( `${ wasmBinaryRelativePath } .zst` , path . join ( distPipelinesDir , `${ path . basename ( wasmBinaryRelativePath ) } .zst` ) )
97- const prefix = wasmBinaryRelativePath . substring ( 0 , wasmBinaryRelativePath . length - 5 )
98- fs . copyFileSync ( `${ prefix } .js` , path . join ( distPipelinesDir , `${ path . basename ( prefix ) } .js` ) )
99-
100- const { interfaceJson, parsedPath } = wasmBinaryInterfaceJson ( outputDir , buildDir , wasmBinaryName )
116+ fs . copyFileSync (
117+ wasmBinaryRelativePath ,
118+ path . join ( distPipelinesDir , path . basename ( wasmBinaryRelativePath ) )
119+ )
120+ fs . copyFileSync (
121+ `${ wasmBinaryRelativePath } .zst` ,
122+ path . join (
123+ distPipelinesDir ,
124+ `${ path . basename ( wasmBinaryRelativePath ) } .zst`
125+ )
126+ )
127+ const prefix = wasmBinaryRelativePath . substring (
128+ 0 ,
129+ wasmBinaryRelativePath . length - 5
130+ )
131+ fs . copyFileSync (
132+ `${ prefix } .js` ,
133+ path . join ( distPipelinesDir , `${ path . basename ( prefix ) } .js` )
134+ )
135+
136+ const { interfaceJson, parsedPath } = wasmBinaryInterfaceJson (
137+ outputDir ,
138+ buildDir ,
139+ wasmBinaryName
140+ )
101141
102142 outputOptionsCheck ( interfaceJson )
103143 inputArrayCheck ( interfaceJson )
@@ -119,12 +159,25 @@ if (!params.has('functionName')) {
119159 }
120160
121161 const useCamelCase = true
122- const functionDemoHtml = interfaceFunctionsDemoHtml ( interfaceJson , functionName , useCamelCase )
162+ const functionDemoHtml = interfaceFunctionsDemoHtml (
163+ interfaceJson ,
164+ functionName ,
165+ useCamelCase
166+ )
123167 if ( functionDemoHtml ) {
124168 demoFunctionsHtml += functionDemoHtml
125169 pipelinesFunctionsTabs += ` <sl-tab slot="nav" panel="${ functionName } -panel">${ functionName } </sl-tab>\n`
126- const demoTypeScriptOutputPath = path . join ( outputDir , 'test' , 'browser' , 'demo-app' )
127- interfaceFunctionsDemoTypeScript ( packageName , interfaceJson , demoTypeScriptOutputPath )
170+ const demoTypeScriptOutputPath = path . join (
171+ outputDir ,
172+ 'test' ,
173+ 'browser' ,
174+ 'demo-app'
175+ )
176+ interfaceFunctionsDemoTypeScript (
177+ packageName ,
178+ interfaceJson ,
179+ demoTypeScriptOutputPath
180+ )
128181 demoFunctionsTypeScript += `import './${ interfaceJson . name } -controller.js'\n`
129182 } else {
130183 pipelinesFunctionsTabs += ` <sl-tab slot="nav" panel="${ functionName } -panel" disabled>${ functionName } </sl-tab>\n`
@@ -133,22 +186,49 @@ if (!params.has('functionName')) {
133186 readmeInterface += ` ${ moduleCamelCase } ${ nodeTextCamel } ,\n`
134187
135188 const resultsModuleFileName = `${ moduleKebabCase } ${ nodeTextKebab } -result`
136- const { readmeResult } = resultsModule ( srcOutputDir , interfaceJson , forNode , modulePascalCase , nodeTextCamel , resultsModuleFileName )
189+ const { readmeResult } = resultsModule (
190+ srcOutputDir ,
191+ interfaceJson ,
192+ forNode ,
193+ modulePascalCase ,
194+ nodeTextCamel ,
195+ resultsModuleFileName
196+ )
137197 indexContent += `\n\nimport ${ modulePascalCase } ${ nodeTextCamel } Result from './${ resultsModuleFileName } .js'\n`
138198 indexContent += `export type { ${ modulePascalCase } ${ nodeTextCamel } Result }\n\n`
139199
140- const filteredParameters = interfaceJson . parameters . filter ( p => { return p . name !== 'memory-io' && p . name !== 'version' } )
200+ const filteredParameters = interfaceJson . parameters . filter ( ( p ) => {
201+ return p . name !== 'memory-io' && p . name !== 'version'
202+ } )
141203 const haveOptions = ! ! filteredParameters . length || ! forNode
142204
143205 const optionsModuleFileName = `${ moduleKebabCase } ${ nodeTextKebab } -options`
144- const { readmeOptions } = optionsModule ( srcOutputDir , interfaceJson , modulePascalCase , nodeTextCamel , haveOptions , forNode , optionsModuleFileName )
206+ const { readmeOptions } = optionsModule (
207+ srcOutputDir ,
208+ interfaceJson ,
209+ modulePascalCase ,
210+ nodeTextCamel ,
211+ haveOptions ,
212+ forNode ,
213+ optionsModuleFileName
214+ )
145215 if ( haveOptions ) {
146216 indexContent += `import ${ modulePascalCase } ${ nodeTextCamel } Options from './${ optionsModuleFileName } .js'\n`
147217 indexContent += `export type { ${ modulePascalCase } ${ nodeTextCamel } Options }\n\n`
148218 }
149219
150- const { readmeFunction, usedInterfaceTypes } = functionModule ( srcOutputDir , forNode , interfaceJson , modulePascalCase , moduleKebabCase , moduleCamelCase , nodeTextCamel , nodeTextKebab , haveOptions )
151- usedInterfaceTypes . forEach ( iType => allUsedInterfaceTypes . add ( iType ) )
220+ const { readmeFunction, usedInterfaceTypes } = functionModule (
221+ srcOutputDir ,
222+ forNode ,
223+ interfaceJson ,
224+ modulePascalCase ,
225+ moduleKebabCase ,
226+ moduleCamelCase ,
227+ nodeTextCamel ,
228+ nodeTextKebab ,
229+ haveOptions
230+ )
231+ usedInterfaceTypes . forEach ( ( iType ) => allUsedInterfaceTypes . add ( iType ) )
152232
153233 indexContent += `import ${ moduleCamelCase } ${ nodeTextCamel } from './${ moduleKebabCase } ${ nodeTextKebab } .js'\n`
154234 indexContent += `export { ${ moduleCamelCase } ${ nodeTextCamel } }\n`
@@ -160,7 +240,10 @@ if (!params.has('functionName')) {
160240
161241 if ( allUsedInterfaceTypes . size > 0 ) {
162242 indexCommonContent += '\n'
163- allUsedInterfaceTypes . forEach ( iType => indexCommonContent += `export type { ${ iType } } from 'itk-wasm'\n` )
243+ allUsedInterfaceTypes . forEach (
244+ ( iType ) =>
245+ ( indexCommonContent += `export type { ${ iType } } from 'itk-wasm'\n` )
246+ )
164247 }
165248
166249 if ( ! forNode ) {
@@ -191,9 +274,18 @@ export * from './index-node-only.js'
191274 writeIfOverrideNotPresent ( indexAllPath , indexAllContent )
192275
193276 if ( ! forNode ) {
194- const demoIndexPath = path . join ( outputDir , 'test' , 'browser' , 'demo-app' , 'index.html' )
195- let demoIndexContent = fs . readFileSync ( bindgenResource ( path . join ( 'demo-app' , 'index.html' ) ) , { encoding : 'utf8' , flag : 'r' } )
196- const shoelaceScript = `
277+ const demoIndexPath = path . join (
278+ outputDir ,
279+ 'test' ,
280+ 'browser' ,
281+ 'demo-app' ,
282+ 'index.html'
283+ )
284+ let demoIndexContent = fs . readFileSync (
285+ bindgenResource ( path . join ( 'demo-app' , 'index.html' ) ) ,
286+ { encoding : 'utf8' , flag : 'r' }
287+ )
288+ const shoelaceScript = `
197289<script type="module">
198290 import '@shoelace-style/shoelace/dist/themes/light.css';
199291 import '@shoelace-style/shoelace/dist/themes/dark.css';
@@ -224,25 +316,61 @@ const shoelaceScript = `
224316 }
225317</script>
226318`
227- demoIndexContent = demoIndexContent . replaceAll ( '@shoelaceScript@' , shoelaceScript )
319+ demoIndexContent = demoIndexContent . replaceAll (
320+ '@shoelaceScript@' ,
321+ shoelaceScript
322+ )
228323 const packageNameLanguageLogos = `${ packageName } <img src="./javascript-logo.svg" alt="JavaScript logo" class="language-logo"/><img src="./typescript-logo.svg" alt="TypeScript logo" class="language-logo"/>`
229- demoIndexContent = demoIndexContent . replaceAll ( '@bindgenPackageName@' , packageNameLanguageLogos )
230- demoIndexContent = demoIndexContent . replaceAll ( '@bindgenPackageDescription@' , options . packageDescription )
324+ demoIndexContent = demoIndexContent . replaceAll (
325+ '@bindgenPackageName@' ,
326+ packageNameLanguageLogos
327+ )
328+ demoIndexContent = demoIndexContent . replaceAll (
329+ '@bindgenPackageDescription@' ,
330+ options . packageDescription
331+ )
231332 const bindgenGitHubCorner = githubCorner ( options )
232- demoIndexContent = demoIndexContent . replaceAll ( '@bindgenGitHubCorner@' , bindgenGitHubCorner )
233- demoIndexContent = demoIndexContent . replaceAll ( '@bindgenFunctions@' , demoFunctionsHtml )
234- demoIndexContent = demoIndexContent . replaceAll ( '@pipelinesFunctionsTabs@' , pipelinesFunctionsTabs )
235- const indexModule = `
333+ demoIndexContent = demoIndexContent . replaceAll (
334+ '@bindgenGitHubCorner@' ,
335+ bindgenGitHubCorner
336+ )
337+ demoIndexContent = demoIndexContent . replaceAll (
338+ '@bindgenFunctions@' ,
339+ demoFunctionsHtml
340+ )
341+ demoIndexContent = demoIndexContent . replaceAll (
342+ '@pipelinesFunctionsTabs@' ,
343+ pipelinesFunctionsTabs
344+ )
345+ const indexModule = `
236346<script type="module" src="./index.ts"></script>
237347`
238348 demoIndexContent = demoIndexContent . replaceAll ( '@indexModule@' , indexModule )
239349 writeIfOverrideNotPresent ( demoIndexPath , demoIndexContent , '<!--' )
240350
241- const demoTypeScriptPath = path . join ( outputDir , 'test' , 'browser' , 'demo-app' , 'index.ts' )
242- let demoTypeScriptContent = fs . readFileSync ( bindgenResource ( path . join ( 'demo-app' , 'index.ts' ) ) , { encoding : 'utf8' , flag : 'r' } )
243- demoTypeScriptContent = demoTypeScriptContent . replaceAll ( '@bindgenBundleName@' , bundleName )
244- demoTypeScriptContent = demoTypeScriptContent . replaceAll ( '@bindgenBundleNameCamelCase@' , camelCase ( bundleName ) )
245- demoTypeScriptContent = demoTypeScriptContent . replaceAll ( '@bindgenFunctionLogic@' , demoFunctionsTypeScript )
351+ const demoTypeScriptPath = path . join (
352+ outputDir ,
353+ 'test' ,
354+ 'browser' ,
355+ 'demo-app' ,
356+ 'index.ts'
357+ )
358+ let demoTypeScriptContent = fs . readFileSync (
359+ bindgenResource ( path . join ( 'demo-app' , 'index.ts' ) ) ,
360+ { encoding : 'utf8' , flag : 'r' }
361+ )
362+ demoTypeScriptContent = demoTypeScriptContent . replaceAll (
363+ '@bindgenBundleName@' ,
364+ bundleName
365+ )
366+ demoTypeScriptContent = demoTypeScriptContent . replaceAll (
367+ '@bindgenBundleNameCamelCase@' ,
368+ camelCase ( bundleName )
369+ )
370+ demoTypeScriptContent = demoTypeScriptContent . replaceAll (
371+ '@bindgenFunctionLogic@' ,
372+ demoFunctionsTypeScript
373+ )
246374 writeIfOverrideNotPresent ( demoTypeScriptPath , demoTypeScriptContent )
247375 }
248376
0 commit comments