@sugarcoated/royal-disk
v1.0.0
Published
Manipulate the file system.
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.
pathis expected as aStringrepresenting 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.
pathis expected as aStringrepresenting the path to the file.Disk.ext('/myFile.txt'); // .txt
.file(path, extension)
Derive the file name in a path.
pathis expected as aString, representing the path to the file.extensionis 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.
pathis 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.
pathis expected as aString, representing the path to the file.encodingis 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 Strings, or further Collections containing further file contents as Strings. The returned Collection will only contain further Collections 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.
pathis expected as aString, representing the path to the file.recursiveis expected as aBoolean, representing whether or not to recursively path through the directory. Whenrecursiveistrue, each contained directory will be mapped as aCollectionof 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.
pathis expected as aString, representing the path to the file.contentis 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.
pathis expected as aString, representing the path to the file.nameis 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.
pathis expected as aString, representing the path to the new file.contentsis 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.
pathis 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.
pathis 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.
pathis 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.
pathis expected as aStringrepresenting 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.
pathis expected as aString, representing the path to the directory to delete.Disk.deleteDirectory('/myFolder/');
