From 0c845a434fd1c4b68097d4021a18416a71627d26 Mon Sep 17 00:00:00 2001
From: takuma-ru <kanekotakuma@team-lab.com>
Date: Wed, 24 Jan 2024 12:46:32 +0900
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E3=83=AA=E3=83=AA=E3=83=BC?=
 =?UTF-8?q?=E3=82=B9=E7=94=A8Action=E3=81=A8config=E8=BF=BD=E5=8A=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .github/workflows/pr-check.yml                | 119 ++++++++++++++++
 .github/workflows/publish-package.yml         | 132 ++++++++++++++++++
 packages/auto-story-generator/README.md       |  32 ++++-
 packages/auto-story-generator/package.json    |  18 ++-
 packages/auto-story-generator/release-it.json |  14 ++
 5 files changed, 311 insertions(+), 4 deletions(-)
 create mode 100644 .github/workflows/pr-check.yml
 create mode 100644 .github/workflows/publish-package.yml
 create mode 100644 packages/auto-story-generator/release-it.json

diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml
new file mode 100644
index 0000000..1e6453f
--- /dev/null
+++ b/.github/workflows/pr-check.yml
@@ -0,0 +1,119 @@
+name: PR Check
+on: [pull_request]
+
+jobs:
+  init:
+    runs-on: ubuntu-latest
+    env:
+      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
+      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+      cache-name: cache-node-modules
+
+    steps:
+      - uses: actions/checkout@v4
+        with:
+          fetch-depth: 0
+
+      - name: Set git config
+        run: |
+          git config user.name "${GITHUB_ACTOR}"
+          git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
+
+      - name: Setup node
+        uses: actions/setup-node@v4
+        with:
+          node-version-file: "package.json"
+
+      - uses: pnpm/action-setup@v2
+        name: Install pnpm
+        id: pnpm-install
+        with:
+          version: 8.9.2
+          run_install: false
+
+      - name: Get pnpm store directory
+        id: pnpm-cache
+        shell: bash
+        run: |
+          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
+
+      - uses: actions/cache@v3
+        name: Setup pnpm cache
+        with:
+          path: '**/node_modules'
+          key: ${{ runner.os }}-pnpm-store-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
+          restore-keys: |
+            ${{ runner.os }}-pnpm-store-${{ env.cache-name }}-
+
+      - name: Install Node Dependencies
+        run: pnpm install
+
+      - name: Set .npmrc
+        run: echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc
+
+    # Output the cache-name environment variable
+    # so that it can be used in other jobs
+    outputs:
+      cache-name: ${{ env.cache-name }}
+
+  type-check:
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    needs: init
+    steps:
+      - uses: actions/checkout@v4
+      - uses: actions/setup-node@v4
+      - uses: pnpm/action-setup@v2
+      - uses: actions/cache@v3
+        with:
+          path: '**/node_modules'
+          key: ${{ runner.os }}-pnpm-store-${{ needs.init.outputs.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
+      - name: Type check
+        run: pnpm asg lint:type-check
+
+  eslint-check:
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    needs: init
+    steps:
+      - uses: actions/checkout@v4
+      - uses: actions/setup-node@v4
+      - uses: pnpm/action-setup@v2
+      - uses: actions/cache@v3
+        with:
+          path: '**/node_modules'
+          key: ${{ runner.os }}-pnpm-store-${{ needs.init.outputs.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
+      - name: ESlint check
+        run: pnpm asg lint
+
+  build-check:
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    needs: init
+    steps:
+      - uses: actions/checkout@v4
+      - uses: actions/setup-node@v4
+      - uses: pnpm/action-setup@v2
+      - uses: actions/cache@v3
+        with:
+          path: '**/node_modules'
+          key: ${{ runner.os }}-pnpm-store-${{ needs.init.outputs.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
+      - name: Build check
+        run: pnpm asg build
+
+  publish-check:
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    needs: [init, build-check]
+    steps:
+      - uses: actions/checkout@v4
+      - uses: actions/setup-node@v4
+      - uses: pnpm/action-setup@v2
+      - uses: actions/cache@v3
+        with:
+          path: '**/node_modules'
+          key: ${{ runner.os }}-pnpm-store-${{ needs.init.outputs.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
+      - name: Build check
+        run: pnpm asg build
+      - name: Publish check
+        run: pnpm asg publish --dry-run --no-git-checks
diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml
new file mode 100644
index 0000000..5fe99e6
--- /dev/null
+++ b/.github/workflows/publish-package.yml
@@ -0,0 +1,132 @@
+name: Release @takuma-ru/auto-story-generator package
+
+run-name: Release ${{ inputs.releaseLevel }} version
+
+on:
+  workflow_dispatch:
+    inputs:
+      releaseLevel:
+        description: "Release level"
+        required: true
+        default: "patch(0.0.X)"
+        type: choice
+        options:
+          - major(X.0.0)
+          - minor(0.X.0)
+          - patch(0.0.X)
+      isBeta:
+        description: "Is beta release (exp: X.0.0-beta.X)"
+        required: false
+        type: boolean
+
+jobs:
+  release:
+    runs-on: ubuntu-latest
+    permissions:
+      actions: write
+      checks: write
+      contents: write
+      deployments: write
+      issues: write
+      packages: write
+      pull-requests: write
+      repository-projects: write
+      security-events: write
+      statuses: write
+    environment:
+      name: Publish
+    env:
+      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
+      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+      NODE_OPTIONS: --max_old_space_size=8192
+
+    steps:
+      - uses: actions/checkout@v4
+        with:
+          fetch-depth: 0
+
+      - name: Set git config
+        run: |
+          git config user.name "${GITHUB_ACTOR}"
+          git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
+
+      - name: Setup node
+        uses: actions/setup-node@v4
+        with:
+          node-version-file: "package.json"
+          registry-url: "https://registry.npmjs.org/"
+
+      - uses: pnpm/action-setup@v2
+        name: Install pnpm
+        id: pnpm-install
+        with:
+          version: 8.14.1
+          run_install: false
+
+      - name: Get pnpm store directory
+        id: pnpm-cache
+        shell: bash
+        run: |
+          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
+
+      - uses: actions/cache@v3
+        name: Setup pnpm cache
+        with:
+          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
+          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
+          restore-keys: |
+            ${{ runner.os }}-pnpm-store-
+
+      - name: Install Node Dependencies
+        run: pnpm install
+
+      - name: Set .npmrc
+        run: echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > packages/core/.npmrc
+
+      - name: Create release branch
+        id: create_branch
+        run: |
+          git checkout -b release
+          git push --set-upstream origin release
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+      - name: Major release
+        id: major
+        run: pnpm asg publish:major
+        if: contains(inputs.releaseLevel, 'major(X.0.0)') && contains(inputs.isBeta, 'false')
+
+      - name: Major Beta release
+        id: major-beta
+        run: pnpm asg publish:major-beta
+        if: contains(inputs.releaseLevel, 'major(X.0.0)') && contains(inputs.isBeta, 'true')
+
+      - name: Minor release
+        id: minor
+        run: pnpm asg publish:minor
+        if: contains(inputs.releaseLevel, 'minor(0.X.0)') && contains(inputs.isBeta, 'false')
+
+      - name: Minor Beta release
+        id: minor-beta
+        run: pnpm asg publish:minor-beta
+        if: contains(inputs.releaseLevel, 'minor(0.X.0)') && contains(inputs.isBeta, 'true')
+
+      - name: Patch release
+        id: patch
+        run: pnpm asg publish:patch
+        if: contains(inputs.releaseLevel, 'patch(0.0.X)') && contains(inputs.isBeta, 'false')
+
+      - name: Patch Beta release
+        id: patch-beta
+        run: pnpm asg publish:patch-beta
+        if: contains(inputs.releaseLevel, 'patch(0.0.X)') && contains(inputs.isBeta, 'true')
+
+      - name: Create Pull Request
+        uses: peter-evans/create-pull-request@v4
+        if: steps.major.conclusion == 'success' || steps.minor.conclusion == 'success' || steps.patch.conclusion == 'success' || steps.major-beta.conclusion == 'success' || steps.minor-beta.conclusion == 'success' || steps.patch-beta.conclusion == 'success'
+        with:
+          title: "Update package.json"
+          draft: false
+          base: main
+          branch: release
+          assignees: "${{ github.actor }}"
diff --git a/packages/auto-story-generator/README.md b/packages/auto-story-generator/README.md
index 8f6d55b..1051792 100644
--- a/packages/auto-story-generator/README.md
+++ b/packages/auto-story-generator/README.md
@@ -6,20 +6,46 @@
 Automatic real-time story file generation from React, Vue, and Lit component files
 
 ## Getting Started
-1. Install the package
+### 1. Install the package
 ```bash
 npm i @takuma-ru/auto-story-generator
 ```
 
+### 2. Add config
+Add settings to main.ts in Storybook (`./storybook/main.ts`)
+
+For `React`, `Vite`
+```ts
+import type { StorybookConfig } from "@storybook/react-vite";
+
+import { mergeConfig } from "vite";
+
+import autoStoryGenerator from "@takuma-ru/auto-story-generator";
+
+const config: StorybookConfig = {
+  viteFinal: async (config) =>
+    mergeConfig(config, {
+      plugins: [
+        autoStoryGenerator.vite({
+          preset: "react",
+          imports: ["**/src/components/**/*.tsx"],
+        }),
+      ],
+    }),
+};
+
+export default config;
+```
+
 ## Supported Frameworks
-> 🚧: Work in progress
 > ✅: Supported
+> 🚧: Work in progress
 > ❌: Not supported
 > 📝: Not yet implemented
 
 | Framework | Supported |
 | --------- | --------- |
-| React     | 🚧         |
+| React     | ✅         |
 | Vue       | 🚧         |
 | Lit       | ✅         |
 | Angular   | ❌         |
diff --git a/packages/auto-story-generator/package.json b/packages/auto-story-generator/package.json
index 2674e00..5416bfd 100644
--- a/packages/auto-story-generator/package.json
+++ b/packages/auto-story-generator/package.json
@@ -19,9 +19,25 @@
     "LICENSE.md",
     "README.md"
   ],
+  "publishConfig": {
+    "access": "public",
+    "registry": "https://registry.npmjs.org/"
+  },
   "scripts": {
     "build": "tsup",
-    "build:watch": "tsup --watch"
+    "build:watch": "tsup --watch",
+    "lint": "eslint --ext .ts,.js .",
+    "lint:fix": "eslint --ext .ts,.js . --fix",
+    "lint:type-check": "tsc --noEmit",
+    "format": "prettier --write .",
+    "format:check": "prettier --check .",
+    "publish:major": "pnpm build && pnpm dlx release-it major --ci",
+    "publish:minor": "pnpm build && pnpm dlx release-it minor --ci",
+    "publish:patch": "pnpm build && pnpm dlx release-it patch --ci",
+    "publish:major-beta": "pnpm build && pnpm dlx release-it major --preRelease=beta --ci",
+    "publish:minor-beta": "pnpm build && pnpm dlx release-it minor --preRelease=beta --ci",
+    "publish:patch-beta": "pnpm build && pnpm dlx release-it patch --preRelease=beta --ci",
+    "publish:prerelease": "pnpm build && pnpm dlx release-it prerelease --ci"
   },
   "keywords": [],
   "author": "takuma-ru <kcattdev@gmail.com> (https://github.com/takuma-ru/)",
diff --git a/packages/auto-story-generator/release-it.json b/packages/auto-story-generator/release-it.json
new file mode 100644
index 0000000..08ac035
--- /dev/null
+++ b/packages/auto-story-generator/release-it.json
@@ -0,0 +1,14 @@
+{
+  "npm": {
+    "publish": true
+  },
+  "github": {
+    "release": true
+  },
+  "git": {
+    "requireCleanWorkingDir": false,
+    "addFiles": ["package.json"],
+    "commitMessage": ":bookmark: release @takuma-ru/auto-story-generator@${version}"
+  },
+  "plugins": {}
+}