Comparing version 1.3.0 to 1.4.0
@@ -19,3 +19,3 @@ { | ||
"args": [ | ||
"es5/test/spec/erase.js" | ||
"test/spec/read.js" | ||
], | ||
@@ -22,0 +22,0 @@ "env": { |
@@ -0,1 +1,8 @@ | ||
## 2 January 2018 | ||
### 1.4.0 | ||
* [feature] `read` now accepts options, with `binary` property to read as a | ||
`Buffer`. | ||
## 1 January 2018 | ||
@@ -2,0 +9,0 @@ |
var _require = require('fs'), | ||
createReadStream = _require.createReadStream; | ||
var Catchment = require('catchment'); | ||
var Catchment = require('catchment/es5'); | ||
@@ -9,2 +9,5 @@ /** | ||
* @param {string} path path to the file to read | ||
* @param {object} options options | ||
* @param {boolean} [options.binary=false] whether to return a Buffer instead of a | ||
* string | ||
* @returns {Promise<string>} Resolves with contents of the file, rejects if | ||
@@ -14,14 +17,16 @@ * file not found. | ||
function read(path) { | ||
return new Promise(function ($return, $error) { | ||
var rs, catchmentRes; | ||
var $args = arguments;return new Promise(function ($return, $error) { | ||
var options, _options$binary, binary, rs, catchmentRes; | ||
options = $args.length > 1 && $args[1] !== undefined ? $args[1] : {}; | ||
_options$binary = options.binary, binary = _options$binary === undefined ? false : _options$binary; | ||
rs = createReadStream(path); | ||
return Promise.resolve(new Promise(function (resolve, reject) { | ||
return new Promise(function ($return, $error) { | ||
var catchment, res; | ||
var _ref, promise, res; | ||
rs.on('error', reject); | ||
catchment = new Catchment(); | ||
rs.pipe(catchment); | ||
return Promise.resolve(catchment.promise).then(function ($await_1) { | ||
_ref = new Catchment({ rs, binary }), promise = _ref.promise; | ||
return Promise.resolve(promise).then(function ($await_1) { | ||
try { | ||
@@ -28,0 +33,0 @@ res = $await_1; |
var assert = require('assert'); | ||
var Catchment = require('catchment'); | ||
var Catchment = require('catchment/es5'); | ||
var fs = require('fs'); | ||
@@ -14,10 +14,13 @@ var makePromise = require('makepromise/es5/src/'); | ||
var _require3 = require('../../src/'), | ||
createWritable = _require3.createWritable, | ||
write = _require3.write, | ||
erase = _require3.erase, | ||
readDir = _require3.readDir, | ||
readDirStructure = _require3.readDirStructure, | ||
read = _require3.read; | ||
var _require3 = require('noddy/es5'), | ||
nodeGte = _require3.nodeGte; | ||
var _require4 = require('../../src/'), | ||
createWritable = _require4.createWritable, | ||
write = _require4.write, | ||
erase = _require4.erase, | ||
readDir = _require4.readDir, | ||
readDirStructure = _require4.readDirStructure, | ||
read = _require4.read; | ||
var fixturesStructure = require('../fixtures/expected/read-dir-structure'); | ||
@@ -83,3 +86,3 @@ var expectedJSON = require('../fixtures/test.json'); | ||
return new Promise(function ($return, $error) { | ||
var testData, ws, rs, catchment, res; | ||
var testData, ws, rs, _ref2, promise, res; | ||
@@ -94,5 +97,4 @@ testData = `some-test-data-${Date.now()}`; | ||
catchment = new Catchment(); | ||
rs.pipe(catchment); | ||
return Promise.resolve(catchment.promise).then(function ($await_8) { | ||
_ref2 = new Catchment({ rs }), promise = _ref2.promise; | ||
return Promise.resolve(promise).then(function ($await_8) { | ||
try { | ||
@@ -138,2 +140,4 @@ res = $await_8; | ||
}); | ||
this.TEST_DATA_BUFFER = nodeGte('v5.10.0') ? Buffer.from(this.TEST_DATA) : new Buffer(this.TEST_DATA); | ||
tempFileWs = void 0; | ||
@@ -241,3 +245,3 @@ Object.defineProperties(this, { | ||
}, | ||
createTempFileWithData: { value() { | ||
createTempFileWithData: { value: function value() { | ||
return new Promise(function ($return, $error) { | ||
@@ -251,5 +255,5 @@ var tempFile, ws; | ||
tempFileWs = ws; | ||
return Promise.resolve(write(ws, this.TEST_DATA)).then(function ($await_16) { | ||
return Promise.resolve(write(ws, _this.TEST_DATA)).then(function ($await_16) { | ||
try { | ||
this._tempFile = tempFile; | ||
_this._tempFile = tempFile; | ||
return $return(); | ||
@@ -330,5 +334,5 @@ } catch ($boundEx) { | ||
} | ||
}.bind(this);var $Try_2_Catch = function (_ref2) { | ||
}.bind(this);var $Try_2_Catch = function (_ref3) { | ||
try { | ||
message = _ref2.message; | ||
message = _ref3.message; | ||
@@ -351,4 +355,4 @@ if (/EEXIST/.test(message)) { | ||
}.bind(this), $Try_2_Catch); | ||
} catch (_ref2) { | ||
$Try_2_Catch(_ref2) | ||
} catch (_ref3) { | ||
$Try_2_Catch(_ref3) | ||
} | ||
@@ -355,0 +359,0 @@ }.bind(this)); |
@@ -49,3 +49,3 @@ var _require = require('zoroaster/assert'), | ||
args: [invalidJSONpath], | ||
message: 'Unexpected token h in JSON at position 1' | ||
message: /Unexpected token h/ | ||
})).then(function ($await_3) { | ||
@@ -52,0 +52,0 @@ try { |
@@ -1,2 +0,6 @@ | ||
var assert = require('assert'); | ||
var _require = require('zoroaster/assert'), | ||
throws = _require.throws, | ||
equal = _require.equal, | ||
deepEqual = _require.deepEqual; | ||
var context = require('../context/WroteContext'); | ||
@@ -7,11 +11,33 @@ var read = require('../../src/read'); | ||
context, | ||
'should read a file'(ctx) { | ||
'should read a file'(_ref) { | ||
return new Promise(function ($return, $error) { | ||
var res; | ||
return Promise.resolve(ctx.createTempFileWithData()).then(function ($await_3) { | ||
var tempFile, createTempFileWithData, TEST_DATA, res; | ||
tempFile = _ref.tempFile, createTempFileWithData = _ref.createTempFileWithData, TEST_DATA = _ref.TEST_DATA; | ||
return Promise.resolve(createTempFileWithData()).then(function ($await_1) { | ||
try { | ||
return Promise.resolve(read(ctx.tempFile)).then(function ($await_4) { | ||
return Promise.resolve(read(tempFile)).then(function ($await_2) { | ||
try { | ||
res = $await_2; | ||
equal(res, TEST_DATA); | ||
return $return(); | ||
} catch ($boundEx) { | ||
return $error($boundEx); | ||
} | ||
}.bind(this), $error); | ||
} catch ($boundEx) { | ||
return $error($boundEx); | ||
} | ||
}.bind(this), $error); | ||
}.bind(this)); | ||
}, | ||
'should read a file in binary'(_ref2) { | ||
return new Promise(function ($return, $error) { | ||
var tempFile, createTempFileWithData, TEST_DATA_BUFFER, res; | ||
tempFile = _ref2.tempFile, createTempFileWithData = _ref2.createTempFileWithData, TEST_DATA_BUFFER = _ref2.TEST_DATA_BUFFER; | ||
return Promise.resolve(createTempFileWithData()).then(function ($await_3) { | ||
try { | ||
return Promise.resolve(read(tempFile, { binary: true })).then(function ($await_4) { | ||
try { | ||
res = $await_4; | ||
assert.equal(res, ctx.TEST_DATA); | ||
deepEqual(res, TEST_DATA_BUFFER); | ||
return $return(); | ||
@@ -30,6 +56,11 @@ } catch ($boundEx) { | ||
return new Promise(function ($return, $error) { | ||
var filename, code, message; | ||
var filename; | ||
filename = `${Math.floor(Math.random() * 1e5)}.data`; | ||
var $Try_1_Post = function () { | ||
return Promise.resolve(throws({ | ||
fn: read, | ||
args: [filename], | ||
code: 'ENOENT', | ||
message: new RegExp(filename) | ||
})).then(function ($await_5) { | ||
try { | ||
@@ -40,24 +71,3 @@ return $return(); | ||
} | ||
}.bind(this);var $Try_1_Catch = function (_ref) { | ||
try { | ||
code = _ref.code; | ||
message = _ref.message; | ||
assert(code, 'ENOENT'); | ||
assert(message.indexOf(filename) !== -1); | ||
return $Try_1_Post(); | ||
} catch ($boundEx) { | ||
return $error($boundEx); | ||
} | ||
}.bind(this);try { | ||
return Promise.resolve(read(filename)).then(function ($await_5) { | ||
try { | ||
throw new Error('should have been rejected with ENOENT'); | ||
} catch ($boundEx) { | ||
return $Try_1_Catch($boundEx); | ||
} | ||
}.bind(this), $Try_1_Catch); | ||
} catch (_ref) { | ||
$Try_1_Catch(_ref) | ||
} | ||
}.bind(this), $error); | ||
}.bind(this)); | ||
@@ -67,4 +77,6 @@ }, | ||
return new Promise(function ($return, $error) { | ||
var message; | ||
var $Try_2_Post = function () { | ||
return Promise.resolve(throws({ | ||
fn: read, | ||
message: /path must be a string/ | ||
})).then(function ($await_6) { | ||
try { | ||
@@ -75,23 +87,3 @@ return $return(); | ||
} | ||
}.bind(this);var $Try_2_Catch = function (_ref2) { | ||
try { | ||
message = _ref2.message; | ||
assert(message.indexOf('path must be a string') !== -1); | ||
return $Try_2_Post(); | ||
} catch ($boundEx) { | ||
return $error($boundEx); | ||
} | ||
}.bind(this); | ||
try { | ||
return Promise.resolve(read()).then(function ($await_6) { | ||
try { | ||
throw new Error('should have been rejected'); | ||
} catch ($boundEx) { | ||
return $Try_2_Catch($boundEx); | ||
} | ||
}.bind(this), $Try_2_Catch); | ||
} catch (_ref2) { | ||
$Try_2_Catch(_ref2) | ||
} | ||
}.bind(this), $error); | ||
}.bind(this)); | ||
@@ -98,0 +90,0 @@ } |
@@ -9,3 +9,3 @@ var _require = require('stream'), | ||
var assert = require('assert'); | ||
var Catchment = require('catchment'); | ||
var Catchment = require('catchment/es5'); | ||
var write = require('../../src/write'); | ||
@@ -72,3 +72,3 @@ | ||
return new Promise(function ($return, $error) { | ||
var testString, _createWs3, ws, rs, catchment; | ||
var testString, _createWs3, ws, rs, _ref, promise; | ||
@@ -84,5 +84,4 @@ testString = 'hello world'; | ||
}); | ||
catchment = new Catchment(); | ||
rs.pipe(catchment); | ||
return Promise.resolve(catchment.promise).then(function ($await_3) { | ||
_ref = new Catchment({ rs }), promise = _ref.promise; | ||
return Promise.resolve(promise).then(function ($await_3) { | ||
try { | ||
@@ -89,0 +88,0 @@ return Promise.resolve(throws({ |
@@ -6,2 +6,4 @@ const { read } = require('..'); | ||
console.log(res) | ||
const buffer = await read(__filename, { binary: true }) | ||
console.log(buffer) // <Buffer 63 6f 6e 73 74 20 7b 20 72 65 61 64 20 7d 20 3d 20 72 65 71 75 69 72 65 28 27 2e 2e 27 29 3b 0a 0a28 61 73 79 6e 63 20 28 29 20 3d 3e 20 7b 0a 20 20 ... > | ||
})() |
{ | ||
"name": "wrote", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Promise-based writing to filesystem for Node.js", | ||
@@ -38,8 +38,9 @@ "main": "src/index.js", | ||
"fast-async": "6.3.0", | ||
"noddy": "1.1.0", | ||
"npm-run-all": "4.1.2", | ||
"spawncommand": "1.1.0", | ||
"zoroaster": "0.5.2" | ||
"zoroaster": "0.5.3" | ||
}, | ||
"dependencies": { | ||
"catchment": "1.0.0", | ||
"catchment": "2.0.1", | ||
"makepromise": "1.1.1", | ||
@@ -46,0 +47,0 @@ "restream": "1.2.0" |
@@ -199,3 +199,3 @@ # wrote | ||
## `wrote.read(path: string): Promise.<string>` | ||
## `wrote.read(path: string, { binary?: boolean }): Promise.<string>` | ||
@@ -216,2 +216,13 @@ Read a file fully. Returns a Promise resolved with the file contents, and | ||
Pass `{ binary: true }` options to read as a `Buffer`: | ||
```js | ||
const { read } = require('wrote'); | ||
(async () => { | ||
const buffer = await read(__filename, { binary: true }) | ||
console.log(buffer) // // <Buffer 63 6f 6e 73 74 20 7b ... > | ||
})() | ||
``` | ||
## `wrote.readJSON(path: string): Promise.<object>` | ||
@@ -371,2 +382,7 @@ | ||
- `eraseDir` to rm -rf | ||
- `cloneFile` to clone a single file | ||
- `write` with string as path | ||
- `erase` with string as path | ||
- `clone` with permissions | ||
- pass options to `fs.createWriteStream` in `wrote.createWritable` | ||
@@ -373,0 +389,0 @@ |
@@ -7,12 +7,15 @@ const { createReadStream } = require('fs') | ||
* @param {string} path path to the file to read | ||
* @param {object} options options | ||
* @param {boolean} [options.binary=false] whether to return a Buffer instead of a | ||
* string | ||
* @returns {Promise<string>} Resolves with contents of the file, rejects if | ||
* file not found. | ||
*/ | ||
async function read(path) { | ||
async function read(path, options = {}) { | ||
const { binary = false } = options | ||
const rs = createReadStream(path) | ||
const catchmentRes = await new Promise(async (resolve, reject) => { | ||
rs.on('error', reject) | ||
const catchment = new Catchment() | ||
rs.pipe(catchment) | ||
const res = await catchment.promise | ||
const { promise } = new Catchment({ rs, binary }) | ||
const res = await promise | ||
resolve(res) | ||
@@ -19,0 +22,0 @@ }) |
@@ -8,2 +8,3 @@ const assert = require('assert') | ||
const spawnCommand = require('spawncommand') | ||
const { nodeGte } = require('noddy') | ||
const { | ||
@@ -42,5 +43,4 @@ createWritable, write, erase, readDir, readDirStructure, read, | ||
const catchment = new Catchment() | ||
rs.pipe(catchment) | ||
const res = await catchment.promise | ||
const { promise } = new Catchment({ rs }) | ||
const res = await promise | ||
assert.equal(res, testData) | ||
@@ -65,2 +65,6 @@ } | ||
}) | ||
this.TEST_DATA_BUFFER = nodeGte('v5.10.0') | ||
? Buffer.from(this.TEST_DATA) | ||
: new Buffer(this.TEST_DATA) | ||
let tempFileWs | ||
@@ -112,3 +116,3 @@ Object.defineProperties(this, { | ||
}, | ||
createTempFileWithData: { async value() { | ||
createTempFileWithData: { value: async () => { | ||
const tempFile = createTempFilePath() | ||
@@ -115,0 +119,0 @@ const ws = await createWritable(tempFile) |
@@ -22,3 +22,3 @@ const { deepEqual, throws } = require('zoroaster/assert') | ||
args: [invalidJSONpath], | ||
message: 'Unexpected token h in JSON at position 1', | ||
message: /Unexpected token h/, | ||
}) | ||
@@ -25,0 +25,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
const assert = require('assert') | ||
const { throws, equal, deepEqual } = require('zoroaster/assert') | ||
const context = require('../context/WroteContext') | ||
@@ -7,24 +7,30 @@ const read = require('../../src/read') | ||
context, | ||
async 'should read a file'(ctx) { | ||
await ctx.createTempFileWithData() | ||
const res = await read(ctx.tempFile) | ||
assert.equal(res, ctx.TEST_DATA) | ||
async 'should read a file'({ | ||
tempFile, createTempFileWithData, TEST_DATA, | ||
}) { | ||
await createTempFileWithData() | ||
const res = await read(tempFile) | ||
equal(res, TEST_DATA) | ||
}, | ||
async 'should read a file in binary'({ | ||
tempFile, createTempFileWithData, TEST_DATA_BUFFER, | ||
}) { | ||
await createTempFileWithData() | ||
const res = await read(tempFile, { binary: true }) | ||
deepEqual(res, TEST_DATA_BUFFER) | ||
}, | ||
async 'should reject if file not found'() { | ||
const filename = `${Math.floor(Math.random() * 1e5)}.data` | ||
try { | ||
await read(filename) | ||
throw new Error('should have been rejected with ENOENT') | ||
} catch ({ code, message }) { | ||
assert(code, 'ENOENT') | ||
assert(message.indexOf(filename) !== -1) | ||
} | ||
await throws({ | ||
fn: read, | ||
args: [filename], | ||
code: 'ENOENT', | ||
message: new RegExp(filename), | ||
}) | ||
}, | ||
async 'should reject if path is not a string'() { | ||
try { | ||
await read() | ||
throw new Error('should have been rejected') | ||
} catch ({ message }) { | ||
assert(message.indexOf('path must be a string') !== -1) | ||
} | ||
await throws({ | ||
fn: read, | ||
message: /path must be a string/, | ||
}) | ||
}, | ||
@@ -31,0 +37,0 @@ } |
@@ -51,5 +51,4 @@ const { Readable, Writable } = require('stream') | ||
}) | ||
const catchment = new Catchment() | ||
rs.pipe(catchment) | ||
await catchment.promise | ||
const { promise } = new Catchment({ rs }) | ||
await promise | ||
@@ -56,0 +55,0 @@ await throws({ |
Sorry, the diff of this file is not supported yet
192220
4652
392
9
+ Addedcatchment@2.0.1(transitive)
- Removedcatchment@1.0.0(transitive)
Updatedcatchment@2.0.1