FSGOD
A library for omnipotent control over your file system with ease
The only component thus far is the Virtual Directory Tree (VDT).
API
VDT
The VDT is a recursively created JavaScript object generated from a path you designate in your file system. The VDT makes it much easier to search files for content, write/append/prepend/delete files, create files and directories, and perform a multitude of other file system oriented tasks that would typically be repetitive and tricky to do dynamically and efficiently.
const fsgod = require('fsgod');
fsgod.VDT('./', (vdt) => {
for ( var i = 0; i < vdt.content.length; i++ ){
console.log(vdt.content[i].name);
}
});
The above code should log the names of all the items in the directory it's currently in to the console
The vdt object that gets passed to the callback in fsgod.VDT is the Virtual Directory Tree. fsgod.VDT recursively walks all the way through the specified folder and builds each item into a JavaScript object and passes it back to the user in the callback.
VDT Directory Methods
vdt.search
Every directory in the VDT (including the root/target directory) has a search method. The search method uses regular expressions to locate any item containing the string passed in the first argument. The search method only searches in and under the directory your are performing the search on
fsgod.VDT('./', (vdt) => {
vdt.search('test', { content: false }, (results) => {
console.log('Found ' + results.length + ' items');
for (var i = 0; i < results.length; i++){
console.log(results[i].fullPath);
}
});
});
The above code should find file and directory names that contain the phrase test and print their full paths.
You should notice the second argument takes an object which specifies search options; in the case of the above example, we don't want to look through file content for our search string, just names. The code below illustrates a search method without filters
fsgod.VDT('./', (vdt) => {
vdt.search('test', (results) => {
console.log('Found ' + results.length + ' items');
for (var i = 0; i < results.length; i++){
console.log(results[i].fullPath);
}
});
});
The filter options you can give to the object are
Filter | Type | Usage |
---|
content | Boolean | Decides whether or not to search file content for results |
names | Boolean | Decides whether or not to search file/directory names for results |
files | Boolean | Decides whether or not to include files in search results |
directories | Boolean | Decides whether or not to include directories in results |
excludes | Array | Any search results containing a string in the 'excludes' array will be removed |
vdt.get
Because dynamically getting objects at variable depths can be very tricky, you can use the get method to easily navigate the VDT. For example, if you had a directory named Foo and a file named bar.txt inside it, you could use the following code to retrieve bar.txt
fsgod.VDT('./Foo', (vdt) => {
var bar = vdt.get('bar.txt');
});
Chaining vdt.get
The get method can be chained either in the url passed into the first argument, or together in sequence. Lets say directory Foo has 3 directories underneath it, all named Foo and bar.txt is at the bottom of this tree. i.e. the local path to bar.txt is Foo/Foo/Foo/Foo/bar.txt. You could get it in two ways
fsgod.VDT('./Foo', (vdt) => {
var bar = vdt.get('Foo').get('Foo').get('Foo').get('bar.txt');
});
fsgod.VDT('./Foo', (vdt) => {
var bar = vdt.get('Foo/Foo/Foo/bar.txt');
});
vdt.mkdir
The mkdir method will create a directory for the vdt object and in the appropriate place in your file system. Lets say we wanted to create a directory named Foo, and then create a directory inside Foo named Bar. This operation can be made easy using the get method along side mkdir
fsgod.VDT('./', (vdt) => {
vdt.mkdir('Foo', (err) => {
if (err) throw err;
vdt.get('Foo').mkdir('Bar', (err) => {
if (err) throw err;
});
});
});
vdt.mkfile
The mkfile method will create a file for the vdt object and in the appropriate place in your file system. Lets say we wanted to create a directory named Foo, and then create a text file inside Foo named bar.txt
fsgod.VDT('./', (vdt) => {
vdt.mkdir('Foo', (err) => {
if (err) throw err;
vdt.get('Foo').mkfile('bar.txt', (err) => {
if (err) throw err;
});
});
});
You can also give the file content when you create it like so
fsgod.VDT('./', (vdt) => {
vdt.mkfile('bar.txt', 'Hello World', (err) => {
});
});
VDT File Methods
vdt.write
The write method overwrites the content of the source file and VDT file object
Note: write and overwrite reference the same function
fsgod.VDT('./Foo', (vdt) => {
var bar = vdt.get('bar.txt');
bar.write('Goodbye world', (err) => {
if (err) throw err;
});
});
vdt.append
The append method adds content to the end of the source file content and VDT file object
fsgod.VDT('./Foo', (vdt) => {
var bar = vdt.get('bar.txt');
bar.append('World', (err) => {
if (err) throw err;
});
});
vdt.prepend
The prepend method adds content to the begining of the source file content and VDT file object
fsgod.VDT('./Foo', (vdt) => {
var bar = vdt.get('bar.txt');
bar.prepend('Hello ', (err) => {
if (err) throw err;
});
});
Virtual Directory
Each directory in the vdt callback is it's own Virtual Directory Tree with the same methods as the intial target directory
Key | Type | Usage |
---|
content | Array | An array of both directories (VDT's) and files |
name | String | The name of the file or directory |
type | String | Will let you know if its a directory or file |
fullPath | String | The full path to the item |
size | Integer | Size of the item in bytes |
search | Function | Search in and under the directory for a search string |
get | Function | Gets the specified object and makes it's methods easily available |
mkdir | Function | Creates a directory in the VDT and your file system |
mkfile | Function | Creates a file in the VDT and your file system |
Virtual File
Key | Type | Usage |
---|
content | String | File contents |
name | String | File name |
type | String | Will let you know if it's a directory or file |
fullPath | String | The full path to the item |
size | Integer | Size of the item in bytes |
write/overwrite | Function | Overwrites the content of the file |
append | Function | Adds new content to the end of the file |
prepend | Function | Adds new content to the beginning of the file |