22
33'use strict'
44
5+ const fs = require ( 'fs' )
6+ const path = require ( 'path' )
57const yargs = require ( 'yargs/yargs' )
68const updateNotifier = require ( 'update-notifier' )
79const readPkgUp = require ( 'read-pkg-up' )
@@ -18,16 +20,16 @@ updateNotifier({
1820} ) . notify ( )
1921
2022const MSG_USAGE = `Usage:
21- ipfs - Global p2p merkle-dag filesystem.
23+ ipfs - Global p2p merkle-dag filesystem.
2224
2325 ipfs [options] <command> ...`
24- const MSG_EPILOGUE = `Use 'ipfs <command> --help' to learn more about each command.
26+ const MSG_EPILOGUE = `Use 'ipfs <command> --help' to learn more about each command.
2527
2628ipfs uses a repository in the local file system. By default, the repo is
2729located at ~/.ipfs. To change the repo location, set the $IPFS_PATH
2830environment variable:
2931
30- export IPFS_PATH=/path/to/ipfsrepo
32+ export IPFS_PATH=/path/to/ipfsrepo
3133
3234EXIT STATUS
3335
@@ -39,6 +41,9 @@ The CLI will exit with one of the following values:
3941const MSG_NO_CMD = 'You need at least one command before moving on'
4042
4143const argv = process . argv . slice ( 2 )
44+ const commandNames = fs . readdirSync ( path . join ( __dirname , 'commands' ) )
45+ const isCommand = commandNames . includes ( `${ argv [ 0 ] } .js` )
46+
4247let args = { }
4348let cli = yargs ( argv )
4449 . usage ( MSG_USAGE )
@@ -63,13 +68,25 @@ let cli = yargs(argv)
6368 desc : 'Use a specific API instance.' ,
6469 type : 'string'
6570 } )
66- . commandDir ( 'commands' )
67- // NOTE: This creates an alias of
68- // `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
69- // This will stay until https://github.com/ipfs/specs/issues/98 is resolved.
70- . command ( addCmd )
71- . command ( catCmd )
72- . command ( getCmd )
71+ . commandDir ( 'commands' , {
72+ // Only include the commands for the sub-system we're using, or include all
73+ // if no sub-system command has been passed.
74+ include ( path , filename ) {
75+ if ( ! isCommand ) return true
76+ return `${ argv [ 0 ] } .js` === filename
77+ }
78+ } )
79+
80+ if ( ! isCommand ) {
81+ cli
82+ // NOTE: This creates an alias of
83+ // `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
84+ // This will stay until https://github.com/ipfs/specs/issues/98 is resolved.
85+ . command ( addCmd )
86+ . command ( catCmd )
87+ . command ( getCmd )
88+ }
89+ cli
7390 . demandCommand ( 1 , MSG_NO_CMD )
7491 . alias ( 'help' , 'h' )
7592 . epilogue ( MSG_EPILOGUE )
0 commit comments