Skip to content

Commit a932073

Browse files
authored
chore: improve generating new package and component scripts (#11420)
- Move the bundle from the root folder to the `src` folder and update the test page to use Vite environment variables for resolving the bundle path. - Remove the `componentName` and `tagName` options from both the CLI and the configuration wizard. - Update the test specification file to use the correct tag for the initially created component, replacing the previously "hardcoded-button" tag with the component's actual tag. - Ensure that all new components are generated with the `my-` prefix, maintaining consistent naming whether the component name is a single word or multiple words.
1 parent 06d206e commit a932073

File tree

7 files changed

+16
-46
lines changed

7 files changed

+16
-46
lines changed

packages/create-package/README.md

-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Usage:
1616
1717
Options:
1818
--name <string> - defines the package name
19-
--component-name <string> - defines the component class name that will be created in your new package
20-
--tag <string> - defines the tag name of the sample web component that will be created in your new package
2119
--test-setup <"cypress" | "manual"> - defines whether the predefined test setup should be added or it will be configured manually.
2220
--skip - skips configuration and generates package with a default value for each parameter that wasn't passed
2321
```
@@ -32,8 +30,6 @@ Usage:
3230
yarn create @ui5/webcomponents-package [OPTIONS]
3331
Options:
3432
--name <string> - defines the package name
35-
--component-name <string> - defines the component class name that will be created in your new package
36-
--tag <string> - defines the tag name of the sample web component that will be created in your new package
3733
--test-setup <"cypress" | "manual"> - defines whether the predefined test setup should be added or it will be configured manually.
3834
--skip - skips configuration and generates package with a default value for each parameter that wasn't passed
3935
```

packages/create-package/create-package.js

+3-31
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,21 @@ const SUPPORTED_TEST_SETUPS = ["cypress", "manual"];
2121
const SRC_DIR = path.join(__dirname, "template");
2222
const FILES_TO_RENAME = {
2323
"npmrc": ".npmrc",
24+
"env": ".env",
2425
"gitignore": ".gitignore",
2526
"tsconfig.template.json": "tsconfig.json",
2627
"cypress/tsconfig.template.json": "cypress/tsconfig.json"
2728
};
2829
const FILES_TO_COPY = ["test/pages/img/logo.png"];
2930

3031
// Validation Patterns
31-
const ComponentNamePattern = /^[A-Z][A-Za-z0-9]+$/;
3232
const PackageNamePattern =
3333
/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
3434
const TagPattern = /^[a-z0-9]+?-[a-zA-Z0-9\-_]+?[a-z0-9]$/;
3535

3636
// Utility Functions
3737
const isPackageNameValid = name =>
3838
typeof name === "string" && PackageNamePattern.test(name);
39-
const isComponentNameValid = name =>
40-
typeof name === "string" && ComponentNamePattern.test(name);
4139
const isTagValid = tag => typeof tag === "string" && TagPattern.test(tag);
4240
const isTestSetupValid = setup =>
4341
typeof setup === "string" && SUPPORTED_TEST_SETUPS.includes(setup);
@@ -75,7 +73,7 @@ const generateFilesContent = async (
7573
: "",
7674
INIT_PACKAGE_CYPRESS_DEV_DEPS:
7775
testSetup === "cypress"
78-
? `"@ui5/cypress-ct-ui5-webc": "^0.0.3",\n"cypress": "^13.11.0",`
76+
? `"@ui5/cypress-ct-ui5-webc": "^0.0.4",\n"cypress": "^13.11.0",`
7977
: "",
8078
INIT_PACKAGE_CYPRESS_TEST_COMMANDS:
8179
testSetup === "cypress"
@@ -176,26 +174,14 @@ const createWebcomponentsPackage = async () => {
176174
);
177175
}
178176

179-
if (argv.componentName && !isComponentNameValid(argv.componentName)) {
180-
throw new Error(
181-
"The component name should be a string, starting with a capital letter [A-Z][a-z], for example: Button, MyButton, etc.",
182-
);
183-
}
184-
185-
if (argv.tag && !isTagValid(argv.tag)) {
186-
throw new Error(
187-
"The tag should be in kebab-case (f.e my-component) and it can't be a single word.",
188-
);
189-
}
190-
191177
if (argv.testSetup && !isTestSetupValid(argv.testSetup)) {
192178
throw new Error(
193179
`The test setup should be a string and one of the following options: ${SUPPORTED_TEST_SETUPS.join(", ")}`,
194180
);
195181
}
196182

197183
let packageName = argv.name || "my-package";
198-
let componentName = argv.componentName || "MyComponent";
184+
let componentName = "MyComponent";
199185
let testSetup = argv.testSetup || "manual";
200186
const skipSubfolder = !!argv.skipSubfolder;
201187

@@ -221,20 +207,6 @@ const createWebcomponentsPackage = async () => {
221207
packageName = response.name;
222208
}
223209

224-
if (!argv.componentName) {
225-
response = await prompts({
226-
type: "text",
227-
name: "componentName",
228-
message: "Component name:",
229-
initial: "MyComponent",
230-
validate: value =>
231-
isComponentNameValid(value)
232-
? true
233-
: "Component name should follow PascalCase naming convention (f.e. Button, MyButton, etc.).",
234-
});
235-
componentName = response.componentName;
236-
}
237-
238210
if (!argv.testSetup) {
239211
response = await prompts({
240212
type: "select",

packages/create-package/template/cypress/component/MyFirstComponent.cy.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ describe('{{INIT_PACKAGE_VAR_CLASS_NAME}}.cy.tsx', () => {
44
it('playground', () => {
55
cy.mount(<{{INIT_PACKAGE_VAR_CLASS_NAME}} />)
66

7-
cy.get("[hardcoded-button]")
7+
cy.get("[{{INIT_PACKAGE_VAR_TAG}}]")
88
.click();
99

10-
cy.get("[hardcoded-button]")
10+
cy.get("[{{INIT_PACKAGE_VAR_TAG}}]")
1111
.should("have.prop", "count", 1)
1212
})
1313
})

packages/create-package/template/env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_BUNDLE_PATH="../../dist/bundle.esm.js"

packages/create-package/template/bundle.esm.js renamed to packages/create-package/template/src/bundle.esm.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import { getNoConflict, setNoConflict } from "@ui5/webcomponents-base/dist/confi
99
import { getFirstDayOfWeek } from "@ui5/webcomponents-base/dist/config/FormatSettings.js";
1010

1111
// Enable additional themes and i18n texts
12-
import "./dist/Assets.js";
12+
import "./Assets.js";
1313

14-
// Import your web components here from the dist/ directory
15-
import "./dist/{{INIT_PACKAGE_VAR_CLASS_NAME}}.js";
14+
// Import your web components here from the src/ directory
15+
import "./{{INIT_PACKAGE_VAR_CLASS_NAME}}.js";
1616

17+
// @ts-expect-error
1718
window["sap-ui-webcomponents-bundle"] = {
1819
renderFinished,
1920
configuration: {

packages/create-package/template/test/pages/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</script>
1717

1818
<link rel="stylesheet" type="text/css" href="./css/index.css">
19-
<script src="../../bundle.esm.js" type="module"></script>
19+
<script src="%VITE_BUNDLE_PATH%" type="module"></script>
2020
</head>
2121

2222
<body>

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ const Component = require("./Component.js");
44
const ComponentTemplate= require("./ComponentTemplate.js");
55

66
/**
7-
* Hyphanates the given PascalCase string, f.e.:
8-
* Foo -> "my-foo" (adds preffix)
9-
* FooBar -> "foo-bar"
7+
* Hyphanates the given PascalCase string and adds prefix, f.e.:
8+
* Foo -> "my-foo"
9+
* FooBar -> "my-foo-bar"
1010
*/
1111
const hyphaneteComponentName = (componentName) => {
1212
const result = componentName.replace(/([a-z])([A-Z])/g, '$1-$2' ).toLowerCase();
1313

14-
return result.includes("-") ? result : `my-${result}`;
14+
return `my-${result}`;
1515
};
1616

1717
/**
@@ -75,7 +75,7 @@ const generateFiles = (componentName, tagName, library, packageName) => {
7575

7676
// Change the color of the output
7777
console.warn('\x1b[33m%s\x1b[0m', `
78-
Now, import the component via: "import ${componentName} from ./${componentName}.js";
78+
Now, import the component via: "import ${componentName} from "./${componentName}.js";
7979
And, add it to your HTML: <${tagName}></${tagName}>.`);
8080
}
8181

0 commit comments

Comments
 (0)