Skip to content

Commit f42e2cc

Browse files
authored
chore(yargs-gen): use lodash.clonedeep instead of structured clone (#32537)
This will make it easier to run the build on node 16. Adjacent changes: pkglint had a bug that made it fail for packages with a `.` in their name. Instead now allow to pass the json path directly as an array. Move inline function extraction completely into config and not part of yargs-gen. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent bf026bd commit f42e2cc

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

packages/aws-cdk/lib/config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,11 @@ export class DynamicValue {
422422
}
423423

424424
public static fromInline(f: () => any): DynamicResult {
425+
const ARROW = '=>';
426+
const body = f.toString();
425427
return {
426428
dynamicType: 'function',
427-
dynamicValue: f.toString(),
429+
dynamicValue: body.substring(body.indexOf(ARROW) + ARROW.length).trim(),
428430
};
429431
}
430432
}

packages/aws-cdk/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"scripts": {
99
"build": "cdk-build",
10-
"yargs-gen": "yarn ts-node --preferTsExts scripts/yargs-gen.ts",
10+
"yargs-gen": "ts-node --preferTsExts scripts/yargs-gen.ts",
1111
"watch": "cdk-watch",
1212
"lint": "cdk-lint",
1313
"pkglint": "pkglint -f",
@@ -75,8 +75,8 @@
7575
"@types/fs-extra": "^9.0.13",
7676
"@types/glob": "^7.2.0",
7777
"@types/jest": "^29.5.12",
78-
"@types/node": "^18.18.14",
7978
"@types/mockery": "^1.4.33",
79+
"@types/node": "^18.18.14",
8080
"@types/promptly": "^3.0.5",
8181
"@types/semver": "^7.5.8",
8282
"@types/sinon": "^9.0.11",
@@ -97,6 +97,7 @@
9797
"nock": "^13.5.5",
9898
"sinon": "^9.2.4",
9999
"ts-jest": "^29.2.5",
100+
"ts-node": "^10.9.2",
100101
"ts-mock-imports": "^1.3.16",
101102
"xml-js": "^1.6.11"
102103
},
@@ -128,12 +129,12 @@
128129
"@jsii/check-node": "1.104.0",
129130
"@smithy/middleware-endpoint": "3.1.4",
130131
"@smithy/node-http-handler": "3.2.4",
132+
"@smithy/property-provider": "3.1.10",
131133
"@smithy/shared-ini-file-loader": "3.1.8",
132134
"@smithy/types": "3.5.0",
133135
"@smithy/util-retry": "3.0.7",
134136
"@smithy/util-stream": "3.1.9",
135137
"@smithy/util-waiter": "3.1.6",
136-
"@smithy/property-provider": "3.1.10",
137138
"archiver": "^5.3.2",
138139
"camelcase": "^6.3.0",
139140
"cdk-assets": "^3.0.0-rc.48",

