Skip to content

モノレポのセットアップ

新しいモノレポを作成するには、目的のディレクトリ内で次のコマンドを実行します:

ワークスペースを作成@aws/nx-workspace

pnpm create @aws/nx-workspace dungeon-adventure --iac=cdk
このステップのオプション2

必須

これにより、dungeon-adventureディレクトリ内にNXモノレポがセットアップされます。VSCodeでディレクトリを開くと、次のファイル構造が表示されます:

  • Directory.nx/
    • …
  • Directory.vscode/
    • …
  • Directorynode_modules/
    • …
  • Directorypackages/ サブプロジェクトが配置される場所
    • …
  • .gitignore
  • biome.json Biomeのリントとフォーマット設定
  • nx.json Nx CLIとモノレポのデフォルト設定
  • package.json すべてのnode依存関係がここで定義されます
  • pnpm-lock.yaml または bun.lock、yarn.lock、package-lock.json(パッケージマネージャーによる)
  • pnpm-workspace.yaml pnpmを使用する場合
  • README.md
  • tsconfig.base.json すべてのnodeベースのサブプロジェクトがこれを拡張します
  • tsconfig.json
  • aws-nx-plugin.config.mts Nx Plugin for AWSの設定

タスク2: ダンジョンアドベンチャーゲームのスキャフォールディング

Section titled “タスク2: ダンジョンアドベンチャーゲームのスキャフォールディング”

ワークスペースが整ったら、ゲームのサブプロジェクト(Game API、Story Agent、Inventory MCPサーバー、ゲームデータベース、ウェブサイト)と、それらを接続するコネクションをスキャフォールディングします。これには2つの方法があります:

  • クイック — 以下の図からコマンドを直接コピーして実行します。同じ開始点に到達する最速の方法です。
  • ステップバイステップ — 以下のセクションを展開して、各ジェネレーターを自分で実行し、それぞれが何を生成するかを正確に確認します。

以下の図は、ダンジョンアドベンチャーワークスペース_そのもの_です: このモジュールで構築するすべてのプロジェクト、コンポーネント、コネクションが含まれています。Copy commandsをクリックしてシリーズ全体を取得し、タスク1で作成したdungeon-adventureディレクトリ内から実行してください。

Loading the diagram…

ステップバイステップ: 各ジェネレーターを自分で実行して、何が生成されるかを確認する

タスク3: インフラストラクチャの更新

Section titled “タスク3: インフラストラクチャの更新”

packages/infra/src/stacks/application-stack.tsを更新して、生成されたコンストラクトのいくつかをインスタンス化しましょう:

import {
GameApi,
GameUI,
InventoryMcpServer,
StoryAgent,
UserIdentity,
} from '@dungeon-adventure/common-constructs';
import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const userIdentity = new UserIdentity(this, 'UserIdentity');
const gameApi = new GameApi(this, 'GameApi', {
integrations: GameApi.defaultIntegrations(this).build(),
});
const mcpServer = new InventoryMcpServer(this, 'InventoryMcpServer');
// Use Cognito for user authentication with the agent
const storyAgent = new StoryAgent(this, 'StoryAgent', {
identity: userIdentity,
});
new CfnOutput(this, 'StoryAgentArn', {
value: storyAgent.agentCoreRuntime.agentRuntimeArn,
});
new CfnOutput(this, 'InventoryMcpArn', {
value: mcpServer.agentCoreRuntime.agentRuntimeArn,
});
// Grant the agent permissions to invoke our mcp server
mcpServer.grantInvokeAccess(storyAgent);
// Grant the authenticated role access to invoke the api
gameApi.grantInvokeAccess(userIdentity.identityPool.authenticatedRole);
new GameUI(this, 'GameUI');
}
}
初めてコードをビルドする時が来ました

コマンドラインを使用して、まずリントの問題を修正するために次のコマンドを実行します:

Terminal window
pnpm lint

次に、フルビルドのために次のコマンドを実行します:

Terminal window
pnpm build

次のプロンプトが表示される場合があります:

Terminal window
NX The workspace is out of sync
[@nx/js:typescript-sync]: Some TypeScript configuration files are missing project references to the projects they depend on, contain stale project references, or have duplicate project references.
[@aws/nx-plugin:ts#sync]: Some files are out of sync.
? Would you like to sync the identified changes to get your workspace up to date? …
Yes, sync the changes and run the tasks
No, run the tasks without syncing the changes

このメッセージは、NXが自動的に更新できるファイルを検出したことを示しています。この場合、参照プロジェクトにTypeScript参照が設定されていないtsconfig.jsonファイルを指しています。

Yes, sync the changes and run the tasksオプションを選択して続行します。syncジェネレーターが欠落しているTypeScript参照を自動的に追加するため、すべてのIDE関連のインポートエラーが自動的に解決されることに気付くはずです!

すべてのビルドアーティファクトは、モノレポのルートにあるdist/フォルダー内で利用できるようになりました。これは、@aws/nx-pluginによって生成されたプロジェクトを使用する際の標準的な慣行であり、生成されたファイルでファイルツリーを汚染しません。ファイルをクリーンアップしたい場合は、ビルドアーティファクトがファイルツリー全体に散らばることを心配せずにdist/フォルダーを削除してください。

おめでとうございます!AI Dungeon Adventureゲームのコアの実装を開始するために必要なすべてのサブプロジェクトを作成しました。🎉🎉🎉