Deployer
The Deployer handles the deployment of standalone Mastra applications by packaging code and managing environment files while serving applications through the Hono framework. Concrete implementations must define the deploy method for specific deployment targets.
Usage exampleUsage example的直接链接
If you want to create a custom deployer, you can extend the abstract Deployer class and implement the deploy method with your specific deployment logic. Here's an example:
import { Deployer } from '@mastra/deployer'
class CustomDeployer extends Deployer {
constructor() {
super({ name: 'custom-deployer' })
}
async deploy(outputDirectory: string): Promise<void> {
// Prepare the output directory
await this.prepare(outputDirectory)
// Bundle the application
await this._bundle('server.ts', 'mastra.ts', outputDirectory)
// Custom deployment logic
}
}
ParametersParameters的直接链接
Constructor parametersConstructor parameters的直接链接
args:
args.name:
deploy Parametersdeploy Parameters的直接链接
outputDirectory:
MethodsMethods的直接链接
getEnvFiles:
deploy:
Inherited methods from bundlerInherited methods from bundler的直接链接
The Deployer class inherits the following key methods from the Bundler class:
prepare:
writePackageJson:
_bundle:
Core conceptsCore concepts的直接链接
Deployment LifecycleDeployment Lifecycle的直接链接
The Deployer abstract class implements a structured deployment lifecycle:
- Initialization: The deployer is initialized with a name and creates a Deps instance for dependency management.
- Environment Setup: The
getEnvFilesmethod identifies the environment files (.env,.env.local,.env.production) to be used during deployment. - Preparation: The
preparemethod (inherited from Bundler) cleans the output directory and creates necessary subdirectories. - Bundling: The
_bundlemethod (inherited from Bundler) packages the application code and its dependencies. - Deployment: The abstract
deploymethod is implemented by subclasses to handle the actual deployment process.
Environment File ManagementEnvironment File Management的直接链接
The Deployer class includes built-in support for environment file management through the getEnvFiles method. This method:
- Looks for environment files in a predefined order, from lowest to highest precedence:
.env,.env.local,.env.production - Returns every file that exists, in that order, so values in later files override earlier ones
- Returns an empty array if no environment files are found
getEnvFiles(): Promise<string[]> {
const possibleFiles = ['.env', '.env.local', '.env.production'];
const fileService = new FileService();
return Promise.resolve(fileService.getExistingFiles(possibleFiles));
}
Bundling and Deployment RelationshipBundling and Deployment Relationship的直接链接
The Deployer class extends the Bundler class, establishing a clear relationship between bundling and deployment:
- Bundling as a Prerequisite: Bundling is a prerequisite step for deployment, where the application code is packaged into a deployable format.
- Shared Infrastructure: Both bundling and deployment share common infrastructure like dependency management and file system operations.
- Specialized Deployment Logic: While bundling focuses on code packaging, deployment adds environment-specific logic for deploying the bundled code.
- Extensibility: The abstract
deploymethod allows for creating specialized deployers for different target environments.