2
2
import fioriWebComponentsSpec from '@ui5/webcomponents-fiori/dist/api.json' assert { type : 'json ' } ;
3
3
import mainWebComponentsSpec from '@ui5/webcomponents/dist/api.json' assert { type : 'json ' } ;
4
4
import dedent from 'dedent' ;
5
- import fs from 'node:fs' ;
5
+ import { spawnSync } from 'node:child_process' ;
6
+ import { existsSync , mkdirSync , readdirSync , readFileSync , statSync , writeFileSync } from 'node:fs' ;
6
7
import path from 'node:path' ;
7
8
import prettier from 'prettier' ;
8
9
import PATHS from '../../../config/paths.js' ;
@@ -199,7 +200,7 @@ const allWebComponents = [
199
200
return true ;
200
201
} ) ;
201
202
202
- fs . writeFileSync (
203
+ writeFileSync (
203
204
path . join ( PATHS . root , 'scripts' , 'web-component-wrappers' , 'interfaces.json' ) ,
204
205
JSON . stringify ( Array . from ( interfaces ) )
205
206
) ;
@@ -234,34 +235,45 @@ const replaceTagNameWithModuleName = (description) => {
234
235
return parsedDescription ;
235
236
} ;
236
237
237
- const getEventParameters = ( name , parameters ) => {
238
- const resolvedEventParameters = parameters . map ( ( property ) => {
239
- return {
240
- ...property ,
241
- ...Utils . getTypeDefinitionForProperty ( property , { event : true } )
242
- } ;
243
- } ) ;
238
+ const getEventParameters = ( moduleName , eventSpec ) => {
239
+ const eventTarget = `${ moduleName } DomRef` ;
240
+ if ( eventSpec . native === 'true' ) {
241
+ if ( eventSpec . name === 'click' ) {
242
+ return {
243
+ tsType : `MouseEventHandler<${ eventTarget } >` ,
244
+ importStatements : [ "import { MouseEventHandler } from 'react';" ]
245
+ } ;
246
+ } else if ( eventSpec . name === 'drop' ) {
247
+ return {
248
+ tsType : `DragEventHandler<${ eventTarget } >` ,
249
+ importStatements : [ "import { DragEventHandler } from 'react';" ]
250
+ } ;
251
+ } else {
252
+ console . warn (
253
+ `----------------------\n${ moduleName } : ${ eventSpec . name } event didn't receive its type, please add it to the script! \n----------------------`
254
+ ) ;
255
+ }
256
+ }
244
257
245
- const importStatements = [ `import type { Ui5CustomEvent } from '../../interfaces/index.js';` ] ;
258
+ const importSpecifier = `@ui5/webcomponents${
259
+ componentsFromFioriPackage . has ( moduleName ) ? '-fiori' : ''
260
+ } /dist/${ moduleName } .js`;
246
261
247
- const eventTarget = `${ name } DomRef ` ;
262
+ const eventName = `${ moduleName } ${ Utils . capitalizeFirstLetter ( Utils . snakeToCamel ( eventSpec . name ) ) } EventDetail ` ;
248
263
249
- if ( resolvedEventParameters . length === 0 ) {
264
+ const importStatements = [ `import type { Ui5CustomEvent } from '../../interfaces/index.js';` ] ;
265
+
266
+ if ( ( eventSpec . parameters ?? [ ] ) . length === 0 ) {
250
267
return {
251
268
tsType : `(event: Ui5CustomEvent<${ eventTarget } >) => void` ,
252
269
importStatements
253
270
} ;
254
271
}
255
272
256
- const detailPayload = resolvedEventParameters . map ( ( parameter ) => {
257
- if ( parameter . importStatement ) {
258
- importStatements . push ( parameter . importStatement ) ;
259
- }
260
- return `${ parameter . name } : ${ parameter . tsType } ` ;
261
- } ) ;
273
+ importStatements . unshift ( `import type { ${ eventName } } from '${ importSpecifier } ';` ) ;
262
274
263
275
return {
264
- tsType : `(event: Ui5CustomEvent<${ eventTarget } , { ${ detailPayload . join ( '; ' ) } }>) => void` ,
276
+ tsType : `(event: Ui5CustomEvent<${ eventTarget } , ${ eventName } >) => void` ,
265
277
importStatements
266
278
} ;
267
279
} ;
@@ -442,7 +454,7 @@ const resolveInheritedAttributes = (componentSpec) => {
442
454
443
455
` ;
444
456
445
- fs . writeFileSync ( path . join ( ENUMS_DIR , `${ spec . basename } .ts` ) , prettier . format ( template , Utils . prettierConfig ) ) ;
457
+ writeFileSync ( path . join ( ENUMS_DIR , `${ spec . basename } .ts` ) , prettier . format ( template , Utils . prettierConfig ) ) ;
446
458
} ) ;
447
459
448
460
const propDescription = ( componentSpec , property ) => {
@@ -549,27 +561,7 @@ allWebComponents
549
561
( componentSpec . events || [ ] )
550
562
. filter ( ( eventSpec ) => eventSpec . visibility === 'public' )
551
563
. forEach ( ( eventSpec ) => {
552
- let eventParameters ;
553
- if ( eventSpec . native === 'true' ) {
554
- const eventTarget = `${ componentSpec . module } DomRef` ;
555
- if ( eventSpec . name === 'click' ) {
556
- eventParameters = {
557
- tsType : `MouseEventHandler<${ eventTarget } >` ,
558
- importStatements : [ "import { MouseEventHandler } from 'react';" ]
559
- } ;
560
- } else if ( eventSpec . name === 'drop' ) {
561
- eventParameters = {
562
- tsType : `DragEventHandler<${ eventTarget } >` ,
563
- importStatements : [ "import { DragEventHandler } from 'react';" ]
564
- } ;
565
- } else {
566
- console . warn (
567
- `----------------------\n${ componentSpec . module } : ${ eventSpec . name } event didn't receive its type, please add it to the script! \n----------------------`
568
- ) ;
569
- }
570
- } else {
571
- eventParameters = getEventParameters ( componentSpec . module , eventSpec . parameters || [ ] ) ;
572
- }
564
+ const eventParameters = getEventParameters ( componentSpec . module , eventSpec ) ;
573
565
importStatements . push ( ...eventParameters . importStatements ) ;
574
566
let onChangeDescription ;
575
567
if ( INPUT_COMPONENTS . has ( componentSpec . module ) && eventSpec . name === 'change' ) {
@@ -616,14 +608,14 @@ allWebComponents
616
608
617
609
// check if folder exists and create it if necessary
618
610
const webComponentFolderPath = path . join ( WEB_COMPONENTS_ROOT_DIR , componentSpec . module ) ;
619
- if ( ! fs . existsSync ( webComponentFolderPath ) ) {
620
- fs . mkdirSync ( webComponentFolderPath ) ;
611
+ if ( ! existsSync ( webComponentFolderPath ) ) {
612
+ mkdirSync ( webComponentFolderPath ) ;
621
613
}
622
614
623
615
// create empty index file for eslint
624
616
const webComponentWrapperPath = path . join ( webComponentFolderPath , 'index.tsx' ) ;
625
- if ( ! fs . existsSync ( webComponentWrapperPath ) ) {
626
- fs . writeFileSync ( webComponentWrapperPath , '' ) ;
617
+ if ( ! existsSync ( webComponentWrapperPath ) ) {
618
+ writeFileSync ( webComponentWrapperPath , '' ) ;
627
619
}
628
620
629
621
// fill index
@@ -650,12 +642,12 @@ allWebComponents
650
642
( componentSpec . slots || [ ] ) . filter ( filterNonPublicAttributes ) . map ( ( { name } ) => name ) ,
651
643
( componentSpec . events || [ ] ) . filter ( filterNonPublicAttributes ) . map ( ( { name } ) => name )
652
644
) ;
653
- fs . writeFileSync ( webComponentWrapperPath , webComponentWrapper ) ;
645
+ writeFileSync ( webComponentWrapperPath , webComponentWrapper ) ;
654
646
655
647
// create test
656
- if ( ! fs . existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .cy.tsx` ) ) ) {
648
+ if ( ! existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .cy.tsx` ) ) ) {
657
649
const webComponentTest = renderTest ( { name : componentSpec . module , tagname : componentSpec . tagname } ) ;
658
- fs . writeFileSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .cy.tsx` ) , webComponentTest ) ;
650
+ writeFileSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .cy.tsx` ) , webComponentTest ) ;
659
651
}
660
652
661
653
// create demo
@@ -670,7 +662,7 @@ allWebComponents
670
662
componentSpec ,
671
663
false
672
664
) } \n${ formatDemoDescription ( description , componentSpec , false ) } `;
673
- fs . writeFileSync (
665
+ writeFileSync (
674
666
path . join ( webComponentFolderPath , `${ componentSpec . module } Description.md` ) ,
675
667
subComponentDescription
676
668
) ;
@@ -679,7 +671,7 @@ allWebComponents
679
671
const formattedDescription = formatDemoDescription ( description , componentSpec ) ;
680
672
// create component description
681
673
if ( formattedDescription ) {
682
- fs . writeFileSync (
674
+ writeFileSync (
683
675
path . join ( webComponentFolderPath , `${ componentSpec . module } Description.md` ) ,
684
676
formattedDescription
685
677
) ;
@@ -692,22 +684,20 @@ allWebComponents
692
684
] ;
693
685
694
686
if ( publicProperties . length ) {
695
- fs . writeFileSync (
687
+ writeFileSync (
696
688
path . join ( webComponentFolderPath , `${ componentSpec . module } DomRef.json` ) ,
697
689
prettier . format ( JSON . stringify ( publicProperties ) , {
698
690
...Utils . prettierConfig ,
699
691
parser : 'json'
700
692
} )
701
693
) ;
702
694
let hasMethodsTable = false ;
703
- if ( fs . existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.mdx` ) ) ) {
704
- hasMethodsTable = fs
705
- . readFileSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.mdx` ) )
695
+ if ( existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.mdx` ) ) ) {
696
+ hasMethodsTable = readFileSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.mdx` ) )
706
697
. toString ( )
707
698
. includes ( `<DomRefTable rows={${ componentSpec . module } DomRef.json} />` ) ;
708
- } else if ( fs . existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .mdx` ) ) ) {
709
- hasMethodsTable = fs
710
- . readFileSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .mdx` ) )
699
+ } else if ( existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .mdx` ) ) ) {
700
+ hasMethodsTable = readFileSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .mdx` ) )
711
701
. toString ( )
712
702
. includes ( `<DomRefTable rows={${ componentSpec . module } DomRef.json} />` ) ;
713
703
}
@@ -721,8 +711,8 @@ allWebComponents
721
711
722
712
if (
723
713
CREATE_SINGLE_COMPONENT === componentSpec . module ||
724
- ( ! fs . existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.mdx` ) ) &&
725
- ! fs . existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.tsx` ) ) )
714
+ ( ! existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.mdx` ) ) &&
715
+ ! existsSync ( path . join ( webComponentFolderPath , `${ componentSpec . module } .stories.tsx` ) ) )
726
716
) {
727
717
await createStory ( componentSpec , allComponentProperties ) ;
728
718
await createDocumentation ( componentSpec , allComponentProperties , description ) ;
@@ -732,11 +722,12 @@ allWebComponents
732
722
} ) ;
733
723
734
724
// create index file for exporting all web components
735
- fs . writeFileSync (
725
+ writeFileSync (
736
726
path . join ( WEB_COMPONENTS_ROOT_DIR , 'index.ts' ) ,
737
- fs
738
- . readdirSync ( WEB_COMPONENTS_ROOT_DIR )
739
- . filter ( ( f ) => fs . statSync ( path . join ( WEB_COMPONENTS_ROOT_DIR , f ) ) . isDirectory ( ) )
727
+ readdirSync ( WEB_COMPONENTS_ROOT_DIR )
728
+ . filter ( ( f ) => statSync ( path . join ( WEB_COMPONENTS_ROOT_DIR , f ) ) . isDirectory ( ) )
740
729
. map ( ( folder ) => `export * from './${ folder } /index.js';` )
741
730
. join ( '\n' )
742
731
) ;
732
+
733
+ spawnSync ( 'prettier' , [ WEB_COMPONENTS_ROOT_DIR , '--write' ] , { stdio : [ 0 , 1 , 2 ] } ) ;
0 commit comments