@@ -7,6 +7,7 @@ const byteman = require('byteman')
7
7
const reduce = require ( 'async/reduce' )
8
8
const mh = require ( 'multihashes' )
9
9
const multibase = require ( 'multibase' )
10
+ const toPull = require ( 'stream-to-pull-stream' )
10
11
const { print, isDaemonOn, createProgressBar } = require ( '../utils' )
11
12
const { cidToString } = require ( '../../utils/cid' )
12
13
const globSource = require ( '../../utils/files/glob-source' )
@@ -20,15 +21,9 @@ function getTotalBytes (paths, cb) {
20
21
} , cb )
21
22
}
22
23
23
- function addPipeline ( paths , addStream , options ) {
24
- const {
25
- recursive,
26
- quiet,
27
- quieter,
28
- silent
29
- } = options
24
+ function addPipeline ( source , addStream , options ) {
30
25
pull (
31
- globSource ( ... paths , { recursive } ) ,
26
+ source ,
32
27
addStream ,
33
28
pull . collect ( ( err , added ) => {
34
29
if ( err ) {
@@ -39,14 +34,15 @@ function addPipeline (paths, addStream, options) {
39
34
throw err
40
35
}
41
36
42
- if ( silent ) return
43
- if ( quieter ) return print ( added . pop ( ) . hash )
37
+ if ( options . silent ) return
38
+ if ( options . quieter ) return print ( added . pop ( ) . hash )
44
39
45
40
sortBy ( added , 'path' )
46
41
. reverse ( )
47
42
. map ( ( file ) => {
48
- const log = [ 'added' , cidToString ( file . hash , { base : options . cidBase } ) ]
49
- if ( ! quiet && file . path . length > 0 ) log . push ( file . path )
43
+ const log = options . quiet ? [ ] : [ 'added' ]
44
+ log . push ( cidToString ( file . hash , { base : options . cidBase } ) )
45
+ if ( ! options . quiet && file . path . length > 0 ) log . push ( file . path )
50
46
return log . join ( ' ' )
51
47
} )
52
48
. forEach ( ( msg ) => print ( msg ) )
@@ -55,7 +51,7 @@ function addPipeline (paths, addStream, options) {
55
51
}
56
52
57
53
module . exports = {
58
- command : 'add < file...> ' ,
54
+ command : 'add [ file...] ' ,
59
55
60
56
describe : 'Add a file to IPFS using the UnixFS data format' ,
61
57
@@ -163,8 +159,15 @@ module.exports = {
163
159
throw new Error ( 'Error: Enabling the sharding experiment should be done on the daemon' )
164
160
}
165
161
166
- if ( ! argv . progress ) {
167
- return addPipeline ( argv . file , ipfs . addPullStream ( options ) , argv )
162
+ const source = argv . file
163
+ ? globSource ( ...argv . file , { recursive : argv . recursive } )
164
+ : toPull . source ( process . stdin ) // Pipe directly to ipfs.add
165
+
166
+ const adder = ipfs . addPullStream ( options )
167
+
168
+ // No progress or piping directly to ipfs.add: no need to getTotalBytes
169
+ if ( ! argv . progress || ! argv . file ) {
170
+ return addPipeline ( source , adder , argv )
168
171
}
169
172
170
173
getTotalBytes ( argv . file , ( err , totalBytes ) => {
@@ -176,7 +179,7 @@ module.exports = {
176
179
bar . update ( byteLength / totalBytes , { progress : byteman ( byteLength , 2 , 'MB' ) } )
177
180
}
178
181
179
- addPipeline ( argv . file , ipfs . addPullStream ( options ) , argv )
182
+ addPipeline ( source , adder , argv )
180
183
} )
181
184
}
182
185
}
0 commit comments