@sugarcoated/royal-disk
v1.0.0
Published
Manipulate the file system.
Downloads
1
Readme
Node FS has a bunch of wrappers all over the internet. Here's mine.
- View the source at src/statics/Disk.js
- View the NPM package at @sugarcoated/royal-disk
API Reference
.name(path)
Derives the name of a file from a path.
path
is expected as aString
representing the path to a file.Disk.name('./myFile.txt'); // myFile
.ext(path)
Derives the extension from a file using Path#extname
. Returns the extension as a String
.
path
is expected as aString
representing the path to the file.Disk.ext('/myFile.txt'); // .txt
.file(path, extension)
Derive the file name in a path.
path
is expected as aString
, representing the path to the file.extension
is expected as aString
, representing the extension of the file.Disk.file('/myFile', '.txt'); // myFile.txt
.directory(path)
Derives the directory a file is located in. Returns the directory as a String
.
path
is expected as aString
, representing the path to the file.Disk.directory('/myFolder/myFile.txt'); // myFolder
.readFile(path, [encoding])
Reads a file. Returns a Promise
that resolves with the file contents as a String
.
path
is expected as aString
, representing the path to the file.encoding
is an optional parameter, expected as aString
, representing the encoding of the file to read. If one is not specified, this defaults toutf8
.Disk.readFile('/myFile.txt');
.readFiles(path, [recursive])
Reads files within a directory, optionally recursively. Returns a Promise
that resolves with a Collection
containing the contents of the files as String
s, or further Collection
s containing further file contents as Strings.
The returned Collection
will only contain further Collection
s should the recursive
parameter be passed as true
, as each contained Collection
represents a directory that was read, and will be mapped under the directory's name in the base Collection
.
path
is expected as aString
, representing the path to the file.recursive
is expected as aBoolean
, representing whether or not to recursively path through the directory. Whenrecursive
istrue
, each contained directory will be mapped as aCollection
of that directory's file's contents.Disk.readFiles('/myFolder');
.appendFile(path, content)
Appends content to a file. Returns Promise
that resolves with a Boolean
representing whether or not the operation was successful.
path
is expected as aString
, representing the path to the file.content
is expected as aString
, representing the content to append to the file.Disk.appendFile('/myFile.txt'); // .txt
.renameFile(path, name)
Renames a file. Returns a Promise
that resolves with a Boolean
representing whether or not the operation was successful.
path
is expected as aString
, representing the path to the file.name
is expected as aString
, representing the new name of the file.Disk.appendFile('/myFile.txt');
.writeFile(path, [contents])
Writes/creates a file. Returns a Promise
that resolves with a Boolean
representing whether or not the operation was successful.
path
is expected as aString
, representing the path to the new file.contents
is an optional parameter, expected as aString
, representing the contents of the new file.Disk.writeFile('/myFile.txt');
.deleteFile(path)
Deletes a file. Returns with a Promise
that resolves with a Boolean
representing whether or not the operation was successful.
path
is expected as aString
, representing the path to the file to delete.Disk.deleteFile('/myFile.txt');
.tryDirectory(path)
Attempts to access a directory. Returns a Promise
that resolves with a Boolean
representing whether access was granted.
path
is expected as aString
, representing the path to the directory.Disk.tryDirectory('/myFolder/');
.readDirectory(path)
Read the listing of a directory. Returns a Promise
that resolves with an Array.<String>
, each String
being the name of a file or [further] directory within the directory that was read.
path
is expected as aString
, representing the path to the directory to read.Disk.readDirectory('/myFolder/');
.writeDirectory(path)
Creates a directory. Returns a Promise
that resolves with a Boolean
representing whether or not the operation was successful.
path
is expected as aString
representing the path to the new directory.Disk.writeDirectory('/myFolder/');
.deleteDirectory(path)
Deletes a directory. Returns a Promise
that resolves with a Boolean
representing whether or not the operation was a success, and rejects with an UnhandledPromiseRejection
containing the Error
thrown.
path
is expected as aString
, representing the path to the directory to delete.Disk.deleteDirectory('/myFolder/');