Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ prettier でフォーマットできるよう機能更新 #4

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions demo/react/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @type {import('prettier').Config}
*/
const config = {
printWidth: 80,
tabWidth: 4,
semi: false,
singleQuote: true,
bracketSameLine: false,
singleAttributePerLine: true,
};

module.exports = config;
1 change: 1 addition & 0 deletions demo/react/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const config: StorybookConfig = {
autoStoryGenerator.vite({
preset: "react",
imports: ["**/src/components/**/*.tsx"],
prettierConfigPath: resolve(__dirname, "../.prettierrc"),
}),
],

Expand Down
27 changes: 27 additions & 0 deletions demo/react/src/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Meta, StoryObj } from '@storybook/react'

import { Button } from './Button'

const meta: Meta<typeof Button> = {
title: 'components/Button',
component: Button,
tags: ['autodocs'],
args: {
primary: undefined,
backgroundColor: undefined,
size: undefined,
label: undefined,
onClick: undefined,
},
argTypes: {
primary: { control: 'boolean' },
backgroundColor: { control: 'text' },
size: { control: 'select', options: ['small', 'medium', 'large'] },
label: { control: 'text' },
},
}

export default meta
type Story = StoryObj<typeof meta>

export const Primary: Story = {}
2 changes: 1 addition & 1 deletion demo/react/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Button: FC<ButtonProps> = ({
<button
type="button"
className={["storybook-button", `storybook-button--${size}`, mode].join(
" "
" ",
)}
style={{ backgroundColor }}
{...props}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"release": true
},
"git": {
"requireBranch": "release",
"requireCleanWorkingDir": false,
"addFiles": ["package.json"],
"commitMessage": ":bookmark: release @takuma-ru/auto-story-generator@${version}"
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-story-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"consola": "^3.2.3",
"magicast": "^0.3.2",
"minimatch": "^9.0.3",
"prettier": "^3.2.4",
"scule": "^1.1.1",
"ts-morph": "^21.0.1",
"unplugin": "^1.5.1"
Expand All @@ -76,7 +77,6 @@
"eslint": "^8.52.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-unused-imports": "^3.0.0",
"prettier": "^3.1.1",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
}
Expand Down
10 changes: 7 additions & 3 deletions packages/auto-story-generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { consola } from "consola";

Check warning on line 1 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console

Check warning on line 1 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console
import { loadFile } from "magicast";
import { minimatch } from "minimatch";
import { Project } from "ts-morph";
Expand All @@ -22,6 +22,8 @@
* `**\/src/components/**\/*.ts`
*/
imports?: string[];

prettierConfigPath?: string;
};

const unplugin = createUnplugin((options: AsgOptions) => {
Expand All @@ -37,7 +39,7 @@
: [true];

if (!isMatches.includes(true)) {
return consola.error("Not a target file for automatic generation");

Check warning on line 42 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console
}

const projectRootDir = process.cwd();
Expand All @@ -46,8 +48,8 @@
const componentName = fileName?.replace(`.${fileType}`, "");
const relativeSourceFilePath = file.replace(projectRootDir, "");

if (!componentName) {
if (!componentName || !fileName) {
return consola.error("Could not find component name");

Check warning on line 52 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console
}

// consola.info(`${componentName} component has been changed`);
Expand All @@ -56,17 +58,18 @@
const project = new Project();
const sourceFile = project.createSourceFile(fileName || "", mod.$code);

consola.start(`${componentName} Story file is being generated ....`);

Check warning on line 61 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console

switch (options.preset) {
case "lit": {
await genLitStoryFile({
componentName,
fileName: fileName || "",
fileName: fileName,
path: file,
type: `.${fileType}`,
relativeSourceFilePath,
sourceFile,
prettierConfigPath: options.prettierConfigPath,
});

break;
Expand All @@ -75,28 +78,29 @@
case "react": {
await genReactStoryFile({
componentName,
fileName: fileName || "",
fileName: fileName,
path: file,
type: `.${fileType}`,
relativeSourceFilePath,
sourceFile,
prettierConfigPath: options.prettierConfigPath,
});

break;
}

case "vue": {
consola.error("Not yet supported.");

Check warning on line 93 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console
break;
}

case "custom": {
consola.error("Not yet supported.");

Check warning on line 98 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console
break;
}

default: {
consola.error(

Check warning on line 103 in packages/auto-story-generator/src/index.ts

View workflow job for this annotation

GitHub Actions / eslint-check

Logs are output on the console
`Preset ${options.preset} is not supported. Please use one of the following: lit, react, vue, custom`,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const genLitStoryFile = async ({
type,
relativeSourceFilePath,
sourceFile,
prettierConfigPath,
}: GenStoryFileOptions["fileOptions"]) => {
const propTypes = genLitPropTypes({ sourceFile, componentName });
const pascalComponentName = pascalCase(componentName);
Expand Down Expand Up @@ -132,6 +133,7 @@ export const Primary: ${pascalComponentName}Story = {};
type,
relativeSourceFilePath,
sourceFile,
prettierConfigPath,
},
generateOptions: {
fileType: ".stories.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const genReactStoryFile = async ({
type,
relativeSourceFilePath,
sourceFile,
prettierConfigPath,
}: GenStoryFileOptions["fileOptions"]) => {
const propTypes = genReactPropTypes({ sourceFile, componentName });
const pascalComponentName = pascalCase(componentName);
Expand Down Expand Up @@ -100,6 +101,7 @@ export const Primary: Story = {};
type,
relativeSourceFilePath,
sourceFile,
prettierConfigPath,
},
generateOptions: {
fileType: ".stories.tsx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type GenStoryFileOptions = {
type: `.${string}`;
relativeSourceFilePath: string;
sourceFile: SourceFile;
prettierConfigPath?: string;
};
generateOptions: {
fileType: `.stories.${string}`;
Expand Down
25 changes: 24 additions & 1 deletion packages/auto-story-generator/src/utils/genStoryFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "fs";

import { consola } from "consola";
import * as prettier from "prettier";
import { Project, SyntaxKind } from "ts-morph";

import { GenStoryFileOptions } from "~/src/types/GenStoryFileType";
Expand Down Expand Up @@ -95,10 +96,32 @@ export const genStoryFile = async ({
// ファイルを保存する
await storiesProject
.save()
.then(() => {
.then(async () => {
consola.success(
`Successfully updated args in ${storiesSourceFile.getFilePath()}`,
);

const fileContent = fs.readFileSync(storiesFilePath, "utf-8");

const config: prettier.Options | null = fileOptions.prettierConfigPath
? await prettier.resolveConfig(fileOptions.prettierConfigPath)
: {
semi: true,
trailingComma: "all",
singleQuote: false,
printWidth: 80,
tabWidth: 2,
endOfLine: "lf",
};

// Format the content using Prettier
const formattedContent = await prettier.format(fileContent, {
...config,
parser: "typescript",
});

// Write the formatted content back to the file
fs.writeFileSync(storiesFilePath, formattedContent);
})
.catch((err) => {
consola.error(err);
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading