创建块应用程序
用于脚手架的 CLI 工具 AWS Blocks 项目.
概览
create-blocks-app 在现有应用程序中添加块或从模板中创建新项目. 它建立了 aws-blocks/ 工作空间,配置 CDK 基础设施,并提供启动代码.
使用量
创建新工程
npx @aws-blocks/create-blocks-app my-app
只包含良性元数据的目标目录(VCS/editor/OS文件)或 INSTRUCTIONS.md 仍然被视为一个新项目; 任何其他先前存在的文件块脚手架,所以没有任何东西被覆盖.
从模板开始
npx @aws-blocks/create-blocks-app my-app --template react
模板和一行描述的完整列表,运行:
npx @aws-blocks/create-blocks-app --help
模板描述来自每个模板的 package.json (单位:千美元)blocksTemplateDescription因此CLI总是真理的来源.
添加到现有项目
npx @aws-blocks/create-blocks-app .
在扩展 Gen 2 工程中添加块
已经有一个扩展的Gen 2应用程序? 从您的工程根中运行 :
npx @aws-blocks/create-blocks-app .
CLI 检测您的扩展工程(amplify/backend.ts)并与之并列的块块——增加: aws-blocks/ 工作空间,通过无记名符号布线认证,以及用于部署的脚手架npm脚本。 您已有的扩展认证、 数据和主机保存不动 .
创建什么
./
├── aws-blocks/
│ ├── index.ts # Backend entry point
│ ├── index.cdk.ts # CDK stack definition
│ ├── index.handler.ts # Lambda handler
│ ├── client.js # Auto-generated frontend client
│ ├── package.json # Backend workspace dependencies
│ └── scripts/
│ ├── server.ts # Local dev server
│ ├── sandbox.ts # Sandbox deployment
│ ├── deploy.ts # Production deployment
│ └── destroy.ts # Stack teardown
├── cdk.json # CDK configuration
├── src/
│ └── ... # Your existing code
└── package.json # Updated with Blocks scripts
后端结构
aws-块/index.ts
import { ApiNamespace, Scope, AuthBasic, DistributedTable, Realtime } from '@aws-blocks/blocks';
import { z } from 'zod';
const scope = new Scope('my-app');
const auth = new AuthBasic(scope, 'auth', {
passwordPolicy: { minLength: 8 },
});
export const authApi = auth.createApi();
const todoSchema = z.object({
userId: z.string(),
todoId: z.string(),
title: z.string(),
completed: z.boolean(),
});
const todos = new DistributedTable(scope, 'todos', {
schema: todoSchema,
key: { partitionKey: 'userId', sortKey: 'todoId' },
});
export const api = new ApiNamespace(scope, 'api', (context) => ({
async createTodo(title: string) { /* ... */ },
async listTodos() { /* ... */ },
async toggleTodo(todoId: string) { /* ... */ },
async deleteTodo(todoId: string) { /* ... */ },
}));
aws-块/index.cdk.ts
import * as cdk from 'aws-cdk-lib';
import { BlocksStack } from '@aws-blocks/blocks/cdk';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const stackName = 'my-blocks-stack-prod';
const app = new cdk.App();
export const blocksStack = await BlocksStack.create(app, stackName, {
backendHandlerPath: join(__dirname, 'index.handler.ts'),
backendCDKPath: join(__dirname, 'index.ts'),
});
软件包.json 脚本
{
"scripts": {
"dev": "tsx watch aws-blocks/scripts/server.ts",
"sandbox": "tsx aws-blocks/scripts/sandbox.ts",
"sandbox:deploy": "tsx aws-blocks/scripts/sandbox.ts --deploy-only",
"sandbox:destroy": "tsx -C cdk aws-blocks/scripts/sandbox-destroy.ts",
"deploy": "tsx aws-blocks/scripts/deploy.ts",
"destroy": "tsx aws-blocks/scripts/destroy.ts",
"build": "tsc && vite build"
}
}
模板
可用的模板 : default, (中文(简体) ), bare, (中文(简体) ), react, (中文(简体) ), backend, (中文(简体) ), nextjs, (中文(简体) ), auth-cognito, (中文(简体) ), amplify, (中文(简体) ), demo。 。 。 .
下方的文件夹 templates/ 是一个自我注册的模板。 每个 package.json 在该文件夹中设置一个 blocksTemplateDescription 字段,用于构建 --help 目录. 要查看当前模板列表及其一行描述:
npx @aws-blocks/create-blocks-app --help
相关软件包
- 块 - 核心运行时间和建筑区块
许可证
阿帕奇 - 2.0