33const promisify = require ( 'promisify-es6' )
44const UnixFs = require ( 'ipfs-unixfs' )
55const waterfall = require ( 'async/waterfall' )
6+ const series = require ( 'async/series' )
67const {
78 DAGNode
89} = require ( 'ipld-dag-pb' )
@@ -11,6 +12,7 @@ const {
1112 traverseTo,
1213 updateTree,
1314 updateMfsRoot,
15+ toSources,
1416 FILE_SEPARATOR
1517} = require ( './utils' )
1618
@@ -19,50 +21,57 @@ const defaultOptions = {
1921}
2022
2123module . exports = function mfsRm ( ipfs ) {
22- return promisify ( function ( path , options , callback ) {
23- if ( typeof path === 'function' ) {
24- return path ( new Error ( 'Please specify a path to remove' ) )
25- }
24+ return promisify ( function ( ) {
25+ const args = Array . prototype . slice . call ( arguments )
26+ const {
27+ sources,
28+ options,
29+ callback
30+ } = toSources ( args , defaultOptions )
2631
27- if ( ! callback ) {
28- callback = options
29- options = { }
32+ if ( ! sources . length ) {
33+ return callback ( new Error ( 'Please supply at least one path to remove' ) )
3034 }
3135
32- options = Object . assign ( { } , defaultOptions , options )
33-
34- path = path . trim ( )
36+ series (
37+ sources . map ( source => {
38+ return ( done ) => removePath ( ipfs , source . path , options , done )
39+ } ) ,
40+ callback
41+ )
42+ } )
43+ }
3544
36- if ( path === FILE_SEPARATOR ) {
37- return callback ( new Error ( 'Cannot delete root' ) )
38- }
45+ const removePath = ( ipfs , path , options , callback ) => {
46+ if ( path === FILE_SEPARATOR ) {
47+ return callback ( new Error ( 'Cannot delete root' ) )
48+ }
3949
40- waterfall ( [
41- ( cb ) => traverseTo ( ipfs , path , {
42- withCreateHint : false
43- } , cb ) ,
44- ( result , cb ) => {
45- const meta = UnixFs . unmarshal ( result . node . data )
50+ waterfall ( [
51+ ( cb ) => traverseTo ( ipfs , path , {
52+ withCreateHint : false
53+ } , cb ) ,
54+ ( result , cb ) => {
55+ const meta = UnixFs . unmarshal ( result . node . data )
4656
47- if ( meta . type === 'directory' && ! options . recursive ) {
48- return cb ( new Error ( `${ path } is a directory, use -r to remove directories` ) )
49- }
57+ if ( meta . type === 'directory' && ! options . recursive ) {
58+ return cb ( new Error ( `${ path } is a directory, use -r to remove directories` ) )
59+ }
5060
51- waterfall ( [
52- ( next ) => DAGNode . rmLink ( result . parent . node , result . name , next ) ,
53- ( newParentNode , next ) => {
54- ipfs . dag . put ( newParentNode , {
55- cid : new CID ( newParentNode . hash || newParentNode . multihash )
56- } , ( error ) => next ( error , newParentNode ) )
57- } ,
58- ( newParentNode , next ) => {
59- result . parent . node = newParentNode
61+ waterfall ( [
62+ ( next ) => DAGNode . rmLink ( result . parent . node , result . name , next ) ,
63+ ( newParentNode , next ) => {
64+ ipfs . dag . put ( newParentNode , {
65+ cid : new CID ( newParentNode . hash || newParentNode . multihash )
66+ } , ( error ) => next ( error , newParentNode ) )
67+ } ,
68+ ( newParentNode , next ) => {
69+ result . parent . node = newParentNode
6070
61- updateTree ( ipfs , result . parent , next )
62- } ,
63- ( newRoot , next ) => updateMfsRoot ( ipfs , newRoot . node . multihash , next )
64- ] , cb )
65- }
66- ] , callback )
67- } )
71+ updateTree ( ipfs , result . parent , next )
72+ } ,
73+ ( newRoot , next ) => updateMfsRoot ( ipfs , newRoot . node . multihash , next )
74+ ] , cb )
75+ }
76+ ] , callback )
6877}
0 commit comments