-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: script to create new setup for atomic component (#4913)
https://coveord.atlassian.net/browse/KIT-3908 needs #4914 to work correctly so if you want to test it, test it from there. A new script to generate file structure and basic files for new lit atomic components. It is used like this : `npx nx run atomic:generate-component --name=atomic-ball --output=src/components/search/atomic-ball` The output arg is always relative to the atomic package root. This is to help us during the migration and it will keep changing to make it better.
- Loading branch information
1 parent
87681ca
commit f872a49
Showing
209 changed files
with
731 additions
and
558 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/atomic/scripts/generate-component-templates/component.css.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
h1 { | ||
color: #3490dc; | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/atomic/scripts/generate-component-templates/component.mdx.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {Canvas, ArgTypes, Meta, Title, Description} from '@storybook/blocks'; | ||
import * as {{namePascalCase}}Stories from './{{name}}.new.stories'; | ||
|
||
{/* Storybook provides a number of blocs to construct documentation pages */} | ||
{/* https://storybook.js.org/docs/writing-docs/doc-blocks#available-blocks */} | ||
|
||
<Meta of={ {{namePascalCase}}Stories } /> | ||
|
||
<Title /> | ||
<Description /> | ||
|
||
<Canvas of={ {{namePascalCase}}Stories.Default } /> | ||
|
||
## Properties | ||
<ArgTypes /> | ||
|
25 changes: 25 additions & 0 deletions
25
packages/atomic/scripts/generate-component-templates/component.new.stories.tsx.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {parameters} from '@/storybook-utils/common/common-meta-parameters'; | ||
import {renderComponent} from '@/storybook-utils/common/render-component'; | ||
import {wrapInSearchInterface} from '@/storybook-utils/search/search-interface-wrapper'; | ||
// import {wrapInCommerceInterface} from '@/storybook-utils/commerce/commerce-interface-wrapper'; | ||
import type {Meta, StoryObj as Story} from '@storybook/web-components'; | ||
|
||
// Wrap it in whatever interface/component you need | ||
const {decorator, play} = wrapInSearchInterface(); | ||
// const {decorator, play} = wrapInCommerceInterface(); | ||
|
||
const meta: Meta = { | ||
component: '{{name}}', | ||
title: '{{namePascalCase}}', | ||
id: '{{name}}', | ||
render: renderComponent, | ||
decorators: [decorator], | ||
parameters, | ||
play, | ||
}; | ||
|
||
export default meta; | ||
|
||
export const Default: Story = { | ||
name: '{{name}}', | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/atomic/scripts/generate-component-templates/component.spec.ts.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {within} from 'shadow-dom-testing-library'; | ||
import {describe, test, expect, beforeAll, afterAll} from 'vitest'; | ||
import './{{name}}'; | ||
import { {{namePascalCase}} } from './{{name}}'; | ||
|
||
describe('{{namePascalCase}}', () => { | ||
let element: {{namePascalCase}}; | ||
beforeAll(async () => { | ||
element = document.createElement('{{name}}'); | ||
document.body.appendChild(element); | ||
}); | ||
|
||
afterAll(() => { | ||
document.body.removeChild(element); | ||
}); | ||
|
||
test('should render the component', async () => { | ||
expect(element.shadowRoot).toBeTruthy(); | ||
const text = await within(element).findByShadowText('Hello World'); | ||
expect(text).toBeTruthy(); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/atomic/scripts/generate-component-templates/component.ts.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {TailwindLitElement} from '@/src/utils/tailwind.element'; | ||
import {CSSResultGroup, html, unsafeCSS} from 'lit'; | ||
import {customElement, property} from 'lit/decorators.js'; | ||
import styles from './{{name}}.css'; | ||
|
||
/** | ||
* The {{name}} is a component that does something. | ||
*/ | ||
@customElement('{{name}}') | ||
export class {{namePascalCase}} extends TailwindLitElement { | ||
static styles: CSSResultGroup = [ | ||
TailwindLitElement.styles, | ||
unsafeCSS(styles), | ||
]; | ||
|
||
/** | ||
* The name of the {{name}} | ||
*/ | ||
@property() name = 'World'; | ||
|
||
render() { | ||
return html`<div> | ||
<h1>{{name}}</h1> | ||
<p>Hello ${this.name}</p> | ||
</div>`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'{{name}}': {{namePascalCase}}; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/atomic/scripts/generate-component-templates/e2e/component.e2e.ts.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {test, expect} from './fixture'; | ||
|
||
test.describe('{{namePascalCase}}', () => { | ||
test.beforeEach(async ({ {{shorterName}} }) => { | ||
await {{shorterName}}.load(); | ||
}); | ||
|
||
test('should be accessible', async ({makeAxeBuilder}) => { | ||
const accessibilityResults = await makeAxeBuilder().analyze(); | ||
expect(accessibilityResults.violations.length).toEqual(0); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/atomic/scripts/generate-component-templates/e2e/fixture.ts.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {test as base} from '@playwright/test'; | ||
import {AxeFixture, makeAxeBuilder} from '@/playwright-utils/base-fixture'; | ||
import { {{namePascalCase}}PageObject } from './page-object'; | ||
|
||
type Fixtures = { | ||
{{shorterName}}: {{namePascalCase}}PageObject; | ||
}; | ||
|
||
export const test = base.extend<Fixtures & AxeFixture>({ | ||
makeAxeBuilder, | ||
{{shorterName}}: async ({page}, use) => { | ||
await use(new {{namePascalCase}}PageObject(page)); | ||
}, | ||
}); | ||
|
||
export {expect} from '@playwright/test'; |
8 changes: 8 additions & 0 deletions
8
packages/atomic/scripts/generate-component-templates/e2e/page-object.ts.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type {Page} from '@playwright/test'; | ||
import {BasePageObject} from '@/playwright-utils/lit-base-page-object'; | ||
|
||
export class {{namePascalCase}}PageObject extends BasePageObject { | ||
constructor(page: Page) { | ||
super(page, '{{name}}'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import fs from 'fs-extra'; | ||
import handlebars from 'handlebars'; | ||
import path from 'path'; | ||
import prettier from 'prettier'; | ||
|
||
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1); | ||
const kebabToPascal = (str) => str.split('-').map(capitalize).join(''); | ||
|
||
async function formatWithPrettier(content, filePath) { | ||
try { | ||
const options = await prettier.resolveConfig(filePath); | ||
return prettier.format(content, {...options, filepath: filePath}); | ||
} catch (error) { | ||
console.warn(`Failed to format ${filePath} with Prettier`, error); | ||
return content; | ||
} | ||
} | ||
|
||
async function generateFiles(name, outputDir) { | ||
const templatesDir = path.resolve( | ||
import.meta.dirname, | ||
'generate-component-templates' | ||
); | ||
const resolvedOutputDir = path.resolve(outputDir); | ||
const namePascalCase = kebabToPascal(name); | ||
const shorterName = name.replace(/^atomic-/, '').toLowerCase(); | ||
|
||
const files = [ | ||
{template: 'component.ts.hbs', output: `${name}.ts`}, | ||
{template: 'component.mdx.hbs', output: `${name}.mdx`}, | ||
{ | ||
template: 'component.new.stories.tsx.hbs', | ||
output: `${name}.new.stories.tsx`, | ||
}, | ||
{template: 'component.css.hbs', output: `${name}.css`}, | ||
{template: 'component.spec.ts.hbs', output: `${name}.spec.ts`}, | ||
{template: 'e2e/component.e2e.ts.hbs', output: `e2e/${name}.e2e.ts`}, | ||
{template: 'e2e/fixture.ts.hbs', output: `e2e/fixture.ts`}, | ||
{template: 'e2e/page-object.ts.hbs', output: `e2e/page-object.ts`}, | ||
]; | ||
|
||
for (const file of files) { | ||
const templatePath = path.join(templatesDir, file.template); | ||
|
||
const outputPath = path.join( | ||
resolvedOutputDir, | ||
file.output.replace('noop', name) | ||
); | ||
|
||
// Does not overwrite existing files | ||
if (await fs.pathExists(outputPath)) { | ||
console.log(`Skipped (already exists): ${outputPath}`); | ||
continue; | ||
} | ||
|
||
const templateContent = await fs.readFile(templatePath, 'utf8'); | ||
const compiled = handlebars.compile(templateContent); | ||
let content = compiled({name, namePascalCase, shorterName}); | ||
|
||
// Format each file with Prettier | ||
content = await formatWithPrettier(content, outputPath); | ||
|
||
await fs.ensureDir(path.dirname(outputPath)); | ||
await fs.writeFile(outputPath, content, 'utf8'); | ||
console.log(`Created: ${outputPath}`); | ||
} | ||
} | ||
|
||
const [componentName, outputDir] = process.argv.slice(2); | ||
|
||
// Ensure the component name is prefixed with 'atomic-' if it's not already there | ||
const normalizedComponentName = componentName.startsWith('atomic-') | ||
? componentName | ||
: `atomic-${componentName}`; | ||
|
||
let resolvedOutputDir; | ||
|
||
if (outputDir) { | ||
// Use the provided output dir and add the component name | ||
resolvedOutputDir = path.join(outputDir, normalizedComponentName); | ||
} else { | ||
// Default to src/components/commerce/<component-name> if no output dir is provided | ||
resolvedOutputDir = path.resolve( | ||
'src', | ||
'components', | ||
'commerce', | ||
normalizedComponentName | ||
); | ||
} | ||
|
||
if (!componentName) { | ||
console.error( | ||
'Usage: node generate-component.js <component-name> [<output-dir>]' | ||
); | ||
process.exit(1); | ||
} | ||
|
||
generateFiles(normalizedComponentName, resolvedOutputDir).catch(console.error); | ||
|
||
// add the import to the lazy index file | ||
// add the import to the index file | ||
// change the output arg to always start with search/commerce/insight/ipx/recommendations |
Oops, something went wrong.