-
Notifications
You must be signed in to change notification settings - Fork 812
/
Copy pathdependencyAnalysis.js
188 lines (166 loc) · 5.55 KB
/
dependencyAnalysis.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
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
import fs from 'fs/promises';
import { glob } from 'glob';
/**
* Collect project dependencies.
*
* @param {string} root - Monorepo root directory.
* @param {string|null} extra - Extra deps to include, "build" or "test".
* @param {boolean} noDev - Exclude dev dependencies.
* @return {Map} Key is the project slug, value is a Set of slugs depended on.
*/
export async function getDependencies( root, extra = null, noDev = false ) {
const ret = new Map();
// Collect all project slugs.
ret.set( 'monorepo', new Set() );
for ( const file of await glob( 'projects/*/*/composer.json', { cwd: root } ) ) {
ret.set( file.substring( 9, file.length - 14 ), new Set() );
}
// Collect package name→slug mappings.
const packageMap = new Map();
for ( const file of await glob( 'projects/packages/*/composer.json', {
cwd: root,
} ) ) {
const slug = file.substring( 9, file.length - 14 );
if ( ! ret.has( slug ) ) {
// Not an actual project (should never happen here, but...).
continue;
}
const json = JSON.parse( await fs.readFile( `${ root }/${ file }`, { encoding: 'utf8' } ) );
if ( json.name ) {
packageMap.set( json.name, slug );
}
}
// Collect js-package name→slug mappings.
const jsPackageMap = new Map();
for ( const file of await glob( 'projects/js-packages/*/package.json', {
cwd: root,
} ) ) {
const slug = file.substring( 9, file.length - 13 );
if ( ! ret.has( slug ) ) {
// Not an actual project.
continue;
}
const json = JSON.parse( await fs.readFile( `${ root }/${ file }`, { encoding: 'utf8' } ) );
if ( json.name ) {
jsPackageMap.set( json.name, slug );
}
}
// Collect dependencies.
for ( const [ slug, depset ] of ret.entries() ) {
const path = slug === 'monorepo' ? root : `${ root }/projects/${ slug }`;
let deps = [];
// Collect composer require, require-dev, and .extra.dependencies.
const composerJson = JSON.parse(
await fs.readFile( path + '/composer.json', { encoding: 'utf8' } )
);
for ( const [ pkg, pkgslug ] of packageMap.entries() ) {
if (
composerJson.require?.[ pkg ] ||
( composerJson[ 'require-dev' ]?.[ pkg ] && ! noDev )
) {
deps.push( pkgslug );
}
}
if ( extra && composerJson.extra?.dependencies?.[ extra ] ) {
deps.push( ...composerJson.extra.dependencies[ extra ] );
}
// Collect JS dependencies and devDependencies.
if ( ( await fs.access( path + '/package.json' ).catch( () => false ) ) !== false ) {
const packageJson = JSON.parse(
await fs.readFile( path + '/package.json', { encoding: 'utf8' } )
);
for ( const [ pkg, pkgslug ] of jsPackageMap.entries() ) {
if (
packageJson.dependencies?.[ pkg ] ||
( packageJson.devDependencies?.[ pkg ] && ! noDev )
) {
deps.push( pkgslug );
}
}
}
// Remove any test-only dependencies, unless test dependencies were requested.
if ( extra !== 'test' && composerJson.extra?.dependencies?.[ 'test-only' ] ) {
const undeps = new Set( composerJson.extra?.dependencies?.[ 'test-only' ] );
deps = deps.filter( v => ! undeps.has( v ) );
}
// Sort the dependencies and put them in the set.
deps.sort().forEach( d => depset.add( d ) );
}
return ret;
}
/**
* Filter dependencies to a set of projects.
*
* @param {Map} deps - Dependencies.
* @param {string[]} projects - Projects to include.
* @param {object} options - Options.
* @param {boolean} options.dependencies - Keep the dependencies of the specified projects too.
* @param {boolean} options.dependents - Keep the dependents of the specified projects too.
* @return {Map} Filtered dependencies.
*/
export function filterDeps( deps, projects, options = {} ) {
const keep = new Set( projects );
// Apply options.dependencies and options.dependents until there is no further change.
let l = 0;
while ( l !== keep.size ) {
l = keep.size;
if ( options.dependencies ) {
// Keep dependencies: For everything in keep, add its dependencies.
for ( const p of keep.values() ) {
for ( const d of deps.get( p ).values() ) {
keep.add( d );
}
}
}
if ( options.dependents ) {
// Keep dependents: For everything in deps (and not already kept), add it if any of its dependencies are in keep.
for ( const [ p, pd ] of deps.entries() ) {
if ( ! keep.has( p ) ) {
for ( const d of pd ) {
if ( keep.has( d ) ) {
keep.add( p );
break;
}
}
}
}
}
}
const ret = new Map();
for ( const [ p, pd ] of deps.entries() ) {
if ( keep.has( p ) ) {
ret.set( p, new Set( [ ...pd ].filter( d => keep.has( d ) ) ) );
}
}
return ret;
}
/**
* List projects in build order.
*
* @param {Map} deps - Dependencies.
* @return {string[][]} Groups of project slugs. Projects in each group only depend on earlier groups.
* @throws {Error} If the dependencies contain a cycle. The error object has a `deps` property with the residual dependencies.
*/
export function getBuildOrder( deps ) {
// We look for packages that have no outgoing dependencies, collect then and remove them from the dependency graph, then repeat.
// This is basically Kahn's algorithm with some steps interleaved.
const ret = [];
while ( deps.size > 0 ) {
const ok = Array.from( deps.keys() )
.filter( d => deps.get( d ).size === 0 )
.sort();
if ( ok.length === 0 ) {
const e = new Error( 'The dependency graph contains a cycle!' );
e.deps = deps;
throw e;
}
ret.push( ok );
for ( const slug of ok ) {
deps.delete( slug );
}
for ( const v of deps.values() ) {
ok.forEach( d => v.delete( d ) );
}
}
return ret;
}