feat: add custom resource cdk stack parser and transformer#14298
Conversation
… my-gen2-changes
…ify-cli into my-gen2-changes
| const parsed = await this.parser.parseStack(resource.cdkStackPath); | ||
|
|
||
| // Detect patterns | ||
| const patterns = this.patternDetector.detectPatterns(parsed.constructorBody); |
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 4 months ago
To fix the problem, remove the unused variable declaration.
Specifically, delete the line:
const patterns = this.patternDetector.detectPatterns(parsed.constructorBody);found on line 39 in packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/index.ts.
No other code depends on this variable, so its removal will not affect functionality.
No imports nor method definitions need adjustment, as only a single line should be removed.
| @@ -36,7 +36,6 @@ | ||
| const parsed = await this.parser.parseStack(resource.cdkStackPath); | ||
|
|
||
| // Detect patterns | ||
| const patterns = this.patternDetector.detectPatterns(parsed.constructorBody); | ||
|
|
||
| // Extract outputs | ||
| const outputs = this.patternDetector.extractCfnOutputs(parsed.constructorBody); |
| @@ -0,0 +1,40 @@ | |||
| import * as fs from 'fs-extra'; | |||
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 4 months ago
To resolve the issue, we should remove the unused import statement import * as fs from 'fs-extra'; from the top of the file. This change is limited to a single line at the start of the file and will not affect any functionality since fs from fs-extra is not referenced anywhere in this file. No other code changes or dependency adjustments are needed.
| @@ -1,4 +1,3 @@ | ||
| import * as fs from 'fs-extra'; | ||
| import { existsSync, promises as fsPromises } from 'fs'; | ||
| import * as path from 'path'; | ||
| import { CustomResource } from '../types'; |
…-parser-and-transformer
* chore: scaffolding migration * chore: gen2 migration * chore: add codegen-custom-resources directory * chore: mid work * chore: remove non-essential files from tracking * chore: remove additional gen2-migration command files from tracking * chore: remove remaining non-essential files from tracking * chore: remove migration plan documentation from tracking * chore: remove integration test files from tracking --------- Co-authored-by: Eli Polonsky <epolon@amazon.com> Co-authored-by: sanjanaravikumar-az <sanjrkmr@amazon.com> feat: wip feat: wip2 feat: fixed storage issue fix: setup file for jest
* chore: scaffolding migration * chore: gen2 migration * chore: add codegen-custom-resources directory * chore: mid work * chore: remove non-essential files from tracking * chore: remove additional gen2-migration command files from tracking * chore: remove remaining non-essential files from tracking * chore: remove migration plan documentation from tracking * chore: remove integration test files from tracking --------- Co-authored-by: Eli Polonsky <epolon@amazon.com> Co-authored-by: sanjanaravikumar-az <sanjrkmr@amazon.com> feat: wip feat: wip2 feat: fixed storage issue fix: setup file for jest
This PR introduces enhancements to the Amplify CLI Gen2 migration tooling, focusing on custom resource transformation capabilities.
Changes-
Scanner: Traverses the amplify/backend/custom/ directory to discover all custom resources and
locate their cdk-stack.ts files. It provides the initial inventory of resources that need to be
migrated from Gen1 to Gen2.
Parser: Uses TypeScript AST to analyze Gen1 CDK stack files and extract structured information
including imports, class names, constructor bodies, and outputs. It also detects specific Gen1
patterns like CfnParameters, AmplifyHelpers calls, and CDK function references that require
transformation.
Transformer: Converts Gen1 code patterns to Gen2 equivalents using a hybrid approach of TypeScript
AST manipulation and string replacement fallbacks. It handles complex transformations like replacing
cdk.Fn.ref('env') with process.env.AMPLIFY_ENV and AmplifyHelpers.getProjectInfo() with environment
variable access.
Generator: Creates Gen2-compatible resource.ts files and backend integration code from the
transformed data. It filters out Gen1-specific imports, adds required Gen2 imports like constructs,
and generates proper class structures with backend.ts integration code.
Unit Tests:
Scanner: Custom resource discovery and file system operations
Parser: CDK pattern detection and AST analysis
Transformer: AST and string-based code transformations
Generator: Gen2 file generation and backend integration
PR description included
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.