Easy way to build folder structure for node.js
const FolderBuilder = require('@tubu/folder-builder');
// Create a new FolderBuilder instance with path
const fb = new FolderBuilder(__dirname);
// Create a new folder instance with name
const newFolder = fb.createFolder('ex1');
// Build a new folder
newFolder.build(newFolder).catch(console.error);
It helps you to create files and folders easily. It can create files inside other folders recursively. Can create, delete, update files and or folders at the same time. Add this to your package.json file and it is ready to use.
Available on npm registry as a node.js module
node version > 10.16.0
To install this package
$ npm install @tubu/folder-builder
Or
$ yarn add @tubu/folder-builder
FolderBuilder
is used to create a new folder management system.
new FolderBuilder(__dirname);
String -> Folder default path
Object -> Folder and File default Options
Example:
new FolderBuilder({
"defaultFolder": {
"name" : "sample",
"path" : __dirname
},
"defaultFile": {
"mode" : 0o555
}
});
Main Folder Builder Methods
fb.createFolder(folder)
Returns a new folder instance
String -> Folder Name
Object -> Folder Options
const newFolder = fb.createFile(file)
Returns a new file instance
String -> Folder Name
Object -> File Options
Folder instance for management folder works
fb.createFolder({
name : 'sample',
path : __dirname,
force : false,
archive : false
})
- name : Folder name (required)
- path : Path to build current folder
- force : Force to build folder, delete if exists
- archive : Generate a zip file of current folder, current path
Folder Methods
Add a child folder into the current folder, and return a new folder instance
newFolder.addFolder(folder)
String -> Folder Name
Object -> Folder Options
Add a child file into the current folder, return a new file instance
newFolder.addFile(folder)
String -> File Name
Object -> Folder Options
Build the folder with its children as folders and files, recursively.
Also, create zip files if
archive
of the folder istrue
newFolder.build()
File instance for management file works
Options
fb.createFile({
name : 'sample.txt',
content : 'hello',
mode : 0o666
})
- name : File name
- content : Content of file
- contentUrl : A file path to read and fill the content
- mode : File permissions, for detail
Warning : If
content
is not empty,contentUrl
will not be used Warning : To insert the content into the file during building process, mode must include Write Permission
File Methods
Read the contentUrl
path and fill the current file content,
newFile.read(contentUrl)
contentUrl -> A path to read file to file the current file content
if
contentUrl
parameter is empty, it uses current filecontentUrl
Example:
const FolderBuilder = require('@tubu/folder-builder');
// Create a new FolderBuilder instance with path
const fb = new FolderBuilder(__dirname);
// Create a new file instance with name
const newFile = fb.createFile("sample.txt");
// Assume that `sample.txt` contains 'Hello'
const readFile = newFile.read('/sample/sample.txt');
// It prompts 'Hello'
console.log(readFile.content);
Render the current file content, and return rendered file instance
newFile.render(renderObj)
Object -> Key-Value to render object inside the current file content, it uses $[key] to refer a key inside the current file content
Example
const FolderBuilder = require('@tubu/folder-builder');
// Create a new FolderBuilder instance with path
const fb = new FolderBuilder(__dirname);
// Create a new folder instance with name
const newFolder = fb.createFolder('ex5');
// Create a new file with template format by using $[key]
const newFileTemplate = fb.createFile({
name: 'sample.sh',
content: `#!/bin/bash
echo hello, $[name]
if [ $[age] -gt 18 ]
then
echo You $[name]! You are adult.
fi
`,
mode: 0o555,
});
// Basic render created template
const renderedNewFile = newFileTemplate.render({
name: 'FolderBuilder User',
age: 21,
});
// Adding a rendered bash file into created folder
newFolder.addFile(renderedNewFile);
// Build a new folder
newFolder.build().catch(console.error);
// So, it will be executed
// ./ex5/sample.sh
// -> hello from FolderBuilder User
// -> You FolderBuilder User! You are adult
For more examples, look at examples folder .