22
33'use strict'
44
5+ const YargsPromise = require ( 'yargs-promise' )
56const yargs = require ( 'yargs' )
67const updateNotifier = require ( 'update-notifier' )
78const readPkgUp = require ( 'read-pkg-up' )
8- const fs = require ( 'fs' )
9- const path = require ( 'path' )
109const utils = require ( './utils' )
1110const print = utils . print
11+ const mfs = require ( 'ipfs-mfs/cli' )
12+ const debug = require ( 'debug' ) ( 'ipfs:cli' )
1213
1314const pkg = readPkgUp . sync ( { cwd : __dirname } ) . pkg
1415updateNotifier ( {
@@ -18,10 +19,6 @@ updateNotifier({
1819
1920const args = process . argv . slice ( 2 )
2021
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-
2522const cli = yargs
2623 . option ( 'silent' , {
2724 desc : 'Write no output' ,
@@ -34,14 +31,6 @@ const cli = yargs
3431 type : 'string' ,
3532 default : ''
3633 } )
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- } )
4534 . epilog ( utils . ipfsPathHelp )
4635 . demandCommand ( 1 )
4736 . fail ( ( msg , err , yargs ) => {
@@ -56,27 +45,15 @@ const cli = yargs
5645 yargs . showHelp ( )
5746 } )
5847
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-
7348// Need to skip to avoid locking as these commands
7449// don't require a daemon
7550if ( args [ 0 ] === 'daemon' || args [ 0 ] === 'init' ) {
7651 cli
7752 . help ( )
7853 . strict ( )
7954 . completion ( )
55+ . command ( require ( './commands/daemon' ) )
56+ . command ( require ( './commands/init' ) )
8057 . parse ( args )
8158} else {
8259 // here we have to make a separate yargs instance with
@@ -86,19 +63,61 @@ if (args[0] === 'daemon' || args[0] === 'init') {
8663 if ( err ) {
8764 throw err
8865 }
66+
8967 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+ } )
9185
9286 cli
87+ . commandDir ( 'commands' )
9388 . help ( )
9489 . strict ( )
9590 . completion ( )
96- . parse ( args , { ipfs : ipfs } , ( err , argv , output ) => {
97- if ( output ) { print ( output ) }
9891
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+ }
102121 } )
103122 } )
104123 } )
0 commit comments