file-assistant v1.3.1
Description
file-assistant is a module that creates, copies or moves the folders and files into the specified path or modifies the files' content according to the given Array structure object (or .json file path) instructions.
- Any bugs found? Give me to know on dev.rafalko@gmail.com or on Github
The Idea
- Create the abstract
structure objectof the folders and files, that you want to create or use to modify the existing folders and files structure. - Pass your
structure objectas the argument throughfile-assistantfunction - It will execute all the instructions (create, copy, move, remove, merge, modify, overwrite files and folders and overwrite or append content to the files).
Features
const fileAssistant = require('file-assistant');The fileAssistant contains 4 methods:
fileAssistantto handle the folders and files due to thestructure object[see below]fileAssistant.structurizeto automatically generate thestructure objectfor the elements of the given folder path [see below]fileAssistant.compareto compare the differences between two folders (which elements are extraneous, missing and already existing) [see below]fileAssistant.ensureDirto create the folders chain recursively [see below]
List of contents:
- Common tips
- Installation
- Tests
- Usage
- Parameters
- Errors
Structure objectfileAssistant.structurizemethodfileAssistant.comparemethodfileAssistant.ensureDirmethod- Samples
OMG list
If you are confused by the excess of the description, there is a list of common tips:
Files handling:
- I just want to create some empty files [tip]
- I just want to create some empty file (or replace the existing file with the new empty one) [tip]
- I want to copy the file and paste it in the destination folder [tip]
- I want to copy the file and paste it in the destination folder (or replace the file if it already exists there) [tip]
- I want to cut the file and paste it in the destination folder [tip]
- I want to cut the file and paste it in the destination folder (or replace the file if it already exists there) [tip]
- I want to append some extra content to the existing file [tip]
- I want to overwrite the content of the existing file with the new content [tip]
- I want to take the content from one file and append it to another file [tip]
- I want to take the content from one file and overwrite another file's content with it [tip]
Folders handling:
- I just want to create empty folder [tip]
- I want to create the folder with some files and subfolders [tip]
- I want to empty the existing folder [tip]
- I want to empty the existing folder and then add new files and folders there [tip]
- I want to add some extra files and folders to the existing folder [tip]
- I want to replace some files in the existing folder [tip]
- I want to copy the folder with its contents and paste it in the destination folder [tip]
- I want to copy the folder with its contents and paste it in the destination folder (or replace the folder with its all contents if it already exists there) [tip]
- I want to cut the folder with its contents and paste it in the destination folder [tip]
- I want to cut the folder with its contents and paste it in the destination folder (or replace the folder with its all contents if it already exists there) [tip]
- I want to copy only the missing files and subfolders from one folder to another [tip]
- I want to copy the missing files and subfolders from one folder to another (and replace the already existing files there) [tip]
Installation
npm install file-assistant
Tests
> git clone https://github.com/devrafalko/file-assistant
> cd file-assistant
> npm install
> npm test
# or
> npm test deepUsage
fileAssistant(root,structure,done[,each])
const fileAssistant = require('file-assistant');
const root = './dist',
const structure = [
{file:'index.html'},
{file:'styles.css', copy:'./styles/main.css'}
];
fileAssistant(root, structure, done, each);
function done(o){
console.log(o.error);
console.log(o.files);
console.log(o.dirs);
}
function each(o){
console.log(o.success);
console.log(o.item);
console.log(o.relative);
console.log(o.from);
}root String
- It should indicate the String folder path where the elements specified in the
structureobject should be handled. - If the
rootdirectory does not exist, it is created. - If there is some problem with the
root, the Error is passed throughdonecallback.
structure Array|String
- The Array
structureargument should contain the list of files and folders and specify the actions that should be undertaken for them in therootfolder. [See below] - The String
structureargument should indicate the path to the.jsonfile that contain the list of files and folders and specify the actions that should be undertaken for them in therootfolder. [See below] - See how to create the valid
structure objector just follow the instructions passed throughdonecallbackerrorproperty.
You can also generate the
structure object(and.jsonfile containing that structure) for the chosen folder withfileAssistant.structurizemethod.
done Function
- This is the callback function
- It is fired once when all folders and files are (un)successfully created or if some error has occured.
- The one Object argument is passed through
donecallback function with the following properties:rootReturns the String(#root-string) path argument passed through the module functionerrorReturns Error if something went wrong and the continuation of creating folders and files was aborted, eg. if thestructureargument is invalid. Otherwise returnsnull. See also how the errors are handled.dirsReturns the Object object of the following properties:successArray list of the absolute paths of all created/copied/moved/merged/overwritten folders.warningArray list of the absolute paths of all intentionally aborted actions on folders (eg. if the folder was not overwritten, as intended)failureArray list of the absolute paths of all unsuccessful actions on folders (eg. if the folder was inaccessible)
filesReturns the Object object of the following properties:successArray list of the absolute paths of all created/copied/moved/overwritten/modified files.warningArray list of the absolute paths of all intentionally aborted actions on files (eg. if the file was not overwritten, as intended)failureArray list of the absolute paths of all unsuccessful actions on files (eg. if the file was inaccessible)
unrecognizedReturns the Object object of the following properties:failureArray list of the absolute paths of all elements that the access was denied for
each Function (optional)
- This is the callback function
- It is fired for each file and folder separately when the action for the file or folder is (un)successfully done.
- The one Object argument is passed through
eachcallback function with the following properties:success
Returns String message, about the successful action. Otherwise returnsnull.
eg.'The given content was successfully appended to the "script.js" file.'.warning
Returns String message, about the aborted or not-fully-done action. Otherwise returnsnull.
eg.'The already existing folder "components/footer" could not be overwritten by the newly created folder, due to the "overwrite" property settings.'.failure
Returns String message, about the failed action. Otherwise returnsnull.
eg.'The item of the path "modules/locked.js" is inaccessible and could not be moved into the "dist/script.js" path.'.item
Returns String'file'for file element,'dir'for folder element or'unrecognized'when the access to the element was denied.action
Returns the String action undertaken:overwritten
Returns Booleantrueif the file or folder has been overwritten. Otherwise returnsfalse.modified
Returns Booleantrueif thebeforeWriteproperty was used and thefilewas created/overwritten with the modified content. Otherwise returnsfalse.from
Returns the String path to the element from which the file, folder or content was copied, moved or merged (when the propertycopy,move,mergeorwriteFromwas defined in thestructure object). Returnsnullif the file or folder was created or modified (when the propertycontents,writeor none property was defined in thestructure object).absolute
Returns String absolute path to the current element.relative
Returns String relative path to the current element.root
Returns the String(#root-string) path argument passed through the module function.
Handling errors
- The one and only rule that you have to respect is to keep the
donecallback function as a third parameter in thefileAssistantmodule function execution, otherwise theTypeErrorwill be thrown. - Any other errors will be then passed through the
donecallback function, so your app would not collapse. - If you run the
fileAssistantmodule function with an incorrectrootorstructurearguments, you are informed in thedonecallback function about that, so you can follow these instructions to create a validstructure object. - Any failures of files and folders handling are passed through the
doneandeachcallback functions aswarningandfailureproperties. - The failed action of one element does not abort the actions for subsequent files and folders. The
fileAssistantmodule tries to execute as many actions as possible, then returns results indonecallback function (and each single time ineachcallback function as well).
Structure object
Comparison of structure object and the final folders and files structure
Assuming that you want to generate the following folders and files structure in the ./dist path:
dist
├ scripts
│ ├ index.js
│ └ ajax.js
├ styles
│ ├ css
│ │ ├ layout.css
│ │ └ media.css
│ └ scss
│ └ mixins.scss
└ templates
├ main.html
├ menubar.html
├ login.html
└ contact.htmluse this javascript syntax:
const fileAssistant = require('file-assistant');
const destination = './dist';
const structure = [
{dir:'scripts', contents:[
{file:'index.js'},
{file:'ajax.js'}
]},
{dir:'styles', contents:[
{dir:'css', contents:[
{file:'layout.css'},
{file:'media.css'}
]},
{dir:'scss', contents:[
{file:'mixins.scss'}
]}
]},
{dir:'templates', contents:[
{file:'header.html'},
{file:'navbar.html'}
]}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});JSON structure object
The structure object can be stored in the JSON file.
[
{"dir":"scripts", "contents":[
{"file":"index.js"},
{"file":"ajax.js"}
]}
]and then loaded in the structure parameter:
const fileAssistant = require('file-assistant');
const destination = './dist';
const structure = './templates/structure.json';
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});or in the contents property inside of the structure object:
const fileAssistant = require('file-assistant');
const destination = './dist';
const structure = [
{dir:'styles'},
{dir:'scripts', contents:'./templates/structure.json'}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});or even loaded in another .json file:
[
{"dir":"scripts", "contents":[
{"file":"index.js"},
{"file":"ajax.js"}
]},
{"dir":"scripts", "contents":"./templates/scripts.json"}
]You can also generate the
structure object(and.jsonfile containing that structure) for the chosen folder withfileAssistant.structurizemethod.
structure object Properties:
Each Object Item of Array scope can contain the following properties:
file
Type: String
Default: undefined
Description:
- Indicates the name (and extension) of new file, eg.
'index.html','ajax.js'. - If you want to overwrite the existing file with the new one, use
overwriteproperty. - Each Item must contain at least either
fileordirproperty. Item cannot contain bothdirandfileproperty. They exclude each other. Usedirto create folder orfileto create a file. - Avoid using slashes in the value of
dirandfileproperties. In order to create subfolders, usecontentsproperty. (See how thestructure objecttree should be built)
Sample:
{file:'style.css'}It creates a newstyle.cssfile with empty content in the specifiedrootpath.- See also the samples list
dir
Type: String
Default: undefined
Description:
- Indicates the name of new folder, eg.
'styles','modules' - If you want to overwrite the existing folder with the new one, use
overwriteproperty. - Each Item must contain at least either
fileordirproperty. Item cannot contain bothdirandfileproperty. They exclude each other. Usedirto create folder orfileto create a file. - Avoid using slashes in the value of
dirandfileproperties. In order to create subfolders, usecontentsproperty. (See how thestructure objecttree should be built)
Sample:
{dir:'prod'}
It creates a new emptyprodfolder in the specifiedrootpath.- See also the samples list
contents
content is also valid
Type: Array|String
Default: undefined
Target: dir
Description:
- The
contentsproperty defines thedir(local)structure object, that should be created in thedirfolder. - The
contentsproperty can indicate the Array object that contains thestructure objectthat should be created in the defineddirfolder. - The
contentsproperty can also indicate the String path to the.jsonfile that contains thestructure objectthat should be created in the defineddirfolder. - If you want to overwrite the existing folder with the new one (and its
contentselements), useoverwriteproperty. Item cannot contain
move,copy,merge,contents,writeandwriteFromproperties at the same time. They exclude each other.
You can also generate the
structure object(and.jsonfile containing that structure) for the chosen folder withfileAssistant.structurizemethod.
move
Type: String
Default: undefined
Target: file|dir
Description:
- When the
fileproperty is specified in the Item, themoveproperty indicates the path to the file that should be moved to the path of the definedfileproperty. - When the
dirproperty is specified in the Item, themoveproperty indicates the path to the folder that should be moved (with its all contents) to the path of the defineddirproperty. - If you want to overwrite the existing file or folder by the
moveone, useoverwriteproperty. - Mind, that the path value of the
moveproperty is not relative to theroot. It is recommended to use the paths related to the__dirnameor the absolute paths. - Item cannot contain
move,copy,merge,contents,writeandwriteFromproperties at the same time. They exclude each other.
Sample
{file:'style.css', move:'./dist/main.css'}
It cuts the./dist/main.cssfile and pastes it into the specifiedrootpath asstyle.cssfile.{dir:'prod', move:'./dist'}It cuts thedistfolder with its all contents and pastes it into the specifiedrootpath asprodfolder.- See also the samples list
copy
Type: String
Default: undefined
Target: file|dir
Description:
- When the
fileproperty is specified in the Item, thecopyproperty indicates the path to the file that should be copied to the path of the definedfileproperty. - When the
dirproperty is specified in the Item, thecopyproperty indicates the path to the folder that should be copied (with its all contents) to the path of the defineddirproperty. - If you want to overwrite the existing file or folder by the
copyone, useoverwriteproperty. - Mind, that the path value of the
copyproperty is not relative to therootpath. It is recommended to use the paths related to the__dirnameor the absolute paths. - Item cannot contain
move,copy,merge,contents,writeandwriteFromproperties at the same time. They exclude each other.
Sample:
{file:'style.css', copy:'./dist/main.css'}
It copies the./dist/main.cssfile and pastes it into the specifiedrootpath asstyle.cssfile.{dir:'prod', copy:'./dist'}
It copies adistfolder with its all contents and pastes it into the specifiedrootpath asprodfolder.- See also the samples list
merge
Type: String
Default: undefined
Target: dir
Description:
- It indicates the String path to the folder which contents should be merged with the contents of the existing
dirfolder. - If the
dirfolder does not exist, the contents are just copied to the path of the defineddirproperty. - If you want to overwrite the existing files by the
mergeones, useoverwriteproperty. - Item cannot contain
move,copy,merge,contents,writeandwriteFromproperties at the same time. They exclude each other.
Sample:
{dir:'prod', merge:'./dist'}- It copies a
distfolder with its all contents and pastes it into the specifiedrootpath asprodfolder. Ifprodfolder already exists, its contents are merged with the contents of./distfolder. - See also the samples list
write
Type: String
Default: undefined
Target: file
Description:
- The
writeproperty indicates the String content that should be used as the content of the newfile. - If you want to overwrite the already existing file's content rather than append this content, use
overwriteproperty. - Item cannot contain
move,copy,merge,contents,writeandwriteFromproperties at the same time. They exclude each other.
Sample:
{file:'style.css', write:'body{margin:0px}'}
It creates a newstyle.cssfile in the specifiedrootpath with thebody{margin:0px}content.- See also the samples list
writeFrom
writefrom is also valid
Type: String
Default: undefined
Target: file
Description:
- The
dataproperty indicates the String path to the file, which content should be used as the content of the newfile. - If you want to overwrite the already existing file's content rather than append this content, use
overwriteproperty. - Item cannot contain
move,copy,merge,contents,writeandwriteFromproperties at the same time. They exclude each other.
Sample:
{file:'style.css', writeFrom:'./dist/main.css'}
It creates a newstyle.cssfile in the specifiedrootpath with the content read from the./dist/main.cssfile.- See also the samples list
beforeWrite
beforewrite is also valid
Type: Function
Default: undefined
Target: file
Description:
- This property allows to modify the content for the
filebefore this file is created/overwritten. - This property can be used in combination with the following properties:
write: the value content is taken and passed through thebeforeWritefunctionwriteFrom,copy,move: the content is read from the file and passed through thebeforeWritefunction- Mind that if the
beforeWriteproperty is used in combination withwriteorwriteFromandoverwrite:trueproperties, the new modified content is appended to thefilerather than overwrite the current content.
- If the Function
beforeWriteis defined, this function is executed with the following arguments:- 0
content: It gives the access to the (utf8) content taken fromwrite,writeFrom,moveorcopy - 1
resolveThis is callback function. When the new content is already modified, callresolveto continue the process of handling files and folder by thefile-assistantpackage. Remember to pass String|Buffermodifieddata throughresolvecallback function:resolve(modified), othwerwise the action will be failed for this file. - 2
rejectThis is callback function. If something went wrong with modifying the content and you want to abort the action of creating the file with the new modified content, callrejectcallback function. Then theeachcallback will be called withfailuremessage. The additional Stringmessagecan be passed throughrejectcallback function. It will be appended to theeachcallbackfailuremessage:reject(message)
- 0
- The origin
write,writeFrom,moveorcopyfile's content is not affected by this function. The modified content is used only to modify thefilecontent.
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'data.txt', writeFrom:'./dist/data.txt', beforeWrite:parseData}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});
function parseData(getData, resolve, reject){
//getData is the utf-8 content taken from './dist/data.txt' file
if(getData.length>100) {
reject('The text is too long!');
} else {
const modifiedData = getData.toUpperCase();
resolve(modifiedData);
}
}overwrite
Type: Boolean
Default: false
Target: file|dir
Description: It is useful, if the file or folder that you are about to create, move, copy, merge or modify already exists in the specified path. If the file or folder does not exist, the effect will be the same regardless the overwrite property is used with true or false value (or undefined).
- if
overwriteistrue, it removes the existing file or folder and creates the new one, according to thestructureoptions - if
overwriteisfalse(default), it does not remove the existing file or folder, instead of that:
This is how it behaves in words of its syllable:
1. Assume that the style.css file already exists in the destination root path:
{file:'style.css', overwrite:true}
- It removes the existing
style.cssfile and replaces it with the new emptystyle.cssfile.
{file:'style.css', overwrite:false}
- The action for this existing file is aborted. The existing
style.cssfile is not removed and the newstyle.cssis not created. - The
eachwarningproperty will warn you about aborted action and thesuccessproperty will benull.
{file:'style.css', copy:'./dist/main.css', overwrite:true}
- It removes the existing
style.cssfile and replaces it with the copied./dist/main.cssfile.
{file:'style.css', copy:'./dist/main.css', overwrite:false}
- The action for this existing file is aborted. The existing
style.cssfile is not removed and the./dist/main.cssfile is not copied/pasted. - The
eachwarningproperty will warn you about aborted action and thesuccessproperty will benull.
{file:'style.css', move:'./dist/main.css', overwrite:true}
- It removes the existing
style.cssfile and replaces it with the cut./dist/main.cssfile.
{file:'style.css', move:'./dist/main.css', overwrite:false}
- The action for this existing file is aborted. The existing
style.cssfile is not removed and the./dist/main.cssfile is not cut/pasted. - The
eachwarningproperty will warn you about aborted action and thesuccessproperty will benull.
{file:'style.css', write:'body{margin:0px}', overwrite:true}
- It replaces the content of the existing
style.cssfile with the newbody{margin:0px}content.
{file:'style.css', write:'body{margin:0px}', overwrite:false}
- It appends the new
body{margin:0px}content at the end of the content of the existingstyle.cssfile.
{file:'style.css', writeFrom:'./dist/main.css', overwrite:true}
- It replaces the content of the existing
style.cssfile with the content read from the./dist/main.cssfile.
{file:'style.css', writeFrom:'./dist/main.css', overwrite:false}
- It appends the content read from the
./dist/main.cssfile at the end of the content of the existingstyle.cssfile.
2. Assume that the prod folder already exists in the destination root path:
{dir:'prod', overwrite:true}
- It removes the existing
prodfolder and replaces it with the new emptyprodfolder. - If the
contentsproperty is defined in the Item with subfolders and subfiles specified, it also creates all of them inside of the newprodfolder.
{dir:'prod', overwrite:false}
- The action for this existing folder is aborted. The existing
prodfolder is not removed and the newprodfolder is not created. - The
eachwarningproperty will warn you about aborted action and thesuccessproperty will benull. - If the
contentsproperty is defined in the Item with subfolders and subfiles specified, the action for them is undertaken according to their ownoverwritesetting.
In the following sample, if the prod folder already exists, it is not removed and replaced by the new prod folder with all its contents (it keeps its all current contents). The styles.css and index.html have their own overwrite property. If the styles.css file already exists, it is replaced by the new styles.css file. If the index.html file already exists, it is not replaced by the new index.html file.
const structure = [
{dir:'prod', overwrite:false, contents:[
{file:'styles.css', overwrite:true},
{file:'index.html', overwrite:false}
]}
];{dir:'prod', copy:'./dist', overwrite:true}
- It removes the existing
prodfolder and replaces it with the copieddistfolder with all its contents.
{dir:'prod', copy:'./dist', overwrite:false}
- The action for this existing folder is aborted. The existing
prodfolder is not removed and thedistfolder is not copied/pasted. - The
eachwarningproperty will warn you about aborted action and thesuccessproperty will benull.
{dir:'prod', move:'./dist', overwrite:true}
- It removes the existing
prodfolder, and replaces it by the cutdistfolder with all its contents.
{dir:'prod', move:'./dist', overwrite:false}
- The action for this existing folder is aborted. The existing
prodfolder is not removed and thedistfolder is not cut/pasted. - The
eachwarningproperty will warn you about aborted action and thesuccessproperty will benull.
{dir:'prod', merge:'./dist', overwrite:true}
- The following steps are taken:
- it compares the structure of
distfolder and the structure ofprodfolder - all the files and folders that do not exist in the
prodfolder are copied fromdistfolder into it - all the folders that already exist in the
prodfolder are not removed and are not replaced by theirdistequivalents - only the files that already exist in the
prodfolder (and all its subfolders) are removed and are replaced by theirdistequivalents (of the same relative path).
- it compares the structure of
#The './dist' folder to be merged:
dist
├ scripts
│ ├ index.js
│ └ ajax.js
└ styles
└ sayout.css
#Already existed 'prod' folder:
prod
├ scripts
│ ├ index.js
│ └ utils.js
└ templates
└ login.html
#After merge process:
prod
├ scripts
│ ├ index.js //this file had already existed and was replaced by './dist/scripts/index.js'
│ ├ ajax.js
│ └ utils.js
├ templates
│ └ login.html
└ styles
└ sayout.css{dir:'prod', merge:'./dist', overwrite:false}
- The following steps are taken:
- it compares the structure of
distfolder and the structure ofprodfolder - all the files and folders that do not exist in the
prodfolder are copied fromdistfolder into it - all the folders that already exist in the
prodfolder are not removed and are not replaced by theirdistequivalents - the files that already exist in the
prodfolder (and all its subfolders) are not removed and are not replaced by theirdistequivalents (of the same relative path). Instead of that:
- it compares the structure of
#The './dist' folder to be merged:
dist
├ scripts
│ ├ index.js
│ └ ajax.js
└ styles
└ sayout.css
#Already existed 'prod' folder:
prod
├ scripts
│ ├ index.js
│ └ utils.js
└ templates
└ login.html
#After merge process:
prod
├ scripts
│ ├ index.js //this file had already existed and the action was aborted
│ ├ ajax.js
│ └ utils.js
├ templates
│ └ login.html
└ styles
└ sayout.cssfileAssistant.structurize(path[,json],callback)
Description
It converts the given path folder's contents into the abstract Array(#structure-object) representation of it. You can for example modify the returned object and/or use it as the structure parameter, to create the files and folders due to this returned structure object.
Parameters
path String
- It indicates the String path to the folder, which content will be structurized into the Array
structure objectrepresentation.
json String (optional)
- If specified, it should indicate the String path, where the
.jsonfile with the returned Arraystructure objectshould be created. - If the specified path does not exist, it is created.
- If the specified file name extension is omitted or is not
.json, the correct extension is automatically appended. - If the specified file already exists, it is replaced by the new one.
callback Function
- The
callbackfunction is executed after the operation is done. - The two arguments are passed through the
callbackfunction:
The ./dist folder to be structurized:
dist
├ scripts
│ ├ index.js
│ └ ajax.js
└ styles
└ layout.cssThe returned Array object representation of the given ./dist folder's contents:
const folder = './dist';
const json = './dist/structure.json';
fileAssistant.structurize(folder,json,(err,data)=>{
console.log(data);
/*
[
{dir:'scripts', contents:[
{file:'index.js'},
{file:'ajax.js'}
]},
{dir:'styles', contents:[
{file:'layout.css'}
]}
]
*/
});fileAssistant.compare(model,compared,[config,]callback)
Description
It compares the paths of model and compared folders contents and returns the information about the differences.
According to the given information, you can generate the
structure objectand use it to create, copy, move or modify files or folders.Parameters
modelString
- It indicates the String path to the folder, which contents will be used as the model, that
comparedfolder should match.
compared String
- It indicates the String path to the folder, which elements are compared with the elements of the
modelfolder.
config Object
- if omitted, the parameters are set to their default values; All the elements of all levels are compared.
- You can configure the
comparemethod with the following Objectconfig's properties:depthNumber|null (default:null)
It indicates how deep thecomparefunction should explore the folders in the givenmodelandcompareddirectories.
If set tonull(default) - it compares all subfiles and subfolders of all levels of themodelandcompareddirectories.
If set to 1 - it compares only the one level ofmodelandcomparedelements; eg../styles,./index.html.
If set to 2 - it compares two levels ofmodelandcomparedelements; eg../styles/css,./scripts/ajax.js.
etc.excludeArray|String (default:[])
It indicates the folders' and files' paths that should be ignored and not compared.
If the folder is indicated, neither the folder nor its contents will be compared.
When String, it can indicate the one path to ignore, eg"./bin".
When Array, it can indicate more than one path to ignore, eg.["./node_modules", "./bin"].
The given paths must be relative to themodelandcomparedpaths, otherwise they will be not recognized.
You can ignore needless paths, eg.'./node_modules'or'./.git'to make thecomparemodule faster.
callback Function
- The
callbackfunction is executed after the operation is done. - The two arguments are passed through the
callbackfunction:[0]Error object, if themodelorcomparedpaths are invalid, inaccessible, etc. Otherwise it returnsnull.[1]Object object with the following properties:modelReturns the String absolute path to themodelfoldercomparedReturns the String absolute path to thecomparedfolderdirsmissingReturns the Array list of the relative paths to the folders that exist in themodelfolder, but do not exist in thecomparedfolderexistingReturns the Array list of the relative paths to the folders that exist both in themodelfolder andcomparedfolderextraneousReturns the Array list of the relative paths to the folders that exist in thecomparedfolder, but do not exist in themodelfolder
filesmissingReturns the Array list of the relative paths to the files that exist in themodelfolder, but do not exist in thecomparedfolderexistingReturns the Array list of the relative paths to the files that exist both in themodelfolder andcomparedfolderextraneousReturns the Array list of the relative paths to the files that exist in thecomparedfolder, but do not exist in themodelfolder
inaccessibleReturns the Array list of all elements that the access was denied for.
const model = './dist';
const compared = './prod';
fileAssistant.compare(model,compared,(err,data)=>{
console.log(err);
console.log(data.dirs.missing);
console.log(data.files.extraneous);
});fileAssistant.ensureDir(path,callback)
Description
It creates the folders chain recursively.
If the folder (or its subfolder) already exists it is neither removed nor overwritten.
Parameters
path String
- It defines the String path of the folder, that should be created
- The path can be either absolute or relative
- The path may specify the one folder or the chain of the folders:
'./dist''./dist/modules/es6/libs'
callback String
- It is called when the folder(s) are created.
- It returns one error argument:
nullif the folder(s) have been successfully created, otherwise Error
Samples
Create empty file, but prevent creating the file, if it already exists
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'styles.css', overwrite:false},
{file:'index.html'} //overwrite:false is default, it can be omitted
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Overwrite the existing file with the new (empty) one
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'styles.css', overwrite:true},
{file:'index.html', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Copy the file, but prevent copying the file, if it already exists
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'readme.md', copy:'./templates/docs.txt', overwrite:false}
{file:'LICENSE', copy:'./templates/mit-license.txt'} //overwrite:false is default, it can be omitted
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Overwrite the existing file with the copied one
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'styles.css', overwrite:true},
{file:'index.html', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Move the file, but prevent moving the file, if it already exists
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'styles.css', move:'./dist/bundled.css', overwrite:false},
{file:'main.js', move:'./dist/bundled.js'} //overwrite:false is default, it can be omitted
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Overwrite the existing file with the moved one
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'styles.css', move:'./dist/bundled.css', overwrite:true},
{file:'main.js', move:'./dist/bundled.js', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Create the new file with the given content or append a new content to the existing file
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'styles.css', write:'body:{box-sizing:border-box}', overwrite:false}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Create the new file with the given content or overwrite the existing file's content with the given one
const fileAssistant = require('file-assistant');
const destination = './prod';
const htmlTemplate =
`<html>
<head></head>
<body></body>
</html>`;
const structure = [
{file:'index.html', write:htmlTemplate, overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Append a new content given from another file to the existing file
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'styles.css', writeFrom:'./templates/reset.css', overwrite:false}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Overwrite the existing file's content with the new one, given from another file
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{file:'index.html', writeFrom:'./templates/html-template.html', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Create empty folder
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'lib', overwrite:false},
{dir:'src'} //overwrite:false is default, it can be omitted
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Create folder with contents
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'lib', overwrite:false},
{dir:'src', contents:[
{dir:'styles'},
{dir:'scripts'},
{dir:'tests'}
]} //overwrite:false is default, it can be omitted
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Replace the existing folder with the new empty folder
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'temp', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Replace the existing folder with the new folder with some contents
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'src', contents:[
{dir:'styles', contents:[
{file:'styles.css'}
]},
{dir:'scripts'},
{dir:'tests'}
], overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Add some files to the existing folder, but prevent replacing the existing files
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'styles', contents:[
{file:'styles.css', overwrite:false},
{file:'reset.css', overwrite:false}
]},
], overwrite:false}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Add some files to the existing folder and replace already existing files there
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'styles', contents:[
{file:'styles.css', overwrite:true},
{file:'reset.css', overwrite:true}
]},
], overwrite:false}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Copy the folder with its contents, but prevent replacing the existing folder
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'styles', copy:'./dist/styles', overwrite:false}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Copy the folder and replace the existing folder with all its contents
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'styles', copy:'./dist/styles', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Move the folder with its contents, but prevent replacing the existing folder
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'styles', move:'./dist/styles', overwrite:false}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Move the folder and replace the existing folder with all its contents
const fileAssistant = require('file-assistant');
const destination = './prod';
const structure = [
{dir:'styles', move:'./dist/styles', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Merge the files, but prevent replacing the existing files
const fileAssistant = require('file-assistant');
const destination = './dist';
const structure = [
{dir:'lib', merge:'./project/modules', overwrite:false}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});Merge the files and replace the already existing files
const fileAssistant = require('file-assistant');
const destination = './project';
const structure = [
{dir:'prod', merge:'./project/dist', overwrite:true}
];
fileAssistant(destination, structure, (done)=>{/*...*/}, (each)=>{/*...*/});