|
| 1 | +// most of the functions in this file were taken from the Madge project: https://github.com/pahen/madge |
| 2 | +// reasons for not using Madge directly: 1) it has issues with TypeScript on Windows. 2) it has issues with tsx files |
| 3 | + |
| 4 | +import os from 'os'; |
| 5 | +import path from 'path'; |
| 6 | +import dependencyTree from 'dependency-tree'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Check if running on Windows. |
| 10 | + * @type {Boolean} |
| 11 | + */ |
| 12 | +const isWin = (os.platform() === 'win32'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Check if path is from NPM folder |
| 16 | + * @param {String} path |
| 17 | + * @return {Boolean} |
| 18 | + */ |
| 19 | +function isNpmPathFunc(path) { |
| 20 | + return path.indexOf('node_modules') >= 0; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Sort tree. |
| 25 | + * @param {Object} tree |
| 26 | + * @return {Object} |
| 27 | + */ |
| 28 | +function sort(tree) { |
| 29 | + return Object |
| 30 | + .keys(tree) |
| 31 | + .sort() |
| 32 | + .reduce((acc, id) => { |
| 33 | + acc[id] = tree[id].sort(); |
| 34 | + return acc; |
| 35 | + }, {}); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Exclude modules from tree using RegExp. |
| 40 | + * @param {Object} tree |
| 41 | + * @param {Array} excludeRegExp |
| 42 | + * @return {Object} |
| 43 | + */ |
| 44 | +function exclude(tree, excludeRegExp) { |
| 45 | + const regExpList = excludeRegExp.map((re) => new RegExp(re)); |
| 46 | + |
| 47 | + function regExpFilter(id) { |
| 48 | + return regExpList.findIndex((regexp) => regexp.test(id)) < 0; |
| 49 | + } |
| 50 | + |
| 51 | + return Object |
| 52 | + .keys(tree) |
| 53 | + .filter(regExpFilter) |
| 54 | + .reduce((acc, id) => { |
| 55 | + acc[id] = tree[id].filter(regExpFilter); |
| 56 | + return acc; |
| 57 | + }, {}); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +/** |
| 62 | + * Process absolute path and return a shorter one. |
| 63 | + * @param {String} absPath |
| 64 | + * @param {Object} cache |
| 65 | + * @param {String} baseDir |
| 66 | + * @return {String} |
| 67 | + */ |
| 68 | +function processPath(absPath, cache, baseDir) { |
| 69 | + if (cache[absPath]) { |
| 70 | + return cache[absPath]; |
| 71 | + } |
| 72 | + |
| 73 | + let relPath = path.relative(baseDir, absPath); |
| 74 | + |
| 75 | + if (isWin) { |
| 76 | + relPath = relPath.replace(/\\/g, '/'); |
| 77 | + } |
| 78 | + |
| 79 | + cache[absPath] = relPath; |
| 80 | + |
| 81 | + return relPath; |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Convert deep tree produced by dependency-tree to a |
| 86 | + * shallow (one level deep) tree used by madge. |
| 87 | + * @param {Object} depTree |
| 88 | + * @param {Object} tree |
| 89 | + * @param {Object} pathCache |
| 90 | + * @param {String} baseDir |
| 91 | + * @return {Object} |
| 92 | + */ |
| 93 | +function convertTree(depTree, tree, pathCache, baseDir) { |
| 94 | + for (const key in depTree) { |
| 95 | + const id = processPath(key, pathCache, baseDir); |
| 96 | + |
| 97 | + if (!tree[id]) { |
| 98 | + tree[id] = []; |
| 99 | + |
| 100 | + for (const dep in depTree[key]) { |
| 101 | + tree[id].push(processPath(dep, pathCache, baseDir)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + convertTree(depTree[key], tree, pathCache, baseDir); |
| 106 | + } |
| 107 | + |
| 108 | + return tree; |
| 109 | +} |
| 110 | + |
| 111 | +function getDetectiveOption(file, existingDetectiveOption) { |
| 112 | + const detectiveOption = existingDetectiveOption || {}; |
| 113 | + const extension = path.extname(file); |
| 114 | + if (extension === '.tsx') { |
| 115 | + detectiveOption.ts = { ecmaFeatures: { jsx: true } }; |
| 116 | + } |
| 117 | + return detectiveOption; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Generate the tree from the given files |
| 122 | + * @param {Array} files |
| 123 | + * @param config |
| 124 | + * @return {Object} |
| 125 | + */ |
| 126 | +export default function generateTree(files, config) { |
| 127 | + const depTree = {}; |
| 128 | + const visited = {}; |
| 129 | + const nonExistent = []; |
| 130 | + const npmPaths = {}; |
| 131 | + const pathCache = {}; |
| 132 | + |
| 133 | + files.forEach((file) => { |
| 134 | + if (visited[file]) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const detective = getDetectiveOption(file, config.detectiveOptions); |
| 139 | + Object.assign(depTree, dependencyTree({ |
| 140 | + filename: file, |
| 141 | + directory: config.baseDir, |
| 142 | + requireConfig: config.requireConfig, |
| 143 | + webpackConfig: config.webpackConfig, |
| 144 | + visited, |
| 145 | + filter: (dependencyFilePath, traversedFilePath) => { |
| 146 | + let dependencyFilterRes = true; |
| 147 | + const isNpmPath = isNpmPathFunc(dependencyFilePath); |
| 148 | + |
| 149 | + if (config.dependencyFilter) { |
| 150 | + dependencyFilterRes = config.dependencyFilter(dependencyFilePath, traversedFilePath, config.baseDir); |
| 151 | + } |
| 152 | + |
| 153 | + if (config.includeNpm && isNpmPath) { |
| 154 | + (npmPaths[traversedFilePath] = npmPaths[traversedFilePath] || []).push(dependencyFilePath); |
| 155 | + } |
| 156 | + |
| 157 | + return !isNpmPath && (dependencyFilterRes || dependencyFilterRes === undefined); |
| 158 | + }, |
| 159 | + detective, |
| 160 | + nonExistent, |
| 161 | + })); |
| 162 | + }); |
| 163 | + |
| 164 | + let tree = convertTree(depTree, {}, pathCache, config.baseDir); |
| 165 | + |
| 166 | + for (const npmKey in npmPaths) { |
| 167 | + const id = processPath(npmKey, pathCache, config.baseDir); |
| 168 | + |
| 169 | + npmPaths[npmKey].forEach((npmPath) => { |
| 170 | + tree[id].push(processPath(npmPath, pathCache, config.baseDir)); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + if (config.excludeRegExp) { |
| 175 | + tree = exclude(tree, config.excludeRegExp); |
| 176 | + } |
| 177 | + |
| 178 | + return { |
| 179 | + tree: sort(tree), |
| 180 | + skipped: nonExistent, |
| 181 | + }; |
| 182 | +} |
0 commit comments