|
3 | 3 | 'use strict'
|
4 | 4 |
|
5 | 5 | const YargsPromise = require('yargs-promise')
|
6 |
| -const yargs = require('yargs/yargs') |
7 | 6 | const updateNotifier = require('update-notifier')
|
8 | 7 | const utils = require('./utils')
|
9 | 8 | const print = utils.print
|
10 | 9 | const mfs = require('ipfs-mfs/cli')
|
11 | 10 | const debug = require('debug')('ipfs:cli')
|
12 | 11 | const pkg = require('../../package.json')
|
| 12 | +const parser = require('./parser') |
13 | 13 |
|
14 | 14 | async function main (args) {
|
15 | 15 | const oneWeek = 1000 * 60 * 60 * 24 * 7
|
16 | 16 | updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify()
|
17 | 17 |
|
18 |
| - const cli = yargs(args) |
19 |
| - .option('silent', { |
20 |
| - desc: 'Write no output', |
21 |
| - type: 'boolean', |
22 |
| - default: false, |
23 |
| - coerce: silent => { |
24 |
| - if (silent) utils.disablePrinting() |
25 |
| - return silent |
26 |
| - } |
27 |
| - }) |
28 |
| - .option('pass', { |
29 |
| - desc: 'Pass phrase for the keys', |
30 |
| - type: 'string', |
31 |
| - default: '' |
32 |
| - }) |
33 |
| - .epilog(utils.ipfsPathHelp) |
34 |
| - .demandCommand(1) |
35 |
| - .fail((msg, err, yargs) => { |
36 |
| - if (err) { |
37 |
| - throw err // preserve stack |
38 |
| - } |
39 |
| - |
40 |
| - if (args.length > 0) { |
41 |
| - print(msg) |
42 |
| - } |
43 |
| - |
44 |
| - yargs.showHelp() |
45 |
| - }) |
46 |
| - |
47 |
| - // Function to get hold of a singleton ipfs instance |
48 |
| - const getIpfs = utils.singleton(cb => utils.getIPFS(yargs(args).argv, cb)) |
| 18 | + const cli = new YargsPromise(parser) |
49 | 19 |
|
50 | 20 | // add MFS (Files API) commands
|
51 | 21 | mfs(cli)
|
52 | 22 |
|
53 |
| - cli |
54 |
| - .commandDir('commands') |
55 |
| - .help() |
56 |
| - .strict() |
57 |
| - .completion() |
58 |
| - |
59 |
| - let exitCode = 0 |
60 |
| - |
61 |
| - try { |
62 |
| - const { data } = await new YargsPromise(cli, { getIpfs }).parse(args) |
63 |
| - if (data) print(data) |
64 |
| - } catch (err) { |
65 |
| - debug(err) |
66 |
| - |
67 |
| - // the argument can have a different shape depending on where the error came from |
68 |
| - if (err.message || (err.error && err.error.message)) { |
69 |
| - print(err.message || err.error.message) |
70 |
| - } else { |
71 |
| - print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output') |
72 |
| - } |
| 23 | + let getIpfs = null |
73 | 24 |
|
74 |
| - exitCode = 1 |
75 |
| - } finally { |
76 |
| - // If an IPFS instance was used in the handler then clean it up here |
77 |
| - if (getIpfs.instance) { |
78 |
| - try { |
79 |
| - const cleanup = getIpfs.rest[0] |
80 |
| - await cleanup() |
81 |
| - } catch (err) { |
82 |
| - debug(err) |
83 |
| - exitCode = 1 |
| 25 | + cli |
| 26 | + .parse(args) |
| 27 | + .then(({ data, argv }) => { |
| 28 | + getIpfs = argv.getIpfs |
| 29 | + if (data) { |
| 30 | + print(data) |
84 | 31 | }
|
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - if (exitCode) { |
89 |
| - process.exit(exitCode) |
90 |
| - } |
| 32 | + }) |
| 33 | + .catch(({ error, argv }) => { |
| 34 | + getIpfs = argv.getIpfs |
| 35 | + debug(error) |
| 36 | + // the argument can have a different shape depending on where the error came from |
| 37 | + if (error.message || (error.error && error.error.message)) { |
| 38 | + print(error.message || error.error.message) |
| 39 | + } else { |
| 40 | + print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output') |
| 41 | + } |
| 42 | + process.exit(1) |
| 43 | + }) |
| 44 | + .finally(async () => { |
| 45 | + // If an IPFS instance was used in the handler then clean it up here |
| 46 | + if (getIpfs && getIpfs.instance) { |
| 47 | + try { |
| 48 | + const cleanup = getIpfs.rest[0] |
| 49 | + await cleanup() |
| 50 | + } catch (err) { |
| 51 | + debug(err) |
| 52 | + process.exit(1) |
| 53 | + } |
| 54 | + } |
| 55 | + }) |
91 | 56 | }
|
92 | 57 |
|
93 | 58 | main(process.argv.slice(2))
|
0 commit comments