@davidwells/extract-deps 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/verify-basic.js +0 -2
- package/package.json +1 -1
- package/src/dep-graph.js +26 -15
- package/src/index.js +14 -1
package/examples/verify-basic.js
CHANGED
package/package.json
CHANGED
package/src/dep-graph.js
CHANGED
|
@@ -132,16 +132,18 @@ function verifyImportedValuesExist(dep, targetDeps, targetPath, sourcePath, erro
|
|
|
132
132
|
* @param {boolean} throwOnFirstError - Whether to throw immediately on first error
|
|
133
133
|
* @param {boolean} verify - Whether to verify imports/exports
|
|
134
134
|
* @param {boolean} verifyNodeModules - Whether to verify node_modules can be resolved
|
|
135
|
+
* @param {boolean} includeNodeModules - Whether to include node_modules in the graph
|
|
135
136
|
* @param {Map<string, string[]>|null} graph - Optional map to collect dependency graph
|
|
136
137
|
* @param {Set} visited - Set of already-visited file paths to avoid cycles
|
|
137
138
|
*/
|
|
138
139
|
function buildGraphRecursively(
|
|
139
|
-
filePath,
|
|
140
|
-
errors,
|
|
141
|
-
throwOnFirstError,
|
|
142
|
-
verify,
|
|
143
|
-
verifyNodeModules,
|
|
144
|
-
|
|
140
|
+
filePath,
|
|
141
|
+
errors,
|
|
142
|
+
throwOnFirstError,
|
|
143
|
+
verify,
|
|
144
|
+
verifyNodeModules,
|
|
145
|
+
includeNodeModules,
|
|
146
|
+
graph = null,
|
|
145
147
|
visited = new Set(),
|
|
146
148
|
) {
|
|
147
149
|
// Avoid infinite loops
|
|
@@ -171,7 +173,7 @@ function buildGraphRecursively(
|
|
|
171
173
|
|
|
172
174
|
// Collect all non-relative dependencies (node_modules)
|
|
173
175
|
const nodeModuleDeps = []
|
|
174
|
-
if (verifyNodeModules) {
|
|
176
|
+
if (verifyNodeModules || includeNodeModules) {
|
|
175
177
|
if (deps.requires && deps.requires.length > 0) {
|
|
176
178
|
nodeModuleDeps.push(...deps.requires.filter(req => !req.isRelative))
|
|
177
179
|
}
|
|
@@ -217,7 +219,14 @@ function buildGraphRecursively(
|
|
|
217
219
|
}
|
|
218
220
|
|
|
219
221
|
// Recursively process the imported file
|
|
220
|
-
buildGraphRecursively(resolvedPath, errors, throwOnFirstError, verify, verifyNodeModules, graph, visited)
|
|
222
|
+
buildGraphRecursively(resolvedPath, errors, throwOnFirstError, verify, verifyNodeModules, includeNodeModules, graph, visited)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Add node_modules to graph if enabled
|
|
226
|
+
if (includeNodeModules && graph !== null) {
|
|
227
|
+
for (const dep of nodeModuleDeps) {
|
|
228
|
+
graph.get(normalizedPath).push(dep.from)
|
|
229
|
+
}
|
|
221
230
|
}
|
|
222
231
|
|
|
223
232
|
// Verify node_modules dependencies if enabled
|
|
@@ -255,20 +264,22 @@ function buildGraphRecursively(
|
|
|
255
264
|
* @param {boolean} [options.buildGraph] - If true, build and return dependency graph. Default: true
|
|
256
265
|
* @param {boolean} [options.verifyGraph] - If true, verify imports/exports. Default: true
|
|
257
266
|
* @param {boolean} [options.verifyNodeModules] - If true, verify node_modules packages can be resolved. Default: false
|
|
267
|
+
* @param {boolean} [options.includeNodeModules] - If true, include node_modules package names in the graph. Default: false
|
|
258
268
|
* @param {boolean} [options.throwOnFirstError] - If true, throw immediately on first error. Default: false
|
|
259
|
-
* @returns {Map<string, string[]>|undefined} - Dependency graph where keys are file paths and values are arrays of dependency paths. Returns undefined if buildGraph is false.
|
|
269
|
+
* @returns {Map<string, string[]>|undefined} - Dependency graph where keys are file paths and values are arrays of dependency paths (or package names if includeNodeModules). Returns undefined if buildGraph is false.
|
|
260
270
|
*/
|
|
261
271
|
function depGraph(filePath, options = {}) {
|
|
262
|
-
const {
|
|
263
|
-
buildGraph = true,
|
|
264
|
-
verifyGraph = true,
|
|
265
|
-
verifyNodeModules = false,
|
|
272
|
+
const {
|
|
273
|
+
buildGraph = true,
|
|
274
|
+
verifyGraph = true,
|
|
275
|
+
verifyNodeModules = false,
|
|
276
|
+
includeNodeModules = false,
|
|
266
277
|
throwOnFirstError = false,
|
|
267
278
|
} = options
|
|
268
279
|
const errors = []
|
|
269
280
|
const graph = buildGraph ? new Map() : null
|
|
270
281
|
|
|
271
|
-
buildGraphRecursively(filePath, errors, throwOnFirstError, verifyGraph, verifyNodeModules, graph)
|
|
282
|
+
buildGraphRecursively(filePath, errors, throwOnFirstError, verifyGraph, verifyNodeModules, includeNodeModules, graph)
|
|
272
283
|
|
|
273
284
|
if (errors.length > 0) {
|
|
274
285
|
const errorMessage = errors.map((err, idx) => `Dep graph error:\n ${err.message}`).join('\n')
|
|
@@ -331,7 +342,7 @@ function resolveDepPaths(filePath, options = {}) {
|
|
|
331
342
|
if (require.main === module) {
|
|
332
343
|
const entry = path.resolve(__dirname, '../tests/fixtures/cjs-deep-example/index.js')
|
|
333
344
|
const graph = depGraph(entry, {
|
|
334
|
-
|
|
345
|
+
includeNodeModules: true,
|
|
335
346
|
throwOnFirstError: true
|
|
336
347
|
})
|
|
337
348
|
console.log('graph', graph)
|
package/src/index.js
CHANGED
|
@@ -50,6 +50,19 @@ if (require.main === module) {
|
|
|
50
50
|
}
|
|
51
51
|
`
|
|
52
52
|
|
|
53
|
+
const code2 = `
|
|
54
|
+
// Get users Lambda handler
|
|
55
|
+
import _ from 'lodash'
|
|
56
|
+
|
|
57
|
+
export async function handler(event, context) {
|
|
58
|
+
const data = { message: 'get function called' }
|
|
59
|
+
return {
|
|
60
|
+
statusCode: 200,
|
|
61
|
+
body: JSON.stringify(_.omit(data, [])),
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
`
|
|
65
|
+
|
|
53
66
|
const failCase = `
|
|
54
67
|
const { test } = require('uvu')
|
|
55
68
|
const assert = require('uvu/assert')
|
|
@@ -65,7 +78,7 @@ const config = await import('./config.js')
|
|
|
65
78
|
const lerna = require('lerna')
|
|
66
79
|
const { execSync, spawnSync } = require('child_process')
|
|
67
80
|
`
|
|
68
|
-
const result = extractDeps(
|
|
81
|
+
const result = extractDeps(code2)
|
|
69
82
|
|
|
70
83
|
const { imports, requires } = result
|
|
71
84
|
|