Skip to content

Commit f0f5ec5

Browse files
fifoosidvladitasev
authored andcommitted
feat: add docs to create-ui5-script (#987)
1 parent 434ad21 commit f0f5ec5

File tree

1 file changed

+69
-8
lines changed
  • packages/tools/lib/create-new-component

1 file changed

+69
-8
lines changed

packages/tools/lib/create-new-component/index.js

+69-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const fs = require("fs");
2+
13
const jsFileContentTemplate = componentName => {
24
return `import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
35
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
@@ -6,20 +8,42 @@ import ${componentName}Template from "./generated/templates/${componentName}Temp
68
// Styles
79
import ${componentName}Css from "./generated/themes/${componentName}.css.js";
810
9-
11+
/**
12+
* @public
13+
*/
1014
const metadata = {
11-
tag: "ui5-${componentName.toLowerCase()}",
12-
properties: {
15+
tag: "${tagName}",
16+
properties: /** @lends sap.ui.webcomponents.${library}.${componentName}.prototype */ {
1317
//
1418
},
15-
slots: {
19+
slots: /** @lends sap.ui.webcomponents.${library}.${componentName}.prototype */ {
1620
//
1721
},
18-
events: {
22+
events: /** @lends sap.ui.webcomponents.${library}.${componentName}.prototype */ {
1923
//
2024
},
2125
};
2226
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+
*/
2347
class ${componentName} extends UI5Element {
2448
static get metadata() {
2549
return metadata;
@@ -48,23 +72,60 @@ export default ${componentName};
4872
`;
4973
};
5074

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+
51112
const consoleArguments = process.argv.slice(2);
52113
const componentName = consoleArguments[0];
53114

54115
if (!componentName){
55-
console.error("Please enter component name.")
116+
console.error("Please enter component name.");
56117
return;
57118
}
58119

120+
const tagName = `ui5-${camelToKebabCase(componentName)}`;
121+
59122
const filePaths = {
60123
"js": `./src/${componentName}.js`,
61124
"css": `./src/themes/${componentName}.css`,
62125
"hbs": `./src/${componentName}.hbs`,
63126
};
64127
const sJsFileContentTemplate = jsFileContentTemplate(componentName);
65128

66-
const fs = require("fs");
67-
68129
fs.writeFileSync(filePaths.js, sJsFileContentTemplate, { flag: "wx+" });
69130
fs.writeFileSync(filePaths.css, "", { flag: "wx+" });
70131
fs.writeFileSync(filePaths.hbs, "<div>Hello World</div>", { flag: "wx+" });

0 commit comments

Comments
 (0)