From 1f13ac4016aee9c89effe05aa235193911f0b848 Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Mon, 6 Jun 2016 17:34:53 -0700 Subject: [PATCH 1/2] Implement ipfs.files.add CLI command. --- src/cli/commands/files/add.js | 62 ++++++++++++++++++----------------- test/cli/test-files.js | 31 ++++++++++++++++++ 2 files changed, 63 insertions(+), 30 deletions(-) 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..faf32dc54c 100644 --- a/test/cli/test-files.js +++ b/test/cli/test-files.js @@ -20,6 +20,37 @@ 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) + expect(stdout).to.deep.equal([ + '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']) + done() + }) + }) }) describe('api running', () => { From f3fcc74283a068522549a588a49661f1127d014c Mon Sep 17 00:00:00 2001 From: Stephen Whitmore Date: Mon, 6 Jun 2016 19:51:12 -0700 Subject: [PATCH 2/2] nit --- test/cli/test-files.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/cli/test-files.js b/test/cli/test-files.js index faf32dc54c..635138de37 100644 --- a/test/cli/test-files.js +++ b/test/cli/test-files.js @@ -36,7 +36,7 @@ describe('files', () => { .run((err, stdout, exitcode) => { expect(err).to.not.exist expect(exitcode).to.equal(0) - expect(stdout).to.deep.equal([ + const expected = [ 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', @@ -47,7 +47,9 @@ describe('files', () => { 'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro', 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', 'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour', - 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs']) + 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs' + ] + expect(stdout).to.deep.equal(expected) done() }) })