forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatchers.ts
162 lines (142 loc) · 4.44 KB
/
dispatchers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import * as _ from "lodash";
import * as queue from "./queue";
import * as path from "path";
import { hook } from "./helpers";
import {
ICommandDispatcher,
ICancellationService,
ISysInfo,
IFutureDispatcher,
IQueue,
IErrors,
} from "./declarations";
import { IOptions, IPackageManager, IVersionsService } from "../declarations";
import { IInjector } from "./definitions/yok";
import { injector } from "./yok";
import { PackageManagers } from "../constants";
export class CommandDispatcher implements ICommandDispatcher {
constructor(
private $logger: ILogger,
// required by the hooksService
protected $injector: IInjector,
private $cancellation: ICancellationService,
private $commandsService: ICommandsService,
private $staticConfig: Config.IStaticConfig,
private $sysInfo: ISysInfo,
private $options: IOptions,
private $versionsService: IVersionsService,
private $packageManager: IPackageManager,
private $terminalSpinnerService: ITerminalSpinnerService
) {}
public async dispatchCommand(): Promise<void> {
if (this.$options.version) {
return this.printVersion();
}
if (this.$logger.getLevel() === "TRACE") {
// CommandDispatcher is called from external CLI's only, so pass the path to their package.json
const sysInfo = await this.$sysInfo.getSysInfo({
pathToNativeScriptCliPackageJson: path.join(
__dirname,
"..",
"..",
"package.json"
),
});
this.$logger.trace("System information:");
this.$logger.trace(JSON.stringify(sysInfo, null, 2));
this.$logger.trace("Current CLI version: ", this.$staticConfig.version);
}
let commandName = this.getCommandName();
let commandArguments = this.$options.argv._.slice(1);
const lastArgument: string = _.last(commandArguments);
if (this.$options.help) {
commandArguments.unshift(commandName);
commandName = "help";
} else if (lastArgument === "/?" || lastArgument === "?") {
commandArguments.pop();
commandArguments.unshift(commandName);
commandName = "help";
}
({
commandName,
commandArguments,
argv: process.argv,
} = await this.resolveCommand(commandName, commandArguments, process.argv));
await this.$cancellation.begin("cli");
await this.$commandsService.tryExecuteCommand(
commandName,
commandArguments
);
}
public async completeCommand(): Promise<boolean> {
return this.$commandsService.completeCommand();
}
@hook("resolveCommand")
private async resolveCommand(
commandName: string,
commandArguments: string[],
argv: string[]
) {
// just a hook point
return { commandName, commandArguments, argv };
}
private getCommandName(): string {
const remaining: string[] = this.$options.argv._;
if (remaining.length > 0) {
return remaining[0].toString().toLowerCase();
}
// if only <CLI_NAME> is specified on console, show console help
this.$options.help = true;
return "";
}
private async printVersion(): Promise<void> {
this.$logger.info(this.$staticConfig.version);
const spinner = this.$terminalSpinnerService.createSpinner();
spinner.start("Checking for updates...");
const nativescriptCliVersion = await this.$versionsService.getNativescriptCliVersion();
spinner.stop();
const packageManagerName = await this.$packageManager.getPackageManagerName();
let updateCommand = "";
switch (packageManagerName) {
case PackageManagers.npm:
updateCommand = "npm i -g nativescript";
break;
case PackageManagers.yarn:
updateCommand = "yarn global add nativescript";
break;
case PackageManagers.pnpm:
updateCommand = "pnpm i -g nativescript";
break;
}
if (
nativescriptCliVersion.currentVersion ===
nativescriptCliVersion.latestVersion
) {
// up-to-date
spinner.succeed("Up to date.");
} else {
spinner.info(
`New version of NativeScript CLI is available (${nativescriptCliVersion.latestVersion}), run '${updateCommand}' to update.`
);
}
}
}
injector.register("commandDispatcher", CommandDispatcher);
class FutureDispatcher implements IFutureDispatcher {
private actions: IQueue<any>;
public constructor(private $errors: IErrors) {}
public async run(): Promise<void> {
if (this.actions) {
this.$errors.fail("You cannot run a running future dispatcher.");
}
this.actions = new queue.Queue<any>();
while (true) {
const action = await this.actions.dequeue();
await action();
}
}
public dispatch(action: () => Promise<void>) {
this.actions.enqueue(action);
}
}
injector.register("dispatcher", FutureDispatcher, false);