-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathNsCliService.ts
373 lines (307 loc) · 13.5 KB
/
NsCliService.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import {spawn, execSync, ChildProcess} from 'child_process';
import * as fs from 'fs';
import {EventEmitter} from 'events';
import * as path from 'path';
import * as https from 'https';
import {Version} from '../common/Version';
import {Logger} from '../debug-adapter/utilities';
import {ILaunchRequestArgs, IAttachRequestArgs} from '../debug-adapter/WebKitAdapterInterfaces';
import {ExtensionVersionInfo} from './ExtensionVersionInfo';
export enum CliVersionState {
NotExisting,
OlderThanSupported,
Compatible
}
export class CliVersionInfo {
private static installedCliVersion: number[] = null;
private _state: CliVersionState;
public static getInstalledCliVersion(): number[] {
if (this.installedCliVersion === null) {
// get the currently installed CLI version
let getVersionCommand: string = new CommandBuilder().appendParam('--version').buildAsString(); // tns --version
try {
let versionStr: string = execSync(getVersionCommand).toString().trim(); // execute it
this.installedCliVersion = versionStr ? Version.parse(versionStr) : null; // parse the version string
} catch(e) {
this.installedCliVersion = null;
}
}
return this.installedCliVersion;
}
constructor() {
let installedCliVersion: number[] = CliVersionInfo.getInstalledCliVersion();
if (installedCliVersion === null) {
this._state = CliVersionState.NotExisting;
}
else {
let minSupportedCliVersion = ExtensionVersionInfo.getMinSupportedNativeScriptVersion();
this._state = Version.compareBySubminor(installedCliVersion, minSupportedCliVersion) < 0 ? CliVersionState.OlderThanSupported : CliVersionState.Compatible;
}
}
public getState(): CliVersionState {
return this._state;
}
public isCompatible(): boolean {
return this._state === CliVersionState.Compatible;
}
public getErrorMessage(): string {
switch (this._state) {
case CliVersionState.NotExisting:
return `NativeScript CLI not found, please run 'npm -g install nativescript' to install it.`;
case CliVersionState.OlderThanSupported:
return `The existing NativeScript extension is compatible with NativeScript CLI v${Version.stringify(ExtensionVersionInfo.getMinSupportedNativeScriptVersion())} or greater. The currently installed NativeScript CLI is v${Version.stringify(CliVersionInfo.getInstalledCliVersion())}. You can update the NativeScript CLI by executing 'npm install -g nativescript'.`;
default:
return null;
}
}
}
export abstract class NSProject extends EventEmitter {
private _projectPath: string;
private _tnsOutputFileStream: fs.WriteStream;
private _cliVersionInfo: CliVersionInfo;
constructor(projectPath: string, tnsOutputFilePath?: string) {
super();
this._projectPath = projectPath;
this._tnsOutputFileStream = tnsOutputFilePath ? fs.createWriteStream(tnsOutputFilePath) : null;
this._cliVersionInfo = new CliVersionInfo();
}
public getProjectPath(): string {
return this._projectPath;
}
public getCliVersionInfo() {
return this._cliVersionInfo;
}
public abstract platform(): string;
public abstract run(): Promise<ChildProcess>;
public abstract debug(args: IAttachRequestArgs | ILaunchRequestArgs): Promise<any>;
protected spawnProcess(commandPath: string, commandArgs: string[], tnsOutput?: string): ChildProcess {
let options = { cwd: this.getProjectPath(), shell: true };
let child: ChildProcess = spawn(commandPath, commandArgs, options);
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
return child;
}
protected writeToTnsOutputFile(message: string) {
if (this._tnsOutputFileStream) {
this._tnsOutputFileStream.write(message, 'utf8');
}
}
}
export class IosProject extends NSProject {
constructor(projectPath: string, tnsOutputFilePath?: string) {
super(projectPath, tnsOutputFilePath);
}
public platform(): string {
return 'ios';
}
public run(): Promise<ChildProcess> {
if (!this.isOSX()) {
return Promise.reject('iOS platform is only supported on OS X.');
}
// build command to execute
let command = new CommandBuilder()
.appendParam("run")
.appendParam(this.platform())
.build();
let child: ChildProcess = this.spawnProcess(command.path, command.args);
return Promise.resolve(child);
}
public debug(args: IAttachRequestArgs | ILaunchRequestArgs): Promise<string> {
if (!this.isOSX()) {
return Promise.reject('iOS platform is supported only on OS X.');
}
let rebuild = (args.request == "launch") ? (args as ILaunchRequestArgs).rebuild : true;
// build command to execute
let command = new CommandBuilder(args.nativescriptCliPath)
.appendParam("debug")
.appendParam(this.platform())
.appendParamIf("--emulator", args.emulator)
.appendParamIf("--start", args.request === "attach")
.appendParamIf("--debug-brk", args.request === "launch")
.appendParamIf("--no-rebuild", !rebuild)
.appendParamIf("--syncAllFiles", args.request === "launch" && !rebuild && (args as ILaunchRequestArgs).syncAllFiles)
.appendParam("--no-client")
.appendParams(args.tnsArgs)
.build();
let socketPathPrefix = 'socket-file-location: ';
let socketPathPattern: RegExp = new RegExp(socketPathPrefix + '.*\.sock');
let isSocketOpened = (cliOutput: string): string => {
let matches: RegExpMatchArray = cliOutput.match(socketPathPattern);
if(matches && matches.length > 0) {
return matches[0].substr(socketPathPrefix.length);
}
return null;
};
let isAppSynced = (cliOutput: string) => {
return cliOutput.indexOf('Successfully synced application') > -1;
};
return new Promise<string>((resolve, reject) => {
// run NativeScript CLI command
let child: ChildProcess = this.spawnProcess(command.path, command.args, args.tnsOutput);
let appSynced = false;
let socketPath: string = null;
child.stdout.on('data', (data) => {
let cliOutput: string = data.toString();
this.emit('TNS.outputMessage', cliOutput, 'log');
this.writeToTnsOutputFile(cliOutput);
socketPath = socketPath || isSocketOpened(cliOutput);
appSynced = rebuild ? false : (appSynced || isAppSynced(cliOutput));
if ((rebuild && socketPath) || (!rebuild && socketPath && appSynced)) {
resolve(socketPath);
}
});
child.stderr.on('data', (data) => {
this.emit('TNS.outputMessage', data, 'error');
this.writeToTnsOutputFile(data.toString());
});
child.on('close', (code, signal) => {
reject("The debug process exited unexpectedly code:" + code);
});
});
}
private isOSX(): boolean {
return /^darwin/.test(process.platform);
}
}
export class AndroidProject extends NSProject {
constructor(projectPath: string, tnsOutputFilePath?: string) {
super(projectPath, tnsOutputFilePath);
}
public platform(): string {
return 'android';
}
public run(): Promise<ChildProcess> {
// build command to execute
let command = new CommandBuilder()
.appendParam("run")
.appendParam(this.platform())
.build();
let child: ChildProcess = this.spawnProcess(command.path, command.args);
return Promise.resolve(child);
}
public debug(params: IAttachRequestArgs | ILaunchRequestArgs): Promise<void> {
if (params.request === "attach") {
return Promise.resolve<void>();
}
else if (params.request === "launch") {
let args: ILaunchRequestArgs = params as ILaunchRequestArgs;
let that = this;
let launched = false;
return new Promise<void>((resolve, reject) => {
let command = new CommandBuilder(args.nativescriptCliPath)
.appendParam("debug")
.appendParam(this.platform())
.appendParamIf("--emulator", args.emulator)
.appendParamIf("--no-rebuild", args.rebuild !== true)
.appendParam("--debug-brk")
.appendParam("--no-client")
.appendParams(args.tnsArgs)
.build();
Logger.log("tns debug command: " + command);
// run NativeScript CLI command
let child: ChildProcess = this.spawnProcess(command.path, command.args, args.tnsOutput);
child.stdout.on('data', function(data) {
let strData: string = data.toString();
that.emit('TNS.outputMessage', data.toString(), 'log');
that.writeToTnsOutputFile(strData);
if (!launched) {
if (args.request === "launch" && strData.indexOf('# NativeScript Debugger started #') > -1) {
launched = true;
//wait a little before trying to connect, this gives a changes for adb to be able to connect to the debug socket
setTimeout(() => {
resolve();
}, 500);
}
}
});
child.stderr.on('data', function(data) {
that.emit('TNS.outputMessage', data.toString(), 'error');
that.writeToTnsOutputFile(data.toString());
});
child.on('close', function(code) {
if (!args.rebuild) {
setTimeout(() => {
reject("The debug process exited unexpectedly code:" + code);
}, 3000);
}
else {
reject("The debug process exited unexpectedly code:" + code);
}
});
});
}
}
public getDebugPort(args: IAttachRequestArgs | ILaunchRequestArgs): Promise<number> {
//TODO: Call CLI to get the debug port
//return Promise.resolve(40001);
//return Promise.resolve(40001);
let command = new CommandBuilder(args.nativescriptCliPath)
.appendParam("debug")
.appendParam(this.platform())
.appendParam("--get-port")
.appendParams(args.tnsArgs)
.build();
let that = this;
// run NativeScript CLI command
return new Promise<number>((resolve, reject) => {
let child: ChildProcess = this.spawnProcess(command.path, command.args, args.tnsOutput);
child.stdout.on('data', function(data) {
that.emit('TNS.outputMessage', data.toString(), 'log');
that.writeToTnsOutputFile(data.toString());
let regexp = new RegExp("(?:debug port: )([\\d]{5})");
//for the new output
// var input = "device: 030b258308e6ce89 debug port: 40001";
let portNumberMatch = null;
let match = data.toString().match(regexp);
if (match)
{
portNumberMatch = match[1];
}
if (portNumberMatch) {
Logger.log("port number match '" + portNumberMatch + "'");
let portNumber = parseInt(portNumberMatch);
if (portNumber) {
Logger.log("port number " + portNumber);
child.stdout.removeAllListeners('data');
resolve(portNumber);
}
}
});
child.stderr.on('data', function(data) {
that.emit('TNS.outputMessage', data.toString(), 'error');
that.writeToTnsOutputFile(data.toString());
});
child.on('close', function(code) {
reject("Getting debug port failed with code: " + code);
});
});
}
}
class CommandBuilder {
private _tnsPath: string;
private _command: string[] = [];
constructor(tnsPath?: string) {
this._tnsPath = tnsPath || "tns";
}
public appendParam(parameter: string): CommandBuilder {
this._command.push(parameter);
return this;
}
public appendParams(parameters: string[] = []): CommandBuilder {
parameters.forEach(param => this.appendParam(param));
return this;
}
public appendParamIf(parameter: string, condtion: boolean): CommandBuilder {
if (condtion) {
this._command.push(parameter);
}
return this;
}
public build(): { path: string, args: string[] } {
return { path: this._tnsPath, args: this._command };
}
public buildAsString(): string {
let result = this.build();
return `${result.path} ` + result.args.join(' ');
}
}