# FileTree

# constructor

constructor(
  path: string, // e.g. "site" or "."
  importMetaUrl: string,
  opts?: {
    exclude?: (path: string, stat: fs.Stats) => any,
  }
)

Loads the tree from disk into memory immediately.

const tree = new FileTree('site', import.meta.url)

# root

public root: string

The full file URL of the tree's root based on constructor's path and importMetaUrl.

Never includes a trailing '/'

# files

files: Map<string, TreeFile>
type TreeFile = {
  path: string,    // always the same as its key in the map
  content: Buffer, // always a buffer (see Pipeline)
  version: number, // increments with changes for decaching
  requiredBy: (requiredBy: string) => void, // for invalidation
}

A list of all files (recursively) at the given path.

const tree = new FileTree('site', import.meta.url)

// if cwd contains:
//   ./site/index.html
//   ./site/about.html
//   ./site/pages/welcome.md
//   ./site/styles/main.css

assertMatches(tree.files, {
  '/index.html':       { path: '/index.html',       content: Buffer },
  '/about.html':       { path: '/about.html',       content: Buffer },
  '/pages/welcome.md': { path: '/pages/welcome.md', content: Buffer },
  '/styles/main.css':  { path: '/styles/main.css',  content: Buffer },
})

# watch

watch(debounce?: number): EventEmitter<FileTreeEvents>

type FileTreeEvents = {
  filesUpdated: [changes: FileTreeChange[]],
  moduleInvalidated: [path: string],
}

type FileTreeChange = { path: string, change: 'add' | 'dif' | 'rem' }

Begins watching the path recursively for changes, and updates the contents of files.

May be called more than once, which are no-ops that just return the same EventEmitter.

Uses fs.watch internally; groups multiple fs events into one via debounce (default 100 ms).

const tree = new FileTree('site', import.meta.url)
tree.watch()

# onModuleInvalidated

onModuleInvalidated(importMetaUrl: string, fn: () => void): void

Calls fn once when the module is invalidated, directly or indirectly.

# addDependency

addDependency(requiredBy: string, requiring: string)

Makes changes to file at requiring invalidate module at requiredBy.

NOTE: You should not usually need to call this directly. It's used internally by useTree. It's only made public so you can use it for non-module files. This allows you to invalidate a module when a data file changes.