Skip to content

Commit

Permalink
npm workspacesを導入する (#1240)
Browse files Browse the repository at this point in the history
* npm workspacesの構成一式をMarisから移植

* mockについてmaia固有のコードに戻し

* フロントエンドCIについてモノレポ構成に対応

* openapi-generatorのバージョンアップ確認CIについて、モノレポ構成に対応

* バックエンドのポート番号はmaia固有なので、env.devを戻し

* Vue.js編にワークスペースの導入方法を追記

* mono-repoに関する説明の参照を削除

* Open API Generatorの設定において、"openapi-client:generate"のパスを修正

---------

Co-authored-by: kentah <158997636+KentaHizume@users.noreply.github.com>
  • Loading branch information
kenjiyoshid-a and KentaHizume authored Sep 19, 2024
1 parent f68e110 commit 27f4276
Show file tree
Hide file tree
Showing 152 changed files with 5,132 additions and 10,715 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/openapi-generator-update-issue.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: openapi-generator-update-issue
about: openapi-generatorのアップデート用のIssueのテンプレートです
title: openapi-generatorをアップデートする from {{ env.CURRENT_VERSION }} to {{ env.LATEST_VERSION }}
title: "[{{ env.TARGET_APP_NAME }}]openapi-generatorをアップデートする from {{ env.CURRENT_VERSION }} to {{ env.LATEST_VERSION }}"
labels: ''
assignees: ''
---
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/check-openapi-generator-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: 現在のOpenAPI Generatorのバージョン取得
id: get-current-openapi-generator-version
run: |
echo "current-version=$(jq -r '.["generator-cli"].version' ./samples/web-csr/dressca-frontend/openapitools.json)" >> $GITHUB_OUTPUT
echo "current-version=$(jq -r '.["generator-cli"].version' ./samples/web-csr/dressca-frontend/consumer/openapitools.json)" >> $GITHUB_OUTPUT
- name: アップデート要否を判定
id: check-version-update
Expand Down Expand Up @@ -69,6 +69,7 @@ jobs:
attempt_delay: 300000
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TARGET_APP_NAME: "Dressca-Consumer"
LATEST_VERSION: ${{ steps.get-latest-openapi-generator-version.outputs.latest-version }}
CURRENT_VERSION: ${{ steps.get-current-openapi-generator-version.outputs.current-version }}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
# cSpell:ignore typecheck
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: dressca-frontend CI
name: dressca-consumer-frontend CI

on:
pull_request:
branches: [main]
paths:
- 'samples/web-csr/dressca-frontend/**'
- '.github/workflows/samples-dressca-frontend.ci.yml'
- 'samples/web-csr/dressca-frontend/consumer/**'
- '.github/workflows/samples-dressca-consumer-frontend.ci.yml'
workflow_dispatch:

defaults:
Expand Down Expand Up @@ -52,19 +52,19 @@ jobs:

- id: run-lint
name: lintの実行
run: npm run lint:ci >> /var/tmp/lint-result.txt 2>&1
run: npm run lint:ci:consumer >> /var/tmp/lint-result.txt 2>&1

- id: run-typecheck
- id: run-type-check
name: TypeScript の型チェック
run: npm run typecheck >> /var/tmp/typecheck-result.txt 2>&1
run: npm run type-check:consumer >> /var/tmp/typecheck-result.txt 2>&1

- id: application-build
name: アプリケーションのビルド
run: npm run build-only:dev >> /var/tmp/build-result.txt 2>&1
run: npm run build-only:dev:consumer >> /var/tmp/build-result.txt 2>&1

- id: run-unit-tests
name: 単体テストの実行
run: npm run test:unit >> /var/tmp/unit-test-result.txt 2>&1
run: npm run test:unit:consumer >> /var/tmp/unit-test-result.txt 2>&1

- name: lintの結果出力
if: ${{ success() || (failure() && steps.run-lint.conclusion == 'failure') }}
Expand All @@ -74,7 +74,7 @@ jobs:
header: 'lintの結果 :pen:'

- name: 型チェックの結果出力
if: ${{ success() || (failure() && steps.run-typecheck.conclusion == 'failure') }}
if: ${{ success() || (failure() && steps.run-type-check.conclusion == 'failure') }}
uses: ./.github/workflows/file-to-summary
with:
body: /var/tmp/typecheck-result.txt
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ package.json の scripts セクションにタスクを追加します。
"scripts": {
"generate-client": "run-s openapi-client:clean openapi-client:generate --print-label",
"openapi-client:clean": "node -e \"fs.promises.rm('./src/generated/api-client', {recursive: true, force: true})\"",
"openapi-client:generate": "openapi-generator-cli generate -g typescript-axios -i ../../dressca-backend/api-docs/api-specification.json --additional-properties=withSeparateModelsAndApi=true,modelPackage=models,apiPackage=api,supportsES6=true -o ./src/generated/api-client"
"openapi-client:generate": "openapi-generator-cli generate -g typescript-axios -i ./../../dressca-backend/api-docs/api-specification.json --additional-properties=withSeparateModelsAndApi=true,modelPackage=models,apiPackage=api,supportsES6=true -o ./src/generated/api-client"
}
}
```
Expand All @@ -69,10 +69,10 @@ openapi-generator-cli の generate コマンドのオプションについて説
-g typescript-axios
```

