folder-sync
v1.3.0
Published
Provides a class which synchronously takes a snapshot of a directory and its contents.
Downloads
3
Readme
folder-sync
Provides a class which synchronously takes a snapshot of a directory and its contents.
Usage:
var Folder = require('folder-sync');
var folder = new Folder('/path/to/directory');
var readme = folder.findFile('README.md');
readme.write('This is my readme!');
readme.read(); // => "This is my readme!"
readme.isFile; // => true
Installation
npm install folder-sync --save-dev
Disclaimer
I DON'T recommend using this module on a web-facing environment. This is because everything in this module is done synchronously, meaning it's a blocking process. I primarily use it for Grunt tasks.
Class: Folder
Creation:var folder = new Folder('/path/to/directory');
A Folder
represents contents of a directory on the file system. It does not update itself - it's just a snapshot.
folder.files
An array of Item
objects, which represent the files in the folder. This does not include dot-files.
folder.folders
An array of Item
objects, which represent the folders in the folder. This does not include dot-files.
folder.dotFiles
An array of Item
objects, which represent the dot-files in the folder.
folder.dotFolders
An array of Item
objects, which represent the dot-folders in the folder.
folder.allFiles
An array of Item
objects, which represent all files in the folder (dot-files included).
folder.allFolders
An array of Item
objects, which represent all folders in the folder (dot-folders included).
folder.findFile(name)
Searches the folder for a file with the given name
(e.g. myFile.txt), and returns an Item
object of that file. If not found, it returns null
.
folder.findFolder(name)
Same as above, but for folders instead of files.
folder.filterFiles(regexp [, group])
Returns an array of file Item
objects that contain the given pattern. By default, it won't return dot-files, but the optional group
argument can be dot
to only return dot-files, or all
to return any type of file.
folder.filterFolders(regexp [, group])
Same as above, but for folders instead of files.
Class: Item
item.name
The name of the item (e.g. myFile.txt)
item.dirname
The path to the item's parent directory (e.g. /path/to)
item.path
The entire path to the item (e.g. /path/to/myFile.txt)
item.bytes
The byte-size of the item.
item.bytesOnDisk
The number of bytes that the item takes up on the disk.
item.changed
An instance of Date
, which is the last time the item was changed in any way (contents, permissions, properties, etc.)
item.modified
An instance of Date
, which is the last time the item's contents were changed.
item.accessed
An instance of Date
, which is the last time the item was accessed in any way.
item.created
An instance of Date
, which is the time when the item was created.
item.isFile
Boolean. Whether or not the item is a file or not.
item.isDirectory
Boolean. Whether or not the item is a directory or not.
item.isFolder
Same as item.isDirectory
.
item.read()
Returns the text content of the file, as a string.
If the item is not a file, returns undefined
.
item.write(string)
Overwrites the file contents with string
, and returns true.
If the item is not a file, returns undefined
.
item.readFolder()
Returns an instance of Folder
which represents this item.
If the item is not a directory, returns undefined
.