> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Deployer Deployer は、コードのパッケージ化、環境ファイルの管理、Hono フレームワークを使用したアプリケーションの配信を行い、スタンドアロン Mastra アプリケーションのデプロイを処理します。具象実装では、特定のデプロイ先に対応する deploy メソッドを定義する必要があります。 ## 使用例 カスタム deployer を作成するには、抽象 `Deployer` クラスを拡張し、デプロイロジックに応じた `deploy` メソッドを実装します。次に例を示します。 ```typescript import { Deployer } from '@mastra/deployer' class CustomDeployer extends Deployer { constructor() { super({ name: 'custom-deployer' }) } async deploy(outputDirectory: string): Promise { // Prepare the output directory await this.prepare(outputDirectory) // Bundle the application await this._bundle('server.ts', 'mastra.ts', outputDirectory) // Custom deployment logic } } ``` ## パラメーター ### コンストラクターパラメーター **args** (`object`): Deployer の設定オプション。 **args.name** (`string`): deployer インスタンスの一意な名前。 ### deploy のパラメーター **outputDirectory** (`string`): バンドルされ、デプロイ可能になったアプリケーションの出力先ディレクトリ。 ## メソッド **getEnvFiles** (`() => Promise`): デプロイ中に使用する環境ファイルの一覧を返します。デフォルトでは、'.env.production' ファイルと '.env' ファイルを検索します。 **deploy** (`(outputDirectory: string) => Promise`): サブクラスで実装する必要がある抽象メソッド。指定した出力ディレクトリへのデプロイ処理を行います。 ## Bundler から継承するメソッド Deployer クラスは、Bundler クラスから次の主要なメソッドを継承します。 **prepare** (`(outputDirectory: string) => Promise`): 出力ディレクトリをクリーンアップし、必要なサブディレクトリを作成して準備します。 **writePackageJson** (`(outputDirectory: string, dependencies: Map) => Promise`): 指定した依存関係を含む package.json ファイルを出力ディレクトリに生成します。 **\_bundle** (`(serverFile: string, mastraEntryFile: string, outputDirectory: string, bundleLocation?: string) => Promise`): 指定したサーバーファイルと Mastra エントリーファイルを使用してアプリケーションをバンドルします。 ## コアコンセプト ### デプロイのライフサイクル Deployer 抽象クラスは、構造化されたデプロイのライフサイクルを実装します。 1. **初期化**:deployer は名前を指定して初期化され、依存関係を管理する Deps インスタンスを作成します。 2. **環境のセットアップ**:`getEnvFiles` メソッドは、デプロイ中に使用する環境ファイル(.env.production、.env)を特定します。 3. **準備**:`prepare` メソッド(Bundler から継承)は出力ディレクトリをクリーンアップし、必要なサブディレクトリを作成します。 4. **バンドル**:`_bundle` メソッド(Bundler から継承)は、アプリケーションコードとその依存関係をパッケージ化します。 5. **デプロイ**:抽象 `deploy` メソッドをサブクラスで実装し、実際のデプロイ処理を行います。 ### 環境ファイルの管理 Deployer クラスは、`getEnvFiles` メソッドによる環境ファイル管理を標準でサポートしています。このメソッドは次の処理を行います。 - 事前定義された順序(.env.production、.env)で環境ファイルを検索する - FileService を使用して、最初に存在するファイルを見つける - 見つかった環境ファイルの配列を返す - 環境ファイルが見つからない場合は空の配列を返す ```typescript getEnvFiles(): Promise { const possibleFiles = ['.env.production', '.env.local', '.env']; try { const fileService = new FileService(); const envFile = fileService.getFirstExistingFile(possibleFiles); return Promise.resolve([envFile]); } catch {} return Promise.resolve([]); } ``` ### バンドルとデプロイの関係 Deployer クラスは Bundler クラスを拡張し、バンドルとデプロイの間に明確な関係を確立します。 1. **前提条件としてのバンドル**:バンドルはデプロイの前提となるステップで、アプリケーションコードをデプロイ可能な形式にパッケージ化します。 2. **共有インフラストラクチャ**:バンドルとデプロイはどちらも、依存関係の管理やファイルシステム操作などの共通インフラストラクチャを共有します。 3. **特化したデプロイロジック**:バンドルがコードのパッケージ化に重点を置く一方、デプロイではバンドルされたコードを配置するための環境固有のロジックを追加します。 4. **拡張性**:抽象 `deploy` メソッドにより、異なる対象環境に特化した deployer を作成できます。