@@ -17,86 +17,88 @@ process.once('unhandledRejection', (err) => {
1717
1818const semver = require ( 'semver' )
1919const pkg = require ( '../../package.json' )
20+
2021// Check for node version
2122if ( ! semver . satisfies ( process . versions . node , pkg . engines . node ) ) {
2223 console . error ( `Please update your Node.js version to ${ pkg . engines . node } ` )
2324 process . exit ( 1 )
2425}
2526
2627const updateNotifier = require ( 'update-notifier' )
27- const debug = require ( 'debug' ) ( 'ipfs:cli' )
2828const { InvalidRepoVersionError } = require ( 'ipfs-repo/src/errors/index' )
2929const { NotEnabledError } = require ( '../core/errors' )
30- const parser = require ( './parser' )
31-
32- const commandAlias = require ( './command-alias' )
3330const { print, getIpfs, getRepoPath } = require ( './utils' )
31+ const debug = require ( 'debug' ) ( 'ipfs:cli' )
32+ const cli = require ( './' )
3433
3534// Check if an update is available and notify
3635const oneWeek = 1000 * 60 * 60 * 24 * 7
3736updateNotifier ( { pkg, updateCheckInterval : oneWeek } ) . notify ( )
3837
39- // Apply command aliasing (eg `refs local` -> `refs-local`)
40- const args = commandAlias ( process . argv . slice ( 2 ) )
41- const repoPath = getRepoPath ( )
38+ async function main ( ) {
39+ let exitCode = 0
40+ let ctx = {
41+ print,
42+ getStdin : ( ) => process . stdin ,
43+ repoPath : getRepoPath ( ) ,
44+ cleanup : ( ) => { }
45+ }
4246
43- let ctx = {
44- print,
45- repoPath,
46- cleanup : ( ) => { } ,
47- getStdin : ( ) => process . stdin
48- }
49- parser
50- . middleware ( async ( argv ) => {
51- if ( ! [ 'daemon' , 'init' ] . includes ( argv . _ [ 0 ] ) ) {
52- const { ipfs, isDaemon, cleanup } = await getIpfs ( argv )
53- ctx = {
54- print,
55- repoPath,
56- ipfs,
57- isDaemon,
58- cleanup,
59- getStdin : ctx . getStdin
47+ const command = process . argv . slice ( 2 )
48+
49+ try {
50+ const data = await cli ( command , async ( argv ) => {
51+ if ( ! [ 'daemon' , 'init' ] . includes ( command [ 0 ] ) ) {
52+ const { ipfs, isDaemon, cleanup } = await getIpfs ( argv )
53+
54+ ctx = {
55+ ...ctx ,
56+ ipfs,
57+ isDaemon,
58+ cleanup
59+ }
6060 }
61- }
6261
63- argv . ctx = ctx
64- return argv
65- } )
66- . onFinishCommand ( async ( data ) => {
62+ argv . ctx = ctx
63+
64+ return argv
65+ } )
66+
6767 if ( data ) {
6868 print ( data )
6969 }
70-
71- await ctx . cleanup ( )
72- } )
73- . fail ( async ( msg , err , yargs ) => {
74- // Handle yargs errors
75- if ( msg ) {
76- yargs . showHelp ( )
77- print . error ( '\n' )
78- print . error ( `Error: ${ msg } ` )
70+ } catch ( err ) {
71+ if ( err . code === InvalidRepoVersionError . code ) {
72+ err . message = 'Incompatible repo version. Migration needed. Pass --migrate for automatic migration'
7973 }
8074
81- // Handle commands handler errors
82- if ( err ) {
83- if ( err . code === InvalidRepoVersionError . code ) {
84- err . message = 'Incompatible repo version. Migration needed. Pass --migrate for automatic migration'
85- }
86-
87- if ( err . code === NotEnabledError . code ) {
88- err . message = `no IPFS repo found in ${ getRepoPath ( ) } .\nplease run: 'ipfs init'`
89- }
75+ if ( err . code === NotEnabledError . code ) {
76+ err . message = `no IPFS repo found in ${ ctx . repoPath } .\nplease run: 'ipfs init'`
77+ }
9078
91- if ( debug . enabled ) {
92- debug ( err )
93- } else {
94- print . error ( err . message )
95- }
79+ // Handle yargs errors
80+ if ( err . code === 'ERR_YARGS' ) {
81+ err . yargs . showHelp ( )
82+ ctx . print . error ( '\n' )
83+ ctx . print . error ( `Error: ${ err . message } ` )
84+ } else if ( debug . enabled ) {
85+ // Handle commands handler errors
86+ debug ( err )
87+ } else {
88+ ctx . print . error ( err . message )
9689 }
9790
91+ exitCode = 1
92+ } finally {
9893 await ctx . cleanup ( )
94+ }
9995
100- process . exit ( 1 )
101- } )
102- . parse ( args )
96+ if ( command [ 0 ] === 'daemon' && exitCode === 0 ) {
97+ // don't shut down the daemon process
98+ return
99+ }
100+
101+ process . exit ( exitCode )
102+ }
103+
104+ main ( )
0 commit comments