watch-dependency-graph
Advanced tools
Comparing version 0.3.0 to 0.4.0
69
index.js
@@ -9,11 +9,12 @@ const path = require('path') | ||
function walk ( | ||
children, | ||
entryPointer, | ||
parentChildren, | ||
function walk ({ | ||
ids, | ||
register, | ||
entryPointer, | ||
currentPointer, | ||
childrenOfCurrent, | ||
nextChildren, | ||
visited = [] | ||
) { | ||
for (const { id, children: childs } of children) { | ||
}) { | ||
for (const { id, children: childs } of nextChildren) { | ||
// push to all files | ||
@@ -25,8 +26,11 @@ if (!ids.includes(id)) ids.push(id) | ||
// push to previous parent's children | ||
if (!parentChildren.includes(pointer)) parentChildren.push(pointer) | ||
if (!childrenOfCurrent.includes(pointer)) childrenOfCurrent.push(pointer) | ||
// set module values | ||
if (!register[id]) register[id] = { entries: [], children: [] } // setup | ||
if (!register[id]) | ||
register[id] = { pointer, entries: [], children: [], parents: [] } // setup | ||
if (!register[id].entries.includes(entryPointer)) | ||
register[id].entries.push(entryPointer) // set entries | ||
if (!register[id].parents.includes(currentPointer)) | ||
register[id].parents.push(currentPointer) // set entries | ||
@@ -36,3 +40,12 @@ // recurse, but only if we haven't walked these children yet | ||
visited.push(id) | ||
walk(childs, entryPointer, register[id].children, ids, register, visited) | ||
walk({ | ||
ids, | ||
register, | ||
entryPointer, | ||
currentPointer: pointer, | ||
childrenOfCurrent: register[id].children, | ||
nextChildren: childs, | ||
visited | ||
}) | ||
} | ||
@@ -82,8 +95,18 @@ } | ||
register[id] = { | ||
pointer: entryPointer, | ||
entries: [entryPointer], // self-referential | ||
parents: [], | ||
children: [] | ||
} | ||
if (children) | ||
walk(children, entryPointer, register[id].children, ids, register) | ||
if (children) { | ||
walk({ | ||
ids, | ||
register, | ||
entryPointer, | ||
currentPointer: entryPointer, | ||
childrenOfCurrent: register[id].children, | ||
nextChildren: children | ||
}) | ||
} | ||
} | ||
@@ -116,3 +139,3 @@ | ||
} else if (e === 'change') { | ||
const { entries, children } = register[fullEmittedFilepath] | ||
const { entries, children, parents } = register[fullEmittedFilepath] | ||
@@ -178,15 +201,21 @@ const prev = | ||
// clear modules that require this module | ||
for (const parentPointer of parents) { | ||
delete require.cache[ids[parentPointer]] | ||
} | ||
for (const entryPointer of entries) { | ||
const parentFile = ids[entryPointer] | ||
const fileId = ids[entryPointer] | ||
// clear entries so users can re-require | ||
delete require.cache[parentFile] | ||
delete require.cache[fileId] | ||
walk( | ||
next.children, | ||
walk({ | ||
ids, | ||
register, | ||
entryPointer, | ||
register[parentFile].children, | ||
ids, | ||
register | ||
) | ||
currentPointer: entryPointer, | ||
childrenOfCurrent: register[fileId].children, | ||
nextChildren: next.children | ||
}) | ||
} | ||
@@ -193,0 +222,0 @@ |
{ | ||
"name": "watch-dependency-graph", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
11
test.js
@@ -56,7 +56,7 @@ const fs = require('fs-extra') | ||
fs.outputFileSync( | ||
fixtures.childOfChildren, | ||
fs.readFileSync(fixtures.childOfChildren) | ||
) | ||
const before = require(fixtures.childOfChildren) | ||
assert(before.foo === undefined) | ||
fs.outputFileSync(fixtures.childOfChildren, `module.exports = { foo: true }`) | ||
const updated = await subscriber | ||
@@ -68,2 +68,5 @@ | ||
const after = require(fixtures.childOfChildren) | ||
assert(after.foo === true) | ||
await instance.close() | ||
@@ -70,0 +73,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15391
398
8