lune-org/lune

Add an API for watching for file changes

Opened this issue · 0 comments

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...