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

Commit bf62c28

Browse files
committed
Merge pull request #312 from noffle/add-cli
fix `ipfs files add` CLI command.
2 parents 0aebd47 + f3fcc74 commit bf62c28

File tree

2 files changed

+65
-30
lines changed

2 files changed

+65
-30
lines changed

src/cli/commands/files/add.js

+32-30
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,39 @@ module.exports = Command.extend({
5959
if (err) {
6060
throw err
6161
}
62-
const i = ipfs.files.add()
63-
var filePair
64-
i.on('data', (file) => {
65-
console.log('added', bs58.encode(file.multihash).toString(), file.path)
66-
})
67-
i.once('end', () => {
68-
return
69-
})
70-
if (res.length !== 0) {
71-
const index = inPath.lastIndexOf('/')
72-
parallelLimit(res.map((element) => (callback) => {
73-
if (!fs.statSync(element).isDirectory()) {
74-
i.write({
75-
path: element.substring(index + 1, element.length),
76-
content: fs.createReadStream(element)
77-
})
78-
}
79-
callback()
80-
}), 10, (err) => {
81-
if (err) {
82-
throw err
83-
}
84-
i.end()
62+
ipfs.files.createAddStream((err, i) => {
63+
if (err) throw err
64+
var filePair
65+
i.on('data', (file) => {
66+
console.log('added', bs58.encode(file.node.multihash()).toString(), file.path)
8567
})
86-
} else {
87-
rs = fs.createReadStream(inPath)
88-
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
89-
filePair = {path: inPath, content: rs}
90-
i.write(filePair)
91-
i.end()
92-
}
68+
i.once('end', () => {
69+
return
70+
})
71+
if (res.length !== 0) {
72+
const index = inPath.lastIndexOf('/')
73+
parallelLimit(res.map((element) => (callback) => {
74+
if (!fs.statSync(element).isDirectory()) {
75+
i.write({
76+
path: element.substring(index + 1, element.length),
77+
content: fs.createReadStream(element)
78+
})
79+
}
80+
callback()
81+
}), 10, (err) => {
82+
if (err) {
83+
throw err
84+
}
85+
i.end()
86+
})
87+
} else {
88+
rs = fs.createReadStream(inPath)
89+
inPath = inPath.substring(inPath.lastIndexOf('/') + 1, inPath.length)
90+
filePair = {path: inPath, content: rs}
91+
i.write(filePair)
92+
i.end()
93+
}
94+
})
9395
})
9496
})
9597
}

test/cli/test-files.js

+33
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,39 @@ describe('files', () => {
2020
done()
2121
})
2222
})
23+
24+
it('add', (done) => {
25+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', 'src/init-files/init-docs/readme'], {env})
26+
.run((err, stdout, exitcode) => {
27+
expect(err).to.not.exist
28+
expect(exitcode).to.equal(0)
29+
expect(stdout[0]).to.equal('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme')
30+
done()
31+
})
32+
})
33+
34+
it('add recursively', (done) => {
35+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', '-r', 'src/init-files/init-docs'], {env})
36+
.run((err, stdout, exitcode) => {
37+
expect(err).to.not.exist
38+
expect(exitcode).to.equal(0)
39+
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',
47+
'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro',
48+
'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs',
49+
'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour',
50+
'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs'
51+
]
52+
expect(stdout).to.deep.equal(expected)
53+
done()
54+
})
55+
})
2356
})
2457

2558
describe('api running', () => {

0 commit comments

Comments
 (0)