diff --git a/packages/tools/lib/create-new-component/index.js b/packages/tools/lib/create-new-component/index.js index c937bdbcc988..44d21f646f19 100644 --- a/packages/tools/lib/create-new-component/index.js +++ b/packages/tools/lib/create-new-component/index.js @@ -1,80 +1,7 @@ const fs = require("fs"); - -const jsFileContentTemplate = componentName => { - return `import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js"; -import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js"; -import ${componentName}Template from "./generated/templates/${componentName}Template.lit.js"; - -// Styles -import ${componentName}Css from "./generated/themes/${componentName}.css.js"; - -/** - * @public - */ -const metadata = { - tag: "${tagName}", - properties: /** @lends sap.ui.webc.${library}.${componentName}.prototype */ { - // - }, - slots: /** @lends sap.ui.webc.${library}.${componentName}.prototype */ { - // - }, - events: /** @lends sap.ui.webc.${library}.${componentName}.prototype */ { - // - }, -}; - -/** - * @class - * - *
${tagName}
- * import ${packageName}/dist/${componentName}.js";
- *
- * @constructor
- * @author SAP SE
- * @alias sap.ui.webc.${library}.${componentName}
- * @extends sap.ui.webc.base.UI5Element
- * @tagname ${tagName}
- * @public
- */
-class ${componentName} extends UI5Element {
- static get metadata() {
- return metadata;
- }
-
- static get render() {
- return litRender;
- }
-
- static get styles() {
- return ${componentName}Css;
- }
-
- static get template() {
- return ${componentName}Template;
- }
-
- static get dependencies() {
- return [];
- }
-
- static async onDefine() {
-
- }
-}
-
-${componentName}.define();
-
-export default ${componentName};
-`;
-};
+const prompts = require("prompts");
+const jsFileContentTemplate = require("./jsFileContentTemplate.js");
+const tsFileContentTemplate = require("./tsFileContentTemplate.js");
const getPackageName = () => {
if (!fs.existsSync("./package.json")) {
@@ -108,47 +35,117 @@ const getLibraryName = packageName => {
return packageName.substr("webcomponents-".length);
};
-const camelToKebabCase = string => string.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
+// String manipulation
+const capitalizeFirstLetter = string => string.charAt(0).toUpperCase() + string.slice(1);
+
+// Validation of user input
+const isNameValid = name => typeof name === "string" && name.match(/^[a-zA-Z][a-zA-Z0-9_-]*$/);
+const isTagNameValid = tagName => tagName.match(/^([a-z][a-z0-9]*-)([a-z0-9]+(-[a-z0-9]+)*)$/);
+
+const generateFiles = (componentName, tagName, library, packageName, isTypeScript) => {
+ componentName = capitalizeFirstLetter(componentName);
+ const filePaths = {
+ "main": isTypeScript
+ ? `./src/${componentName}.ts`
+ : `./src/${componentName}.js`,
+ "css": `./src/themes/${componentName}.css`,
+ "template": `./src/${componentName}.hbs`,
+ };
-const packageName = getPackageName();
-const library = getLibraryName(packageName);
+ const FileContentTemplate = isTypeScript
+ ? tsFileContentTemplate(componentName, tagName, library, packageName)
+ : jsFileContentTemplate(componentName, tagName, library, packageName);
-const consoleArguments = process.argv.slice(2);
-const componentName = consoleArguments[0];
+ fs.writeFileSync(filePaths.main, FileContentTemplate, { flag: "wx+" });
+ fs.writeFileSync(filePaths.css, "", { flag: "wx+" });
+ fs.writeFileSync(filePaths.template, "${tagName}
+ * import ${packageName}/dist/${componentName}.js";
+ *
+ * @constructor
+ * @author SAP SE
+ * @alias sap.ui.webc.${library}.${componentName}
+ * @extends sap.ui.webc.base.UI5Element
+ * @tagname ${tagName}
+ * @public
+ */
+class ${componentName} extends UI5Element {
+ static get metadata() {
+ return metadata;
+ }
+
+ static get render() {
+ return litRender;
+ }
+
+ static get styles() {
+ return ${componentName}Css;
+ }
+
+ static get template() {
+ return ${componentName}Template;
+ }
+
+ static get dependencies() {
+ return [];
+ }
+
+ static async onDefine() {
+
+ }
+}
+
+${componentName}.define();
+
+export default ${componentName};
+`;
+};
+
+module.exports = jsFileContentTemplate;
diff --git a/packages/tools/lib/create-new-component/tsFileContentTemplate.js b/packages/tools/lib/create-new-component/tsFileContentTemplate.js
new file mode 100644
index 000000000000..169a41d59485
--- /dev/null
+++ b/packages/tools/lib/create-new-component/tsFileContentTemplate.js
@@ -0,0 +1,84 @@
+const tsFileContentTemplate = (componentName, tagName, library, packageName) => {
+ return `import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
+import customElement from "@ui5/webcomponents-base/dist/decorators/customElement.js";
+import property from "@ui5/webcomponents-base/dist/decorators/property.js";
+import slot from "@ui5/webcomponents-base/dist/decorators/slot.js";
+import event from "@ui5/webcomponents-base/dist/decorators/event.js";
+import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
+
+import ${componentName}Template from "./generated/templates/${componentName}Template.lit.js";
+
+// Styles
+import ${componentName}Css from "./generated/themes/${componentName}.css.js";
+
+/**
+ * @class
+ *
+ * ${tagName}
+ * import ${packageName}/dist/${componentName}.js";
+ *
+ * @constructor
+ * @author SAP SE
+ * @alias sap.ui.webc.${library}.${componentName}
+ * @extends sap.ui.webc.base.UI5Element
+ * @tagname ${tagName}
+ * @public
+ */
+@customElement({
+ tag: "${tagName}",
+ renderer: litRender,
+ styles: ${componentName}Css,
+ template: ${componentName}Template,
+ dependencies: [],
+})
+
+/**
+ * Example custom event.
+ * Please keep in mind that all public events should be documented in the API Reference as shown below.
+ *
+ * @event sap.ui.webc.${library}.${componentName}#interact
+ * @public
+ */
+@event("interact", { detail: { /* event payload ( optional ) */ } })
+class ${componentName} extends UI5Element {
+ /**
+ * Defines the value of the component.
+ *
+ * @type {string}
+ * @name sap.ui.webc.${library}.${componentName}.prototype.value
+ * @defaultvalue ""
+ * @public
+ */
+ @property()
+ value!: string;
+
+ /**
+ * Defines the text of the component.
+ *
+ * @type {Node[]}
+ * @name sap.ui.webc.${library}.${componentName}.prototype.default
+ * @slot
+ * @public
+ */
+ @slot({ type: Node, "default": true })
+ text!: Array