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

Commit 3edc2b9

Browse files
dignifiedquiredaviddias
authored andcommitted
fix(cli): make ipfs files add work online and offline
Fixes #480
1 parent d682b0c commit 3edc2b9

File tree

3 files changed

+103
-42
lines changed

3 files changed

+103
-42
lines changed

src/cli/commands/files/add.js

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const path = require('path')
99
const glob = require('glob')
1010
const sortBy = require('lodash.sortby')
1111
const pull = require('pull-stream')
12-
const pullFile = require('pull-file')
1312
const paramap = require('pull-paramap')
1413
const zip = require('pull-zip')
14+
const toPull = require('stream-to-pull-stream')
1515

1616
function checkPath (inPath, recursive) {
1717
// This function is to check for the following possible inputs
@@ -59,46 +59,71 @@ module.exports = {
5959
throw err
6060
}
6161

62-
glob(path.join(inPath, '/**/*'), (err, list) => {
62+
// TODO: revist when interface-ipfs-core exposes pull-streams
63+
let createAddStream = (cb) => {
64+
ipfs.files.createAddStream((err, stream) => {
65+
cb(err, err ? null : toPull.transform(stream))
66+
})
67+
}
68+
69+
if (typeof ipfs.files.createAddPullStream === 'function') {
70+
createAddStream = (cb) => {
71+
cb(null, ipfs.files.createAddPullStream())
72+
}
73+
}
74+
75+
createAddStream((err, addStream) => {
6376
if (err) {
6477
throw err
6578
}
66-
if (list.length === 0) {
67-
list = [inPath]
68-
}
6979

70-
pull(
71-
zip(
72-
pull.values(list),
73-
pull(
74-
pull.values(list),
75-
paramap(fs.stat.bind(fs), 50)
76-
)
77-
),
78-
pull.map((pair) => ({
79-
path: pair[0],
80-
isDirectory: pair[1].isDirectory()
81-
})),
82-
pull.filter((file) => !file.isDirectory),
83-
pull.map((file) => ({
84-
path: file.path.substring(index, file.path.length),
85-
content: pullFile(file.path)
86-
})),
87-
ipfs.files.createAddPullStream(),
88-
pull.map((file) => ({
89-
hash: file.hash,
90-
path: file.path
91-
})),
92-
pull.collect((err, added) => {
93-
if (err) throw err
94-
95-
sortBy(added, 'path')
96-
.reverse()
97-
.map((file) => `added ${file.hash} ${file.path}`)
98-
.forEach((msg) => console.log(msg))
99-
})
100-
)
80+
glob(path.join(inPath, '/**/*'), (err, list) => {
81+
if (err) {
82+
throw err
83+
}
84+
if (list.length === 0) {
85+
list = [inPath]
86+
}
87+
88+
addPipeline(index, addStream, list)
89+
})
10190
})
10291
})
10392
}
10493
}
94+
95+
function addPipeline (index, addStream, list) {
96+
pull(
97+
zip(
98+
pull.values(list),
99+
pull(
100+
pull.values(list),
101+
paramap(fs.stat.bind(fs), 50)
102+
)
103+
),
104+
pull.map((pair) => ({
105+
path: pair[0],
106+
isDirectory: pair[1].isDirectory()
107+
})),
108+
pull.filter((file) => !file.isDirectory),
109+
pull.map((file) => ({
110+
path: file.path.substring(index, file.path.length),
111+
content: fs.createReadStream(file.path)
112+
})),
113+
addStream,
114+
pull.map((file) => ({
115+
hash: file.hash,
116+
path: file.path
117+
})),
118+
pull.collect((err, added) => {
119+
if (err) {
120+
throw err
121+
}
122+
123+
sortBy(added, 'path')
124+
.reverse()
125+
.map((file) => `added ${file.hash} ${file.path}`)
126+
.forEach((msg) => console.log(msg))
127+
})
128+
)
129+
}

src/core/ipfs/files.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ const toStream = require('pull-stream-to-stream')
1313
const toPull = require('stream-to-pull-stream')
1414

1515
module.exports = function files (self) {
16-
const createAddPullStream = () => pull(
17-
pull.map(normalizeContent),
18-
pull.flatten(),
19-
importer(self._dagS),
20-
pull.asyncMap(prepareFile.bind(null, self))
21-
)
16+
const createAddPullStream = () => {
17+
return pull(
18+
pull.map(normalizeContent),
19+
pull.flatten(),
20+
importer(self._dagS),
21+
pull.asyncMap(prepareFile.bind(null, self))
22+
)
23+
}
2224

2325
return {
2426
createAddStream: (callback) => {

test/cli/test-files.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,39 @@ describe('files', () => {
8484
done()
8585
})
8686
})
87+
88+
it('add', (done) => {
89+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', 'src/init-files/init-docs/readme'], {env})
90+
.run((err, stdout, exitcode) => {
91+
expect(err).to.not.exist
92+
expect(exitcode).to.equal(0)
93+
expect(stdout[0]).to.equal('added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB readme')
94+
done()
95+
})
96+
})
97+
98+
it('add recursively', (done) => {
99+
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'files', 'add', '-r', 'src/init-files/init-docs'], {env})
100+
.run((err, stdout, exitcode) => {
101+
expect(err).to.not.exist
102+
expect(exitcode).to.equal(0)
103+
104+
const expected = [
105+
'added QmYE7xo6NxbHEVEHej1yzxijYaNY51BaeKxjXxn6Ssa6Bs init-docs/tour/0.0-intro',
106+
'added QmciSU8hfpAXKjvK5YLUSwApomGSWN5gFbP4EpDAEzu2Te init-docs/tour',
107+
'added QmTumTjvcYCAvRRwQ8sDRxh8ezmrcr88YFU7iYNroGGTBZ init-docs/security-notes',
108+
'added QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB init-docs/readme',
109+
'added QmdncfsVm2h5Kqq9hPmU7oAVX2zTSVP3L869tgTbPYnsha init-docs/quick-start',
110+
'added QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7 init-docs/help',
111+
'added QmQN88TEidd3RY2u3dpib49fERTDfKtDpvxnvczATNsfKT init-docs/docs/index',
112+
'added QmegvLXxpVKiZ4b57Xs1syfBVRd8CbucVHAp7KpLQdGieC init-docs/docs',
113+
'added QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y init-docs/contact',
114+
'added QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V init-docs/about',
115+
'added QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU init-docs'
116+
]
117+
expect(stdout).to.deep.equal(expected)
118+
done()
119+
})
120+
})
87121
})
88122
})

0 commit comments

Comments
 (0)