wrote
Promise-based write and read operations for Node.js
ES5
The package uses some newer language features. For your convenience, it's been transpiled to be compatible with Node 4. You can use the following snippet.
const wrote =
wrote.createWritable(path=: string): Promise.<Writable>
Create an open Writable stream to the file.
const Writable = const path = const HOME_DIR = const createWritable = const file = path; async { const ws = await console // true console // ~/wrote-35558.data}
If you don't have a file, a new one in the temp directory will be created for you.
async { const ws = await console // true console // /var/folders/s0/1h2g/T/wrote-48315.data}
wrote.exists(path: string): Promise.<boolean>
Check if the path on the filesystem exists. Throws if path is not accessible due to permissions.
const exists = ; async { await // false await // true await // true}
wrote.assertExists(path: string): Promise
Check if the path on the filesystem exists, and throw if it does not, or cannot be accessed.
const assertExists = ; async { try await catch err console // Path unknown-path does not exist. await // ok}
wrote.assertDoesNotExist(path: string): Promise
Check if the path on the filesystem does not exists, and throw if it does, or cannot be accessed.
const assertDoesNotExist = ; async { try await catch err console // Path /wrote/examples/assert-does-not-exist.js exists. await // ok}
wrote.clone({ from: string, to: string, regexes: [] }): Promise
Clone a directory by copying contents of files and creating symlinks. Regular expressions can be used to transform data being copied.
const clone = ; async { const from = './directory' const to = './clone' await // or, // await clone({ to, from })}
wrote.erase(ws: Writable): Promise.<Writable>
Erase file and close stream.
const createWritable erase = const Writable = const path = const HOME_DIR = const file = path; async { const ws = await console // true console // true console // ~/wrote-35558.data await console // true}
wrote.write(ws: Writable, data?: string|Readable): Promise.<Writable>
Pipe a Readable
to the Writable
stream and wait until it is finished, or end
Writable
with given data (pass null
to end stream without any more data).
const write = const assert = const Writable = const testString = 'hello world'const buffer = Bufferconst allRawData = const ws = { allRawData }; async { await console // [ 'hello world' ] assert}
wrote.ensurePath(filePath: string): Promise<string>
Create all required directories for the filepath to exist. If a directory on the way is non-executable, the Promise will be rejected. Resolves with the filepath.
const ensurePath = const resolve = ; async { const path = 'path/to/temp/file.data' const res = await console // path/to/temp/file.data, path/to/temp is created in your cwd const absolutePath = const res2 = await console // $(pwd)/path/to/temp/file.data}
wrote.read(path: string, { binary?: boolean }): Promise.<string>
Read a file fully. Returns a Promise resolved with the file contents, and
rejects if path is not a string or file not found (ENOENT
).
const read = ; async { const res = await console}
examples/read.js
: this program will print the contents of itself
Pass { binary: true }
options to read as a Buffer
:
const read = ; async { const buffer = await console // // <Buffer 63 6f 6e 73 74 20 7b ... >}
wrote.readJSON(path: string): Promise.<object>
Read a json file and parse its contents.
const readJSON = ; async { const res = await console}
wrote.writeJSON(path: string, object: object, { replacer?: function, space?: string|number }): Promise
Serialise an object with JSON.stringify
and write it to a file. Pass space
and replacer
in the options object.
const writeJSON = ; async { const object = test: 'data' date: await }
wrote.readDir(dirPath: string, recursive=: boolean): Promise<object>
Read a directory, and return contents of contained files.
For example, the following directory structure:
directory - subdirectory - subdirFileA.txt ` subdirFileB.txt - fileA.txt - fileB.txt ` fileC.txt
can be read either shallowly (by default):
const readDir = const path = const dirPath = path; async { const res = await console // { 'fileA.txt': 'fileA\n', // 'fileB.txt': 'fileB\n', // 'fileC.txt': 'fileC\n' }}
or recursively:
async { const res = await console // { 'fileA.txt': 'fileA\n', // 'fileB.txt': 'fileB\n', // 'fileC.txt': 'fileC\n', // subdirectory: // { 'subdirFileA.txt': 'subdirFileA\n', // 'subdirFileB.txt': 'subdirFileB\n' } }}
wrote.readDirStructure(dirPath: string): Promise<DirectoryStructure>
Get the full structure of the directory recursively. An array of either file names as strings, or an object representing all directories of the current one, with keys being their names, and values being arrays similar to the root one.
const path = const readDirStructure = const DIR_PATH = path; /** * Read directory's structure. */ async { const res = await console}
{ "type": "Directory", "content": { "subdirectory-ln": { "type": "SymbolicLink" }, "test-ln.data": { "type": "SymbolicLink" }, "test.data": { "type": "File" }, "subdirectory": { "type": "Directory", "content": { "file.data": { "type": "File" }, "file2.data": { "type": "File" } } }, "subdirectory2": { "type": "Directory", "content": { "file3.data": { "type": "File" }, "subsubdir": { "type": "Directory", "content": { "file4.py": { "type": "File" } } }, "subsubdir2": { "type": "Directory", "content": {} } } } }}
todo
eraseDir
to rm -rfcloneFile
to clone a single filewrite
with string as patherase
with string as pathclone
with permissions- pass options to
fs.createWriteStream
inwrote.createWritable
Licence: MIT
(c) Sobesednik-Media 2017