Implementing your own template/file management system which consistently reads/updates file content can be tedious. LiveDirectory aims to solve that by acting as an automated file content store making a directory truly come alive. Powered by the efficient file watching library chokidar, LiveDirectory can be an efficient solution for fast and iterative web development.
- Simple-to-use API
- Sub-Directory Support
- Asynchronous By Nature
- Instantaneous Hot Reloading
- Memory Efficient
- Supports Windows, Linux & MacOS
LiveDirectory can be installed using node package manager (npm
)
npm i live-directory
Below are varios examples that make use of most classes and methods in LiveDirectory. The micromustache
template renderer is used in the examples below but you can use any other renderer/framework.
const LiveDirectory = require('live-directory');
// Create LiveDirectory instance
const live_templates = new LiveDirectory({
path: './templates/html'
});
// Create server route for dashboard user page
some_server.get('/dashboard/user', (request, response) => {
// Some processing done here which generates some user_options for rendering page uniquely
// Retrieve template LiveFile instance
// Below relative path is translated under the hood to './templates/html/dashboard/user.html'
let template = live_templates.get('/dashboard/user.html');
// Send html string as response body
return response.type('html').send(template.content);
});
const MicroMustache = require('micromustache');
const LiveDirectory = require('live-directory');
// Create LiveDirectory instance
const live_templates = new LiveDirectory({
path: './templates/html'
});
// Handle 'reload' event from LiveDirectory so we can re-generate a new compiled micromustache instance on each file content update
live_templates.on('file_reload', (file) => {
// We can attach our own properties to the LiveFile object
// Using this, we can recompile a micromustache renderer and attach onto LiveFile
const compiled = MicroMustache.compile(file.content);
compiled_templates[file.path].render = compiled.render;
});
// Create server route for dashboard user page
some_server.get('/dashboard/user', (request, response) => {
// Some processing done here which generates some user_options for rendering page uniquely
// Retrieve template LiveFile instance
// Below relative path is translated under the hood to './templates/html/dashboard/user.html'
let template = live_templates.get('/dashboard/user.html');
// Generate rendered template code
let html = template.render(user_options);
// Send rendered html code in response
return response.type('html').send(html);
});
Below is a breakdown of the LiveDirectory
class generated when creating a new LiveDirectory instance.
path
[String
]: Path to the directory.- Example:
./templates/
- Required for a LiveDirectory Instance.
- Example:
keep
[Object
|Function
]: Whitelist filter that can either be an object or a function.- Object Schema:
names
[Array
]: List of file names to keep.extensions
[Array
]: List of file extensions to keep.
- Function Schema:
(String: path, FS.stats: stats) => { /* Return true to keep */}
- Note! If you provide your own function, the first execution will be without the
stats
parameter and the second will be with thestats
parameter. - Note! the
keep
filter is like a whitelist and is applied before theignore
filter.
- Object Schema:
ignore
[Object
|Function
]: Blacklist filter that can either be an object or a function.- Object Schema:
names
[Array
]: List of file/directory names to ignore.extensions
[Array
]: List of file extensions to ignore.
- Function Schema:
(String: path, FS.stats: stats) => { /* Return true to ignore */}
- Note! If you provide your own function, the first execution will be without the
stats
parameter and the second will be with thestats
parameter. - Note! the
ignore
filter is like a global blacklist and thus is applied after thekeep
filter.
- Object Schema:
retry
[Object
]: File content reading retry policy.every
[Number
]: Delay between retries in milliseconds.max
[Number
]: Maximum number of retries.
Property | Type | Description |
---|---|---|
path |
String |
Root directory path. |
watcher |
FS.Watcher |
Underlying Chokidar watcher instance. |
tree |
Object |
Directory tree with heirarchy. |
files |
Object |
All loaded files with their relative paths. |
ready()
: Returns aPromise
which is then resolved once instance is fully ready.get(String: path)
: ReturnsLiveFile
instance for file at specified path.- Returns a
LiveFile
instance orundefined
- Supported Formats: When root path is
/root/var/www/webserver/templates
.- System Path:
/root/var/www/webserver/templates/dashboard/index.html
- Relative Path:
/dashboard/index.html
- Simple Path:
dashboard/index.html
- System Path:
- Returns a
on(String: type, Function: handler)
: Binds a handler forLiveDirectory
events.- Event
'directory_create'
: Reports newly created directories.handler
:(String: path) => {}
- Event
'directory_destroy'
: Reports when a directory is deleted.handler
:(String: path) => {}
- Event
'file_reload'
: Reports when a file is created/is reloaded.handler
:(LiveFile: file) => {}
- See
LiveFile
documentation for available properties and methods.
- Event
'file_destroy'
: Reports when a file is destroyed.handler
:(LiveFile: file) => {}
- See
LiveFile
documentation for available properties and methods.
- Event
'file_error'
: Reports FileSystem errors for a file.handler
:(LiveFile: file, Error: error) => {}
- See
LiveFile
documentation for available properties and methods.
- Event
'error'
: ReportsLiveDirectory
instance errors.handler
:(Error: error) => {}
- Event
Below is a breakdown of the LiveFile
instance class that represents all files.
Property | Type | Description |
---|---|---|
path |
String |
System file path. |
name |
String |
File name. |
extension |
String |
File extension. |
etag |
String |
Unique etag compatible file hash. |
content |
String |
File text content. |
buffer |
Buffer |
File raw content. |
last_update |
Number |
Last file text content update timestamp in milliseconds |
ready()
: Returns aPromise
which is resolved once file is ready with initial content.reload()
: Returns aPromise
which is resolved once the File's content is reloaded.