forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.ts
1020 lines (864 loc) · 31.7 KB
/
rules.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import caseUtils = require('case');
import fs = require('fs');
import path = require('path');
import semver = require('semver');
import { LICENSE, NOTICE } from './licensing';
import { PackageJson, ValidationRule } from './packagejson';
import {
deepGet, deepSet,
expectDevDependency, expectJSON,
fileShouldBe, fileShouldContain,
findInnerPackages,
monoRepoRoot, monoRepoVersion
} from './util';
// tslint:disable-next-line: no-var-requires
const AWS_SERVICE_NAMES = require('./aws-service-official-names.json');
/**
* Verify that the package name matches the directory name
*/
export class PackageNameMatchesDirectoryName extends ValidationRule {
public readonly name = 'naming/package-matches-directory';
public validate(pkg: PackageJson): void {
const parts = pkg.packageRoot.split(path.sep);
const expectedName = parts[parts.length - 2].startsWith('@')
? parts.slice(parts.length - 2).join('/')
: parts[parts.length - 1];
expectJSON(this.name, pkg, 'name', expectedName);
}
}
/**
* Verify that all packages have a description
*/
export class DescriptionIsRequired extends ValidationRule {
public readonly name = 'package-info/require-description';
public validate(pkg: PackageJson): void {
if (!pkg.json.description) {
pkg.report({ ruleName: this.name, message: 'Description is required' });
}
}
}
/**
* Repository must be our GitHub repo
*/
export class RepositoryCorrect extends ValidationRule {
public readonly name = 'package-info/repository';
public validate(pkg: PackageJson): void {
expectJSON(this.name, pkg, 'repository.type', 'git');
expectJSON(this.name, pkg, 'repository.url', 'https://github.com/awslabs/aws-cdk.git');
const pkgDir = path.relative(monoRepoRoot(), pkg.packageRoot);
expectJSON(this.name, pkg, 'repository.directory', pkgDir);
}
}
/**
* Homepage must point to the GitHub repository page.
*/
export class HomepageCorrect extends ValidationRule {
public readonly name = 'package-info/homepage';
public validate(pkg: PackageJson): void {
expectJSON(this.name, pkg, 'homepage', 'https://github.com/awslabs/aws-cdk');
}
}
/**
* The license must be Apache-2.0.
*/
export class License extends ValidationRule {
public readonly name = 'package-info/license';
public validate(pkg: PackageJson): void {
expectJSON(this.name, pkg, 'license', 'Apache-2.0');
}
}
/**
* There must be a license file that corresponds to the Apache-2.0 license.
*/
export class LicenseFile extends ValidationRule {
public readonly name = 'license/license-file';
public validate(pkg: PackageJson): void {
fileShouldBe(this.name, pkg, 'LICENSE', LICENSE);
}
}
/**
* There must be a NOTICE file.
*/
export class NoticeFile extends ValidationRule {
public readonly name = 'license/notice-file';
public validate(pkg: PackageJson): void {
fileShouldBe(this.name, pkg, 'NOTICE', NOTICE);
}
}
/**
* Author must be AWS (as an Organization)
*/
export class AuthorAWS extends ValidationRule {
public readonly name = 'package-info/author';
public validate(pkg: PackageJson): void {
expectJSON(this.name, pkg, 'author.name', 'Amazon Web Services');
expectJSON(this.name, pkg, 'author.url', 'https://aws.amazon.com');
expectJSON(this.name, pkg, 'author.organization', true);
}
}
/**
* There must be a README.md file.
*/
export class ReadmeFile extends ValidationRule {
public readonly name = 'package-info/README.md';
public validate(pkg: PackageJson): void {
const readmeFile = path.join(pkg.packageRoot, 'README.md');
const scopes = pkg.json['cdk-build'] && pkg.json['cdk-build'].cloudformation;
if (!scopes) {
return;
}
const scope: string = typeof scopes === 'string' ? scopes : scopes[0];
const serviceName = AWS_SERVICE_NAMES[scope];
const headline = serviceName && `${serviceName} Construct Library`;
if (!fs.existsSync(readmeFile)) {
pkg.report({
ruleName: this.name,
message: 'There must be a README.md file at the root of the package',
fix: () => fs.writeFileSync(
readmeFile,
[
`## ${headline || pkg.json.description}`,
'This module is part of the[AWS Cloud Development Kit](https://github.com/awslabs/aws-cdk) project.'
].join('\n')
)
});
} else if (headline) {
const requiredFirstLine = `## ${headline}`;
const [firstLine, ...rest] = fs.readFileSync(readmeFile, { encoding: 'utf8' }).split('\n');
if (firstLine !== requiredFirstLine) {
pkg.report({
ruleName: this.name,
message: `The title of the README.md file must be "${headline}"`,
fix: () => fs.writeFileSync(readmeFile, [requiredFirstLine, ...rest].join('\n')),
});
}
}
}
}
/**
* There must be a stability setting, and that the appropriate banner is present in the README.md file.
*/
export class StabilitySetting extends ValidationRule {
public readonly name = 'package-info/stability';
public validate(pkg: PackageJson): void {
if (pkg.json.private) {
// Does not apply to private packages!
return;
}
let stability = pkg.json.stability;
switch (stability) {
case 'experimental':
case 'stable':
case 'deprecated':
if (pkg.json.deprecated && stability !== 'deprecated') {
pkg.report({
ruleName: this.name,
message: `Package is deprecated, but is marked with stability "${stability}"`,
fix: () => pkg.json.stability = 'deprecated',
});
stability = 'deprecated';
}
break;
default:
const defaultStability = pkg.json.deprecated ? 'deprecated' : 'experimental';
pkg.report({
ruleName: this.name,
message: `Invalid stability configuration in package.json: ${JSON.stringify(stability)}`,
fix: () => pkg.json.stability = defaultStability,
});
stability = defaultStability;
}
this.validateReadmeHasBanner(pkg, stability);
}
private validateReadmeHasBanner(pkg: PackageJson, stability: string) {
const badge = this.readmeBadge(stability);
if (!badge) {
// Somehow, we don't have a badge for this stability level
return;
}
const readmeFile = path.join(pkg.packageRoot, 'README.md');
if (!fs.existsSync(readmeFile)) {
// Presence of the file is asserted by another rule
return;
}
const readmeContent = fs.readFileSync(readmeFile, { encoding: 'utf8' });
const badgeRegex = new RegExp(badge.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\w+/g, '\\w+'));
if (!badgeRegex.test(readmeContent)) {
// Removing a possible old, now invalid stability indication from the README.md before adding a new one
const [title, ...body] = readmeContent.replace(/<!--BEGIN STABILITY BANNER-->(?:.|\n)+<!--END STABILITY BANNER-->\n+/m, '').split('\n');
pkg.report({
ruleName: this.name,
message: `Missing stability banner for ${stability} in README.md file`,
fix: () => fs.writeFileSync(readmeFile, [title, badge, ...body].join('\n')),
});
}
}
private readmeBadge(stability: string) {
switch (stability) {
case 'deprecated':
return _div(
{ label: 'Deprecated', color: 'critical' },
'This API may emit warnings. Backward compatibility is not guaranteed.',
);
case 'experimental':
return _div(
{ label: 'Experimental', color: 'important' },
'**This is a _developer preview_ (public beta) module. Releases might lack important features and might have',
'future breaking changes.**',
'',
'This API is still under active development and subject to non-backward',
'compatible changes or removal in any future version. Use of the API is not recommended in production',
'environments. Experimental APIs are not subject to the Semantic Versioning model.',
);
case 'stable':
return _div(
{ label: 'Stable', color: 'success' },
'This API is subject to the Semantic Versioning model. It will not be subject to',
'non-backward compatible changes or removal in a subsequent patch or feature release.'
);
default:
return undefined;
}
function _div(badge: { label: string, color: string }, ...messages: string[]) {
return [
'<!--BEGIN STABILITY BANNER-->',
'',
'---',
'',
`![Stability: ${badge.label}](https://img.shields.io/badge/stability-${badge.label}-${badge.color}.svg?style=for-the-badge)`,
'',
...messages.map(message => `> ${message}`),
'',
'---',
'<!--END STABILITY BANNER-->',
'',
].join('\n');
}
}
}
/**
* Keywords must contain CDK keywords and be sorted
*/
export class CDKKeywords extends ValidationRule {
public readonly name = 'package-info/keywords';
public validate(pkg: PackageJson): void {
if (!pkg.json.keywords) {
pkg.report({
ruleName: this.name,
message: 'Must have keywords',
fix: () => { pkg.json.keywords = []; }
});
}
const keywords = pkg.json.keywords || [];
if (keywords.indexOf('cdk') === -1) {
pkg.report({
ruleName: this.name,
message: 'Keywords must mention CDK',
fix: () => { pkg.json.keywords.splice(0, 0, 'cdk'); }
});
}
if (keywords.indexOf('aws') === -1) {
pkg.report({
ruleName: this.name,
message: 'Keywords must mention AWS',
fix: () => { pkg.json.keywords.splice(0, 0, 'aws'); }
});
}
}
}
/**
* JSII Java package is required and must look sane
*/
export class JSIIJavaPackageIsRequired extends ValidationRule {
public readonly name = 'jsii/java';
public validate(pkg: PackageJson): void {
if (!isJSII(pkg)) { return; }
const moduleName = cdkModuleName(pkg.json.name);
expectJSON(this.name, pkg, 'jsii.targets.java.maven.groupId', 'software.amazon.awscdk');
expectJSON(this.name, pkg, 'jsii.targets.java.maven.artifactId', moduleName.mavenArtifactId, /-/g);
const java = deepGet(pkg.json, ['jsii', 'targets', 'java', 'package']) as string | undefined;
expectJSON(this.name, pkg, 'jsii.targets.java.package', moduleName.javaPackage, /\./g);
if (java) {
const expectedPrefix = moduleName.javaPackage.split('.').slice(0, 3).join('.');
const actualPrefix = java.split('.').slice(0, 3).join('.');
if (expectedPrefix !== actualPrefix) {
pkg.report({
ruleName: this.name,
message: `JSII "java" package must share the first 3 elements of the expected one: ${expectedPrefix} vs ${actualPrefix}`,
fix: () => deepSet(pkg.json, ['jsii', 'targets', 'java', 'package'], moduleName.javaPackage)
});
}
}
}
}
export class JSIIPythonTarget extends ValidationRule {
public readonly name = 'jsii/python';
public validate(pkg: PackageJson): void {
if (!isJSII(pkg)) { return; }
const moduleName = cdkModuleName(pkg.json.name);
expectJSON(this.name, pkg, 'jsii.targets.python.distName', moduleName.python.distName);
expectJSON(this.name, pkg, 'jsii.targets.python.module', moduleName.python.module);
}
}
export class AWSDocsMetadata extends ValidationRule {
public readonly name = 'jsii/awsdocs';
public validate(pkg: PackageJson): void {
const scopes = pkg.json['cdk-build'] && pkg.json['cdk-build'].cloudformation;
if (!scopes) {
return;
}
const scope: string = typeof scopes === 'string' ? scopes : scopes[0];
const serviceName = AWS_SERVICE_NAMES[scope];
const title = deepGet(pkg.json, ['jsii', 'metadata', 'awsdocs:title']);
if (!title || (serviceName && serviceName !== title)) {
pkg.report({
ruleName: this.name,
message: [
`JSII packages bound to a CloudFormation scope must have the awsdocs:title JSII metadata entry.`,
`Service names are recorded in ${require.resolve('./aws-service-official-names.json')}`,
].join('\n'),
fix: serviceName ? () => deepSet(pkg.json, ['jsii', 'metadata', 'awsdocs:title'], serviceName) : undefined
});
}
}
}
export class CDKPackage extends ValidationRule {
public readonly name = 'package-info/scripts/package';
public validate(pkg: PackageJson): void {
// skip private packages
if (pkg.json.private) { return; }
const merkleMarker = '.LAST_PACKAGE';
expectJSON(this.name, pkg, 'scripts.package', 'cdk-package');
const outdir = 'dist';
// if this is
if (isJSII(pkg)) {
expectJSON(this.name, pkg, 'jsii.outdir', outdir);
}
fileShouldContain(this.name, pkg, '.npmignore', outdir);
fileShouldContain(this.name, pkg, '.gitignore', outdir);
fileShouldContain(this.name, pkg, '.npmignore', merkleMarker);
fileShouldContain(this.name, pkg, '.gitignore', merkleMarker);
}
}
export class NoTsBuildInfo extends ValidationRule {
public readonly name = 'no-tsc-build-info';
public validate(pkg: PackageJson): void {
// skip private packages
if (pkg.json.private) { return; }
// Stop 'tsconfig.tsbuildinfo' and regular '.tsbuildinfo' files from being
// published to NPM.
// We might at some point also want to strip tsconfig.json but for now,
// the TypeScript DOCS BUILD needs to it to load the typescript source.
fileShouldContain(this.name, pkg, '.npmignore', '*.tsbuildinfo');
}
}
/**
* Verifies there is no dependency on "jsii" since it's defined at the repo
* level.
*/
export class NoJsiiDep extends ValidationRule {
public readonly name = 'dependencies/no-jsii';
public validate(pkg: PackageJson): void {
const predicate = (s: string) => s.startsWith('jsii');
if (pkg.getDevDependency(predicate)) {
pkg.report({
ruleName: this.name,
message: 'packages should not have a devDep on jsii since it is defined at the repo level',
fix: () => pkg.removeDevDependency(predicate)
});
}
}
}
/**
* Verifies that the expected versions of node will be supported.
*/
export class NodeCompatibility extends ValidationRule {
public readonly name = 'dependencies/node-version';
public validate(pkg: PackageJson): void {
const atTypesNode = pkg.getDevDependency('@types/node');
if (atTypesNode && !atTypesNode.startsWith('^8.')) {
pkg.report({
ruleName: this.name,
message: `packages must support node version 8 and up, but ${atTypesNode} is declared`,
fix: () => pkg.addDevDependency('@types/node', '^8.10.38')
});
}
}
}
/**
* Verifies that the ``@types/`` dependencies are correctly recorded in ``devDependencies`` and not ``dependencies``.
*/
export class NoAtTypesInDependencies extends ValidationRule {
public readonly name = 'dependencies/at-types';
public validate(pkg: PackageJson): void {
const predicate = (s: string) => s.startsWith('@types/');
for (const dependency of pkg.getDependencies(predicate)) {
pkg.report({
ruleName: this.name,
message: `dependency on ${dependency.name}@${dependency.version} must be in devDependencies`,
fix: () => {
pkg.addDevDependency(dependency.name, dependency.version);
pkg.removeDependency(predicate);
}
});
}
}
}
/**
* Computes the module name for various other purposes (java package, ...)
*/
function cdkModuleName(name: string) {
const isCdkPkg = name === '@aws-cdk/cdk';
name = name.replace(/^aws-cdk-/, '');
name = name.replace(/^@aws-cdk\//, '');
const dotnetSuffix = name.split('-')
.map(s => s === 'aws' ? 'AWS' : caseUtils.pascal(s))
.join('.');
const pythonName = name.replace(/^@/g, "").replace(/\//g, ".").split(".").map(caseUtils.kebab).join(".");
return {
javaPackage: `software.amazon.awscdk${isCdkPkg ? '' : `.${name.replace(/^aws-/, 'services-').replace(/-/g, '.')}`}`,
mavenArtifactId:
isCdkPkg ? 'cdk'
: name.startsWith('aws-') || name.startsWith('alexa-') ? name.replace(/^aws-/, '')
: `cdk-${name}`,
dotnetNamespace: `Amazon.CDK${isCdkPkg ? '' : `.${dotnetSuffix}`}`,
python: {
distName: `aws-cdk.${pythonName}`,
module: `aws_cdk.${pythonName.replace(/-/g, "_")}`,
},
};
}
/**
* JSII .NET namespace is required and must look sane
*/
export class JSIIDotNetNamespaceIsRequired extends ValidationRule {
public readonly name = 'jsii/dotnet';
public validate(pkg: PackageJson): void {
if (!isJSII(pkg)) { return; }
const dotnet = deepGet(pkg.json, ['jsii', 'targets', 'dotnet', 'namespace']) as string | undefined;
const moduleName = cdkModuleName(pkg.json.name);
expectJSON(this.name, pkg, 'jsii.targets.dotnet.namespace', moduleName.dotnetNamespace, /\./g, /*case insensitive*/ true);
if (dotnet) {
const actualPrefix = dotnet.split('.').slice(0, 2).join('.');
const expectedPrefix = moduleName.dotnetNamespace.split('.').slice(0, 2).join('.');
if (actualPrefix !== expectedPrefix) {
pkg.report({
ruleName: this.name,
message: `.NET namespace must share the first two segments of the default namespace, '${expectedPrefix}' vs '${actualPrefix}'`,
fix: () => deepSet(pkg.json, ['jsii', 'targets', 'dotnet', 'namespace'], moduleName.dotnetNamespace)
});
}
}
}
}
/**
* Strong-naming all .NET assemblies is required.
*/
export class JSIIDotNetStrongNameIsRequired extends ValidationRule {
public readonly name = 'jsii/dotnet/strong-name';
public validate(pkg: PackageJson): void {
if (!isJSII(pkg)) { return; }
const signAssembly = deepGet(pkg.json, ['jsii', 'targets', 'dotnet', 'signAssembly']) as boolean | undefined;
const signAssemblyExpected = true;
if (signAssembly !== signAssemblyExpected) {
pkg.report({
ruleName: this.name,
message: `.NET packages must have strong-name signing enabled.`,
fix: () => deepSet(pkg.json, ['jsii', 'targets', 'dotnet', 'signAssembly'], signAssemblyExpected)
});
}
const assemblyOriginatorKeyFile = deepGet(pkg.json, ['jsii', 'targets', 'dotnet', 'assemblyOriginatorKeyFile']) as string | undefined;
const assemblyOriginatorKeyFileExpected = "../../key.snk";
if (assemblyOriginatorKeyFile !== assemblyOriginatorKeyFileExpected) {
pkg.report({
ruleName: this.name,
message: `.NET packages must use the strong name key fetched by fetch-dotnet-snk.sh`,
fix: () => deepSet(pkg.json, ['jsii', 'targets', 'dotnet', 'assemblyOriginatorKeyFile'], assemblyOriginatorKeyFileExpected)
});
}
}
}
/**
* The package must depend on cdk-build-tools
*/
export class MustDependOnBuildTools extends ValidationRule {
public readonly name = 'dependencies/build-tools';
public validate(pkg: PackageJson): void {
if (!shouldUseCDKBuildTools(pkg)) { return; }
expectDevDependency(this.name, pkg, 'cdk-build-tools', '^' + monoRepoVersion());
}
}
/**
* Build script must be 'cdk-build'
*/
export class MustUseCDKBuild extends ValidationRule {
public readonly name = 'package-info/scripts/build';
public validate(pkg: PackageJson): void {
if (!shouldUseCDKBuildTools(pkg)) { return; }
expectJSON(this.name, pkg, 'scripts.build', 'cdk-build');
// cdk-build will write a hash file that we have to ignore.
const merkleMarker = '.LAST_BUILD';
fileShouldContain(this.name, pkg, '.gitignore', merkleMarker);
fileShouldContain(this.name, pkg, '.npmignore', merkleMarker);
}
}
/**
* Dependencies in both regular and peerDependencies must agree in semver
*
* In particular, verify that depVersion satisfies peerVersion. This prevents
* us from instructing NPM to construct impossible closures, where we say:
*
* peerDependency: A@1.0.0
* dependency: A@2.0.0
*
* There is no version of A that would satisfy this.
*
* The other way around is not necessary--the depVersion can be bumped without
* bumping the peerVersion (if the API didn't change this may be perfectly
* valid). This prevents us from restricting a user's potential combinations of
* libraries unnecessarily.
*/
export class RegularDependenciesMustSatisfyPeerDependencies extends ValidationRule {
public readonly name = 'dependencies/peer-dependencies-satisfied';
public validate(pkg: PackageJson): void {
for (const [depName, peerVersion] of Object.entries(pkg.peerDependencies)) {
const depVersion = pkg.dependencies[depName];
if (depVersion === undefined) { continue; }
// Make sure that depVersion satisfies peerVersion.
if (!semver.intersects(depVersion, peerVersion)) {
pkg.report({
ruleName: this.name,
message: `dependency ${depName}: concrete version ${depVersion} does not match peer version '${peerVersion}'`,
fix: () => pkg.addPeerDependency(depName, depVersion)
});
}
}
}
}
export class MustIgnoreSNK extends ValidationRule {
public readonly name = 'ignore/strong-name-key';
public validate(pkg: PackageJson): void {
fileShouldContain(this.name, pkg, '.npmignore', '*.snk');
fileShouldContain(this.name, pkg, '.gitignore', '*.snk');
}
}
export class NpmIgnoreForJsiiModules extends ValidationRule {
public readonly name = 'ignore/jsii';
public validate(pkg: PackageJson): void {
if (!isJSII(pkg)) { return; }
fileShouldContain(this.name, pkg, '.npmignore',
'*.ts',
'!*.d.ts',
'!*.js',
'coverage',
'.nyc_output',
'*.tgz',
);
}
}
/**
* nodeunit and @types/nodeunit must appear in devDependencies if
* the test script uses "nodeunit"
*/
export class GlobalDevDependencies extends ValidationRule {
public readonly name = 'dependencies/global-dev';
public validate(pkg: PackageJson): void {
const deps = [
'typescript',
'tslint',
'nodeunit',
'@types/nodeunit',
'@types/node',
'nyc'
];
for (const dep of deps) {
if (pkg.getDevDependency(dep)) {
pkg.report({
ruleName: this.name,
message: `devDependency ${dep} is defined at the repo level`,
fix: () => pkg.removeDevDependency(dep)
});
}
}
}
}
/**
* Must use 'cdk-watch' command
*/
export class MustUseCDKWatch extends ValidationRule {
public readonly name = 'package-info/scripts/watch';
public validate(pkg: PackageJson): void {
if (!shouldUseCDKBuildTools(pkg)) { return; }
expectJSON(this.name, pkg, 'scripts.watch', 'cdk-watch');
}
}
/**
* Must use 'cdk-test' command
*/
export class MustUseCDKTest extends ValidationRule {
public readonly name = 'package-info/scripts/test';
public validate(pkg: PackageJson): void {
if (!shouldUseCDKBuildTools(pkg)) { return; }
if (!hasTestDirectory(pkg)) { return; }
expectJSON(this.name, pkg, 'scripts.test', 'cdk-test');
// 'cdk-test' will calculate coverage, so have the appropriate
// files in .gitignore.
fileShouldContain(this.name, pkg, '.gitignore', '.nyc_output');
fileShouldContain(this.name, pkg, '.gitignore', 'coverage');
fileShouldContain(this.name, pkg, '.gitignore', '.nycrc');
}
}
/**
* Must declare minimum node version
*/
export class MustHaveNodeEnginesDeclaration extends ValidationRule {
public readonly name = 'package-info/engines';
public validate(pkg: PackageJson): void {
expectJSON(this.name, pkg, 'engines.node', '>= 8.10.0');
}
}
/**
* Scripts that run integ tests must also have the individual 'integ' script to update them
*
* This commands comes from the dev-dependency cdk-integ-tools.
*/
export class MustHaveIntegCommand extends ValidationRule {
public readonly name = 'package-info/scripts/integ';
public validate(pkg: PackageJson): void {
if (!hasIntegTests(pkg)) { return; }
expectJSON(this.name, pkg, 'scripts.integ', 'cdk-integ');
expectDevDependency(this.name, pkg, 'cdk-integ-tools', '^' + monoRepoVersion());
}
}
export class PkgLintAsScript extends ValidationRule {
public readonly name = 'package-info/scripts/pkglint';
public validate(pkg: PackageJson): void {
const script = 'pkglint -f';
expectDevDependency(this.name, pkg, 'pkglint', '^' + monoRepoVersion());
if (!pkg.npmScript('pkglint')) {
pkg.report({
ruleName: this.name,
message: 'a script called "pkglint" must be included to allow fixing package linting issues',
fix: () => pkg.changeNpmScript('pkglint', () => script)
});
}
if (pkg.npmScript('pkglint') !== script) {
pkg.report({
ruleName: this.name,
message: 'the pkglint script should be: ' + script,
fix: () => pkg.changeNpmScript('pkglint', () => script)
});
}
}
}
export class NoStarDeps extends ValidationRule {
public readonly name = 'dependencies/no-star';
public validate(pkg: PackageJson) {
reportStarDeps(this.name, pkg.json.depedencies);
reportStarDeps(this.name, pkg.json.devDependencies);
function reportStarDeps(ruleName: string, deps?: any) {
deps = deps || {};
Object.keys(deps).forEach(d => {
if (deps[d] === '*') {
pkg.report({
ruleName,
message: `star dependency not allowed for ${d}`
});
}
});
}
}
}
interface VersionCount {
version: string;
count: number;
}
/**
* All consumed versions of dependencies must be the same
*
* NOTE: this rule will only be useful when validating multiple package.jsons at the same time
*/
export class AllVersionsTheSame extends ValidationRule {
public readonly name = 'dependencies/versions-consistent';
private readonly ourPackages: {[pkg: string]: string} = {};
private readonly usedDeps: {[pkg: string]: VersionCount[]} = {};
public prepare(pkg: PackageJson): void {
this.ourPackages[pkg.json.name] = "^" + pkg.json.version;
this.recordDeps(pkg.json.dependencies);
this.recordDeps(pkg.json.devDependencies);
}
public validate(pkg: PackageJson): void {
this.validateDeps(pkg, 'dependencies');
this.validateDeps(pkg, 'devDependencies');
}
private recordDeps(deps: {[pkg: string]: string} | undefined) {
if (!deps) { return; }
Object.keys(deps).forEach(dep => {
this.recordDep(dep, deps[dep]);
});
}
private validateDeps(pkg: PackageJson, section: string) {
if (!pkg.json[section]) { return; }
Object.keys(pkg.json[section]).forEach(dep => {
this.validateDep(pkg, section, dep);
});
}
private recordDep(dep: string, version: string) {
if (version === '*') {
// '*' does not give us info, so skip
return;
}
if (!(dep in this.usedDeps)) {
this.usedDeps[dep] = [];
}
const i = this.usedDeps[dep].findIndex(vc => vc.version === version);
if (i === -1) {
this.usedDeps[dep].push({ version, count: 1 });
} else {
this.usedDeps[dep][i].count += 1;
}
}
private validateDep(pkg: PackageJson, depField: string, dep: string) {
if (dep in this.ourPackages) {
expectJSON(this.name, pkg, depField + '.' + dep, this.ourPackages[dep]);
return;
}
// Otherwise, must match the majority version declaration. Might be empty if we only
// have '*', in which case that's fine.
if (!(dep in this.usedDeps)) { return; }
const versions = this.usedDeps[dep];
versions.sort((a, b) => b.count - a.count);
expectJSON(this.name, pkg, depField + '.' + dep, versions[0].version);
}
}
export class AwsLint extends ValidationRule {
public readonly name = 'awslint';
public validate(pkg: PackageJson) {
if (!isJSII(pkg)) {
return;
}
if (!isAWS(pkg)) {
return;
}
expectJSON(this.name, pkg, 'scripts.awslint', 'cdk-awslint');
}
}
export class Cfn2Ts extends ValidationRule {
public readonly name = 'cfn2ts';
public validate(pkg: PackageJson) {
if (!isJSII(pkg)) {
return;
}
if (!isAWS(pkg)) {
return;
}
expectJSON(this.name, pkg, 'scripts.cfn2ts', 'cfn2ts');
}
}
export class JestCoverageTarget extends ValidationRule {
public readonly name = 'jest-coverage-target';
public validate(pkg: PackageJson) {
if (pkg.json.jest) {
// We enforce the key exists, but the value is just a default
const defaults: { [key: string]: number } = {
branches: 80,
statements: 80
};
for (const key of Object.keys(defaults)) {
const deepPath = ['coverageThreshold', 'global', key];
const setting = deepGet(pkg.json.jest, deepPath);
if (setting == null) {
pkg.report({
ruleName: this.name,
message: `When jest is used, jest.coverageThreshold.global.${key} must be set`,
fix: () => {
deepSet(pkg.json.jest, deepPath, defaults[key]);
},
});
}
}
}
}
}
/**
* Packages inside JSII packages (typically used for embedding Lambda handles)
* must only have dev dependencies and their node_modules must have been
* blacklisted for publishing
*
* We might loosen this at some point but we'll have to bundle all runtime dependencies
* and we don't have good transitive license checks.
*/
export class PackageInJsiiPackageNoRuntimeDeps extends ValidationRule {
public readonly name = 'lambda-packages-no-runtime-deps';
public validate(pkg: PackageJson) {
if (!isJSII(pkg)) { return; }
for (const inner of findInnerPackages(pkg.packageRoot)) {
const innerPkg = PackageJson.fromDirectory(inner);
if (Object.keys(innerPkg.dependencies).length > 0) {
pkg.report({
ruleName: `${this.name}:1`,
message: `NPM Package '${innerPkg.packageName}' inside jsii package can only have devDepencencies`
});
}
const nodeModulesRelPath = path.relative(pkg.packageRoot, innerPkg.packageRoot) + '/node_modules';
fileShouldContain(`${this.name}:2`, pkg, '.npmignore', nodeModulesRelPath);
}
}
}
/**
* Requires packages to have fast-fail build scripts, allowing to combine build, test and package in a single command.
* This involves two targets: `build+test:pack` and `build+test` (to skip the pack).
*/
export class FastFailingBuildScripts extends ValidationRule {
public readonly name = 'fast-failing-build-scripts';
public validate(pkg: PackageJson) {
const scripts = pkg.json.scripts || {};
const hasTest = 'test' in scripts;
const hasPack = 'package' in scripts;
const cmdBuild = 'npm run build';
expectJSON(this.name, pkg, 'scripts.build+test', hasTest ? [cmdBuild, 'npm test'].join(' && ') : cmdBuild);
const cmdBuildTest = 'npm run build+test';
expectJSON(this.name, pkg, 'scripts.build+test+package', hasPack ? [cmdBuildTest, 'npm run package'].join(' && ') : cmdBuildTest);
}
}
/**
* Determine whether this is a JSII package
*
* A package is a JSII package if there is 'jsii' section in the package.json
*/
function isJSII(pkg: PackageJson): boolean {
return pkg.json.jsii;
}
/**
* Indicates that this is an "AWS" package (i.e. that it it has a cloudformation source)
* @param pkg
*/
function isAWS(pkg: PackageJson): boolean {
return pkg.json['cdk-build'] && pkg.json['cdk-build'].cloudformation;
}
/**
* Determine whether the package has tests
*
* A package has tests if the root/test directory exists
*/
function hasTestDirectory(pkg: PackageJson) {
return fs.existsSync(path.join(pkg.packageRoot, 'test'));
}