watch-dependency-graph
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -13,3 +13,4 @@ const fs = require('fs-extra') | ||
B: path.join(fixturesRoot, 'B.entry.js'), | ||
renameableEntry: path.join(fixturesRoot, 'renameableEntry.entry.js') | ||
renameableEntry: path.join(fixturesRoot, 'renameableEntry.entry.js'), | ||
addedEntry: path.join(fixturesRoot, 'addedEntry.entry.js') | ||
} | ||
@@ -16,0 +17,0 @@ |
37
index.js
@@ -7,2 +7,3 @@ const path = require('path') | ||
const uniq = require('@arr/unique') | ||
const debug = require('debug')('wdg') | ||
@@ -53,3 +54,5 @@ function walk ( | ||
module.exports = function graph (...globs) { | ||
module.exports = function graph (...globbies) { | ||
const globs = globbies.flat(2).map(g => path.resolve(process.cwd(), g)) | ||
// once instance | ||
@@ -86,6 +89,15 @@ const events = new EventEmitter() | ||
watcher = chokidar.watch(ids, { ignoreInitial: true }) | ||
watcher = chokidar.watch(globs.concat(ids), { ignoreInitial: true }) | ||
watcher.on('all', (e, f) => { | ||
watcher.on('all', async (e, f) => { | ||
debug('chokidar', e, f) | ||
const fullEmittedFilepath = require.resolve(f) | ||
debug('fullEmittedFilepath', fullEmittedFilepath) | ||
if (e === 'add') { | ||
await watcher.close() | ||
events.emit('add', [fullEmittedFilepath]) | ||
init() | ||
// shouldn't ever happen | ||
@@ -96,3 +108,3 @@ } else if (e === 'unlink') { | ||
if (removedModule) { | ||
watcher.close() | ||
await watcher.close() | ||
events.emit('remove', [removedModule.id]) | ||
@@ -104,9 +116,9 @@ init() | ||
} else if (e === 'change') { | ||
const updatedFilepath = require.resolve(f) | ||
const { entries, children } = register[updatedFilepath] | ||
const { entries, children } = register[fullEmittedFilepath] | ||
const prev = require.cache[updatedFilepath] || require(updatedFilepath) | ||
delete require.cache[updatedFilepath] | ||
require(updatedFilepath) | ||
const next = require.cache[updatedFilepath] | ||
const prev = | ||
require.cache[fullEmittedFilepath] || require(fullEmittedFilepath) | ||
delete require.cache[fullEmittedFilepath] | ||
require(fullEmittedFilepath) | ||
const next = require.cache[fullEmittedFilepath] | ||
@@ -128,3 +140,3 @@ // diff prev/next | ||
for (const filepath of Object.keys(register)) { | ||
if (filepath === updatedFilepath) { | ||
if (filepath === fullEmittedFilepath) { | ||
const localChildren = register[filepath].children | ||
@@ -202,2 +214,5 @@ const localPointer = localChildren.indexOf(removedModulePointer) | ||
async close () { | ||
events.removeAllListeners('update') | ||
events.removeAllListeners('add') | ||
events.removeAllListeners('remove') | ||
return watcher.close() | ||
@@ -204,0 +219,0 @@ } |
{ | ||
"name": "watch-dependency-graph", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -37,2 +37,3 @@ "main": "index.js", | ||
"chokidar": "^3.4.2", | ||
"debug": "^4.2.0", | ||
"matched": "^5.0.0" | ||
@@ -39,0 +40,0 @@ }, |
59
test.js
@@ -176,2 +176,61 @@ const fs = require('fs-extra') | ||
test('handles adding new entry file', async () => { | ||
const instance = require('./')('./fixtures/*.entry.js') | ||
const added = subscribe('add', instance) | ||
await wait(500) | ||
fs.outputFileSync(fixtures.addedEntry, 'module.exports = {}') | ||
const ids = await added | ||
assert(ids.includes(fixtures.addedEntry)) | ||
assert(instance.ids.includes(fixtures.addedEntry)) | ||
await instance.close() | ||
}) | ||
test('can remove listeners', async () => { | ||
const instance = require('./')(fixtures.A) | ||
let calls = 0 | ||
// should not fire | ||
instance.on('update', () => { | ||
calls += 1 | ||
})() | ||
const close = instance.on('update', () => { | ||
calls += 1 | ||
}) | ||
fs.outputFileSync(fixtures.childOfA, fs.readFileSync(fixtures.childOfA)) | ||
await wait(500) | ||
assert(calls === 1) | ||
close() | ||
await instance.close() | ||
}) | ||
test('resets listeners after close', async () => { | ||
const instance = require('./')(fixtures.A) | ||
let calls = 0 | ||
instance.on('update', () => { | ||
calls += 1 | ||
}) | ||
await instance.close() | ||
fs.outputFileSync(fixtures.childOfA, fs.readFileSync(fixtures.childOfA)) | ||
await wait(500) | ||
assert(calls === 0) | ||
}) | ||
!(async function () { | ||
@@ -178,0 +237,0 @@ console.time('test') |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
14506
370
4
+ Addeddebug@^4.2.0
+ Addeddebug@4.4.0(transitive)
+ Addedms@2.1.3(transitive)