diff --git a/src/api/files.js b/src/api/files.js deleted file mode 100644 index 2fbf827eb..000000000 --- a/src/api/files.js +++ /dev/null @@ -1,118 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - cp: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/cp', - args: args, - qs: opts - }, callback) - }), - ls: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - return send({ - path: 'files/ls', - args: args, - qs: opts - }, callback) - }), - mkdir: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/mkdir', - args: args, - qs: opts - }, callback) - }), - stat: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/stat', - args: args, - qs: opts - }, callback) - }), - rm: promisify((path, opts, callback) => { - if (typeof opts === 'function' && - !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof callback === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'files/rm', - args: path, - qs: opts - }, callback) - }), - read: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/read', - args: args, - qs: opts - }, callback) - }), - write: promisify((pathDst, files, opts, callback) => { - if (typeof opts === 'function' && - !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof callback === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'files/write', - args: pathDst, - qs: opts, - files: files - }, callback) - }), - mv: promisify((args, opts, callback) => { - if (typeof opts === 'function' && - callback === undefined) { - callback = opts - opts = {} - } - send({ - path: 'files/mv', - args: args, - qs: opts - }, callback) - }) - } -} diff --git a/src/api/add.js b/src/api/files/add.js similarity index 92% rename from src/api/add.js rename to src/api/files/add.js index eafee5285..19d0cc4fa 100644 --- a/src/api/add.js +++ b/src/api/files/add.js @@ -2,7 +2,7 @@ const isStream = require('isstream') const promisify = require('promisify-es6') -const DAGNodeStream = require('../dagnode-stream') +const DAGNodeStream = require('../../dagnode-stream') module.exports = (send) => { return promisify((files, callback) => { diff --git a/src/api/cat.js b/src/api/files/cat.js similarity index 91% rename from src/api/cat.js rename to src/api/files/cat.js index 2af53bd10..5d899e267 100644 --- a/src/api/cat.js +++ b/src/api/files/cat.js @@ -1,7 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const cleanCID = require('../clean-cid') +const cleanCID = require('../../clean-cid') const v = require('is-ipfs') module.exports = (send) => { diff --git a/src/api/files/cp.js b/src/api/files/cp.js new file mode 100644 index 000000000..b808ccc39 --- /dev/null +++ b/src/api/files/cp.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/cp', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/create-add-stream.js b/src/api/files/create-add-stream.js similarity index 100% rename from src/api/create-add-stream.js rename to src/api/files/create-add-stream.js diff --git a/src/api/get.js b/src/api/files/get.js similarity index 88% rename from src/api/get.js rename to src/api/files/get.js index 6b6fc5f3c..b68b7d360 100644 --- a/src/api/get.js +++ b/src/api/files/get.js @@ -1,8 +1,8 @@ 'use strict' const promisify = require('promisify-es6') -const cleanCID = require('../clean-cid') -const TarStreamToObjects = require('../tar-stream-to-objects') +const cleanCID = require('../../clean-cid') +const TarStreamToObjects = require('../../tar-stream-to-objects') const v = require('is-ipfs') module.exports = (send) => { diff --git a/src/api/files/index.js b/src/api/files/index.js new file mode 100644 index 000000000..9538f3ac9 --- /dev/null +++ b/src/api/files/index.js @@ -0,0 +1,22 @@ +'use strict' + +const moduleConfig = require('../../module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + add: require('./add')(send), + createAddStream: require('./create-add-stream')(send), + get: require('./get')(send), + cat: require('./cat')(send), + cp: require('./cp')(send), + ls: require('./ls')(send), + mkdir: require('./mkdir')(send), + stat: require('./stat')(send), + rm: require('./rm')(send), + read: require('./read')(send), + write: require('./write')(send), + mv: require('./mv')(send) + } +} diff --git a/src/api/files/ls.js b/src/api/files/ls.js new file mode 100644 index 000000000..e87f07719 --- /dev/null +++ b/src/api/files/ls.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'files/ls', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/files/mkdir.js b/src/api/files/mkdir.js new file mode 100644 index 000000000..393baecf4 --- /dev/null +++ b/src/api/files/mkdir.js @@ -0,0 +1,18 @@ + +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/mkdir', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/files/mv.js b/src/api/files/mv.js new file mode 100644 index 000000000..1f5a85f1f --- /dev/null +++ b/src/api/files/mv.js @@ -0,0 +1,18 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof opts === 'function' && + callback === undefined) { + callback = opts + opts = {} + } + send({ + path: 'files/mv', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/files/read.js b/src/api/files/read.js new file mode 100644 index 000000000..bf339c24b --- /dev/null +++ b/src/api/files/read.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/read', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/files/rm.js b/src/api/files/rm.js new file mode 100644 index 000000000..2cfd209b6 --- /dev/null +++ b/src/api/files/rm.js @@ -0,0 +1,27 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((path, opts, callback) => { + if (typeof opts === 'function' && + !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && + typeof callback === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'files/rm', + args: path, + qs: opts + }, callback) + }) +} diff --git a/src/api/files/stat.js b/src/api/files/stat.js new file mode 100644 index 000000000..845901b98 --- /dev/null +++ b/src/api/files/stat.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/stat', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/files/write.js b/src/api/files/write.js new file mode 100644 index 000000000..17c7ce8df --- /dev/null +++ b/src/api/files/write.js @@ -0,0 +1,28 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((pathDst, files, opts, callback) => { + if (typeof opts === 'function' && + !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && + typeof callback === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'files/write', + args: pathDst, + qs: opts, + files: files + }, callback) + }) +} diff --git a/src/load-commands.js b/src/load-commands.js index 810381d3d..a17d9044c 100644 --- a/src/load-commands.js +++ b/src/load-commands.js @@ -3,9 +3,9 @@ function requireCommands () { const cmds = { // add and createAddStream alias - add: require('./api/add'), - cat: require('./api/cat'), - createAddStream: require('./api/create-add-stream'), + add: require('./api/files/add'), + cat: require('./api/files/cat'), + createAddStream: require('./api/files/create-add-stream'), bitswap: require('./api/bitswap'), block: require('./api/block'), bootstrap: require('./api/bootstrap'), @@ -15,7 +15,7 @@ function requireCommands () { diag: require('./api/diag'), id: require('./api/id'), key: require('./api/key'), - get: require('./api/get'), + get: require('./api/files/get'), log: require('./api/log'), ls: require('./api/ls'), mount: require('./api/mount'), @@ -35,10 +35,6 @@ function requireCommands () { // compatibility, until 'files vs mfs' naming decision is resolved. cmds.files = function (send) { const files = require('./api/files')(send) - files.add = require('./api/add')(send) - files.createAddStream = require('./api/create-add-stream.js')(send) - files.get = require('./api/get')(send) - files.cat = require('./api/cat')(send) return files } @@ -46,7 +42,7 @@ function requireCommands () { cmds.util = function (send) { const util = { addFromFs: require('./api/util/fs-add')(send), - addFromStream: require('./api/add')(send), + addFromStream: require('./api/files/add')(send), addFromURL: require('./api/util/url-add')(send) } return util diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index d0380d09d..1d8d9628e 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -6,6 +6,11 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) +const defaultConfig = require('../src/default-config.js') +const config = defaultConfig() +config.host = 'test' +config.port = '1111' + describe('submodules', () => { describe('bitswap', () => { it('.wantlist', () => { @@ -301,4 +306,23 @@ describe('submodules', () => { expect(pubsub).to.be.a('function') }) }) + + describe('files', () => { + const files = require('../src/api/files')(config) + + it('.files', () => { + expect(files.add).to.be.a('function') + expect(files.createAddStream).to.be.a('function') + expect(files.get).to.be.a('function') + expect(files.cat).to.be.a('function') + expect(files.cp).to.be.a('function') + expect(files.ls).to.be.a('function') + expect(files.mkdir).to.be.a('function') + expect(files.stat).to.be.a('function') + expect(files.rm).to.be.a('function') + expect(files.read).to.be.a('function') + expect(files.write).to.be.a('function') + expect(files.mv).to.be.a('function') + }) + }) })