|
3 | 3 | /* eslint-disable no-console */
|
4 | 4 | 'use strict'
|
5 | 5 |
|
6 |
| -process.on('uncaughtException', (err) => { |
7 |
| - console.info(err) |
8 |
| - |
9 |
| - throw err |
| 6 | +// Handle any uncaught errors |
| 7 | +process.once('uncaughtException', (err, origin) => { |
| 8 | + if (origin === 'uncaughtException') { |
| 9 | + console.error(err) |
| 10 | + process.exit(1) |
| 11 | + } |
10 | 12 | })
|
11 |
| - |
12 |
| -process.on('unhandledRejection', (err) => { |
13 |
| - console.info(err) |
14 |
| - |
15 |
| - throw err |
| 13 | +process.once('unhandledRejection', (err) => { |
| 14 | + console.error(err) |
| 15 | + process.exit(1) |
16 | 16 | })
|
17 | 17 |
|
18 | 18 | const semver = require('semver')
|
19 | 19 | const pkg = require('../../package.json')
|
20 |
| - |
| 20 | +// Check for node version |
21 | 21 | if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
|
22 | 22 | console.error(`Please update your Node.js version to ${pkg.engines.node}`)
|
23 | 23 | process.exit(1)
|
24 | 24 | }
|
25 | 25 |
|
26 | 26 | const YargsPromise = require('yargs-promise')
|
27 | 27 | const updateNotifier = require('update-notifier')
|
28 |
| -const { print } = require('./utils') |
29 |
| -const mfs = require('ipfs-mfs/cli') |
30 | 28 | const debug = require('debug')('ipfs:cli')
|
31 | 29 | const parser = require('./parser')
|
32 | 30 | const commandAlias = require('./command-alias')
|
| 31 | +const { print } = require('./utils') |
33 | 32 |
|
34 |
| -function main (args) { |
35 |
| - const oneWeek = 1000 * 60 * 60 * 24 * 7 |
36 |
| - updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify() |
37 |
| - |
38 |
| - const cli = new YargsPromise(parser) |
39 |
| - |
40 |
| - // add MFS (Files API) commands |
41 |
| - mfs(cli) |
| 33 | +// Check if an update is available and notify |
| 34 | +const oneWeek = 1000 * 60 * 60 * 24 * 7 |
| 35 | +updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify() |
42 | 36 |
|
43 |
| - let getIpfs = null |
| 37 | +const cli = new YargsPromise(parser) |
44 | 38 |
|
45 |
| - // Apply command aliasing (eg `refs local` -> `refs-local`) |
46 |
| - args = commandAlias(args) |
| 39 | +let getIpfs = null |
47 | 40 |
|
48 |
| - cli |
49 |
| - .parse(args) |
50 |
| - .then(({ data, argv }) => { |
51 |
| - getIpfs = argv.getIpfs |
52 |
| - if (data) { |
53 |
| - print(data) |
54 |
| - } |
55 |
| - }) |
56 |
| - .catch(({ error, argv }) => { |
57 |
| - getIpfs = argv && argv.getIpfs |
| 41 | +// Apply command aliasing (eg `refs local` -> `refs-local`) |
| 42 | +const args = commandAlias(process.argv.slice(2)) |
| 43 | +cli |
| 44 | + .parse(args) |
| 45 | + .then(({ data, argv }) => { |
| 46 | + getIpfs = argv.getIpfs |
| 47 | + if (data) { |
| 48 | + print(data) |
| 49 | + } |
| 50 | + }) |
| 51 | + .catch(({ error, argv }) => { |
| 52 | + getIpfs = argv.getIpfs |
| 53 | + if (error.message) { |
| 54 | + print(error.message) |
58 | 55 | debug(error)
|
59 |
| - // the argument can have a different shape depending on where the error came from |
60 |
| - if (error.message || (error.error && error.error.message)) { |
61 |
| - print(error.message || error.error.message) |
62 |
| - } else { |
63 |
| - print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output') |
64 |
| - } |
65 |
| - process.exit(1) |
66 |
| - }) |
67 |
| - .finally(async () => { |
68 |
| - // If an IPFS instance was used in the handler then clean it up here |
69 |
| - if (getIpfs && getIpfs.instance) { |
70 |
| - try { |
71 |
| - const cleanup = getIpfs.rest[0] |
72 |
| - await cleanup() |
73 |
| - } catch (err) { |
74 |
| - debug(err) |
75 |
| - process.exit(1) |
76 |
| - } |
77 |
| - } |
78 |
| - }) |
79 |
| -} |
80 |
| - |
81 |
| -main(process.argv.slice(2)) |
| 56 | + } else { |
| 57 | + print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output') |
| 58 | + debug(error) |
| 59 | + } |
| 60 | + process.exit(1) |
| 61 | + }) |
| 62 | + .finally(() => { |
| 63 | + if (getIpfs && getIpfs.instance) { |
| 64 | + const cleanup = getIpfs.rest[0] |
| 65 | + return cleanup() |
| 66 | + } |
| 67 | + }) |
0 commit comments