deno-musings/transpiler/walker.html

61 lines
2.0 KiB
HTML
Raw Normal View History

2024-10-11 15:54:15 -04:00
<html>
<head></head>
<body>
<button id="open">open</button>
<script type="module">
// Function to walk through the directory and check files
async function checkFiles(dirHandle, extension, date, path="", /** @type {Map<string, File>}*/map, /** @type {Map<string, File>}*/deleteList, /** @type {Array<{fullPath:string, file:File>}*/focusList) {
for await (const entry of dirHandle.values())
{
if (entry.kind === 'file' && entry.name.endsWith(extension))
{
const file = await entry.getFile();
const fullPath = path + entry.name;
map.set(fullPath, file);
deleteList.delete(fullPath);
if (file.lastModified > date)
{
focusList.push({file, fullPath});
}
} else if (entry.kind === 'directory')
{
await checkFiles(entry, extension, date, path+entry.name, map, deleteList, focusList); // Recursively check subdirectories
}
}
}
// Main function to set up the interval polling task
async function main() {
const dirHandle = await window.showDirectoryPicker();
const extension = '.html'; // Change this to the desired file extension
let oldFiles = new Map();
let focusFiles = [];
let time = new Date().getTime();
const Loop =async()=>
{
console.log("checking");
focusFiles = [];
/** @type {Map<string, File>} */
const newFiles = new Map();
await checkFiles(dirHandle, extension, time, undefined, newFiles, oldFiles, focusFiles);
// oldFiles now represent delete list
oldFiles.size && console.log("delete:", oldFiles);
// process updated files
focusFiles.length && console.log("update:", focusFiles);
// reset resources
oldFiles = newFiles;
time = new Date().getTime();
setTimeout(Loop, 3000);
}
Loop();
}
document.getElementById("open").addEventListener("click", main);
</script>
</body>
</html>