This package is meant to do powerful files operations with a simple API, inside NodeJS applications. All methods will not throw anything if there is something wrong, so it will not break your running server.
Targeted files which does not exists are just ignored, the same way JQuery target DOM nodes.
By default, console logs are enabled to show you what this package does. A Typescript definition file is along the main Javasript file. It's all automatic if you use Typescript.
If you install the colors npm package, you'll have nice logs. Colors is not a dependency so the bundle stays light if you don't need colors.
Target files and folders
Target an existing file
Target an existing folder
Target a file or a folder
Target a file or a folder which does not exist yet
Target all files inside a folder
Target all folders inside a folder
Target all files and folders inside a folder
Target all images inside a folder
Target files that have an extension and are inside sub-folders of a folder
Target all images wherever they are inside a sub-folder
Target all files wherever they are inside a sub-folder but do not target images
Browse files, folders and globs
Returns an array of targeted existing files as strings.
Browse a list
Files.getFiles('*').all( file => console.log(file) )
Will browse through existing files. Here, file is a string, not a Files object.
Show glob used to target files or folders
Will return '*' as a string.
Will return 'not-existing-yet-file.txt' as a string.
Check if a file exists
Will return true if there is a txt file inside the folder. In fact, it will just check that files.length is more than 0.
Manage files and folders
Delete a file
Be careful with this method ! Will show deleted files in console if verbose is not disabled. If file does not exists, nothing will happen. Check files.length to now how many files are deleted.
Delete files and folders inside a folder
Move a file
Note the trailing slash. This is important here otherwise the file will be renamed.
Rename a file
No trailing slash here, so this is a renaming.
Copy files into a new folder
Copy a file
Read a file
Will return file content as string
Write a new file
Override an existing file
Append a line to a file
Files.any('existing-or-not.log').append('new log line')
Append content to a file without new line
Files.any('existing-or-not.log').append('new log part', false)
Alter content of a file
Alter JSON content
Rename several files
TODO : Other examples.
Enable or disable logs
Files.setVerbose( true / false )
Common issues with API
Single files operations
You will face problems if you try to do single files operation on multiple targeted files.
Single file methods :
Those methods uses the glob (which is here '*') and not the files list, because the file may not exist yet.
Files.getFiles('*').write('new file content')
This will not work, because this method is not usable on several files at once.
Multiple files to one file destination
Those methods will target one file as destination if there is no trailing slash on destination path.
Example : Copying several files to a single file
Without trailing slash, only the last file targeted will be renamed as 'output' This is surely not what you want !
Example : Moving several files to a single file
Same as above