deno-musings/transpiler/walker.html

78 lines
2.5 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">
2024-10-11 16:11:42 -04:00
// Function to walk through the directory and update file handles
async function updateFileHandles(dirHandle, extension, path = "", map) {
for await (const entry of dirHandle.values()) {
if (entry.kind === 'file' && entry.name.endsWith(extension)) {
2024-10-11 15:54:15 -04:00
const file = await entry.getFile();
const fullPath = path + entry.name;
2024-10-11 16:11:42 -04:00
if (!map.has(dirHandle)) {
map.set(dirHandle, new Set());
}
map.get(dirHandle).add(fullPath);
} else if (entry.kind === 'directory') {
await updateFileHandles(entry, extension, path + entry.name + "/", map); // Recursively check subdirectories
}
}
}
// Function to check modified dates of known file handles
async function checkModifiedDates(map, date) {
const focusFiles = [];
for (const [dirHandle, filenames] of map.entries()) {
for (const filename of filenames) {
try
{
const fileHandle = await dirHandle.getFileHandle(filename.split('/').pop());
const file = await fileHandle.getFile();
if (file.lastModified > date.getTime()) {
focusFiles.push({ file, fullPath: filename });
}
}
catch(e)
2024-10-11 15:54:15 -04:00
{
2024-10-11 16:11:42 -04:00
console.log("file deleted?", filename);
filenames.delete(filename);
2024-10-11 15:54:15 -04:00
}
2024-10-11 16:11:42 -04:00
2024-10-11 15:54:15 -04:00
}
}
2024-10-11 16:11:42 -04:00
return focusFiles;
2024-10-11 15:54:15 -04:00
}
2024-10-11 16:11:42 -04:00
// Main function to set up the intervals
2024-10-11 15:54:15 -04:00
async function main() {
const dirHandle = await window.showDirectoryPicker();
const extension = '.html'; // Change this to the desired file extension
2024-10-11 16:11:42 -04:00
let fileHandlesMap = new Map();
let lastCheckTime = new Date().getTime();
2024-10-11 15:54:15 -04:00
2024-10-11 16:11:42 -04:00
const CoarseCheck = async () => {
console.log("===========coarse check==================");
fileHandlesMap = new Map();
await updateFileHandles(dirHandle, extension, "", fileHandlesMap);
setTimeout(CoarseCheck, 10000);
};
const FineCheck =async()=>{
const focusFiles = await checkModifiedDates(fileHandlesMap, new Date(lastCheckTime));
if (focusFiles.length) {
console.log("Updated files:", focusFiles);
// Open the updated files or handle them as needed
}
lastCheckTime = new Date().getTime();
setTimeout(FineCheck, 1000);
2024-10-11 15:54:15 -04:00
}
2024-10-11 16:11:42 -04:00
CoarseCheck();
FineCheck();
2024-10-11 15:54:15 -04:00
}
2024-10-11 16:11:42 -04:00
2024-10-11 15:54:15 -04:00
document.getElementById("open").addEventListener("click", main);
</script>
</body>
2024-10-11 16:11:42 -04:00
</html>