diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 09466b319e..5cae372ccd 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -59,37 +59,39 @@ module.exports = Command.extend({ if (err) { throw err } - const i = ipfs.files.add() - var filePair - i.on('data', (file) => { - console.log('added', bs58.encode(file.multihash).toString(), file.path) - }) - i.once('end', () => { - return - }) - if (res.length !== 0) { - const index = inPath.lastIndexOf('/') - parallelLimit(res.map((element) => (callback) => { - if (!fs.statSync(element).isDirectory()) { - i.write({ - path: element.substring(index + 1, element.length), - content: fs.createReadStream(element) - }) - } - callback() - }), 10, (err) => { - if (err) { - throw err - } - i.end() + ipfs.files.createAddStream((err, i) => { + if (err) throw err + var filePair + i.on('data', (file) => { + console.log('added', bs58.encode(file.node.multihash()).toString(), file.path) }) - } else { - rs = fs.createReadStream(inPath) - inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length) - filePair = {path: inPath, content: rs} - i.write(filePair) - i.end() - } + i.once('end', () => { + return + }) + if (res.length !== 0) { + const index = inPath.lastIndexOf('/') + parallelLimit(res.map((element) => (callback) => { + if (!fs.statSync(element).isDirectory()) { + i.write({ + path: element.substring(index + 1, element.length), + content: fs.createReadStream(element) + }) + } + callback() + }), 10, (err) => { + if (err) { + throw err + } + i.end() + }) + } else { + rs = fs.createReadStream(inPath) + inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length) + filePair = {path: inPath, content: rs} + i.write(filePair) + i.end() + } + }) }) }) } diff --git a/test/cli/test-files.js b/test/cli/test-files.js index 512b088c9e..635138de37 100644 --- a/test/cli/test-files.js +++ b/test/cli/test-files.js @@ -20,6 +20,39 @@ describe('files', () => { done() }) }) + + it('add', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', 'src/init-files/init-docs/readme'], {env}) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(0) + expect(stdout[0]).to.equal('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme') + done() + }) + }) + + it('add recursively', (done) => { + nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', '-r', 'src/init-files/init-docs'], {env}) + .run((err, stdout, exitcode) => { + expect(err).to.not.exist + expect(exitcode).to.equal(0) + const expected = [ + 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', + 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', + 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', + 'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help', + 'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start', + 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme', + 'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes', + 'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro', + 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', + 'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour', + 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs' + ] + expect(stdout).to.deep.equal(expected) + done() + }) + }) }) describe('api running', () => {