UNPKG

1.2 kBJavaScriptView Raw
1/** Copyright (c) 2018 Uber Technologies, Inc.
2 *
3 * This source code is licensed under the MIT license found in the
4 * LICENSE file in the root directory of this source tree.
5 *
6 * @flow
7 */
8
9/* eslint-env node */
10
11process.on('unhandledRejection', e => {
12 throw e;
13});
14
15const sade = require('sade');
16
17module.exports.run = (args /*: any*/) => {
18 const data = require('../commands/index.js');
19 const instance = sade('fusion').version(require('../package.json').version);
20 for (const [command, metadata] of Object.entries(data)) {
21 if (metadata instanceof Object) {
22 // $FlowFixMe
23 instance.command(command).describe(metadata.descr);
24 // Add subcommands
25 // $FlowFixMe
26 if (metadata.options) {
27 // $FlowFixMe
28 for (const [opt, optmeta] of Object.entries(metadata.options)) {
29 if (optmeta instanceof Object) {
30 // $FlowFixMe
31 instance.option('--' + opt, optmeta.describe, optmeta.default);
32 }
33 }
34 }
35 }
36 instance.action((...args) =>
37 // $FlowFixMe
38 require(`../commands/${command}.js`).run(...args)
39 );
40 }
41 return instance.parse(typeof args === 'string' ? args.split(' ') : args);
42};