1 |
|
2 | const yargs = require('yargs');
|
3 |
|
4 |
|
5 | const {_} = require('@gigster/gig-utils');
|
6 | const {Command, PluginManager} = require('@gigster/gig-models');
|
7 | const {
|
8 | AnalyticsService,
|
9 | ConfigService,
|
10 | LogService,
|
11 | NetworkService,
|
12 | UpdateService,
|
13 | } = require('@gigster/gig-services');
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | class CLI {
|
20 |
|
21 | constructor({name, version}) {
|
22 | this.name = name;
|
23 | this.version = version;
|
24 | this.pluginManager = new PluginManager();
|
25 | ConfigService.version = version;
|
26 | }
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 | parseArgs() {
|
33 | const {commands} = this.pluginManager;
|
34 | const createCommand = (args, command) => {
|
35 | return args.command({
|
36 | command: command.name,
|
37 | aliases: command.aliases,
|
38 | description: command.description,
|
39 | builder: command.builder,
|
40 | handler: this.createHandler({command}),
|
41 | });
|
42 | };
|
43 |
|
44 | return _.tap(commands.reduce(createCommand, yargs), (args) => {
|
45 |
|
46 |
|
47 | args.$0 = this.name;
|
48 | args.options(Command.globalFlags);
|
49 | });
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 | createHandler({command}) {
|
57 | return (argv) => {
|
58 | try {
|
59 | const [name, ...args] = argv._;
|
60 | const context = {name, args, flags: argv};
|
61 |
|
62 | argv.resolve(command.execute(context));
|
63 | } catch (error) {
|
64 | argv.reject(error);
|
65 | }
|
66 | };
|
67 | }
|
68 |
|
69 | async runHandlers({input, args}) {
|
70 | const showHelp = _.includes(input, '--help');
|
71 | const showVersion = _.includes(input, '--version');
|
72 | const positionalArgs = _.filter(input, arg => !arg.startsWith('-'));
|
73 |
|
74 |
|
75 | if (!showHelp && !showVersion && positionalArgs.length === 0) {
|
76 | return args.showHelp();
|
77 | }
|
78 |
|
79 | return new Promise((resolve, reject) => {
|
80 |
|
81 | const context = {resolve, reject};
|
82 |
|
83 | args
|
84 | .exitProcess(false)
|
85 | .fail((message, error) => reject(error))
|
86 | .parse(input, context, (error, argv, output) => {
|
87 | if (error) {
|
88 | return reject(error);
|
89 | }
|
90 |
|
91 | if (output) {
|
92 | console.log(output);
|
93 | }
|
94 |
|
95 | return resolve();
|
96 | });
|
97 | });
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 | async initializeNetworkMode() {
|
104 | ConfigService.online = await NetworkService.isInternetAvailable();
|
105 |
|
106 | if (!ConfigService.online) {
|
107 | LogService.info('Your computer seems to be offline.');
|
108 | LogService.notice('To use the CLI offline, you will need to ensure all the used packages are cached.');
|
109 | LogService.info('Proceeding with offline mode.');
|
110 | }
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 | async execute({input}) {
|
117 | const event = `CLI`;
|
118 | return AnalyticsService.time(event, {}, async () => {
|
119 |
|
120 | const verbose = _.includes(input, '--verbose');
|
121 | LogService.setVerbose(verbose);
|
122 |
|
123 |
|
124 | await this.initializeNetworkMode();
|
125 |
|
126 |
|
127 | if (ConfigService.isOnline) {
|
128 | await this.checkAndUpdate();
|
129 | }
|
130 |
|
131 |
|
132 | this.pluginManager.parseCommands();
|
133 |
|
134 |
|
135 | const args = this.parseArgs();
|
136 |
|
137 |
|
138 |
|
139 | await this.runHandlers({input, args});
|
140 | });
|
141 | }
|
142 |
|
143 | async checkAndUpdate() {
|
144 | const {autoupdate, isDevelopmentOrCI} = ConfigService;
|
145 |
|
146 | if (!autoupdate || isDevelopmentOrCI) {
|
147 |
|
148 | this.pluginManager.fetchPlugins();
|
149 | return;
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 | await UpdateService.updateAndRestartCli();
|
155 |
|
156 |
|
157 | await this.pluginManager.updateAndFetchPlugins();
|
158 | }
|
159 |
|
160 | }
|
161 |
|
162 | module.exports = CLI;
|