myfs
My lightweight, Node-specific, no dependancy, simplified, cross-platform methods for manipulating files and folders syncronously with Node.js.
Install
npm install myfs --save
... or
yarn add myfs
Quick Reference
NOTE: See the "docs" folder details (this is just a basic listing).
Method | Arguments | Description |
---|---|---|
copy | src, dest, binary | Copies a file from one location to another. |
cp | src, dest, binary | alias for copy |
cpdir | from, to | Copies the entire folder's heirarchy folder from one location to another. If the other location doesn't exists, it will be constructed. |
empty | who, dryRun | Recursively empties a folder of all it's contents (and all the sub-folder's contents), but leaves the source folder. |
exists | src | Checks to see if a file or folder exists. |
launch | target, opts | A file launcher. Opens stuff like websites, files, executables using the native system program. |
list | from, filter, recursive, store | Read a folder and returns an object containing a "files" array and a "dirs" array. Each array lists full system paths. |
listExt | from, exts, recursive | Returns an array of paths for files that have the extension(s). The ext argument can be an array to return multiple extensions. |
ls | alias for list | |
makedir | destination | Creates a folder at the specified location. The sub-folder heirarchy is constructed as needed. |
move | src | Moves a file or folder. |
rename | src | Alias for move. |
open | src, binary | Reads the text or binary data out of a file. Use the "binary" (boolean) argument for non-text files. |
rm | src | Deletes a file from the system. |
rmdir | who, dryRun | Recursively removes a folder and all of it's sub-folders as well. |
save | src, data, binary | Saves text data to a file. Overwrites entire file with provided data. |
write | src, data, binary | Alias for save. |
isFile | src | Checks to see if src is a file, as opposed to exists, which checks if either file OR folder exists. |
isDir | src | Checks to see if src is a folder, as opposed to exists, which checks if either file OR folder exists. |
touch | src | Creates or updates the timestamp on a specific file or folder. |
dupe | src | Duplicates a file "in place" by making a copy with appended "copy N" in the file name. |
isBinary | src | A cheap/fast check to see if a file's extension is in a list of known binary extensions. |
Path Stuff
Method | Arguments | Description |
---|---|---|
addSlash | path | Adds a trailing slash from path (if doesn't exist). |
addTrailingSlash | alias for addSlash | |
removeSlash | path | Removes a trailing slash from path (if exists). |
removeTrailingSlash | path | alias for removeSlash |
resolve | path... | Generates an absolute path based on the provided arguments. |
base | name | alias for basename |
basename | path, ext | Returns the last portion of a path, generally the "filename". |
clean | arg | Normalizes slashes by converting double \ to single \ and / to \ or \ tp / based on the current platform requirements. |
dir | alias for dirname | |
ext | src | Returns the bare, base extension (no dot). |
filename | src | alias for basename |
format | obj | Combines the elements of an object into a string. The opposite of path.parse(). |
isAbsolute | src | Determines if path is an absolute path. |
join | paths... | Joins path segments and resolves relativity. |
name | path | npath.name("/foo/bar/bob.txt") --> "bob" |
normalize | path | Resolves ".." and "." portions of a path. Reduces double slashes to single (e.g. // -> / ). Forces back-slashes to forward slashes (e.g. \ -> / ). Retains trailing slash if exists. |
parent | path | Returns the path to the parent folder that the item resides within. |
parse | path | Note that this uses the mind-warping name, basename, ext of the default Node.path.parse() plus some props that don't hurt my brain. Extracts basic path and file parts. root, dir, base, ext, name, ext2, extension, basename, filename, parent |
relative | from, to | Creates a relative path between "from" and "to" |
cwd | tack | Gets the current working directory. Resolves the argument to the path. |
swapExt | src | Changes the path's (or filename's) extension. |
Properties
Property | Description |
---|---|
sep | The kind of seperator used in paths. Windows = \ or POSIX = / |
delimiter | Platform environment $PATH delimiter. |
du | Access to the underlaying directory utility class for access to additional methods that are not so common. |
fu | Access to the underlaying file utility class for access to additional methods that are not so common. |
path | Essentially the same as Node's built-in path, but has a few extras and unified cross-platform'd. This provides access to the underlaying path utility class for access to additional methods that are not so common. |
Usage
var myfs = require('myfs');
var data = myfs.open("/path/to/file.txt");
myfs.save("/path/to/file.txt", "This is the contents of my file.");
Open / Read
var myfile = myfs.open("/path/to/folder/file1.txt"); // Yeilds the text contents of the file.
var myfile = myfs.read("/path/to/folder/file1.txt"); // same as above
Save / Write
var myfile = myfs.save("/path/to/folder/file1.txt", data); // Wites the text data to the file.
var myfile = myfs.write("/path/to/folder/file1.txt", data); // same as above
Listing files from a folder
var mylist = myfs.list("/path/to/folder");
//
// Yeilds
//
// {
// files : [
// "/path/to/folder/file1.txt",
// "/path/to/folder/file2.txt",
// "/path/to/folder/file3.txt"
// ],
// dirs : [
// "/path/to/folder/folder1",
// "/path/to/folder/folder2",
// "/path/to/folder/folder3"
// ]
// }
Listing only files of X extension.
var mylist = myfs.listExt("/path/to/folder", "txt");
//
// Yeilds
// [
// "/path/to/folder/file1.txt",
// "/path/to/folder/file2.txt",
// "/path/to/folder/file3.txt"
// ]
Listing files of X & Y extension
var mylist = myfs.listExt("/path/to/folder", ["txt", "foo"]);
//
// Yeilds
// [
// "/path/to/folder/file1.txt",
// "/path/to/folder/file2.txt",
// "/path/to/folder/bob1.foo",
// "/path/to/folder/bob2.foo"
// ]
Change log
1.0.22 - Oct 20, 2019
- Added isBinary and dupe file methods.
1.021 - Dec. 20, 2018
- readExt/listExt properly documented and available... One was missing and the docs explained the other, now they're both in their.
1.0.19 - Sept. 8, 2018
- readExt/listExt would not capture file with upper-case extensions. Made upper/lower-case agnostic. When call this method, use lower-case strings for your extension(s).
- added "path" and "src" to parse, both of which return the original source path string provided.
- fixed issue with readdir when stats fails (mac)
1.0.14 - August 14, 2018
- added filter to the
readExt
1.0.13 - August 14, 2018
- Check if dir exists before attempt to delete
copy
now copies folders too.- Added
touch
1.0.12 - May 1, 2018
- ensure exists added into a couple other places in fileutils
- fileutils.write now honors a string in the binary argument
- added better documentation on readdir filter
1.0.8 - May 1, 2018
- Added swapExt
License
MIT - Mike Gieson