-
Notifications
You must be signed in to change notification settings - Fork 51
/
dependency-graph.js
67 lines (54 loc) · 1.94 KB
/
dependency-graph.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
const readPkgUp = require('read-pkg-up')
const path = require('path')
const dependencyGraph = new Map()
exports.dependencyGraph = dependencyGraph
exports.entries = new Set()
const entryWhitelist = [/\/tests?\//, /\.test\.js$/, /\.config\.js$/]
exports.checkEntriesWhitelist = filename => {
for (const re of entryWhitelist) {
if (re.test(filename)) {
exports.entries.add(filename)
}
}
}
const packageFile = readPkgUp.sync()
function recordPackageEntry(entry) {
exports.entries.add(path.resolve(packageFile.path, '..', entry))
}
if (packageFile) {
for (const key in packageFile.packageJson) {
if (key === 'main') {
recordPackageEntry(packageFile.packageJson.main)
} else if (key === 'entries') {
packageFile.packageJson.entries.forEach(recordPackageEntry)
} else if (/-bundles$/.test(key)) {
// github-asset-pipeline internal manifest format
Object.keys(packageFile.packageJson[key]).forEach(recordPackageEntry)
}
}
}
function gatherImported() {
const filenames = new Set()
const identifiers = new Set()
for (const {imports} of dependencyGraph.values()) {
for (const [importedFilename, importedIdentifiers] of imports) {
// require.resolve will expand any symlinks to their fully qualified
// directories. We can use this (with the absolute path given in
// importedFilename to quickly expand symlinks, which allows us to have
// symlinks (aka workspaces) in node_modules, and not fail the lint.
const fullyQualifiedImportedFilename = require.resolve(importedFilename)
filenames.add(fullyQualifiedImportedFilename)
for (const importedIdentifier of importedIdentifiers) {
identifiers.add(`${fullyQualifiedImportedFilename}#${importedIdentifier}`)
}
}
}
return {filenames, identifiers}
}
let importedCache = null
exports.imported = function() {
if (!importedCache) {
importedCache = gatherImported()
}
return importedCache
}