From c2ff344bf735ce0ae4cb9b975495283c0ca0047a Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Wed, 3 Aug 2016 17:23:18 +0200 Subject: [PATCH] refactor(cli): restructure files add command --- package.json | 2 + src/cli/commands/files/add.js | 79 ++++++++++++++++++++++------------- test/cli/test-files.js | 17 ++++---- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index 03da6c9272..94bf6828d4 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,9 @@ "libp2p-ipfs-browser": "^0.12.0", "lodash.get": "^4.4.0", "lodash.set": "^4.3.0", + "lodash.sortby": "^4.6.1", "mafmt": "^2.1.1", + "map-limit": "0.0.1", "multiaddr": "^2.0.2", "multihashes": "^0.2.2", "path-exists": "^3.0.0", diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 5caa5d85a4..0d26af066d 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -6,9 +6,10 @@ const log = debug('cli:version') log.error = debug('cli:version:error') const bs58 = require('bs58') const fs = require('fs') -const parallelLimit = require('run-parallel-limit') const path = require('path') const glob = require('glob') +const sortBy = require('lodash.sortby') +const mapLimit = require('map-limit') function checkPath (inPath, recursive) { // This function is to check for the following possible inputs @@ -48,50 +49,70 @@ module.exports = { }, handler (argv) { - let rs - let inPath = checkPath(argv.file, argv.recursive) - glob(path.join(inPath, '/**/*'), (err, res) => { + utils.getIPFS((err, ipfs) => { if (err) { throw err } - utils.getIPFS((err, ipfs) => { + + glob(path.join(inPath, '/**/*'), (err, res) => { if (err) { throw err } + ipfs.files.createAddStream((err, i) => { - if (err) throw err - var filePair + if (err) { + throw err + } + const added = [] + i.on('data', (file) => { - console.log('added', bs58.encode(file.node.multihash()).toString(), file.path) + const hash = bs58.encode(file.node.multihash()).toString() + added.push({hash, path: file.path}) }) - i.once('end', () => { - return + + i.on('end', () => { + sortBy(added, 'path') + .reverse() + .map((file) => `added ${file.hash} ${file.path}`) + .forEach((msg) => console.log(msg)) }) - 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 (res.length === 0) { + res = [inPath] + } + + const writeToStream = (stream, element) => { + const index = inPath.lastIndexOf('/') + 1 + i.write({ + path: element.substring(index, element.length), + content: fs.createReadStream(element) + }) + } + + mapLimit(res, 50, (file, cb) => { + fs.stat(file, (err, stat) => { if (err) { - throw err + return cb(err) } - i.end() + return cb(null, { + path: file, + isDirectory: stat.isDirectory() + }) }) - } else { - rs = fs.createReadStream(inPath) - inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length) - filePair = {path: inPath, content: rs} - i.write(filePair) + }, (err, res) => { + if (err) { + throw err + } + + res + .filter((elem) => !elem.isDirectory) + .map((elem) => elem.path) + .forEach((elem) => writeToStream(i, elem)) + i.end() - } + }) }) }) }) diff --git a/test/cli/test-files.js b/test/cli/test-files.js index 635138de37..ffe0a5ae78 100644 --- a/test/cli/test-files.js +++ b/test/cli/test-files.js @@ -36,17 +36,18 @@ describe('files', () => { .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 QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes', + 'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme', + 'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start', + 'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help', + 'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index', + 'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs', + 'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact', + 'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about', 'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs' ] expect(stdout).to.deep.equal(expected)