tools/@aws-cdk/pkglint/lib/rules.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ export class AllVersionsTheSame extends ValidationRule {
13701370

13711371
private validateDep(pkg: PackageJson, depField: string, dep: string) {
13721372
if (dep in this.ourPackages) {
1373-
expectJSON(this.name, pkg, depField + '.' + dep, this.ourPackages[dep]);
1373+
expectJSON(this.name, pkg, [depField, dep], this.ourPackages[dep]);
13741374
return;
13751375
}
13761376

@@ -1380,7 +1380,7 @@ export class AllVersionsTheSame extends ValidationRule {
13801380

13811381
const versions = this.usedDeps[dep];
13821382
versions.sort((a, b) => b.count - a.count);
1383-
expectJSON(this.name, pkg, depField + '.' + dep, versions[0].version);
1383+
expectJSON(this.name, pkg, [depField, dep], versions[0].version);
13841384
}
13851385
}
13861386

tools/@aws-cdk/pkglint/lib/util.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { PackageJson, PKGLINT_IGNORES } from './packagejson';
88
export function expectJSON(
99
ruleName: string,
1010
pkg: PackageJson,
11-
jsonPath: string,
11+
jsonPath: string | string[],
1212
expected: any,
1313
ignore?: RegExp,
1414
caseInsensitive: boolean = false,
1515
regexMatch: boolean = false,
1616
) {
17-
const parts = jsonPath.split('.');
17+
const parts = Array.isArray(jsonPath) ? jsonPath : jsonPath.split('.');
1818
const actual = deepGet(pkg.json, parts);
1919
if (checkEquality()) {
2020
pkg.report({

tools/@aws-cdk/yargs-gen/lib/yargs-gen.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { EsLintRules } from '@cdklabs/typewriter/lib/eslint-rules';
33
import * as prettier from 'prettier';
44
import { CliConfig, CliOption, YargsOption } from './yargs-types';
55

6+
// to import lodash.clonedeep properly, we would need to set esModuleInterop: true
7+
// however that setting does not work in the CLI, so we fudge it.
8+
// eslint-disable-next-line @typescript-eslint/no-require-imports
9+
const cloneDeep = require('lodash.clonedeep');
10+
611
export async function renderYargs(config: CliConfig): Promise<string> {
712
const scope = new Module('aws-cdk');
813

@@ -106,7 +111,7 @@ function makeOptions(prefix: Expression, options: { [optionName: string]: CliOpt
106111
let optionsExpr = prefix;
107112
for (const option of Object.keys(options)) {
108113
const theOption: CliOption = options[option];
109-
const optionProps: YargsOption = structuredClone(theOption);
114+
const optionProps: YargsOption = cloneDeep(theOption);
110115
const optionArgs: { [key: string]: Expression } = {};
111116

112117
// Array defaults
@@ -121,9 +126,7 @@ function makeOptions(prefix: Expression, options: { [optionName: string]: CliOpt
121126
optionArgs[optionProp] = code.expr.ident(optionValue.dynamicValue);
122127
} else if (optionValue && optionValue.dynamicType === 'function') {
123128
const inlineFunction: string = optionValue.dynamicValue;
124-
const NUMBER_OF_SPACES_BETWEEN_ARROW_AND_CODE = 3;
125-
// this only works with arrow functions, like () =>
126-
optionArgs[optionProp] = code.expr.directCode(inlineFunction.substring(inlineFunction.indexOf('=>') + NUMBER_OF_SPACES_BETWEEN_ARROW_AND_CODE));
129+
optionArgs[optionProp] = code.expr.directCode(inlineFunction);
127130
} else {
128131
optionArgs[optionProp] = lit(optionValue);
129132
}

tools/@aws-cdk/yargs-gen/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
"license": "Apache-2.0",
3030
"dependencies": {
3131
"@cdklabs/typewriter": "^0.0.4",
32+
"lodash.clonedeep": "^4.5.0",
3233
"prettier": "^2.8.8"
3334
},
3435
"devDependencies": {
3536
"@aws-cdk/cdk-build-tools": "0.0.0",
3637
"@aws-cdk/pkglint": "0.0.0",
3738
"@types/jest": "^29.5.12",
39+
"@types/lodash.clonedeep": "^4.5.0",
3840
"@types/node": "^18",
3941
"jest": "^29.7.0"
4042
},

yarn.lock

+17
Original file line numberDiff line numberDiff line change
@@ -7927,6 +7927,18 @@
79277927
resolved "https://registry.npmjs.org/@types/license-checker/-/license-checker-25.0.6.tgz#c346285ee7e42bac58a4922059453f50a5d4175d"
79287928
integrity sha512-ju/75+YPkNE5vX1iPer+qtI1eI/LqJVYZgOsmSHI1iiEM1bQL5Gh1lEvyjR9T7ZXVE1FwJa2doWJEEmPNwbZkw==
79297929

7930+
"@types/lodash.clonedeep@^4.5.0":
7931+
version "4.5.9"
7932+
resolved "https://registry.npmjs.org/@types/lodash.clonedeep/-/lodash.clonedeep-4.5.9.tgz#ea48276c7cc18d080e00bb56cf965bcceb3f0fc1"
7933+
integrity sha512-19429mWC+FyaAhOLzsS8kZUsI+/GmBAQ0HFiCPsKGU+7pBXOQWhyrY6xNNDwUSX8SMZMJvuFVMF9O5dQOlQK9Q==
7934+
dependencies:
7935+
"@types/lodash" "*"
7936+
7937+
"@types/lodash@*":
7938+
version "4.17.13"
7939+
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.13.tgz#786e2d67cfd95e32862143abe7463a7f90c300eb"
7940+
integrity sha512-lfx+dftrEZcdBPczf9d0Qv0x+j/rfNCMuC6OcfXmO8gkfeNAY88PgKUbvG56whcN23gc27yenwF6oJZXGFpYxg==
7941+
79307942
"@types/lodash@^4.17.12":
79317943
version "4.17.12"
79327944
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.12.tgz#25d71312bf66512105d71e55d42e22c36bcfc689"
@@ -14259,6 +14271,11 @@ locate-path@^6.0.0:
1425914271
dependencies:
1426014272
p-locate "^5.0.0"
1426114273

14274+
lodash.clonedeep@^4.5.0:
14275+
version "4.5.0"
14276+
resolved "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
14277+
integrity sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==
14278+
1426214279
lodash.defaults@^4.2.0:
1426314280
version "4.2.0"
1426414281
resolved "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"

0 commit comments

Comments
 (0)