Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
refactor(cli): restructure files add command
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 3, 2016
1 parent 1771ce3 commit ac629ea
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 37 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@
"libp2p-ipfs-browser": "^0.12.0",
"lodash.get": "^4.3.0",
"lodash.set": "^4.2.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",
Expand Down
80 changes: 51 additions & 29 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,50 +49,71 @@ 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) {
console.log(err)
throw err
}

res
.filter((elem) => !elem.isDirectory)
.map((elem) => elem.path)
.forEach((elem) => writeToStream(i, elem))

i.end()
}
})
})
})
})
Expand Down
17 changes: 9 additions & 8 deletions test/cli/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ac629ea

Please sign in to comment.