-
Notifications
You must be signed in to change notification settings - Fork 27
/
check-dependencies.js
executable file
·131 lines (103 loc) · 3.78 KB
/
check-dependencies.js
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
#!/usr/bin/env node
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
'use strict';
const fs = require( 'fs' );
const path = require( 'path' );
const glob = require( 'glob' );
const depCheck = require( 'depcheck' );
const chalk = require( 'chalk' );
const { table, getBorderCharacters } = require( 'table' );
const cwd = process.cwd();
const packageJson = require( path.join( cwd, 'package.json' ) );
const tableData = [
[
chalk.yellow( 'Invalid itself imports' ),
chalk.red( 'Missing dependencies' ),
chalk.red( 'Missing devDependencies' ),
chalk.cyan( 'Unused dependencies' ),
chalk.cyan( 'Unused devDependencies' ),
]
];
const depCheckOptions = {
ignoreDirs: [ 'docs', 'build' ],
ignoreMatches: [ 'eslint', 'husky', 'lint-staged', 'webpack-cli' ]
};
if ( Array.isArray( packageJson.depcheckIgnore ) ) {
depCheckOptions.ignoreMatches.push( ...packageJson.depcheckIgnore );
}
console.log( 'Checking dependencies...' );
depCheck( cwd, depCheckOptions )
.then( unused => {
const missingPackages = groupMissingPackages( unused.missing, packageJson.name );
const tableRow = [
[ ...getInvalidItselfImports( cwd ) ].join( '\n' ),
missingPackages.dependencies.join( '\n' ),
missingPackages.devDependencies.join( '\n' ),
unused.dependencies.join( '\n' ),
unused.devDependencies.filter( packageName => !unused.dependencies.includes( packageName ) ).join( '\n' ),
];
const hasErrors = tableRow.some( entry => !!entry );
if ( !hasErrors ) {
console.log( chalk.green( 'All dependencies are defined correctly.' ) );
return;
}
tableData.push( tableRow );
console.log( chalk.red( 'Found some issue with dependencies.\n' ) );
const preparedTable = table( tableData, {
border: getBorderCharacters( 'norc' )
} );
console.log( preparedTable );
process.exit( -1 );
} );
/**
* Returns a Set that contains list of files that import modules using full package name instead of relative path.
*
* @param repositoryPath An absolute path to the directory which should be ckecked.
* @returns {Set<String>}
*/
function getInvalidItselfImports( repositoryPath ) {
const packageJson = require( path.join( repositoryPath, 'package.json' ) );
const globPattern = path.join( repositoryPath, '@(src|tests)/**/*.js' );
const invalidImportsItself = new Set();
for ( const filePath of glob.sync( globPattern ) ) {
const fileContent = fs.readFileSync( filePath, 'utf-8' );
const matchedImports = fileContent.match( /^import[^;]+from '(@ckeditor\/[^/]+)[^']+';/mg );
if ( !matchedImports ) {
continue;
}
matchedImports
.map( importLine => importLine.match( /(@ckeditor\/[^/]+)/ ) )
.filter( matchedImport => !!matchedImport )
.forEach( matchedImport => {
// Current package should use relative links to itself.
if ( packageJson.name === matchedImport[ 1 ] ) {
invalidImportsItself.add( filePath.replace( repositoryPath + '/', '' ) );
}
} );
}
return invalidImportsItself;
}
/**
* Groups missing dependencies returned by `depcheck` as `dependencies` or `devDependencies`.
*
* @param {Object} missingPackages The `missing` value from object returned by `depcheck`.
* @param {String} currentPackage Name of current package.
* @returns {Object.<String, Array.<String>>}
*/
function groupMissingPackages( missingPackages, currentPackage ) {
delete missingPackages[ currentPackage ];
const dependencies = [];
const devDependencies = [];
for ( const packageName of Object.keys( missingPackages ) ) {
const isDevDependency = missingPackages[ packageName ].every( absolutePath => absolutePath.match( /tests/ ) );
if ( isDevDependency ) {
devDependencies.push( packageName );
} else {
dependencies.push( packageName );
}
}
return { dependencies, devDependencies };
}