diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 099a33ecc5..e8afb525d2 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -13,6 +13,8 @@ const paramap = require('pull-paramap') const zip = require('pull-zip') const toPull = require('stream-to-pull-stream') +const WRAPPER = 'wrapper/' + function checkPath (inPath, recursive) { // This function is to check for the following possible inputs // 1) "." add the cwd but throw error for no recursion flag @@ -53,6 +55,11 @@ module.exports = { type: 'boolean', default: false, describe: 'Use the trickle DAG builder' + }, + 'wrap-with-directory': { + alias: 'w', + type: 'boolean', + default: false } }, @@ -94,14 +101,14 @@ module.exports = { list = [inPath] } - addPipeline(index, addStream, list) + addPipeline(index, addStream, list, argv.wrapWithDirectory) }) }) }) } } -function addPipeline (index, addStream, list) { +function addPipeline (index, addStream, list, wrapWithDirectory) { pull( zip( pull.values(list), @@ -117,12 +124,16 @@ function addPipeline (index, addStream, list) { pull.filter((file) => !file.isDirectory), pull.map((file) => ({ path: file.path.substring(index, file.path.length), - content: fs.createReadStream(file.path) + originalPath: file.path + })), + pull.map((file) => ({ + path: wrapWithDirectory ? path.join(WRAPPER, file.path) : file.path, + content: fs.createReadStream(file.originalPath) })), addStream, pull.map((file) => ({ hash: file.hash, - path: file.path + path: wrapWithDirectory ? file.path.substring(WRAPPER.length) : file.path })), pull.collect((err, added) => { if (err) { @@ -131,7 +142,13 @@ function addPipeline (index, addStream, list) { sortBy(added, 'path') .reverse() - .map((file) => `added ${file.hash} ${file.path}`) + .map((file) => { + const log = [ 'added', file.hash ] + + if (file.path.length > 0) log.push(file.path) + + return log.join(' ') + }) .forEach((msg) => console.log(msg)) }) ) diff --git a/test/cli/files.js b/test/cli/files.js index 010e656b78..7584d1e405 100644 --- a/test/cli/files.js +++ b/test/cli/files.js @@ -127,6 +127,15 @@ describe('files', () => runOnAndOff((thing) => { }) }) + it('add and wrap with a directory', () => { + return ipfs('add -w src/init-files/init-docs/readme').then((out) => { + expect(out).to.be.eql([ + 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme', + 'added QmapdaVPjHXdcswef82FnGQUauMNpk9xYFkLDZKgAxhMwq' + ].join('\n')) + }) + }) + it('cat', () => { return ipfs('files cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB') .then((out) => {