入力の API 仕様書として `./../dressca-backend/api-docs/api-specification.json` というファイルを指定します。
入力の API 仕様書として `./../../dressca-backend/api-docs/api-specification.json` というファイルを指定します。

``` terminal
-i ./../dressca-backend/api-docs/api-specification.json
-i ./../../dressca-backend/api-docs/api-specification.json
```

以下のプロパティを追加します。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,60 @@ description: Vue.js を用いた クライアントサイドアプリケーシ

# ブランクプロジェクトの作成 {#top}

下記の手順では、 Node.js のルートプロジェクトとワークスペースを作成し、作成したワークスペースに Vue.js のブランクプロジェクトを作成します。

## プロジェクトの全体像 {#project-overview}

mono-repo 構成では、
複数のフロントエンドアプリケーションのプロジェクトを 1 つのリポジトリで管理します。
[npm workspaces](https://docs.npmjs.com/cli/v10/using-npm/workspaces){ target=_blank } を用いることで、
プロジェクトごとにワークスペースを作成し、管理できます。
プロジェクトをまたがるワークスペースや、ワークスペースをまたがるプロジェクトを作成できますが、
原則としてワークスペースとプロジェクトが 1:1 で対応するようにします。

![プロジェクトフォルダの構造](../../../images/guidebooks/how-to-develop/vue-js/project-folder-structure-light.png#only-light){ loading=lazy align=right }
![プロジェクトフォルダの構造](../../../images/guidebooks/how-to-develop/vue-js/project-folder-structure-dark.png#only-dark){ loading=lazy align=right }

## プロジェクトの初期化 {#init-npm-project}

以下のコマンドを実行して、ルートプロジェクトを初期化します。

```terminal
npm init -y
```

実行に成功すると、 package.json ファイルが作成されます。

```terminal
Wrote to ...\package.json:
{
"name": "root-project-name",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
```

## Vue.js およびオプションのインストール {#install-vue-js-and-options}

以下のコマンドを実行して Vue.js をインストールします。
以下のコマンドを実行し、任意のワークスペース名(プロジェクト名)を指定して Vue.js をインストールします。

```terminal
npm create vue@latest
npm init -w <workspace-name> vue@latest .
```

create-vue パッケージをインストールする必要があり、続行するかどうかを確認するメッセージが表示されるので、「y」を選択します。

プロジェクト名を入力します
`-w` オプションで指定したワークスペース名と同じ名称を入力します

```terminal
Project name: ... <project-name>
Package name: ... <workspace-name>
```

インストールオプションを確認されるので、左右カーソルキーで Yes / No を選択します。クライアントサイドのアーキテクチャに基づき、使用するものに対して Yes を選択すると、以下のようになります。
Expand All @@ -40,15 +80,18 @@ create-vue パッケージをインストールする必要があり、続行す
以下のようにコマンドを実行し、必要なパッケージをインストールしてアプリケーションを実行します。

```terminal
cd <project-name>
npm install
npm run format
npm run dev
npm run format -w <workspace-name>
npm run dev -w <workspace-name>
```

`npm run dev` が成功すると以下のように表示されるので、「 Local: 」に表示された URL をブラウザーで表示します。ブランクプロジェクトのランディングページが表示されます。

```terminal
> workspace-name@0.0.0 dev
> vite
VITE v5.x.x ready in xxxx ms
- Local: http://localhost:5173/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,36 @@ description: Vue.js を用いた クライアントサイドアプリケーシ

# ブランクプロジェクトのフォルダー構造 {#top}

ブランクプロジェクトを作成した時点でのフォルダー構造は以下のようになっています
[ブランクプロジェクトの作成](./create-vuejs-blank-project.md) 時点でのフォルダー構造は以下のようになっています

```terminal linenums="0"
<project-name>
├ cypress/ ---------------- cypress による End-to-End テスト用のフォルダー
├ dist/ ------------------- ビルド後の成果物が配置されるフォルダー
├ public/ ----------------- メディアファイルや favicon など静的な資産が配置されるフォルダー
├ src/ -------------------- アプリケーションのソースコードが配置されるフォルダー
│ ├ assets/ --------------- コードや動的ファイルが必要とするCSSや画像などのアセットが配置されるフォルダー
│ ├ components/ ----------- ページを構成する部品のコードが配置されるフォルダー
│ ├ router/ --------------- ルーティング制御を行うコードが配置されるフォルダー
│ ├ stores/ --------------- 状態管理を行うコードが配置されるフォルダー
│ ├ views/ ---------------- ルーティングの対象となるページのコードが配置されるフォルダー
│ ├ App.vue --------------- 画面のフレームを構成するコード
│ └ main.ts --------------- 各ライブラリ等を読み込むためのコード
├ .eslintrc.cjs ----------- ESLint の設定ファイル
├ cypress.config.ts ------- cypress の設定ファイル
├ env.d.ts ---------------- TypeScript でコード補完機能(Intellisense)を適用するための設定ファイル
├ index.html -------------- Web サイトのトップページとなるファイル
├ package.json ------------ パッケージのメタデータ(依存するパッケージなど)を定義するファイル
├ README.md --------------- ブランクプロジェクト作成時点ではテンプレートの説明が記述されたファイル
├ tsconfig.app.json ------- アプリケーションの TypeScript として読み込む対象を定義する設定ファイル
├ tsconfig.node.json ------ TypeScript の設定ファイルとして読み込む対象を定義する設定ファイル
├ tsconfig.json ----------- TypeScript の設定ファイル
├ tsconfig.vitest.json ---- 単体テストの TypeScript として読み込む対象を定義する設定ファイル
├ vite.config.ts ---------- Vite の設定ファイル
└ vitest.config.ts -------- 単体テストの設定ファイル
<root-project-name>
├ package.json -------------- ルートプロジェクトのメタデータ、依存関係、スクリプトなどを定義するファイル
├ package-lock.json --------- npm によって自動生成される、パッケージの依存関係を記録するファイル
└ <workspace-name>
├ cypress/ ---------------- cypress による End-to-End テスト用のフォルダー
├ dist/ ------------------- ビルド後の成果物が配置されるフォルダー
├ public/ ----------------- メディアファイルや favicon など静的な資産が配置されるフォルダー
├ src/ -------------------- アプリケーションのソースコードが配置されるフォルダー
│ ├ assets/ --------------- コードや動的ファイルが必要とするCSSや画像などのアセットが配置されるフォルダー
│ ├ components/ ----------- ページを構成する部品のコードが配置されるフォルダー
│ ├ router/ --------------- ルーティング制御を行うコードが配置されるフォルダー
│ ├ stores/ --------------- 状態管理を行うコードが配置されるフォルダー
│ ├ views/ ---------------- ルーティングの対象となるページのコードが配置されるフォルダー
│ ├ App.vue --------------- 画面のフレームを構成するコード
│ └ main.ts --------------- 各ライブラリ等を読み込むためのコード
├ .eslintrc.cjs ----------- ESLint の設定ファイル
├ cypress.config.ts ------- cypress の設定ファイル
├ env.d.ts ---------------- TypeScript でコード補完機能(Intellisense)を適用するための設定ファイル
├ index.html -------------- Web サイトのトップページとなるファイル
├ package.json ------------ ワークスペースのメタデータ、依存関係、スクリプトなどを定義するファイル
├ README.md --------------- ブランクプロジェクト作成時点ではテンプレートの説明が記述されたファイル
├ tsconfig.app.json ------- アプリケーションの TypeScript として読み込む対象を定義する設定ファイル
├ tsconfig.node.json ------ TypeScript の設定ファイルとして読み込む対象を定義する設定ファイル
├ tsconfig.json ----------- TypeScript の設定ファイル
├ tsconfig.vitest.json ---- 単体テストの TypeScript として読み込む対象を定義する設定ファイル
├ vite.config.ts ---------- Vite の設定ファイル
└ vitest.config.ts -------- 単体テストの設定ファイル
```

各フォルダーの内部にどのようなサブフォルダーを作成するかは、[アーキテクチャ解説](./../../../app-architecture/client-side-rendering/frontend-application/index.md#project-structure) を参照してください。
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Vue.js を用いてフロントエンドアプリケーションを開発する
1. [事前準備](preparation.md)
1. [ブランクプロジェクトの作成](create-vuejs-blank-project.md)
1. [ブランクプロジェクトのフォルダー構造](folder-structure-of-blank-project.md)
1. [ワークスペースの設定](setting-workspaces.md)
1. [プロジェクトの共通設定](project-settings.md)
1. [開発に使用するパッケージ](optional-packages.md)
1. [CSSの設定とCSSフレームワークの適用](css.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: Vue.js 開発手順
description: Vue.js を用いた クライアントサイドアプリケーションの 開発手順を説明します。
---

# ワークスペースの設定 {#top}

[ブランクプロジェクトの作成](./create-vuejs-blank-project.md) で作成したルートプロジェクトとワークスペースについて、
追加で必要な設定をします。

## ワークスペースの定義 {#definition-workspaces}

ワークスペースの名称は、ルートプロジェクトの package.json の `workspaces` プロパティで定義し、この値はワークスペースの package.json の `name` プロパティと一致している必要があります。
[ブランクプロジェクトの作成](./create-vuejs-blank-project.md) に従って初期設定した場合は、自動的に構成されます。

``` json title="package.json(ルート)"
"workspaces": [
"workspace-name"
]
```

``` json title="package.json(ワークスペース)"
{
"name": "workspace-name",
}
```

## ルートプロジェクトの設定 {#setting-route-project}

ルートプロジェクトの package-json に `"type": "module"``"private": "true"`を追加します。
CJS 形式のファイルを正しく読み込むために、 `"type": "module"` は設定が必須です。
`"private": "true"`は、誤ってルートプロジェクトが公開されることを防ぐため、設定を推奨します。

```json title="package.json(ルート)" hl_lines="6 7"
{
"name": "project-name",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"private": "true",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
```

## スクリプトの登録 {#register-npm-scripts}

CI パイプラインで使用するために、ルートプロジェクトの package.json にスクリプトを登録します。
`-w` オプションでワークスペース名を指定することで、指定したワークスペースの package.json に存在するスクリプトを実行できます。
設定例を下記に示します。

``` json title="package.json(ルート)"
{
"scripts": {
"lint:ci:workspace-name": "npm run lint:ci -w workspace-name",
"type-check:workspace-name": "npm run type-check -w workspace-name",
"build-only:dev:workspace-name": "npm run build-only:dev -w workspace-name",
"build:prod:workspace-name": "npm run build:prod -w workspace-name",
"test:unit:workspace-name": "npm run test:unit -w workspace-name",
"dev:workspace-name": "npm run dev -w workspace-name"
},
}
```

## パッケージの依存関係の管理 {#manage-package-dependency}

パッケージのバージョンは、ルートプロジェクトの package-lock.json で管理されます。各ワークスペースには package-lock.json は作成されません。
パッケージのインストール・バージョン更新は、 npm workspaces を使用しない場合と同様に、各ワークスペースで `npm install` を行います。
しかし、インストールしたパッケージの依存関係は、ルートプロジェクトの package-lock.json に記録されます。
また、各ワークスペースの package.json でバージョンを指定することで、ワークスペース間で異なるバージョンのパッケージを使用できます。
Loading

0 comments on commit 27f4276

Please sign in to comment.