Skip to content

Commit

Permalink
generate-arg-types: Allow multiple exports in component files (#1953)
Browse files Browse the repository at this point in the history
## Description

Allow multiple exports in components. Most radix components will look
like
`export const Tooltip = TooltipPrimitive.Root;`

The others would be a simpe wrappers over radix with just type changes
like like `Omit<TriggerProps, "className">

It's too much to create for each sucj export a standalone file.

After this PR following will work.

```ts
import * as TooltipPrimitive from "@radix-ui/react-tooltip";

export const Tooltip = TooltipPrimitive.Root;
export const TooltipTrigger = TooltipPrimitive.Trigger;
```

Generated exports in case of multiple entries are

```
export propsTooltip: ...
export propsTooltipTrigger ....
```

In case of single export previous behaviour is preserved for now, but
should be changed IMO in the future PRs


Known bugs

Until this merged we should not use displayName (have no idea why we use
it at all) in files with multiple exports
styleguidist/react-docgen-typescript#449

As of now will add comments
```
import * as TooltipPrimitive from "@radix-ui/react-tooltip";

export const Tooltip = TooltipPrimitive.Root;
// We can't use .displayName until this is merged styleguidist/react-docgen-typescript#449
// Tooltip.displayName = "Tooltip";

export const TooltipTrigger = TooltipPrimitive.Trigger;
// We can't use .displayName until this is merged styleguidist/react-docgen-typescript#449
// TooltipTrigger.displayName = "TooltipTrigger";
```


## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio-builder/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env.example`
and the `builder/env-check.js` if mandatory
  • Loading branch information
istarkov authored Jul 18, 2023
1 parent 6bac6f2 commit 0459452
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions packages/generate-arg-types/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (componentFiles.length === 0) {
const tsConfigParser = withCustomConfig(tsConfigPath, options);

// For each component file generate argTypes based on the propTypes
componentFiles.forEach((filePath) => {
for (const filePath of componentFiles) {
const generatedDir = path.join(path.dirname(filePath), GENERATED_FILES_DIR);

if (!fs.existsSync(generatedDir)) {
Expand All @@ -49,14 +49,38 @@ componentFiles.forEach((filePath) => {
const generatedFile = `${path.basename(filePath, ".tsx")}.props.ts`;
const generatedPath = path.join(generatedDir, generatedFile);

const res = tsConfigParser.parse(filePath);
const argTypes = propsToArgTypes(res[0].props);
const componentDocs = tsConfigParser.parse(filePath);

if (componentDocs.length === 0) {
console.error(`No propTypes found for ${filePath}`);
continue;
}

fs.ensureFileSync(generatedPath);
fs.writeFileSync(
generatedPath,
`import type { PropMeta } from "@webstudio-is/generate-arg-types";\n\nexport const props: Record<string, PropMeta> = ${JSON.stringify(
argTypes
)}`
);

let fileContent = `import type { PropMeta } from "@webstudio-is/generate-arg-types";\n`;

if (componentDocs.length === 1) {
const argTypes = propsToArgTypes(componentDocs[0].props);

fileContent = `${fileContent}
export const props: Record<string, PropMeta> = ${JSON.stringify(
argTypes
)}`;
} else {
for (const componentDoc of componentDocs) {
const componentName = componentDoc.displayName;

const argTypes = propsToArgTypes(componentDoc.props);

fileContent = `${fileContent}
export const props${componentName}: Record<string, PropMeta> = ${JSON.stringify(
argTypes
)}`;
}
}

fs.writeFileSync(generatedPath, fileContent);

console.log(`Done generating argTypes for ${generatedPath}`);
});
}

0 comments on commit 0459452

Please sign in to comment.