2
2
3
3
'use strict'
4
4
5
+ const YargsPromise = require ( 'yargs-promise' )
5
6
const yargs = require ( 'yargs' )
6
7
const updateNotifier = require ( 'update-notifier' )
7
8
const readPkgUp = require ( 'read-pkg-up' )
8
- const fs = require ( 'fs' )
9
- const path = require ( 'path' )
10
9
const utils = require ( './utils' )
11
10
const print = utils . print
11
+ const mfs = require ( 'ipfs-mfs/cli' )
12
+ const debug = require ( 'debug' ) ( 'ipfs:cli' )
12
13
13
14
const pkg = readPkgUp . sync ( { cwd : __dirname } ) . pkg
14
15
updateNotifier ( {
@@ -18,10 +19,6 @@ updateNotifier({
18
19
19
20
const args = process . argv . slice ( 2 )
20
21
21
- // Determine if the first argument is a sub-system command
22
- const commandNames = fs . readdirSync ( path . join ( __dirname , 'commands' ) )
23
- const isCommand = commandNames . includes ( `${ args [ 0 ] } .js` )
24
-
25
22
const cli = yargs
26
23
. option ( 'silent' , {
27
24
desc : 'Write no output' ,
@@ -34,14 +31,6 @@ const cli = yargs
34
31
type : 'string' ,
35
32
default : ''
36
33
} )
37
- . commandDir ( 'commands' , {
38
- // Only include the commands for the sub-system we're using, or include all
39
- // if no sub-system command has been passed.
40
- include ( path , filename ) {
41
- if ( ! isCommand ) return true
42
- return `${ args [ 0 ] } .js` === filename
43
- }
44
- } )
45
34
. epilog ( utils . ipfsPathHelp )
46
35
. demandCommand ( 1 )
47
36
. fail ( ( msg , err , yargs ) => {
@@ -56,27 +45,15 @@ const cli = yargs
56
45
yargs . showHelp ( )
57
46
} )
58
47
59
- // If not a sub-system command then load the top level aliases
60
- if ( ! isCommand ) {
61
- // NOTE: This creates an alias of
62
- // `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
63
- // This will stay until https://github.com./ipfs/specs/issues/98 is resolved.
64
- const addCmd = require ( './commands/files/add' )
65
- const catCmd = require ( './commands/files/cat' )
66
- const getCmd = require ( './commands/files/get' )
67
- const aliases = [ addCmd , catCmd , getCmd ]
68
- aliases . forEach ( ( alias ) => {
69
- cli . command ( alias . command , alias . describe , alias . builder , alias . handler )
70
- } )
71
- }
72
-
73
48
// Need to skip to avoid locking as these commands
74
49
// don't require a daemon
75
50
if ( args [ 0 ] === 'daemon' || args [ 0 ] === 'init' ) {
76
51
cli
77
52
. help ( )
78
53
. strict ( )
79
54
. completion ( )
55
+ . command ( require ( './commands/daemon' ) )
56
+ . command ( require ( './commands/init' ) )
80
57
. parse ( args )
81
58
} else {
82
59
// here we have to make a separate yargs instance with
@@ -86,19 +63,61 @@ if (args[0] === 'daemon' || args[0] === 'init') {
86
63
if ( err ) {
87
64
throw err
88
65
}
66
+
89
67
utils . getIPFS ( argv , ( err , ipfs , cleanup ) => {
90
- if ( err ) { throw err }
68
+ if ( err ) {
69
+ throw err
70
+ }
71
+
72
+ // add mfs commands
73
+ mfs ( cli )
74
+
75
+ // NOTE: This creates an alias of
76
+ // `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
77
+ // This will stay until https://github.com./ipfs/specs/issues/98 is resolved.
78
+ const addCmd = require ( './commands/files/add' )
79
+ const catCmd = require ( './commands/files/cat' )
80
+ const getCmd = require ( './commands/files/get' )
81
+ const aliases = [ addCmd , catCmd , getCmd ]
82
+ aliases . forEach ( ( alias ) => {
83
+ cli . command ( alias )
84
+ } )
91
85
92
86
cli
87
+ . commandDir ( 'commands' )
93
88
. help ( )
94
89
. strict ( )
95
90
. completion ( )
96
- . parse ( args , { ipfs : ipfs } , ( err , argv , output ) => {
97
- if ( output ) { print ( output ) }
98
91
99
- cleanup ( ( ) => {
100
- if ( err ) { throw err }
101
- } )
92
+ let exitCode = 0
93
+
94
+ const parser = new YargsPromise ( cli , { ipfs } )
95
+ parser . parse ( args )
96
+ . then ( ( { data, argv } ) => {
97
+ if ( data ) {
98
+ print ( data )
99
+ }
100
+ } )
101
+ . catch ( ( arg ) => {
102
+ debug ( arg )
103
+
104
+ // the argument can have a different shape depending on where the error came from
105
+ if ( arg . message ) {
106
+ print ( arg . message )
107
+ } else if ( arg . error && arg . error . message ) {
108
+ print ( arg . error . message )
109
+ } else {
110
+ print ( 'Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output' )
111
+ }
112
+
113
+ exitCode = 1
114
+ } )
115
+ . then ( ( ) => cleanup ( ) )
116
+ . catch ( ( ) => { } )
117
+ . then ( ( ) => {
118
+ if ( exitCode !== 0 ) {
119
+ process . exit ( exitCode )
120
+ }
102
121
} )
103
122
} )
104
123
} )
0 commit comments