lune-org / lune

A standalone Luau runtime
https://lune-org.github.io/docs
Mozilla Public License 2.0
353 stars 82 forks source link

Add an API for watching for file changes #63

Open filiptibell opened 1 year ago

filiptibell commented 1 year ago

API proposal (subject to change):

-- pattern is a glob, we can use '*' to watch everything,
-- file name globs such as '*.luau', '**/*.json', etc...
fs.watch(rootPath: string, patternOrOptions: string | FsWatchOptions, ...)

export type FsWatchOptions = {
    pattern: string,
    watchFiles: boolean?,
    watchDirectories: boolean?,
}

The proposed api could work in one of several ways:

local watcher = fs.watch(".", "*")

-- 1. similar to how websockets currently work
for path, changeType in watcher.next() do
    -- `path` is some file path that has changed
    -- `changeType` is what happened, such as added, removed, renamed, ...
end

-- 2. similar to how net.serve currently works
fs.watch(".", "*", {
    fileAdded = function(path: string)

    end,
    fileRemoved = function(path: string)

    end,
    fileChanged = function(path: string)

    end,
})

-- 3. some other pattern that we don't use anywhere else yet

Worth noting is that filesystem watching is notoriously tricky and we would want this to work across all platform that Lune currently supports. It should also mesh well with existing async filesystem APIs. As demonstrated above, we also don't really have a consistent API surface for these things, and it might also be worth re-evaluating some other APIs, but that's for another day...

bbslipers commented 2 weeks ago

This would certainly be a very useful API functionality. So far, I'm trying to implement this in some form as a module using metadata (createdAt, modifiedAt).