Đóng góp Generator
Hãy tạo một generator mới để đóng góp cho @aws/nx-plugin. Mục tiêu của chúng ta sẽ là tạo một procedure mới cho tRPC API.
Checkout Plugin
Phần tiêu đề “Checkout Plugin”Đầu tiên, hãy clone plugin:
git clone git@github.com:awslabs/nx-plugin-for-aws.gitTiếp theo, cài đặt và build:
cd nx-plugin-for-awspnpm ipnpm nx run-many --target build --allTạo Generator Rỗng
Phần tiêu đề “Tạo Generator Rỗng”Hãy tạo generator mới trong packages/nx-plugin/src/trpc/procedure.
Chúng tôi cung cấp một generator để tạo các generator mới giúp bạn có thể nhanh chóng scaffold generator mới của mình! Bạn có thể chạy generator này như sau:
pnpm nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC APIyarn nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC APInpx nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC APIbunx nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC APIBạn cũng có thể thực hiện chạy thử để xem những tệp nào sẽ bị thay đổi
pnpm nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC API --dry-runyarn nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC API --dry-runnpx nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC API --dry-runbunx nx g @aws/nx-plugin:ts#nx-generator --project=@aws/nx-plugin --name=ts#trpc-api#procedure --directory=trpc/procedure --description=Adds a procedure to a tRPC API --dry-run- Cài đặt Nx Console VSCode Plugin nếu bạn chưa cài đặt
- Mở Nx Console trong VSCode
- Nhấp
Generate (UI)trong phần "Common Nx Commands" - Tìm kiếm
@aws/nx-plugin - ts#nx-generator - Điền các tham số bắt buộc
- project: @aws/nx-plugin
- name: ts#trpc-api#procedure
- directory: trpc/procedure
- description: Adds a procedure to a tRPC API
- Nhấp
Generate
Bạn sẽ thấy các file sau đã được tạo cho bạn:
Thư mụcpackages/nx-plugin/src/trpc/procedure
- schema.json Định nghĩa input cho generator
- schema.d.ts Interface TypeScript khớp với schema
- generator.ts Hàm mà Nx chạy như generator
- generator.spec.ts Tests cho generator
Thư mụcdocs/src/content/docs/guides/
- trpc-procedure.mdx Tài liệu cho generator
- packages/nx-plugin/generators.json Đã được cập nhật để bao gồm generator
Hãy cập nhật schema để thêm các thuộc tính chúng ta cần cho generator:
{ "$schema": "https://json-schema.org/schema", "$id": "tRPCProcedure", "title": "Adds a procedure to a tRPC API", "type": "object", "properties": { "project": { "type": "string", "description": "tRPC API project", "x-prompt": "Select the tRPC API project to add the procedure to", "x-dropdown": "projects", "x-priority": "important" }, "procedure": { "description": "The name of the new procedure", "type": "string", "x-prompt": "What would you like to call your new procedure?", "x-priority": "important" }, "type": { "description": "The type of procedure to generate", "type": "string", "x-prompt": "What type of procedure would you like to generate?", "x-priority": "important", "default": "query", "enum": ["query", "mutation"] } }, "required": ["project", "procedure"]}export interface TrpcProcedureSchema { project: string; procedure: string; type: 'query' | 'mutation';}Bạn sẽ thấy generator đã được kết nối trong packages/nx-plugin/generators.json:
... "generators": { ... "ts#trpc-api#procedure": { "factory": "./src/trpc/procedure/generator", "schema": "./src/trpc/procedure/schema.json", "description": "Adds a procedure to a tRPC API" } },...Triển khai Generator
Phần tiêu đề “Triển khai Generator”Để thêm một procedure vào tRPC API, chúng ta cần làm hai việc:
- Tạo file TypeScript cho procedure mới
- Thêm procedure vào router
Tạo Procedure mới
Phần tiêu đề “Tạo Procedure mới”Để tạo file TypeScript cho procedure mới, chúng ta sẽ sử dụng một tiện ích gọi là generateFiles. Sử dụng nó, chúng ta có thể định nghĩa một template EJS mà chúng ta có thể render trong generator với các biến dựa trên các tùy chọn được người dùng chọn.
Đầu tiên, chúng ta sẽ định nghĩa template trong packages/nx-plugin/src/trpc/procedure/files/procedures/__procedureNameKebabCase__.ts.template:
import { publicProcedure } from '../init.js';import { z } from 'zod';
export const <%- procedureNameCamelCase %> = publicProcedure .input(z.object({ // TODO: define input })) .output(z.object({ // TODO: define output })) .<%- procedureType %>(async ({ input, ctx }) => { // TODO: implement! return {}; });Trong template, chúng ta đã tham chiếu ba biến:
procedureNameCamelCaseprocedureNameKebabCaseprocedureType
Vì vậy chúng ta cần đảm bảo truyền những biến đó cho generateFiles, cùng với thư mục để tạo file vào, cụ thể là vị trí của các file nguồn (tức là sourceRoot) cho dự án tRPC mà người dùng đã chọn làm input cho generator, mà chúng ta có thể trích xuất từ cấu hình dự án.
Hãy cập nhật generator để làm điều đó:
import { generateFiles, joinPathFragments, readProjectConfiguration, type Tree,} from '@nx/devkit';import type { TrpcProcedureSchema } from './schema.js';import { formatFilesInSubtree } from '../../utils/format';import camelCase from 'lodash.camelcase';import kebabCase from 'lodash.kebabcase';
export const trpcProcedureGenerator = async ( tree: Tree, options: TrpcProcedureSchema,) => { const projectConfig = readProjectConfiguration(tree, options.project);
const procedureNameCamelCase = camelCase(options.procedure); const procedureNameKebabCase = kebabCase(options.procedure);
generateFiles( tree, joinPathFragments(import.meta.dirname, 'files'), projectConfig.sourceRoot, { procedureNameCamelCase, procedureNameKebabCase, procedureType: options.type, }, );
await formatFilesInSubtree(tree);};
export default trpcProcedureGenerator;Thêm Procedure vào Router
Phần tiêu đề “Thêm Procedure vào Router”Tiếp theo, chúng ta muốn generator kết nối procedure mới vào router. Điều này có nghĩa là đọc và cập nhật mã nguồn của người dùng!
Chúng ta sử dụng GritQL để tìm kiếm và chuyển đổi mã nguồn một cách khai báo. Helper addDestructuredImport thêm các named import, và applyGritQL áp dụng một pattern GritQL để thêm procedure vào object literal của router.
import { generateFiles, joinPathFragments, readProjectConfiguration, type Tree,} from '@nx/devkit';import type { TrpcProcedureSchema } from './schema.js';import { formatFilesInSubtree } from '../../utils/format';import camelCase from 'lodash.camelcase';import kebabCase from 'lodash.kebabcase';import { addDestructuredImport, applyGritQL } from '../../utils/ast';
export const trpcProcedureGenerator = async ( tree: Tree, options: TrpcProcedureSchema,) => { const projectConfig = readProjectConfiguration(tree, options.project);
const procedureNameCamelCase = camelCase(options.procedure); const procedureNameKebabCase = kebabCase(options.procedure);
generateFiles( tree, joinPathFragments(import.meta.dirname, 'files'), projectConfig.sourceRoot, { procedureNameCamelCase, procedureNameKebabCase, procedureType: options.type, }, );
const routerPath = joinPathFragments(projectConfig.sourceRoot, 'router.ts');
await addDestructuredImport( tree, routerPath, [procedureNameCamelCase], `./procedures/${procedureNameKebabCase}.js`, );
await applyGritQL( tree, routerPath, `\`router({ $props })\` => \`router({ $props, ${procedureNameCamelCase} })\` where { $props <: not contains \`${procedureNameCamelCase}\` }`, );
await formatFilesInSubtree(tree);};
export default trpcProcedureGenerator;Bây giờ chúng ta đã triển khai generator, hãy compile nó để đảm bảo nó có sẵn cho chúng ta kiểm thử trong dự án dungeon adventure.
pnpm nx compile @aws/nx-pluginKiểm thử Generator
Phần tiêu đề “Kiểm thử Generator”Để kiểm thử generator, chúng ta sẽ liên kết Nx Plugin for AWS cục bộ của mình với một codebase hiện có.
Tạo Test Project với tRPC API
Phần tiêu đề “Tạo Test Project với tRPC API”Trong một thư mục riêng biệt, tạo một test workspace mới:
pnpm create @aws/nx-workspace trpc-generator-testyarn create @aws/nx-workspace trpc-generator-testnpm create @aws/nx-workspace -- trpc-generator-testbun create @aws/nx-workspace trpc-generator-testTiếp theo, hãy tạo một tRPC API để thêm procedure vào:
pnpm nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactiveyarn nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactivenpx nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactivebunx nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactiveBạn cũng có thể thực hiện chạy thử để xem những tệp nào sẽ bị thay đổi
pnpm nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactive --dry-runyarn nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactive --dry-runnpx nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactive --dry-runbunx nx g @aws/nx-plugin:ts#api --name=test-api --framework=trpc --no-interactive --dry-run- Cài đặt Nx Console VSCode Plugin nếu bạn chưa cài đặt
- Mở Nx Console trong VSCode
- Nhấp
Generate (UI)trong phần "Common Nx Commands" - Tìm kiếm
@aws/nx-plugin - ts#api - Điền các tham số bắt buộc
- name: test-api
- framework: trpc
- Nhấp
Generate
Liên kết Nx Plugin for AWS cục bộ của chúng ta
Phần tiêu đề “Liên kết Nx Plugin for AWS cục bộ của chúng ta”Trong codebase của bạn, hãy liên kết @aws/nx-plugin cục bộ của chúng ta:
cd path/to/trpc-generator-testpnpm link path/to/nx-plugin-for-aws/dist/packages/nx-plugincd path/to/trpc-generator-testyarn link path/to/nx-plugin-for-aws/dist/packages/nx-plugincd path/to/trpc-generator-testnpm link path/to/nx-plugin-for-aws/dist/packages/nx-plugincd path/to/nx-plugin-for-aws/dist/packages/nx-pluginbun linkcd path/to/trpc-generator-testbun link @aws/nx-pluginChạy Generator mới
Phần tiêu đề “Chạy Generator mới”Hãy thử generator mới:
pnpm nx g @aws/nx-plugin:ts#trpc-api#procedureyarn nx g @aws/nx-plugin:ts#trpc-api#procedurenpx nx g @aws/nx-plugin:ts#trpc-api#procedurebunx nx g @aws/nx-plugin:ts#trpc-api#procedureBạn cũng có thể thực hiện chạy thử để xem những tệp nào sẽ bị thay đổi
pnpm nx g @aws/nx-plugin:ts#trpc-api#procedure --dry-runyarn nx g @aws/nx-plugin:ts#trpc-api#procedure --dry-runnpx nx g @aws/nx-plugin:ts#trpc-api#procedure --dry-runbunx nx g @aws/nx-plugin:ts#trpc-api#procedure --dry-run- Cài đặt Nx Console VSCode Plugin nếu bạn chưa cài đặt
- Mở Nx Console trong VSCode
- Nhấp
Generate (UI)trong phần "Common Nx Commands" - Tìm kiếm
@aws/nx-plugin - ts#trpc-api#procedure - Điền các tham số bắt buộc
- Nhấp
Generate
Nếu thành công, chúng ta sẽ đã tạo một procedure mới và thêm procedure vào router trong router.ts.
Bài tập
Phần tiêu đề “Bài tập”Nếu bạn đã đi đến đây và vẫn còn thời gian để thử nghiệm với Nx generator, đây là một số gợi ý về các tính năng để thêm vào procedure generator:
1. Nested Operations
Phần tiêu đề “1. Nested Operations”Thử cập nhật generator để hỗ trợ nested router bằng cách:
- Chấp nhận ký hiệu dấu chấm cho input
procedure(ví dụ:games.query) - Tạo một procedure với tên dựa trên ký hiệu dấu chấm đảo ngược (ví dụ:
queryGames) - Thêm nested router phù hợp (hoặc cập nhật nó nếu nó đã tồn tại!)
2. Validation
Phần tiêu đề “2. Validation”Generator của chúng ta nên bảo vệ chống lại các vấn đề tiềm ẩn, chẳng hạn như người dùng chọn một project không phải là tRPC API. Hãy xem generator connection để có ví dụ về điều này.
3. Unit Tests
Phần tiêu đề “3. Unit Tests”Viết một số unit test cho generator. Chúng khá đơn giản để triển khai, và hầu hết đều tuân theo luồng chung:
- Tạo một empty workspace tree sử dụng
createTreeUsingTsSolutionSetup() - Thêm bất kỳ file nào đã tồn tại trong tree (ví dụ:
project.jsonvàsrc/router.tscho tRPC backend) - Chạy generator đang được kiểm thử
- Xác thực các thay đổi mong đợi được thực hiện trên tree
4. End to End Tests
Phần tiêu đề “4. End to End Tests”Chúng tôi có một bộ “smoke tests” chạy các generator trong một workspace mới và đảm bảo rằng mọi thứ đều build được. Ở mức tối thiểu, generator mới của bạn nên được thêm vào cả hai generator matrix để nó được thực thi bởi smoke tests:
e2e/src/smoke-tests/generator-matrix.ts— chạy từng generator thông qua CLI, một lần gọi tại một thời điểm, chính xác như người dùng sẽ làm.packages/nx-plugin/src/internal/test-matrix/generator.ts— một generator ẩn kết hợp tất cả các generator khác để kiểm thử migration giữa các phiên bản.
Lưu ý rằng generator matrix chỉ chạy các generator và build workspace — nó không khởi tạo bất kỳ infrastructure nào. Đối với các generator triển khai infrastructure, hãy xem xét mở rộng các deployment e2e tests (e2e/src/smoke-tests/cdk-deploy.spec.ts và terraform-deploy.spec.ts) để thực sự triển khai các resource của bạn (thông qua cdk deploy / terraform apply), sau đó thêm một assertion vào deploy-invocations.ts để gọi resource đã triển khai và xác minh nó hoạt động như mong đợi.
Nếu generator của bạn cung cấp một local development server, hãy xem xét thêm nó vào local development e2e test (e2e/src/smoke-tests/local-dev.spec.ts), test này khởi động target dev và thực thi server đang chạy.
Hướng dẫn Chung để Tăng tốc Đóng góp
Phần tiêu đề “Hướng dẫn Chung để Tăng tốc Đóng góp”Phần này chứa một số hướng dẫn chung có thể giúp ích khi làm việc với Nx Plugin for AWS.
Làm việc ngược từ một dự án thực
Phần tiêu đề “Làm việc ngược từ một dự án thực”Một cách hữu ích để xây dựng các generator mới hoặc thêm tính năng/sửa lỗi cho một generator hiện có là xây dựng nó thực sự trước. Bằng cách này, bạn có thể xác thực ý tưởng của mình và lặp lại nhanh chóng để đạt được chức năng bạn cần. Sau khi bạn đã quyết định kết quả mong muốn, bạn có thể cập nhật generator.
Trong thực tế, quy trình này có thể trông như sau:
-
Tạo một workspace mới
Terminal window pnpm create @aws/nx-workspace my-projectTerminal window yarn create @aws/nx-workspace my-projectTerminal window npm create @aws/nx-workspace -- my-projectTerminal window bun create @aws/nx-workspace my-project -
Chạy bất kỳ generator nào có thể là điều kiện tiên quyết cho generator/tính năng/sửa lỗi mới của bạn
-
Commit các thay đổi của bạn (
git commit) -
Thực hiện các thay đổi mong muốn và kiểm thử chúng khi cần
-
Sử dụng
git diffcủa các thay đổi để thông báo những thay đổi nào nên được thực hiện cho Nx Plugin for AWS -
Thực hiện một lần kiểm thử end to end cuối cùng (liên kết
@aws/nx-plugincủa bạn) để đảm bảo generator của bạn cung cấp các thay đổi bạn cần