meiro
v0.0.1
Published
Automatically move files from one folder to another
Downloads
4
Readme
meiro
Meiro is a convenient wrapper using
chokidar
to automatically move files around your system*. It's currently still in development and hasn't been tested alot.
Getting Started
Install with npm (meiro uses chokidar
for file detection)
npm install meiro
npm install chokidar
Then require and initialize it with chokidar.
const chokidar = require('chokidar');
const meiro = require('meiro')({
chokidar : chokidar,
logger : console.log /* not yet implemented */
});
Methods
- meiro.startWatcher(obj)
- Returns an instance of
FSWatcher
fromchokidar.watch()
- Starts a new watcher which automatically moves files from Folder A to Folder B.
- Example:
// async/await
const watcher = await meiro.getWatcher({ /* options */});
// default promise
meiro.startWatcher({ /* options */})
.then( watcher => {})
.catch( e => {});
// watcher is an instance of FSWatcher from chokidar.watch()
// with already attached event handler for 'add' event
Options for meiro.startWatcher(options)
watchFolder (required)
: Folder from which files should get moved from. Must be a directory. Uses globe pattern for wildcard*
subfolder. Example:C:/Documents/*/
Will look in each subfolder ofDocuments
.targetExt (optional)
: Only files with specified file extension will be moved. Example:"mov"
will only look for *.mov files. You can only pass an array like["mov", "mxf"]
to look for multiple file extensions. If not specified all files will be moved.destination (required)
: Folder to which files should get moved to. Must be a directory.before (optional)
: Takes a function with 1 parameter. Will be triggered if a matching file was found, before it gets moved. Parameter is a string value with the full path to the matching file. Should returntrue
if the file should get moved,false
if not. Can be used for additional filtering. Example:
let options = {
before : (pathToFile) => {
return true /* File should get moved */
}
}
filename (optional)
: Takes a function with 1 parameter. Will be triggered before the file gets moved. Parameter is a string value with the filename (incl. ext). Should return a string value with the new filename. You need to take care of the file extension yourself. Can be used to rename files. Example:
let options = {
filename : (filename) => {
return Date.now() + path.extname(filename);
// 1574954935145.mov
}
}
after(optional)
: Takes a function with 2 parameters. Will be triggerd after the file was moved. First parameter is a string value with the old path to the file, second parameter is a string value with the new path to the file. Example:
let options = {
after : (from, to) => {
console.log(`File was moved from ${from} to ${to}`);
}
}
ignoreInitial (optional)
- defaut : false - If set tofalse
then meiro already gets triggered for existing files. If set totrue
meiro only gets triggered for new files.disableGlobbing (optional)
- default : false - If set totrue
then the string passed towatchFolder
is treated as literal path name, even if it looks like a glob pattern.awaitWriteFinish (optional)
- default : false - By default, the meiro will fire when a file first appears on disk, before the entire file has been written.options.awaitWriteFinish
can be set to an object in order to adjust timing params:awaitWriteFinish.stabilityThreshold
- default : 6000 - Amount of time in milliseconds for a file size to remain constant before emitting its event.awaitWriteFinish.pollInterval
- default: 200 - File size polling interval, in milliseconds.
How does it work ?
Meiro heavily depends on chokidar
. Chokidar does most of the work underground to detect new files. Meiro is just a convenient wrapper in order to automatically move files from A to B. Check out chokidar
for more details - it's awesome!
Example Usecase 1 : If you get multiple files of various filetypes (extension) written into one directory (e.g. from another service) and you want to sort them automatically into specific subdirectories. For example *.mov
& *.mxf
into a ./video
directory and *.jpg
into a ./images
directory.
Example Usecase 2 : If you get multiple files written into multiple directories across your system (e.g. from another service) and you want to automatically group them into one specific main folder.