Skip to content

Commit

Permalink
refactor: modularize files api(ipfs-inactive#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofmn committed Jun 22, 2017
1 parent 0933049 commit 33b3547
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 131 deletions.
118 changes: 0 additions & 118 deletions src/api/files.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/api/add.js → src/api/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/cat.js → src/api/files/cat.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
17 changes: 17 additions & 0 deletions src/api/files/cp.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
File renamed without changes.
4 changes: 2 additions & 2 deletions src/api/get.js → src/api/files/get.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
22 changes: 22 additions & 0 deletions src/api/files/index.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 17 additions & 0 deletions src/api/files/ls.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
18 changes: 18 additions & 0 deletions src/api/files/mkdir.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
18 changes: 18 additions & 0 deletions src/api/files/mv.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
17 changes: 17 additions & 0 deletions src/api/files/read.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
27 changes: 27 additions & 0 deletions src/api/files/rm.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
17 changes: 17 additions & 0 deletions src/api/files/stat.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
28 changes: 28 additions & 0 deletions src/api/files/write.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
14 changes: 5 additions & 9 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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'),
Expand All @@ -35,18 +35,14 @@ 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
}

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
Expand Down
Loading

0 comments on commit 33b3547

Please sign in to comment.