Skip to content

Commit 30dff7b

Browse files
committed
feat: fixture source files and cdk config
1 parent 8bd27ad commit 30dff7b

File tree

7 files changed

+75
-15
lines changed

7 files changed

+75
-15
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @ts-ignore
2+
import * as cdk from 'aws-cdk-lib';
3+
import { MyStack } from '../lib/myStack';
4+
5+
const app = new cdk.App();
6+
new MyStack(app, 'MyStack');
7+
8+
app.synth();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"app": "ts-node --project=tsconfig.json --prefer-ts-exts cdk/bin/infra.ts",
3+
"watch": {
4+
"include": ["cdk/lib/**", "cdk/bin/**"]
5+
},
6+
"context": {
7+
"aws-cdk-lib/core:enableDiffNoFail": true,
8+
"aws-cdk-lib/core:bootstrapQualifier": "custom-v2",
9+
"aws-cdk-lib/core:checkSecretUsage": true,
10+
"aws-cdk-lib/core:target-partitions": [
11+
"aws",
12+
"aws-cn"
13+
],
14+
"aws-cdk-lib/aws-apigateway:disableCloudWatchRole": true,
15+
"aws-cdk-lib/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
16+
"aws-cdk-lib/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": false,
17+
"aws-cdk-lib/aws-lambda:recognizeLayerVersion": true
18+
}
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @ts-ignore
2+
import { Construct } from 'constructs';
3+
4+
export class MyConstruct extends Construct {
5+
constructor(scope: Construct, id: string) {
6+
super(scope, id);
7+
8+
// The code that defines your stack goes here
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @ts-ignore
2+
import * as cdk from 'aws-cdk-lib';
3+
4+
export class MyStack extends cdk.Stack {
5+
constructor(scope: cdk.App, id: string) {
6+
super(scope, id);
7+
8+
// The code that defines your stack goes here
9+
}
10+
}
11+

packages/knip/src/plugins/aws-cdk/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import type { IsPluginEnabled, Plugin } from '../../types/config.js';
1+
import { toDependency } from 'src/util/input.js';
2+
import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.js';
3+
import { compact } from '../../util/array.js';
24
import { hasDependency } from '../../util/plugin.js';
5+
import type { AwsCdkConfig } from './types.ts';
36

47
// https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib-readme.html
58

@@ -11,17 +14,25 @@ const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependenc
1114

1215
const config: string[] = ["cdk.json"];
1316

17+
const resolveConfig: ResolveConfig<AwsCdkConfig> = async config => {
18+
if (!config) return [];
19+
20+
const app = config.app.split(" ")[0];
21+
const watch = config.watch?.include ?? [];
22+
const context = Object.keys(config.context ?? {}).map(key => key.split("/")[0]) ?? [];
23+
return compact([app, ...watch, ...context]).map(id => toDependency(id));
24+
};
25+
1426
const production: string[] = [
1527
'{src/,cdk/,}bin/**/*.{js,ts}',
1628
'{src/,cdk/,}lib/**/*.{js,ts}',
1729
];
1830

19-
20-
2131
export default {
2232
title,
2333
enablers,
2434
isEnabled,
2535
config,
36+
resolveConfig,
2637
production,
2738
} satisfies Plugin;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type AwsCdkConfig = {
2+
app: string;
3+
watch?: {
4+
include: string[];
5+
};
6+
context?: Record<string, unknown>;
7+
};

packages/knip/test/plugins/aws-cdk.test.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,14 @@ test('Find dependencies with the aws-cdk plugin', async () => {
1515

1616
console.log(issues);
1717

18-
// assert(issues.binaries['package.json']['cdk']);
19-
assert(issues.dependencies['package.json']['aws-cdk-lib']);
20-
assert(issues.dependencies['package.json']['constructs']);
21-
// assert(issues.devDependencies['package.json']['aws-cdk']);
22-
assert(issues.devDependencies['package.json']['@types/aws-cdk-lib']);
23-
assert(issues.devDependencies['package.json']['@types/constructs']);
24-
assert(issues.devDependencies['package.json']['ts-node']);
25-
// assert(issues.devDependencies['package.json']['typescript']);
18+
assert(issues.devDependencies['package.json']['aws-cdk']);
2619

2720
assert.deepEqual(counters, {
2821
...baseCounters,
29-
dependencies: 2,
30-
devDependencies: 4,
31-
processed: 0,
32-
total: 0,
22+
dependencies: 0,
23+
devDependencies: 1,
24+
unlisted: 1,
25+
processed: 3,
26+
total: 3,
3327
});
3428
});

0 commit comments

Comments
 (0)