|
3 | 3 | 'use strict' |
4 | 4 |
|
5 | 5 | const YargsPromise = require('yargs-promise') |
6 | | -const yargs = require('yargs') |
| 6 | +const yargs = require('yargs/yargs') |
7 | 7 | const updateNotifier = require('update-notifier') |
8 | | -const readPkgUp = require('read-pkg-up') |
9 | 8 | const utils = require('./utils') |
10 | 9 | const print = utils.print |
11 | 10 | const mfs = require('ipfs-mfs/cli') |
12 | 11 | const debug = require('debug')('ipfs:cli') |
| 12 | +const pkg = require('../../package.json') |
| 13 | + |
| 14 | +async function main (args) { |
| 15 | + const oneWeek = 1000 * 60 * 60 * 24 * 7 |
| 16 | + updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify() |
| 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 | + } |
13 | 39 |
|
14 | | -const pkg = readPkgUp.sync({ cwd: __dirname }).pkg |
15 | | -updateNotifier({ |
16 | | - pkg, |
17 | | - updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week |
18 | | -}).notify() |
19 | | - |
20 | | -const args = process.argv.slice(2) |
21 | | - |
22 | | -const cli = yargs |
23 | | - .option('silent', { |
24 | | - desc: 'Write no output', |
25 | | - type: 'boolean', |
26 | | - default: false, |
27 | | - coerce: ('silent', silent => { |
28 | | - if (silent) { |
29 | | - utils.disablePrinting() |
| 40 | + if (args.length > 0) { |
| 41 | + print(msg) |
30 | 42 | } |
31 | | - return silent |
| 43 | + |
| 44 | + yargs.showHelp() |
32 | 45 | }) |
33 | | - }) |
34 | | - .option('pass', { |
35 | | - desc: 'Pass phrase for the keys', |
36 | | - type: 'string', |
37 | | - default: '' |
38 | | - }) |
39 | | - .epilog(utils.ipfsPathHelp) |
40 | | - .demandCommand(1) |
41 | | - .fail((msg, err, yargs) => { |
42 | | - if (err) { |
43 | | - throw err // preserve stack |
44 | | - } |
45 | 46 |
|
46 | | - if (args.length > 0) { |
47 | | - print(msg) |
48 | | - } |
| 47 | + // Function to get hold of a singleton ipfs instance |
| 48 | + const getIpfs = utils.singleton(cb => utils.getIPFS(yargs(args).argv, cb)) |
49 | 49 |
|
50 | | - yargs.showHelp() |
51 | | - }) |
| 50 | + // add MFS (Files API) commands |
| 51 | + mfs(cli) |
52 | 52 |
|
53 | | -// Need to skip to avoid locking as these commands |
54 | | -// don't require a daemon |
55 | | -if (args[0] === 'daemon' || args[0] === 'init') { |
56 | 53 | cli |
| 54 | + .commandDir('commands') |
57 | 55 | .help() |
58 | 56 | .strict() |
59 | 57 | .completion() |
60 | | - .command(require('./commands/daemon')) |
61 | | - .command(require('./commands/init')) |
62 | | - |
63 | | - new YargsPromise(cli).parse(args) |
64 | | - .then(({ data }) => { |
65 | | - if (data) print(data) |
66 | | - }) |
67 | | -} else { |
68 | | - // here we have to make a separate yargs instance with |
69 | | - // only the `api` option because we need this before doing |
70 | | - // the final yargs parse where the command handler is invoked.. |
71 | | - yargs().option('api').parse(process.argv, (err, argv, output) => { |
72 | | - if (err) { |
73 | | - throw err |
74 | | - } |
75 | | - |
76 | | - utils.getIPFS(argv, (err, ipfs, cleanup) => { |
77 | | - if (err) { |
78 | | - throw err |
79 | | - } |
80 | | - |
81 | | - // add MFS (Files API) commands |
82 | | - mfs(cli) |
83 | 58 |
|
84 | | - cli |
85 | | - .commandDir('commands') |
86 | | - .help() |
87 | | - .strict() |
88 | | - .completion() |
| 59 | + let exitCode = 0 |
89 | 60 |
|
90 | | - let exitCode = 0 |
| 61 | + try { |
| 62 | + const { data } = await new YargsPromise(cli, { getIpfs }).parse(args) |
| 63 | + if (data) print(data) |
| 64 | + } catch (err) { |
| 65 | + debug(err) |
91 | 66 |
|
92 | | - const parser = new YargsPromise(cli, { ipfs }) |
93 | | - parser.parse(args) |
94 | | - .then(({ data, argv }) => { |
95 | | - if (data) { |
96 | | - print(data) |
97 | | - } |
98 | | - }) |
99 | | - .catch((arg) => { |
100 | | - debug(arg) |
| 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 | + } |
101 | 73 |
|
102 | | - // the argument can have a different shape depending on where the error came from |
103 | | - if (arg.message) { |
104 | | - print(arg.message) |
105 | | - } else if (arg.error && arg.error.message) { |
106 | | - print(arg.error.message) |
107 | | - } else { |
108 | | - print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output') |
109 | | - } |
| 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 |
| 84 | + } |
| 85 | + } |
| 86 | + } |
110 | 87 |
|
111 | | - exitCode = 1 |
112 | | - }) |
113 | | - .then(() => cleanup()) |
114 | | - .catch(() => {}) |
115 | | - .then(() => { |
116 | | - if (exitCode !== 0) { |
117 | | - process.exit(exitCode) |
118 | | - } |
119 | | - }) |
120 | | - }) |
121 | | - }) |
| 88 | + if (exitCode) { |
| 89 | + process.exit(exitCode) |
| 90 | + } |
122 | 91 | } |
| 92 | + |
| 93 | +main(process.argv.slice(2)) |
0 commit comments