From 4c035d6d003fb688646f80267c8be702537a80b3 Mon Sep 17 00:00:00 2001 From: "Anders D. Johnson" Date: Sat, 25 May 2019 11:16:28 -0500 Subject: [PATCH] feat: include only some files Fixes #7 --- hot-reload.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/hot-reload.js b/hot-reload.js index de5e58c..f0865da 100644 --- a/hot-reload.js +++ b/hot-reload.js @@ -1,3 +1,5 @@ +const includes = [] + const filesInDirectory = dir => new Promise (resolve => dir.createReader ().readEntries (entries => @@ -13,9 +15,15 @@ const filesInDirectory = dir => new Promise (resolve => ) ) -const timestampForFilesInDirectory = dir => - filesInDirectory (dir).then (files => - files.map (f => f.name + f.lastModifiedDate).join ()) +const timestampForFilesInDirectory = dir => { + return filesInDirectory (dir).then (files => { + if (includes.length && !includes.every(i => files.some(f => i === f.name))) return; + + return files + .filter(f => includes.length ? includes.includes(f.name) : true) + .map (f => f.name + f.lastModifiedDate).join () + }) +} const reload = () => { @@ -31,7 +39,7 @@ const watchChanges = (dir, lastTimestamp) => { timestampForFilesInDirectory (dir).then (timestamp => { - if (!lastTimestamp || (lastTimestamp === timestamp)) { + if (!timestamp || !lastTimestamp || (lastTimestamp === timestamp)) { setTimeout (() => watchChanges (dir, timestamp), 1000) // retry after 1s @@ -50,3 +58,10 @@ chrome.management.getSelf (self => { chrome.runtime.getPackageDirectoryEntry (dir => watchChanges (dir)) } }) + +if (typeof module === 'object') { + exports.include = (files) => { + includes.push(...files) + } +} +