From 8f7f58d90e11e5945b922d13f07f70e1a3f7b887 Mon Sep 17 00:00:00 2001 From: Ruslan Kharitonov Date: Fri, 18 Feb 2022 18:44:46 -0500 Subject: [PATCH] feat: Support AWS CDK v2 # Description Support AWS CDK v2 Per https://docs.aws.amazon.com/cdk/v1/guide/home.html, the support for CDK v1 is until June 1, 2022. Best to generate CDK v2 project Example: https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html # PR Checklist - [X] Migrations have been added if necessary - [X] Unit tests have been added or updated if necessary - [X] e2e tests have been added or updated if necessary - [X] Changelog has been updated if necessary - [X] Documentation has been updated if necessary # Issue Resolves #7 --- package-lock.json | 222 ++++++++++++++++-- package.json | 9 +- packages/nx-aws-cdk/README.md | 10 +- packages/nx-aws-cdk/migrations.json | 25 ++ packages/nx-aws-cdk/package.json | 2 +- .../application/files/src/main.ts__template__ | 4 +- .../files/src/stacks/app-stack.ts__template__ | 8 +- .../jest-files/src/main.test.ts__template__ | 12 +- .../src/generators/init/init.spec.ts | 4 +- .../nx-aws-cdk/src/generators/init/init.ts | 4 +- .../update-2-0-0/migrate-to-cdk-2.spec.ts | 67 ++++++ .../update-2-0-0/migrate-to-cdk-2.ts | 16 ++ .../update-2-0-0/update-2-0-0.spec.ts | 38 +++ .../migrations/update-2-0-0/update-2-0-0.ts | 10 + packages/nx-aws-cdk/src/utils/cdk-shared.ts | 4 +- 15 files changed, 390 insertions(+), 45 deletions(-) create mode 100644 packages/nx-aws-cdk/migrations.json create mode 100644 packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.spec.ts create mode 100644 packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.ts create mode 100644 packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.spec.ts create mode 100644 packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.ts diff --git a/package-lock.json b/package-lock.json index ef3abec..1c5d398 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,29 +4,41 @@ "lockfileVersion": 1, "dependencies": { "@angular-devkit/architect": { - "version": "0.1200.5", - "resolved": "https://registry.npmjs.org/@angular-devkit/architect/-/architect-0.1200.5.tgz", - "integrity": "sha512-222VZ4OeaDK3vON8V5m+w15SRWfUs5uOb4H9ij/H9/6tyHD83uWfCDoOGg+ax4wJVdWEFJIS+Vn4ijGcZCq9WQ==", + "version": "0.1302.4", + "resolved": "https://registry.npmjs.org/@angular-devkit/architect/-/architect-0.1302.4.tgz", + "integrity": "sha512-9dS0Gvs4ER1DPSnKorJ4uk27A/rWwyvNy5CheOZu4ZTUnggGA7SFtMG7CKPD11Ae01lIMfxKfDqJYE8j2VpQaw==", "dev": true, "requires": { - "@angular-devkit/core": "12.0.5", + "@angular-devkit/core": "13.2.4", "rxjs": "6.6.7" }, "dependencies": { "@angular-devkit/core": { - "version": "12.0.5", - "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-12.0.5.tgz", - "integrity": "sha512-zVSQV+8/vjUjsUKGlj8Kf5LioA6AXJTGI0yhHW9q1dFX4dPpbW63k0R1UoIB2wJ0F/AbYVgpnPGPe9BBm2fvZA==", + "version": "13.2.4", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-13.2.4.tgz", + "integrity": "sha512-hSw1JWA/6dDAF/xleQRXGtzHphfU49TMUhvAoAmsmmz3NAn03xLy1dtqdIXIf+TkFXVvZDaAB2mW8KfRV67GFg==", "dev": true, "requires": { - "ajv": "8.2.0", - "ajv-formats": "2.0.2", + "ajv": "8.9.0", + "ajv-formats": "2.1.1", "fast-json-stable-stringify": "2.1.0", "magic-string": "0.25.7", "rxjs": "6.6.7", "source-map": "0.7.3" } }, + "ajv": { + "version": "8.9.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.9.0.tgz", + "integrity": "sha512-qOKJyNj/h+OWx7s5DePL6Zu1KeM9jPZhwBqs+7DzP6bGOvqzVCSf0xueYmVuaC/oQ/VtS2zLMLHdQFbkka+XDQ==", + "dev": true, + "requires": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + } + }, "source-map": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", @@ -79,14 +91,50 @@ } }, "@angular-devkit/schematics": { - "version": "12.1.2", - "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-12.1.2.tgz", - "integrity": "sha512-/UGcAJChq+g7wf46Exr5iIyuVw3j4TxTIntTpuKg/z/xf9Y+8mzJAZgGittu/yFcHAJ9aYcOHctffrfEdV7QSA==", + "version": "13.2.4", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-13.2.4.tgz", + "integrity": "sha512-VMhYa4cDu5yE31OvHncAd15Rmlchih/Sr6sxFsIwkg4xzRNIIZCtwqxVXgf0TiTN9zrvlvzK7nhPqTGNqqYb2A==", "dev": true, "requires": { - "@angular-devkit/core": "12.1.2", + "@angular-devkit/core": "13.2.4", + "jsonc-parser": "3.0.0", + "magic-string": "0.25.7", "ora": "5.4.1", "rxjs": "6.6.7" + }, + "dependencies": { + "@angular-devkit/core": { + "version": "13.2.4", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-13.2.4.tgz", + "integrity": "sha512-hSw1JWA/6dDAF/xleQRXGtzHphfU49TMUhvAoAmsmmz3NAn03xLy1dtqdIXIf+TkFXVvZDaAB2mW8KfRV67GFg==", + "dev": true, + "requires": { + "ajv": "8.9.0", + "ajv-formats": "2.1.1", + "fast-json-stable-stringify": "2.1.0", + "magic-string": "0.25.7", + "rxjs": "6.6.7", + "source-map": "0.7.3" + } + }, + "ajv": { + "version": "8.9.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.9.0.tgz", + "integrity": "sha512-qOKJyNj/h+OWx7s5DePL6Zu1KeM9jPZhwBqs+7DzP6bGOvqzVCSf0xueYmVuaC/oQ/VtS2zLMLHdQFbkka+XDQ==", + "dev": true, + "requires": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + } + }, + "source-map": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", + "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==", + "dev": true + } } }, "@babel/code-frame": { @@ -1815,6 +1863,97 @@ "rxjs": "^6.5.4", "tslib": "^2.0.0", "yargs": "15.4.1" + }, + "dependencies": { + "@angular-devkit/architect": { + "version": "0.1200.5", + "resolved": "https://registry.npmjs.org/@angular-devkit/architect/-/architect-0.1200.5.tgz", + "integrity": "sha512-222VZ4OeaDK3vON8V5m+w15SRWfUs5uOb4H9ij/H9/6tyHD83uWfCDoOGg+ax4wJVdWEFJIS+Vn4ijGcZCq9WQ==", + "dev": true, + "requires": { + "@angular-devkit/core": "12.0.5", + "rxjs": "6.6.7" + }, + "dependencies": { + "@angular-devkit/core": { + "version": "12.0.5", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-12.0.5.tgz", + "integrity": "sha512-zVSQV+8/vjUjsUKGlj8Kf5LioA6AXJTGI0yhHW9q1dFX4dPpbW63k0R1UoIB2wJ0F/AbYVgpnPGPe9BBm2fvZA==", + "dev": true, + "requires": { + "ajv": "8.2.0", + "ajv-formats": "2.0.2", + "fast-json-stable-stringify": "2.1.0", + "magic-string": "0.25.7", + "rxjs": "6.6.7", + "source-map": "0.7.3" + } + } + } + }, + "@angular-devkit/schematics": { + "version": "12.2.16", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-12.2.16.tgz", + "integrity": "sha512-ToyZBCGilSeeLmhAxmeJ0PykmbKLoME+uK78gC64xJtNu9e3oVnmog8b8g9Ay9hTwZJ96HvNa16po11Gfbbn6A==", + "dev": true, + "requires": { + "@angular-devkit/core": "12.2.16", + "ora": "5.4.1", + "rxjs": "6.6.7" + }, + "dependencies": { + "@angular-devkit/core": { + "version": "12.2.16", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-12.2.16.tgz", + "integrity": "sha512-cnVtUYSET27B5mRIBp38mpKIX0iHv/hWKiPo74WCGrNwTgwmMHngjgQ4ySn/w1W4s8LL6TDW55ZkRdwyk8TVMQ==", + "dev": true, + "requires": { + "ajv": "8.6.2", + "ajv-formats": "2.1.0", + "fast-json-stable-stringify": "2.1.0", + "magic-string": "0.25.7", + "rxjs": "6.6.7", + "source-map": "0.7.3" + } + }, + "ajv": { + "version": "8.6.2", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.6.2.tgz", + "integrity": "sha512-9807RlWAgT564wT+DjeyU5OFMPjmzxVobvDFmNAhY+5zD6A2ly3jDp6sgnfyDtlIQ+7H97oc/DGCzzfu9rjw9w==", + "dev": true, + "requires": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + } + }, + "ajv-formats": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.0.tgz", + "integrity": "sha512-USH2jBb+C/hIpwD2iRjp0pe0k+MvzG0mlSn/FIdCgQhUb9ALPRjt2KIQdfZDS9r0ZIeUAg7gOu9KL0PFqGqr5Q==", + "dev": true, + "requires": { + "ajv": "^8.0.0" + } + } + } + }, + "ajv-formats": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.0.2.tgz", + "integrity": "sha512-Brah4Uo5/U8v76c6euTwtjVFFaVishwnJrQBYpev1JRh4vjA1F4HY3UzQez41YUCszUCXKagG8v6eVRBHV1gkw==", + "dev": true, + "requires": { + "ajv": "^8.0.0" + } + }, + "source-map": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", + "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==", + "dev": true + } } }, "@nrwl/tao": { @@ -2135,9 +2274,9 @@ } }, "debug": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.2.tgz", - "integrity": "sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==", + "version": "4.3.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", + "integrity": "sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==", "dev": true, "requires": { "ms": "2.1.2" @@ -2642,9 +2781,9 @@ "dev": true }, "ajv-formats": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.0.2.tgz", - "integrity": "sha512-Brah4Uo5/U8v76c6euTwtjVFFaVishwnJrQBYpev1JRh4vjA1F4HY3UzQez41YUCszUCXKagG8v6eVRBHV1gkw==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz", + "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==", "dev": true, "requires": { "ajv": "^8.0.0" @@ -9899,6 +10038,51 @@ "integrity": "sha512-h2AatdwYH+JHiZpv7pt/gSX1XoRGb7L/qSIeuqA6GwYoF9w1vP1cw42TO0aI2pNyshRK5893hNSl+1//vHK7hQ==", "dev": true }, + "nx": { + "version": "13.8.2", + "resolved": "https://registry.npmjs.org/nx/-/nx-13.8.2.tgz", + "integrity": "sha512-raCluwNq9lK1sb71omgWfgXiQS8r+mnmXraL4p4aI7P07hEer2Oo8QI9ec4KbVc04HKkLOMkzg6YkZ94Glkc/g==", + "dev": true, + "requires": { + "@nrwl/cli": "13.8.2" + }, + "dependencies": { + "@nrwl/cli": { + "version": "13.8.2", + "resolved": "https://registry.npmjs.org/@nrwl/cli/-/cli-13.8.2.tgz", + "integrity": "sha512-wpSKo/78y1oNeBhwHIRYaqDddNhzdOmTOkYNf+J6wuaeaEMlnrJFgDrTyy14ysgPFiW0vzb5AdrDb6W2tGigYQ==", + "dev": true, + "requires": { + "@nrwl/tao": "13.8.2", + "chalk": "4.1.0", + "enquirer": "~2.3.6", + "v8-compile-cache": "2.3.0", + "yargs-parser": "20.0.0" + } + }, + "@nrwl/tao": { + "version": "13.8.2", + "resolved": "https://registry.npmjs.org/@nrwl/tao/-/tao-13.8.2.tgz", + "integrity": "sha512-68GsErwpOQDZPunJrpXDpauQo40dP66dO7uHQUu1Jf+css0uu1dfXtIR4CrAikvLJ57fZZw09l1xFrI9+OfXjA==", + "dev": true, + "requires": { + "chalk": "4.1.0", + "enquirer": "~2.3.6", + "fast-glob": "3.2.7", + "fs-extra": "^9.1.0", + "ignore": "^5.0.4", + "jsonc-parser": "3.0.0", + "nx": "13.8.2", + "rxjs": "^6.5.4", + "rxjs-for-await": "0.0.2", + "semver": "7.3.4", + "tmp": "~0.2.1", + "tslib": "^2.3.0", + "yargs-parser": "20.0.0" + } + } + } + }, "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", diff --git a/package.json b/package.json index c6dc4cf..c719cb1 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,8 @@ "lint-stage": "lint-staged", "commitlint": "commitlint", "is-ci": "is-ci", - "test": "nx test", - "lint": "nx workspace-lint && nx lint", + "test": "nx test nx-aws-cdk", + "lint": "nx lint nx-aws-cdk", "e2e": "nx e2e", "format": "nx format:write", "build:aws-cdk": "nx build nx-aws-cdk", @@ -29,6 +29,8 @@ "tslib": "^2.0.0" }, "devDependencies": { + "@angular-devkit/architect": "^0.1302.4", + "@angular-devkit/schematics": "^13.2.4", "@commitlint/cli": "^13.2.1", "@commitlint/config-conventional": "^13.2.0", "@jscutlery/semver": "^2.10.0", @@ -43,7 +45,7 @@ "@nrwl/workspace": "12.5.8", "@types/jest": "26.0.8", "@types/node": "14.14.33", - "@typescript-eslint/eslint-plugin": "4.19.0", + "@typescript-eslint/eslint-plugin": "^4.19.0", "@typescript-eslint/parser": "4.19.0", "dotenv": "8.2.0", "eslint": "7.22.0", @@ -52,6 +54,7 @@ "is-ci": "^3.0.0", "jest": "27.0.3", "lint-staged": "^11.2.3", + "nx": "^13.8.2", "prettier": "^2.3.1", "ts-jest": "27.0.3", "ts-node": "~9.1.1", diff --git a/packages/nx-aws-cdk/README.md b/packages/nx-aws-cdk/README.md index 8f3990b..79dd975 100644 --- a/packages/nx-aws-cdk/README.md +++ b/packages/nx-aws-cdk/README.md @@ -35,7 +35,9 @@ yarn add --dev @codebrew/nx-aws-cdk ### Generate Application -Create Aws Cdk Application +Create AWS CDK v2 Application + +More details on AWS CDK v2 can be found on https://docs.aws.amazon.com/cdk/v2/guide/home.html ```shell nx generate @codebrew/nx-aws-cdk:application myApp @@ -64,8 +66,8 @@ Options: Generated applications expose several functions to the CLI that allow users to deploy, destroy and so on. ```shell -nx deploy {Project Name} -nx destroy {Project Name} +nx deploy myApp +nx destroy myApp ``` ## Maintainers @@ -74,7 +76,7 @@ nx destroy {Project Name} ## Contributing -See [the contributing file](../../contributing.md)! +See [the contributing file](../../CONTRIBUTING.md)! PRs accepted. diff --git a/packages/nx-aws-cdk/migrations.json b/packages/nx-aws-cdk/migrations.json new file mode 100644 index 0000000..ce17c60 --- /dev/null +++ b/packages/nx-aws-cdk/migrations.json @@ -0,0 +1,25 @@ +{ + "schematics": { + "update-2.0.0": { + "version": "2.0.0", + "description": "Update libraries", + "factory": "./src/migrations/update-2-0-0/update-2-0-0" + }, + "migrate-to-cdk-2": { + "version": "2.0.0", + "description": "Update AWS CDK to v2: rename '@aws-cdk/core' to 'aws-cdk-lib' and '@aws-cdk/assert' to 'aws-cdk'", + "factory": "./src/migrations/update-2-0-0/migrate-to-cdk-2" + } + }, + "packageJsonUpdates": { + "2.0.0": { + "version": "2.0.0", + "packages": { + "eslint-plugin-cdk": { + "version": "^1.7.0", + "alwaysAddToPackageJson": false + } + } + } + } +} diff --git a/packages/nx-aws-cdk/package.json b/packages/nx-aws-cdk/package.json index 22465aa..8c6722a 100644 --- a/packages/nx-aws-cdk/package.json +++ b/packages/nx-aws-cdk/package.json @@ -1,6 +1,6 @@ { "name": "@codebrew/nx-aws-cdk", - "version": "1.0.3", + "version": "2.0.0", "main": "src/index.js", "generators": "./generators.json", "executors": "./executors.json", diff --git a/packages/nx-aws-cdk/src/generators/application/files/src/main.ts__template__ b/packages/nx-aws-cdk/src/generators/application/files/src/main.ts__template__ index 0854972..c9a1a68 100644 --- a/packages/nx-aws-cdk/src/generators/application/files/src/main.ts__template__ +++ b/packages/nx-aws-cdk/src/generators/application/files/src/main.ts__template__ @@ -1,5 +1,5 @@ -import * as cdk from '@aws-cdk/core'; +import { App } from 'aws-cdk-lib'; import { AppStack } from './stacks/app-stack'; -const app = new cdk.App(); +const app = new App(); new AppStack(app, '<%= projectName %>'); \ No newline at end of file diff --git a/packages/nx-aws-cdk/src/generators/application/files/src/stacks/app-stack.ts__template__ b/packages/nx-aws-cdk/src/generators/application/files/src/stacks/app-stack.ts__template__ index 86c6509..2f8a983 100644 --- a/packages/nx-aws-cdk/src/generators/application/files/src/stacks/app-stack.ts__template__ +++ b/packages/nx-aws-cdk/src/generators/application/files/src/stacks/app-stack.ts__template__ @@ -1,8 +1,8 @@ -import * as cdk from '@aws-cdk/core'; +import { Stack, App, StackProps } from 'aws-cdk-lib'; -export class AppStack extends cdk.Stack { - constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { - super(scope, id, props); +export class AppStack extends Stack { + constructor(scope: App, id: string, props?: StackProps) { + super(scope, id, props); // defines your stack here } diff --git a/packages/nx-aws-cdk/src/generators/application/jest-files/src/main.test.ts__template__ b/packages/nx-aws-cdk/src/generators/application/jest-files/src/main.test.ts__template__ index 5224eee..9e0cb53 100644 --- a/packages/nx-aws-cdk/src/generators/application/jest-files/src/main.test.ts__template__ +++ b/packages/nx-aws-cdk/src/generators/application/jest-files/src/main.test.ts__template__ @@ -1,13 +1,13 @@ -import { expect as expectCDK, matchTemplate, MatchStyle } from '@aws-cdk/assert'; -import * as cdk from '@aws-cdk/core'; +import { Stack, App } from 'aws-cdk-lib'; +import { Template } from 'aws-cdk-lib/assertions'; import { AppStack } from './stacks/app-stack'; test('Empty Stack', () => { - const app = new cdk.App(); + const app = new App(); // WHEN const stack = new AppStack(app, '<%= projectName %>TestStack'); // THEN - expectCDK(stack).to(matchTemplate({ - "Resources": {} - }, MatchStyle.EXACT)) + Template.fromStack(stack as Stack).templateMatches({ + Resources: {} + }); }); diff --git a/packages/nx-aws-cdk/src/generators/init/init.spec.ts b/packages/nx-aws-cdk/src/generators/init/init.spec.ts index f38f07c..41e9aea 100644 --- a/packages/nx-aws-cdk/src/generators/init/init.spec.ts +++ b/packages/nx-aws-cdk/src/generators/init/init.spec.ts @@ -16,7 +16,7 @@ describe('init', () => { await initGenerator(tree, options); const packageJson = readJson(tree, 'package.json'); - expect(packageJson.dependencies['@aws-cdk/core']).toBeDefined(); - expect(packageJson.dependencies['@aws-cdk/assert']).toBeDefined(); + expect(packageJson.dependencies['aws-cdk-lib']).toBeDefined(); + expect(packageJson.dependencies['aws-cdk']).toBeDefined(); }); }); diff --git a/packages/nx-aws-cdk/src/generators/init/init.ts b/packages/nx-aws-cdk/src/generators/init/init.ts index 01a86c0..fb189d3 100644 --- a/packages/nx-aws-cdk/src/generators/init/init.ts +++ b/packages/nx-aws-cdk/src/generators/init/init.ts @@ -22,8 +22,8 @@ export async function initGenerator(host: Tree, options: InitGeneratorSchema) { const installTask = addDependenciesToPackageJson( host, { - '@aws-cdk/core': CDK_VERSION, - '@aws-cdk/assert': CDK_VERSION, + 'aws-cdk': CDK_VERSION, + 'aws-cdk-lib': CDK_VERSION, }, { 'eslint-plugin-cdk': CDK_ESLINT_VERSION, diff --git a/packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.spec.ts b/packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.spec.ts new file mode 100644 index 0000000..d2e04cf --- /dev/null +++ b/packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.spec.ts @@ -0,0 +1,67 @@ +import { Tree } from '@angular-devkit/schematics'; +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; +import { readJsonInTree } from '@nrwl/workspace'; +import * as path from 'path'; +import { createEmptyWorkspace, runSchematic } from '@nrwl/workspace/testing'; +import { CDK_VERSION } from '../../utils/cdk-shared'; + +describe('Update AWS CDK dependencies to v2', () => { + let tree: Tree; + let schematicRunner: SchematicTestRunner; + + beforeEach(async () => { + tree = Tree.empty(); + tree = createEmptyWorkspace(tree); + schematicRunner = new SchematicTestRunner('@nrwl/next', path.join(__dirname, '../../../migrations.json')); + tree.overwrite( + 'package.json', + JSON.stringify({ + devDependencies: { + '@aws-cdk/core': '^1.127.0', + }, + }) + ); + }); + + it(`should update '@aws-cdk/core', if used, to the new package names`, async () => { + tree = await schematicRunner.runSchematicAsync('migrate-to-cdk-2', {}, tree).toPromise(); + + const packageJson = readJsonInTree(tree, '/package.json'); + expect(packageJson).toMatchObject({ + devDependencies: { + 'aws-cdk-lib': CDK_VERSION, + }, + }); + }); + + it(`should update '@aws-cdk/core' to the 'aws-sdk-lib' where imported`, async () => { + tree = await runSchematic('lib', { name: 'library-1' }, tree); + + const moduleThatImports = 'libs/library-1/src/importer.ts'; + tree.create( + moduleThatImports, + `import * as cdk from '@aws-cdk/core'; + const app = new cdk.App(); + ` + ); + + tree = await schematicRunner.runSchematicAsync('migrate-to-cdk-2', {}, tree).toPromise(); + + expect(tree.read(moduleThatImports).toString()).toContain(`import * as cdk from 'aws-cdk-lib';`); + }); + it(`should update '@aws-cdk/assert', if used, to 'aws-cdk-lib/assertions' where imported`, async () => { + tree = await runSchematic('lib', { name: 'library-1' }, tree); + + const moduleThatImports = 'libs/library-1/src/importer.ts'; + tree.create( + moduleThatImports, + `import * as cdk from '@aws-cdk/core'; + const app = new cdk.App(); + ` + ); + + tree = await schematicRunner.runSchematicAsync('migrate-to-cdk-2', {}, tree).toPromise(); + + expect(tree.read(moduleThatImports).toString()).toContain(`import * as cdk from 'aws-cdk-lib';`); + }); +}); diff --git a/packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.ts b/packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.ts new file mode 100644 index 0000000..06d6db1 --- /dev/null +++ b/packages/nx-aws-cdk/src/migrations/update-2-0-0/migrate-to-cdk-2.ts @@ -0,0 +1,16 @@ +import { chain } from '@angular-devkit/schematics'; +import { formatFiles, renameNpmPackages } from '@nrwl/workspace'; +import { CDK_VERSION } from '../../utils/cdk-shared'; +import { renamePackageImports } from '@nrwl/workspace/src/utils/rules/rename-package-imports'; + +export default function update() { + return chain([ + renameNpmPackages({ + '@aws-cdk/core': ['aws-cdk-lib', CDK_VERSION], + }), + renamePackageImports({ + '@aws-cdk/assert': 'aws-cdk-lib/assertions', + }), + formatFiles(), + ]); +} diff --git a/packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.spec.ts b/packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.spec.ts new file mode 100644 index 0000000..0d2e635 --- /dev/null +++ b/packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.spec.ts @@ -0,0 +1,38 @@ +import { Tree } from '@angular-devkit/schematics'; +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; +import { readJsonInTree } from '@nrwl/workspace'; +import * as path from 'path'; +import { createEmptyWorkspace } from '@nrwl/workspace/testing'; + +describe('Update 2.0.0', () => { + let tree: Tree; + let schematicRunner: SchematicTestRunner; + + beforeEach(async () => { + tree = Tree.empty(); + tree = createEmptyWorkspace(tree); + schematicRunner = new SchematicTestRunner('@nrwl/react', path.join(__dirname, '../../../migrations.json')); + }); + + it(`should update libs`, async () => { + tree.overwrite( + 'package.json', + JSON.stringify({ + dependencies: {}, + devDependencies: { + 'eslint-plugin-cdk': '^1.3.0', + }, + }) + ); + + tree = await schematicRunner.runSchematicAsync('update-2.0.0', {}, tree).toPromise(); + + const packageJson = readJsonInTree(tree, '/package.json'); + expect(packageJson).toMatchObject({ + dependencies: {}, + devDependencies: { + 'eslint-plugin-cdk': '^1.7.0', + }, + }); + }); +}); diff --git a/packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.ts b/packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.ts new file mode 100644 index 0000000..ed0c172 --- /dev/null +++ b/packages/nx-aws-cdk/src/migrations/update-2-0-0/update-2-0-0.ts @@ -0,0 +1,10 @@ +import { chain, Rule } from '@angular-devkit/schematics'; +import { formatFiles, updatePackagesInPackageJson } from '@nrwl/workspace'; +import * as path from 'path'; + +export default function update(): Rule { + return chain([ + updatePackagesInPackageJson(path.join(__dirname, '../../../', 'migrations.json'), '2.0.0'), + formatFiles(), + ]); +} diff --git a/packages/nx-aws-cdk/src/utils/cdk-shared.ts b/packages/nx-aws-cdk/src/utils/cdk-shared.ts index 12efff8..424a76b 100644 --- a/packages/nx-aws-cdk/src/utils/cdk-shared.ts +++ b/packages/nx-aws-cdk/src/utils/cdk-shared.ts @@ -1,2 +1,2 @@ -export const CDK_VERSION = '^1.127.0'; -export const CDK_ESLINT_VERSION = '^1.3.0'; +export const CDK_VERSION = '^2.12.0'; +export const CDK_ESLINT_VERSION = '^1.7.0';