|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | const fs = require('node:fs'); |
| 4 | +const path = require('node:path'); |
4 | 5 | const { debuglog } = require('node:util'); |
5 | 6 | const cabinet = require('filing-cabinet'); |
6 | 7 | const precinct = require('precinct'); |
@@ -106,6 +107,7 @@ module.exports._getDependencies = function(config = {}) { |
106 | 107 | webpackConfig: config.webpackConfig, |
107 | 108 | nodeModulesConfig: config.nodeModulesConfig, |
108 | 109 | tsConfig: config.tsConfig, |
| 110 | + tsConfigPath: config.tsConfigPath, |
109 | 111 | noTypeDefinitions: config.noTypeDefinitions |
110 | 112 | }); |
111 | 113 |
|
@@ -167,6 +169,7 @@ function traverse(config = {}) { |
167 | 169 | for (const dependency of dependencies) { |
168 | 170 | const localConfig = config.clone(); |
169 | 171 | localConfig.filename = dependency; |
| 172 | + localConfig.directory = getDirectory(localConfig); |
170 | 173 |
|
171 | 174 | if (localConfig.isListForm) { |
172 | 175 | for (const item of traverse(localConfig)) { |
@@ -198,3 +201,25 @@ function dedupeNonExistent(nonExistent) { |
198 | 201 | i++; |
199 | 202 | } |
200 | 203 | } |
| 204 | + |
| 205 | +// If the file is in a node module, use the root directory of the module |
| 206 | +function getDirectory(localConfig) { |
| 207 | + if (!localConfig.filename.includes('node_modules')) { |
| 208 | + return localConfig.directory; |
| 209 | + } |
| 210 | + |
| 211 | + return getProjectPath(path.dirname(localConfig.filename)) || localConfig.directory; |
| 212 | +} |
| 213 | + |
| 214 | +function getProjectPath(filename) { |
| 215 | + try { |
| 216 | + const nodeModuleParts = filename.split('node_modules'); |
| 217 | + const packageSubPathPath = nodeModuleParts.pop().split(path.sep).filter(Boolean); |
| 218 | + const packageName = packageSubPathPath[0].startsWith('@') ? `${packageSubPathPath[0]}${path.sep}${packageSubPathPath[1]}` : packageSubPathPath[0]; |
| 219 | + |
| 220 | + return path.normalize([...nodeModuleParts, `${path.sep}${packageName}`].join('node_modules')); |
| 221 | + } catch { |
| 222 | + debug(`Could not determine the root directory of package file ${filename}. Using default`); |
| 223 | + return null; |
| 224 | + } |
| 225 | +} |
0 commit comments