diff --git a/package.json b/package.json index b0ad8472a..019713167 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "flatmap": "0.0.3", "glob": "^7.1.2", "ipfs-block": "~0.7.1", - "ipfs-unixfs": "~0.1.14", + "ipfs-unixfs": "~0.1.15", "ipld-dag-cbor": "~0.12.0", "ipld-dag-pb": "~0.14.4", "is-ipfs": "~0.3.2", diff --git a/src/files/cp.js b/src/files/cp.js index b808ccc39..ca62e81eb 100644 --- a/src/files/cp.js +++ b/src/files/cp.js @@ -1,16 +1,19 @@ 'use strict' const promisify = require('promisify-es6') +const findSources = require('../utils/find-sources') module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } + return promisify(function () { + const { + callback, + sources, + opts + } = findSources(Array.prototype.slice.call(arguments)) + send({ path: 'files/cp', - args: args, + args: sources, qs: opts }, callback) }) diff --git a/src/files/ls.js b/src/files/ls.js index 2f5292aaa..ff22f5396 100644 --- a/src/files/ls.js +++ b/src/files/ls.js @@ -21,6 +21,13 @@ module.exports = (send) => { callback = opts opts = {} } + + if (typeof (args) === 'function') { + callback = args + opts = {} + args = null + } + return send.andTransform({ path: 'files/ls', args: args, diff --git a/src/files/mv.js b/src/files/mv.js index 1f5a85f1f..a9ebd640e 100644 --- a/src/files/mv.js +++ b/src/files/mv.js @@ -1,17 +1,19 @@ 'use strict' const promisify = require('promisify-es6') +const findSources = require('../utils/find-sources') module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof opts === 'function' && - callback === undefined) { - callback = opts - opts = {} - } + return promisify(function () { + const { + callback, + sources, + opts + } = findSources(Array.prototype.slice.call(arguments)) + send({ path: 'files/mv', - args: args, + args: sources, qs: opts }, callback) }) diff --git a/src/utils/find-sources.js b/src/utils/find-sources.js new file mode 100644 index 000000000..2a862514a --- /dev/null +++ b/src/utils/find-sources.js @@ -0,0 +1,25 @@ +'use strict' + +module.exports = (args) => { + const callback = args.pop() + let opts = {} + let sources = [] + + if (!Array.isArray(args[args.length - 1]) && typeof args[args.length - 1] === 'object') { + opts = args.pop() + } + + if (args.length === 1 && Array.isArray(args[0])) { + // support ipfs.file.cp([src, dest], opts, cb) + sources = args[0] + } else { + // support ipfs.file.cp(src, dest, opts, cb) and ipfs.file.cp(src1, src2, dest, opts, cb) + sources = args + } + + return { + callback, + sources, + opts + } +} diff --git a/test/files.spec.js b/test/files.spec.js index d3021db3c..160c73b56 100644 --- a/test/files.spec.js +++ b/test/files.spec.js @@ -6,7 +6,6 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const isNode = require('detect-node') const loadFixture = require('aegir/fixtures') const mh = require('multihashes') const CID = require('cids') @@ -330,22 +329,80 @@ describe('.files (the MFS API part)', function () { ipfs.files.flush('/', done) }) - it('files.cp', (done) => { - ipfs.files.cp([ - '/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', - '/test-folder/test-file' - ], (err) => { - expect(err).to.not.exist() - done() - }) + it('files.cp', () => { + const folder = `/test-folder-${Math.random()}` + + return ipfs.files.mkdir(folder) + .then(() => ipfs.files.cp([ + '/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', + `${folder}/test-file-${Math.random()}` + ])) }) - it('files.ls', (done) => { - ipfs.files.ls('/test-folder', (err, res) => { - expect(err).to.not.exist() - expect(res.length).to.equal(1) - done() - }) + it('files.cp with non-array arguments', () => { + const folder = `/test-folder-${Math.random()}` + + return ipfs.files.mkdir(folder) + .then(() => ipfs.files.cp( + '/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', + `${folder}/test-file-${Math.random()}` + )) + }) + + it('files.mv', () => { + const folder = `/test-folder-${Math.random()}` + const source = `${folder}/test-file-${Math.random()}` + const dest = `${folder}/test-file-${Math.random()}` + + return ipfs.files.mkdir(folder) + .then(() => ipfs.files.cp( + '/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', + source + )) + .then(() => ipfs.files.mv([ + source, + dest + ])) + }) + + it('files.mv with non-array arguments', () => { + const folder = `/test-folder-${Math.random()}` + const source = `${folder}/test-file-${Math.random()}` + const dest = `${folder}/test-file-${Math.random()}` + + return ipfs.files.mkdir(folder) + .then(() => ipfs.files.cp( + '/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', + source + )) + .then(() => ipfs.files.mv( + source, + dest + )) + }) + + it('files.ls', () => { + const folder = `/test-folder-${Math.random()}` + const file = `${folder}/test-file-${Math.random()}` + + return ipfs.files.mkdir(folder) + .then(() => ipfs.files.write(file, Buffer.from('Hello, world'), { + create: true + })) + .then(() => ipfs.files.ls(folder)) + .then(files => { + expect(files.length).to.equal(1) + }) + }) + + it('files.ls mfs root by default', () => { + const folder = `test-folder-${Math.random()}` + + return ipfs.files.mkdir(`/${folder}`) + .then(() => ipfs.files.ls()) + .then(files => { + expect(files.find(file => file.name === folder)).to.be.ok() + }) }) it('files.write', (done) => { @@ -374,22 +431,27 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.stat', (done) => { - ipfs.files.stat('/test-folder/test-file', (err, res) => { - expect(err).to.not.exist() - expect(res).to.deep.equal({ - hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', - size: 12, - cumulativeSize: 20, - blocks: 0, - type: 'file', - withLocality: false, - local: undefined, - sizeLocal: undefined + it('files.stat', () => { + const folder = `/test-folder-${Math.random()}` + const file = `${folder}/test-file-${Math.random()}` + + return ipfs.files.mkdir(folder) + .then(() => ipfs.files.write(file, testfile, { + create: true + })) + .then(() => ipfs.files.stat(file)) + .then((stats) => { + expect(stats).to.deep.equal({ + hash: 'QmQhouoDPAnzhVM148yCa9CbUXK65wSEAZBtgrLGHtmdmP', + size: 12, + cumulativeSize: 70, + blocks: 1, + type: 'file', + withLocality: false, + local: undefined, + sizeLocal: undefined + }) }) - - done() - }) }) it('files.stat file that does not exist()', (done) => { @@ -402,16 +464,18 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.read', (done) => { - if (!isNode) { - return done() - } - - ipfs.files.read('/test-folder/test-file', (err, buf) => { - expect(err).to.not.exist() - expect(Buffer.from(buf)).to.deep.equal(testfile) - done() - }) + it('files.read', () => { + const folder = `/test-folder-${Math.random()}` + const file = `${folder}/test-file-${Math.random()}` + + return ipfs.files.mkdir(folder) + .then(() => ipfs.files.write(file, testfile, { + create: true + })) + .then(() => ipfs.files.read(file)) + .then((buf) => { + expect(Buffer.from(buf)).to.deep.equal(testfile) + }) }) it('files.rm without options', (done) => {