1
+ const fs = require ( "fs" ) ;
2
+
1
3
const jsFileContentTemplate = componentName => {
2
4
return `import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
3
5
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
@@ -6,20 +8,42 @@ import ${componentName}Template from "./generated/templates/${componentName}Temp
6
8
// Styles
7
9
import ${ componentName } Css from "./generated/themes/${ componentName } .css.js";
8
10
9
-
11
+ /**
12
+ * @public
13
+ */
10
14
const metadata = {
11
- tag: "ui5- ${ componentName . toLowerCase ( ) } ",
12
- properties: {
15
+ tag: "${ tagName } ",
16
+ properties: /** @lends sap.ui.webcomponents. ${ library } . ${ componentName } .prototype */ {
13
17
//
14
18
},
15
- slots: {
19
+ slots: /** @lends sap.ui.webcomponents. ${ library } . ${ componentName } .prototype */ {
16
20
//
17
21
},
18
- events: {
22
+ events: /** @lends sap.ui.webcomponents. ${ library } . ${ componentName } .prototype */ {
19
23
//
20
24
},
21
25
};
22
26
27
+ /**
28
+ * @class
29
+ *
30
+ * <h3 class="comment-api-title">Overview</h3>
31
+ *
32
+ *
33
+ * <h3>Usage</h3>
34
+ *
35
+ * For the <code>${ tagName } </code>
36
+ * <h3>ES6 Module Import</h3>
37
+ *
38
+ * <code>import ${ packageName } /dist/${ componentName } .js";</code>
39
+ *
40
+ * @constructor
41
+ * @author SAP SE
42
+ * @alias sap.ui.webcomponents.${ library } .${ componentName }
43
+ * @extends UI5Element
44
+ * @tagname ${ tagName }
45
+ * @public
46
+ */
23
47
class ${ componentName } extends UI5Element {
24
48
static get metadata() {
25
49
return metadata;
@@ -48,23 +72,60 @@ export default ${componentName};
48
72
` ;
49
73
} ;
50
74
75
+ const getPackageName = ( ) => {
76
+ if ( ! fs . existsSync ( "./package.json" ) ) {
77
+ throw ( "The current directory doesn't contain package.json file." ) ;
78
+ }
79
+
80
+ const packageJSON = JSON . parse ( fs . readFileSync ( "./package.json" ) ) ;
81
+
82
+ if ( ! packageJSON . name ) {
83
+ throw ( "The package.json file in the current directory doesn't have a name property" ) ;
84
+ }
85
+
86
+ return packageJSON . name ;
87
+ } ;
88
+
89
+ const getLibraryName = packageName => {
90
+ if ( ! packageName . includes ( "/" ) ) {
91
+ return packageName ;
92
+ }
93
+
94
+ if ( packageName === "@ui5/webcomponents" ) {
95
+ return `main` ;
96
+ }
97
+
98
+ packageName = packageName . split ( "/" ) . pop ( ) ;
99
+
100
+ if ( ! packageName . startsWith ( "webcomponents-" ) ) {
101
+ return packageName ;
102
+ }
103
+
104
+ return packageName . substr ( "webcomponents-" . length ) ;
105
+ } ;
106
+
107
+ const camelToKebabCase = string => string . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, "$1-$2" ) . toLowerCase ( ) ;
108
+
109
+ const packageName = getPackageName ( ) ;
110
+ const library = getLibraryName ( packageName ) ;
111
+
51
112
const consoleArguments = process . argv . slice ( 2 ) ;
52
113
const componentName = consoleArguments [ 0 ] ;
53
114
54
115
if ( ! componentName ) {
55
- console . error ( "Please enter component name." )
116
+ console . error ( "Please enter component name." ) ;
56
117
return ;
57
118
}
58
119
120
+ const tagName = `ui5-${ camelToKebabCase ( componentName ) } ` ;
121
+
59
122
const filePaths = {
60
123
"js" : `./src/${ componentName } .js` ,
61
124
"css" : `./src/themes/${ componentName } .css` ,
62
125
"hbs" : `./src/${ componentName } .hbs` ,
63
126
} ;
64
127
const sJsFileContentTemplate = jsFileContentTemplate ( componentName ) ;
65
128
66
- const fs = require ( "fs" ) ;
67
-
68
129
fs . writeFileSync ( filePaths . js , sJsFileContentTemplate , { flag : "wx+" } ) ;
69
130
fs . writeFileSync ( filePaths . css , "" , { flag : "wx+" } ) ;
70
131
fs . writeFileSync ( filePaths . hbs , "<div>Hello World</div>" , { flag : "wx+" } ) ;
0 commit comments