This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathauto-completion-service.ts
255 lines (224 loc) · 9.81 KB
/
auto-completion-service.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
///<reference path="../.d.ts"/>
"use strict";
import * as osenv from "osenv";
import * as path from "path";
import * as util from "util";
export class AutoCompletionService implements IAutoCompletionService {
private scriptsOk = true;
private scriptsUpdated = false;
private _completionShellScriptContent: string;
private _shellProfiles: string[];
private _cliRunCommandsFile: string;
private static COMPLETION_START_COMMENT_PATTERN = "###-%s-completion-start-###";
private static COMPLETION_END_COMMENT_PATTERN = "###-%s-completion-end-###";
private static TABTAB_COMPLETION_START_REGEX_PATTERN = "###-begin-%s-completion-###";
private static TABTAB_COMPLETION_END_REGEX_PATTERN = "###-end-%s-completion-###";
constructor(private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $logger: ILogger,
private $staticConfig: Config.IStaticConfig,
private $hostInfo: IHostInfo) { }
public disableAnalytics = true;
private get shellProfiles(): string[]{
if(!this._shellProfiles) {
this._shellProfiles = [];
this._shellProfiles.push(this.getHomePath(".bash_profile"));
this._shellProfiles.push(this.getHomePath(".bashrc"));
this._shellProfiles.push(this.getHomePath(".zshrc")); // zsh - http://www.acm.uiuc.edu/workshops/zsh/startup_files.html
}
return this._shellProfiles;
}
private get cliRunCommandsFile(): string {
if(!this._cliRunCommandsFile) {
this._cliRunCommandsFile = this.getHomePath(util.format(".%src", this.$staticConfig.CLIENT_NAME.toLowerCase()));
if(this.$hostInfo.isWindows) {
// on Windows bash, file is incorrectly written as C:\Users\<username>, which leads to errors when trying to execute the script:
// $ source ~/.bashrc
// sh.exe": C:Usersusername.appbuilderrc: No such file or directory
this._cliRunCommandsFile = this._cliRunCommandsFile.replace(/\\/g, "/");
}
}
return this._cliRunCommandsFile;
}
private getTabTabObsoleteRegex(clientName: string): RegExp {
let tabTabStartPoint = util.format(AutoCompletionService.TABTAB_COMPLETION_START_REGEX_PATTERN, clientName.toLowerCase());
let tabTabEndPoint = util.format(AutoCompletionService.TABTAB_COMPLETION_END_REGEX_PATTERN, clientName.toLowerCase());
let tabTabRegex = new RegExp(util.format("%s[\\s\\S]*%s", tabTabStartPoint, tabTabEndPoint));
return tabTabRegex;
}
public removeObsoleteAutoCompletion(): IFuture<void> {
return (() => {
// In previous releases we were writing directly in .bash_profile, .bashrc, .zshrc and .profile - remove this old code
let shellProfilesToBeCleared = this.shellProfiles;
// Add .profile only here as we do not want new autocompletion in this file, but we have to remove our old code from it.
shellProfilesToBeCleared.push(this.getHomePath(".profile"));
shellProfilesToBeCleared.forEach(file => {
try {
let text = this.$fs.readText(file).wait();
let newText = text.replace(this.getTabTabObsoleteRegex(this.$staticConfig.CLIENT_NAME), "");
if(this.$staticConfig.CLIENT_NAME_ALIAS) {
newText = newText.replace(this.getTabTabObsoleteRegex(this.$staticConfig.CLIENT_NAME_ALIAS), "");
}
if(newText !== text) {
this.$logger.trace("Remove obsolete AutoCompletion from file %s.", file);
this.$fs.writeFile(file, newText).wait();
}
} catch(error) {
if(error.code !== "ENOENT") {
this.$logger.trace("Error while trying to disable autocompletion for '%s' file. Error is:\n%s", error.toString());
}
}
});
}).future<void>()();
}
private get completionShellScriptContent() {
if(!this._completionShellScriptContent) {
let startText = util.format(AutoCompletionService.COMPLETION_START_COMMENT_PATTERN, this.$staticConfig.CLIENT_NAME.toLowerCase());
let content = util.format("if [ -f %s ]; then \n source %s \nfi", this.cliRunCommandsFile, this.cliRunCommandsFile);
let endText = util.format(AutoCompletionService.COMPLETION_END_COMMENT_PATTERN, this.$staticConfig.CLIENT_NAME.toLowerCase());
this._completionShellScriptContent = util.format("\n%s\n%s\n%s\n", startText, content, endText);
}
return this._completionShellScriptContent;
}
public isAutoCompletionEnabled(): IFuture<boolean> {
return ((): boolean => {
let result = true;
_.each(this.shellProfiles, filePath => {
result = this.isNewAutoCompletionEnabledInFile(filePath).wait() || this.isObsoleteAutoCompletionEnabledInFile(filePath).wait();
if(!result) {
// break each
return false;
}
});
return result;
}).future<boolean>()();
}
public disableAutoCompletion(): IFuture<void> {
return (() => {
_.each(this.shellProfiles, shellFile => this.removeAutoCompletionFromShellScript(shellFile).wait());
this.removeObsoleteAutoCompletion().wait();
if(this.scriptsOk && this.scriptsUpdated) {
this.$logger.out("Restart your shell to disable command auto-completion.");
}
}).future<void>()();
}
public enableAutoCompletion(): IFuture<void> {
return (() => {
this.updateCLIShellScript().wait();
_.each(this.shellProfiles, shellFile => this.addAutoCompletionToShellScript(shellFile).wait());
this.removeObsoleteAutoCompletion().wait();
if(this.scriptsOk && this.scriptsUpdated) {
this.$logger.out("Restart your shell to enable command auto-completion.");
}
}).future<void>()();
}
public isObsoleteAutoCompletionEnabled(): IFuture<boolean> {
return (() => {
let result = true;
_.each(this.shellProfiles, shellProfile => {
result = this.isObsoleteAutoCompletionEnabledInFile(shellProfile).wait();
if(!result) {
// break each
return false;
}
});
return result;
}).future<boolean>()();
}
private isNewAutoCompletionEnabledInFile(fileName: string): IFuture<boolean> {
return ((): boolean => {
try {
let data = this.$fs.readText(fileName).wait();
if(data && data.indexOf(this.completionShellScriptContent) !== -1) {
return true;
}
} catch(err) {
this.$logger.trace("Error while checking is autocompletion enabled in file %s. Error is: '%s'", fileName, err.toString());
}
return false;
}).future<boolean>()();
}
private isObsoleteAutoCompletionEnabledInFile(fileName: string): IFuture<boolean> {
return (() => {
try {
let text = this.$fs.readText(fileName).wait();
return text.match(this.getTabTabObsoleteRegex(this.$staticConfig.CLIENT_NAME)) || text.match(this.getTabTabObsoleteRegex(this.$staticConfig.CLIENT_NAME));
} catch(err) {
this.$logger.trace("Error while checking is obsolete autocompletion enabled in file %s. Error is: '%s'", fileName, err.toString());
}
}).future<boolean>()();
}
private addAutoCompletionToShellScript(fileName: string): IFuture<void> {
return (() => {
try {
if(!this.isNewAutoCompletionEnabledInFile(fileName).wait() || this.isObsoleteAutoCompletionEnabledInFile(fileName).wait()) {
this.$logger.trace("AutoCompletion is not enabled in %s file. Trying to enable it.", fileName);
this.$fs.appendFile(fileName, this.completionShellScriptContent).wait();
this.scriptsUpdated = true;
}
} catch(err) {
this.$logger.out("Unable to update %s. Command-line completion might not work.", fileName);
// When npm is installed with sudo, in some cases the installation cannot write to shell profiles
// Advise the user how to enable autocompletion after the installation is completed.
if((err.code === "EPERM" || err.code === "EACCES") && !this.$hostInfo.isWindows && process.env.SUDO_USER) {
this.$logger.out("To enable command-line completion, run '$ %s autocomplete enable'.", this.$staticConfig.CLIENT_NAME);
}
this.$logger.trace(err);
this.scriptsOk = false;
}
}).future<void>()();
}
private removeAutoCompletionFromShellScript(fileName: string): IFuture<void> {
return (() => {
try {
if(this.isNewAutoCompletionEnabledInFile(fileName).wait()) {
this.$logger.trace("AutoCompletion is enabled in %s file. Trying to disable it.", fileName);
let data = this.$fs.readText(fileName).wait();
data = data.replace(this.completionShellScriptContent, "");
this.$fs.writeFile(fileName, data).wait();
this.scriptsUpdated = true;
}
} catch(err) {
// If file does not exist, autocompletion was not working for it, so ignore this error.
if(err.code !== "ENOENT") {
this.$logger.out("Failed to update %s. Auto-completion may still work or work incorrectly. ", fileName);
this.$logger.out(err);
this.scriptsOk = false;
}
}
}).future<void>()();
}
private updateCLIShellScript(): IFuture<void> {
return (() => {
let filePath = this.cliRunCommandsFile;
try {
let doUpdate = true;
if (this.$fs.exists(filePath).wait()) {
let contents = this.$fs.readText(filePath).wait();
let regExp = new RegExp(util.format("%s\\s+completion\\s+--\\s+", this.$staticConfig.CLIENT_NAME.toLowerCase()));
let matchCondition = contents.match(regExp);
if(this.$staticConfig.CLIENT_NAME_ALIAS) {
matchCondition = matchCondition || contents.match(new RegExp(util.format("%s\\s+completion\\s+--\\s+", this.$staticConfig.CLIENT_NAME_ALIAS.toLowerCase())));
}
if (matchCondition) {
doUpdate = false;
}
}
if(doUpdate) {
let clientExecutableFileName = (this.$staticConfig.CLIENT_NAME_ALIAS || this.$staticConfig.CLIENT_NAME).toLowerCase();
let pathToExecutableFile = path.join(__dirname, `../../../bin/${clientExecutableFileName}.js`);
this.$childProcess.exec(`"${process.argv[0]}" "${pathToExecutableFile}" completion >> "${filePath}"`).wait();
this.$fs.chmod(filePath, "0644").wait();
}
} catch(err) {
this.$logger.out("Failed to update %s. Auto-completion may not work. ", filePath);
this.$logger.trace(err);
this.scriptsOk = false;
}
}).future<void>()();
}
private getHomePath(fileName: string): string {
return path.join(osenv.home(), fileName);
}
}
$injector.register("autoCompletionService", AutoCompletionService);