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

Commit c2ff344

Browse files
refactor(cli): restructure files add command
1 parent ddc91ea commit c2ff344

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@
8282
"libp2p-ipfs-browser": "^0.12.0",
8383
"lodash.get": "^4.4.0",
8484
"lodash.set": "^4.3.0",
85+
"lodash.sortby": "^4.6.1",
8586
"mafmt": "^2.1.1",
87+
"map-limit": "0.0.1",
8688
"multiaddr": "^2.0.2",
8789
"multihashes": "^0.2.2",
8890
"path-exists": "^3.0.0",

src/cli/commands/files/add.js

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ const log = debug('cli:version')
66
log.error = debug('cli:version:error')
77
const bs58 = require('bs58')
88
const fs = require('fs')
9-
const parallelLimit = require('run-parallel-limit')
109
const path = require('path')
1110
const glob = require('glob')
11+
const sortBy = require('lodash.sortby')
12+
const mapLimit = require('map-limit')
1213

1314
function checkPath (inPath, recursive) {
1415
// This function is to check for the following possible inputs
@@ -48,50 +49,70 @@ module.exports = {
4849
},
4950

5051
handler (argv) {
51-
let rs
52-
5352
let inPath = checkPath(argv.file, argv.recursive)
5453

55-
glob(path.join(inPath, '/**/*'), (err, res) => {
54+
utils.getIPFS((err, ipfs) => {
5655
if (err) {
5756
throw err
5857
}
59-
utils.getIPFS((err, ipfs) => {
58+
59+
glob(path.join(inPath, '/**/*'), (err, res) => {
6060
if (err) {
6161
throw err
6262
}
63+
6364
ipfs.files.createAddStream((err, i) => {
64-
if (err) throw err
65-
var filePair
65+
if (err) {
66+
throw err
67+
}
68+
const added = []
69+
6670
i.on('data', (file) => {
67-
console.log('added', bs58.encode(file.node.multihash()).toString(), file.path)
71+
const hash = bs58.encode(file.node.multihash()).toString()
72+
added.push({hash, path: file.path})
6873
})
69-
i.once('end', () => {
70-
return
74+
75+
i.on('end', () => {
76+
sortBy(added, 'path')
77+
.reverse()
78+
.map((file) => `added ${file.hash} ${file.path}`)
79+
.forEach((msg) => console.log(msg))
7180
})
72-
if (res.length !== 0) {
73-
const index = inPath.lastIndexOf('/')
74-
parallelLimit(res.map((element) => (callback) => {
75-
if (!fs.statSync(element).isDirectory()) {
76-
i.write({
77-
path: element.substring(index + 1, element.length),
78-
content: fs.createReadStream(element)
79-
})
80-
}
81-
callback()
82-
}), 10, (err) => {
81+
82+
if (res.length === 0) {
83+
res = [inPath]
84+
}
85+
86+
const writeToStream = (stream, element) => {
87+
const index = inPath.lastIndexOf('/') + 1
88+
i.write({
89+
path: element.substring(index, element.length),
90+
content: fs.createReadStream(element)
91+
})
92+
}
93+
94+
mapLimit(res, 50, (file, cb) => {
95+
fs.stat(file, (err, stat) => {
8396
if (err) {
84-
throw err
97+
return cb(err)
8598
}
86-
i.end()
99+
return cb(null, {
100+
path: file,
101+
isDirectory: stat.isDirectory()
102+
})
87103
})
88-
} else {
89-
rs = fs.createReadStream(inPath)
90-
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
91-
filePair = {path: inPath, content: rs}
92-
i.write(filePair)
104+
}, (err, res) => {
105+
if (err) {
106+
throw err
107+
}
108+
109+
res
110+
.filter((elem) => !elem.isDirectory)
111+
.map((elem) => elem.path)
112+
.forEach((elem) => writeToStream(i, elem))
113+
93114
i.end()
94-
}
115+
})
95116
})
96117
})
97118
})

test/cli/test-files.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ describe('files', () => {
3636
.run((err, stdout, exitcode) => {
3737
expect(err).to.not.exist
3838
expect(exitcode).to.equal(0)
39+
3940
const expected = [
40-
'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about',
41-
'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact',
42-
'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index',
43-
'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help',
44-
'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start',
45-
'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme',
46-
'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes',
4741
'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro',
48-
'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs',
4942
'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour',
43+
'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes',
44+
'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme',
45+
'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start',
46+
'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help',
47+
'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index',
48+
'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs',
49+
'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact',
50+
'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about',
5051
'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs'
5152
]
5253
expect(stdout).to.deep.equal(expected)

0 commit comments

Comments
 (0)