wrote
Promise-based write for Node.js
wrote(filepath?:String) => Promise<Writable> (ws.writable == true)
Create a write stream to your file without hastle.
const Writable = require('stream').Writable
const path = require('path')
const wrote = require('./')
const HOME_DIR = require('os').homedir()
const file = path.join(HOME_DIR, `wrote-${Math.floor(Math.random() * 1e5)}.data`)
return wrote(file)
.then((ws) => {
console.log(ws instanceof Writable)
console.log(ws.path)
})
.catch(console.error)
If you don't have a file, a new one in the temp directory will be created for you.
const wrote = require('wrote')
const Writable = require('stream').Writable
return wrote()
.then((ws) => {
console.log(ws instanceof Writable)
console.log(ws.path)
})
.catch(console.error)
wrote.erase(ws:Writable) => Promise<Writable> (ws.writable == false)
Erase file and close stream.
const wrote = require('wrote')
const Writable = require('stream').Writable
const path = require('path')
const HOME_DIR = require('os').homedir()
const fs = require('fs')
const file = path.join(HOME_DIR, `wrote-${Math.floor(Math.random() * 1e5)}.data`)
return wrote(file)
.then((ws) => {
console.log(ws instanceof Writable)
console.log(ws.writable)
console.log(ws.path)
return wrote.erase(ws)
})
.then((ws) => {
console.log(ws.path)
console.log(ws.writable)
})
.catch(console.error)
wrote.write(ws:Writable, data:any|Readable) => Promise
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 wrote = require('wrote')
const assert = require('assert')
const Writable = require('stream').Writable
const testString = 'hello world'
const buffer = Buffer.from(testString)
const allRawData = []
const ws = new Writable({
write: (chunk, encoding, next) => {
allRawData.push(chunk)
next()
},
})
wrote.write(ws, buffer)
.then(() => {
console.log(allRawData.map(d => String(d)))
assert.deepEqual(allRawData, [
buffer,
])
})
.catch(console.error)
wrote.ensurePath(filePath:string) => Promise
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 wrote = require('wrote')
const tempPath = 'path/to/temp/file.data'
const path = require('path')
wrote.ensurePath(tempPath)
.then((res) => {
console.log(res)
})
.then(() => {
const absolutePath = path.join(process.cwd(), tempPath)
return wrote.ensurePath(absolutePath)
})
.then((res) => {
console.log(res)
})
todo
- pass options to
fs.createWriteStream
Licence: MIT
(c) Sobesednik-Media 2017