@@ -21,8 +21,24 @@ const ts = require('typescript');
21
21
const path = require ( 'path' ) ;
22
22
23
23
async function checkDeps ( ) {
24
- const root = path . normalize ( path . join ( __dirname , '..' , 'packages' , 'playwright-core' ) ) ;
25
- const src = path . normalize ( path . join ( __dirname , '..' , 'packages' , 'playwright-core' , 'src' ) ) ;
24
+ const packages = path . normalize ( path . join ( __dirname , '..' , 'packages' ) ) ;
25
+ const corePackageJson = await innerCheckDeps ( path . join ( packages , 'playwright-core' ) , true ) ;
26
+ const testPackageJson = await innerCheckDeps ( path . join ( packages , 'playwright-test' ) , false ) ;
27
+
28
+ let hasVersionMismatch = false ;
29
+ for ( const [ key , value ] of Object . entries ( corePackageJson . dependencies ) ) {
30
+ const value2 = testPackageJson . dependencies [ key ] ;
31
+ if ( value2 && value2 !== value ) {
32
+ hasVersionMismatch = true ;
33
+ console . log ( `Dependency version mismatch ${ key } : ${ value } != ${ value2 } ` ) ;
34
+ }
35
+ }
36
+ process . exit ( hasVersionMismatch ? 1 : 0 ) ;
37
+ }
38
+
39
+ async function innerCheckDeps ( root , checkDepsFile ) {
40
+ const deps = new Set ( ) ;
41
+ const src = path . join ( root , 'src' ) ;
26
42
const packageJSON = require ( path . join ( root , 'package.json' ) ) ;
27
43
const program = ts . createProgram ( {
28
44
options : {
@@ -40,23 +56,46 @@ async function checkDeps() {
40
56
if ( ! usedDeps . has ( key ) && DEPS [ key ] . length )
41
57
errors . push ( `Stale DEPS entry "${ key } "` ) ;
42
58
}
43
- for ( const error of errors )
44
- console . log ( error ) ;
45
- if ( errors . length ) {
59
+ if ( checkDepsFile && errors . length ) {
60
+ for ( const error of errors )
61
+ console . log ( error ) ;
46
62
console . log ( `--------------------------------------------------------` ) ;
47
63
console . log ( `Changing the project structure or adding new components?` ) ;
48
64
console . log ( `Update DEPS in ./${ path . relative ( root , __filename ) } ` ) ;
49
65
console . log ( `--------------------------------------------------------` ) ;
66
+ process . exit ( 1 ) ;
50
67
}
51
- process . exit ( errors . length ? 1 : 0 ) ;
68
+
69
+ for ( const dep of deps ) {
70
+ const resolved = require . resolve ( dep ) ;
71
+ if ( dep === resolved || ! resolved . includes ( 'node_modules' ) )
72
+ deps . delete ( dep ) ;
73
+ }
74
+ for ( const dep of Object . keys ( packageJSON . dependencies ) )
75
+ deps . delete ( dep ) ;
76
+
77
+ if ( deps . size ) {
78
+ console . log ( 'Dependencies are not declared in package.json:' ) ;
79
+ for ( const dep of deps )
80
+ console . log ( ` ${ dep } ` ) ;
81
+ process . exit ( 1 ) ;
82
+ }
83
+
84
+ return packageJSON ;
52
85
53
86
function visit ( node , fileName ) {
54
87
if ( ts . isImportDeclaration ( node ) && ts . isStringLiteral ( node . moduleSpecifier ) ) {
55
88
const importName = node . moduleSpecifier . text ;
89
+ if ( ! importName . startsWith ( '.' ) && ! node . importClause . isTypeOnly && ! fileName . includes ( path . sep + 'web' + path . sep ) ) {
90
+ if ( importName . startsWith ( '@' ) )
91
+ deps . add ( importName . split ( '/' ) . slice ( 0 , 2 ) . join ( '/' ) ) ;
92
+ else
93
+ deps . add ( importName . split ( '/' ) [ 0 ] ) ;
94
+ }
56
95
const importPath = path . resolve ( path . dirname ( fileName ) , importName ) + '.ts' ;
57
- if ( ! allowImport ( fileName , importPath ) )
96
+ if ( checkDepsFile && ! allowImport ( fileName , importPath ) )
58
97
errors . push ( `Disallowed import from ${ path . relative ( root , fileName ) } to ${ path . relative ( root , importPath ) } ` ) ;
59
- if ( ! allowExternalImport ( fileName , importPath , importName ) )
98
+ if ( checkDepsFile && ! allowExternalImport ( fileName , importPath , importName ) )
60
99
errors . push ( `Disallowed external dependency ${ importName } from ${ path . relative ( root , fileName ) } ` ) ;
61
100
}
62
101
ts . forEachChild ( node , x => visit ( x , fileName ) ) ;
0 commit comments