Skip to content

Commit 2135c65

Browse files
committed
fix: fix pr comments
1 parent d12300e commit 2135c65

17 files changed

+56
-137
lines changed

lib/common/bootstrap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ $injector.require("qr", "./services/qr");
122122
$injector.require("printPluginsService", "./services/plugins/print-plugins-service");
123123
$injector.require("npmPluginsService", "./services/plugins/npm-plugins-service");
124124

125-
$injector.require("lockfile", "./services/lockfile");
125+
$injector.require(["lockfile", "lockService"], "./services/lock-service");

lib/common/mobile/ios/device/ios-device.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class IOSDevice extends IOSDeviceBase {
1717
protected $errors: IErrors,
1818
private $injector: IInjector,
1919
protected $iOSDebuggerPortService: IIOSDebuggerPortService,
20-
protected $lockfile: ILockFile,
20+
protected $lockService: ILockService,
2121
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
2222
protected $processService: IProcessService,
2323
private $deviceLogProvider: Mobile.IDeviceLogProvider,

lib/common/mobile/ios/ios-device-base.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export abstract class IOSDeviceBase implements Mobile.IiOSDevice {
55
protected abstract $errors: IErrors;
66
protected abstract $iOSDebuggerPortService: IIOSDebuggerPortService;
77
protected abstract $processService: IProcessService;
8-
protected abstract $lockfile: ILockFile;
8+
protected abstract $lockService: ILockService;
99
abstract deviceInfo: Mobile.IDeviceInfo;
1010
abstract applicationManager: Mobile.IDeviceApplicationManager;
1111
abstract fileSystem: Mobile.IDeviceFileSystem;
@@ -25,7 +25,7 @@ export abstract class IOSDeviceBase implements Mobile.IiOSDevice {
2525
}
2626

2727
public async getSocket(appId: string): Promise<net.Socket> {
28-
return this.$lockfile.executeActionWithLock(async () => {
28+
return this.$lockService.executeActionWithLock(async () => {
2929
if (this.cachedSockets[appId]) {
3030
return this.cachedSockets[appId];
3131
}

lib/common/mobile/ios/simulator/ios-simulator-device.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class IOSSimulator extends IOSDeviceBase implements Mobile.IiOSDevice {
1414
constructor(private simulator: Mobile.IiSimDevice,
1515
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
1616
protected $errors: IErrors,
17-
protected $lockfile: ILockFile,
17+
protected $lockService: ILockService,
1818
private $injector: IInjector,
1919
protected $iOSDebuggerPortService: IIOSDebuggerPortService,
2020
private $iOSSimResolver: Mobile.IiOSSimResolver,

lib/common/services/lockfile.ts renamed to lib/common/services/lock-service.ts

+21-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as lockfile from "lockfile";
22
import * as path from "path";
33
import { cache } from "../decorators";
44

5-
export class LockFile implements ILockFile {
5+
export class LockService implements ILockService {
66
private currentlyLockedFiles: string[] = [];
77

88
@cache()
@@ -14,10 +14,10 @@ export class LockFile implements ILockFile {
1414
return path.join(this.$settingsService.getProfileDir(), relativeLockFilePath);
1515
}
1616

17-
private get defaultLockParams(): ILockFileOptions {
17+
private get defaultLockParams(): ILockOptions {
1818
// We'll retry 100 times and time between retries is 100ms, i.e. full wait in case we are unable to get lock will be 10 seconds.
1919
// In case lock is older than the `stale` value, consider it stale and try to get a new lock.
20-
const lockParams: ILockFileOptions = {
20+
const lockParams: ILockOptions = {
2121
retryWait: 100,
2222
retries: 100,
2323
stale: 30 * 1000,
@@ -37,8 +37,19 @@ export class LockFile implements ILockFile {
3737
});
3838
}
3939

40-
public lock(lockFilePath?: string, lockFileOpts?: ILockFileOptions): Promise<string> {
41-
const { filePath, fileOpts } = this.getLockFileSettings(lockFilePath, lockFileOpts);
40+
public async executeActionWithLock<T>(action: () => Promise<T>, lockFilePath?: string, lockOpts?: ILockOptions): Promise<T> {
41+
const resolvedLockFilePath = await this.lock(lockFilePath, lockOpts);
42+
43+
try {
44+
const result = await action();
45+
return result;
46+
} finally {
47+
this.unlock(resolvedLockFilePath);
48+
}
49+
}
50+
51+
private lock(lockFilePath?: string, lockOpts?: ILockOptions): Promise<string> {
52+
const { filePath, fileOpts } = this.getLockFileSettings(lockFilePath, lockOpts);
4253
this.currentlyLockedFiles.push(filePath);
4354

4455
// Prevent ENOENT error when the dir, where lock should be created, does not exist.
@@ -51,30 +62,13 @@ export class LockFile implements ILockFile {
5162
});
5263
}
5364

54-
public async executeActionWithLock<T>(action: () => Promise<T>, lockFilePath?: string, lockFileOpts?: ILockFileOptions): Promise<T> {
55-
const resolvedLockFilePath = await this.lock(lockFilePath, lockFileOpts);
56-
57-
try {
58-
const result = await action();
59-
return result;
60-
} finally {
61-
this.unlock(resolvedLockFilePath);
62-
}
63-
}
64-
65-
public unlock(lockFilePath?: string): void {
65+
private unlock(lockFilePath?: string): void {
6666
const { filePath } = this.getLockFileSettings(lockFilePath);
6767
_.remove(this.currentlyLockedFiles, e => e === lockFilePath);
6868
lockfile.unlockSync(filePath);
6969
}
7070

71-
public check(lockFilePath?: string, lockFileOpts?: ILockFileOptions): boolean {
72-
const { filePath, fileOpts } = this.getLockFileSettings(lockFilePath, lockFileOpts);
73-
74-
return lockfile.checkSync(filePath, fileOpts);
75-
}
76-
77-
private getLockFileSettings(filePath?: string, fileOpts?: ILockFileOptions): { filePath: string, fileOpts: ILockFileOptions } {
71+
private getLockFileSettings(filePath?: string, fileOpts?: ILockOptions): { filePath: string, fileOpts: ILockOptions } {
7872
if (filePath && !path.isAbsolute(filePath)) {
7973
filePath = this.getAbsoluteLockFilePath(filePath);
8074
}
@@ -89,4 +83,6 @@ export class LockFile implements ILockFile {
8983
}
9084
}
9185

92-
$injector.register("lockfile", LockFile);
86+
$injector.register("lockService", LockService);
87+
// backwards compatibility
88+
$injector.register("lockfile", LockService);

lib/common/services/user-settings-service.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class UserSettingsServiceBase implements IUserSettingsService {
1010

1111
constructor(userSettingsFilePath: string,
1212
protected $fs: IFileSystem,
13-
protected $lockfile: ILockFile,
13+
protected $lockService: ILockService,
1414
private $logger: ILogger) {
1515
this.userSettingsFilePath = userSettingsFilePath;
1616
}
@@ -21,7 +21,7 @@ export class UserSettingsServiceBase implements IUserSettingsService {
2121
return this.userSettingsData ? this.userSettingsData[settingName] : null;
2222
};
2323

24-
return this.$lockfile.executeActionWithLock<T>(action, this.lockFilePath);
24+
return this.$lockService.executeActionWithLock<T>(action, this.lockFilePath);
2525
}
2626

2727
public async saveSetting<T>(key: string, value: T): Promise<void> {
@@ -39,7 +39,7 @@ export class UserSettingsServiceBase implements IUserSettingsService {
3939
await this.saveSettings();
4040
};
4141

42-
return this.$lockfile.executeActionWithLock<void>(action, this.lockFilePath);
42+
return this.$lockService.executeActionWithLock<void>(action, this.lockFilePath);
4343
}
4444

4545
public saveSettings(data?: any): Promise<void> {
@@ -56,7 +56,7 @@ export class UserSettingsServiceBase implements IUserSettingsService {
5656
this.$fs.writeJson(this.userSettingsFilePath, this.userSettingsData);
5757
};
5858

59-
return this.$lockfile.executeActionWithLock<void>(action, this.lockFilePath);
59+
return this.$lockService.executeActionWithLock<void>(action, this.lockFilePath);
6060
}
6161

6262
// TODO: Remove Promise, reason: writeFile - blocked as other implementation of the interface has async operation.

lib/common/test/unit-tests/mobile/ios-simulator-discovery.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Yok } from "../../../yok";
44
import { assert } from "chai";
55
import { DeviceDiscoveryEventNames, CONNECTED_STATUS } from "../../../constants";
66
import { DevicePlatformsConstants } from "../../../mobile/device-platforms-constants";
7-
import { ErrorsStub, CommonLoggerStub, HooksServiceStub, LockFileStub } from "../stubs";
7+
import { ErrorsStub, CommonLoggerStub, HooksServiceStub, LockServiceStub } from "../stubs";
88
import { FileSystemStub } from "../../../../../test/stubs";
99

1010
let currentlyRunningSimulators: Mobile.IiSimDevice[];
@@ -16,7 +16,7 @@ function createTestInjector(): IInjector {
1616
injector.register("injector", injector);
1717
injector.register("errors", ErrorsStub);
1818
injector.register("iOSDebuggerPortService", {});
19-
injector.register("lockfile", LockFileStub);
19+
injector.register("lockService", LockServiceStub);
2020
injector.register("iOSSimResolver", {
2121
iOSSim: {
2222
getRunningSimulators: async () => currentlyRunningSimulators

lib/common/test/unit-tests/stubs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import * as util from "util";
44
import { EventEmitter } from "events";
55

6-
export class LockFileStub implements ILockFile {
7-
public async executeActionWithLock<T>(action: () => Promise<T>, lockFilePath?: string, lockFileOpts?: ILockFileOptions): Promise<T> {
6+
export class LockServiceStub implements ILockService {
7+
public async executeActionWithLock<T>(action: () => Promise<T>, lockFilePath?: string, lockOpts?: ILockOptions): Promise<T> {
88
const result = await action();
99
return result;
1010
}

lib/declarations.d.ts

-6
Original file line numberDiff line numberDiff line change
@@ -745,17 +745,11 @@ interface IAppDebugSocketProxyFactory extends NodeJS.EventEmitter {
745745
}
746746

747747
interface IiOSNotification extends NodeJS.EventEmitter {
748-
getWaitForDebug(projectId: string): string;
749748
getAttachRequest(projectId: string, deviceId: string): string;
750-
getAppLaunching(projectId: string): string;
751749
getReadyForAttach(projectId: string): string;
752-
getAttachAvailabilityQuery(projectId: string): string;
753-
getAlreadyConnected(projectId: string): string;
754-
getAttachAvailable(projectId: string): string;
755750
}
756751

757752
interface IiOSSocketRequestExecutor {
758-
executeLaunchRequest(device: Mobile.IiOSDevice, timeout: number, readyForAttachTimeout: number, projectId: string, debugOptions: IDebugOptions): Promise<void>;
759753
executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise<void>;
760754
}
761755

lib/definitions/debug.d.ts

-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ interface IDebugOptions {
9696
* The sdk version of the emulator.
9797
*/
9898
sdk?: string;
99-
/**
100-
* Defines if the handshake(AppLaunching notification) between CLI and runtime should be executed. The handshake is not needed when CLI retries to attach to the debugger.
101-
*/
102-
skipHandshake?: boolean;
10399
/**
104100
* Forces the debugger attach event to be emitted.
105101
*/

lib/definitions/lock-service.d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as lockfile from "lockfile";
2+
3+
declare global {
4+
interface ILockOptions extends lockfile.Options { }
5+
6+
/**
7+
* Describes methods that can be used to use file locking.
8+
*/
9+
interface ILockService {
10+
/**
11+
* @param action The code to be locked.
12+
* @param {string} lockFilePath Path to lock file that has to be created. Defaults to `<profile dir>/lockfile.lock`
13+
* @param {ILockOptions} lockOpts Options used for creating the lock file.
14+
* @returns {Promise<T>}
15+
*/
16+
executeActionWithLock<T>(action: () => Promise<T>, lockFilePath?: string, lockOpts?: ILockOptions): Promise<T>
17+
// TODO: expose as decorator
18+
}
19+
}

lib/definitions/lockfile.d.ts

-19
This file was deleted.

lib/device-sockets/ios/notification.ts

-25
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@ import { EventEmitter } from "events";
22
import { ATTACH_REQUEST_EVENT_NAME } from "../../common/constants";
33

44
export class IOSNotification extends EventEmitter implements IiOSNotification {
5-
private static WAIT_FOR_DEBUG_NOTIFICATION_NAME = "WaitForDebugger";
65
private static ATTACH_REQUEST_NOTIFICATION_NAME = "AttachRequest";
7-
private static APP_LAUNCHING_NOTIFICATION_NAME = "AppLaunching";
86
private static READY_FOR_ATTACH_NOTIFICATION_NAME = "ReadyForAttach";
9-
private static ATTACH_AVAILABILITY_QUERY_NOTIFICATION_NAME = "AttachAvailabilityQuery";
10-
private static ALREADY_CONNECTED_NOTIFICATION_NAME = "AlreadyConnected";
11-
private static ATTACH_AVAILABLE_NOTIFICATION_NAME = "AttachAvailable";
12-
13-
public getWaitForDebug(projectId: string) {
14-
return this.formatNotification(IOSNotification.WAIT_FOR_DEBUG_NOTIFICATION_NAME, projectId);
15-
}
167

178
public getAttachRequest(appId: string, deviceId: string): string {
189
// It could be too early to emit this event, but we rely that if you construct attach request,
@@ -21,26 +12,10 @@ export class IOSNotification extends EventEmitter implements IiOSNotification {
2112
return this.formatNotification(IOSNotification.ATTACH_REQUEST_NOTIFICATION_NAME, appId);
2213
}
2314

24-
public getAppLaunching(projectId: string): string {
25-
return this.formatNotification(IOSNotification.APP_LAUNCHING_NOTIFICATION_NAME, projectId);
26-
}
27-
2815
public getReadyForAttach(projectId: string): string {
2916
return this.formatNotification(IOSNotification.READY_FOR_ATTACH_NOTIFICATION_NAME, projectId);
3017
}
3118

32-
public getAttachAvailabilityQuery(projectId: string) {
33-
return this.formatNotification(IOSNotification.ATTACH_AVAILABILITY_QUERY_NOTIFICATION_NAME, projectId);
34-
}
35-
36-
public getAlreadyConnected(projectId: string) {
37-
return this.formatNotification(IOSNotification.ALREADY_CONNECTED_NOTIFICATION_NAME, projectId);
38-
}
39-
40-
public getAttachAvailable(projectId: string) {
41-
return this.formatNotification(IOSNotification.ATTACH_AVAILABLE_NOTIFICATION_NAME, projectId);
42-
}
43-
4419
private formatNotification(notification: string, projectId: string) {
4520
return `${projectId}:NativeScript.Debug.${notification}`;
4621
}

lib/device-sockets/ios/socket-request-executor.ts

+1-29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as constants from "../../common/constants";
33
export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
44
constructor(private $errors: IErrors,
55
private $iOSNotification: IiOSNotification,
6-
private $iOSNotificationService: IiOSNotificationService,
7-
private $logger: ILogger) { }
6+
private $iOSNotificationService: IiOSNotificationService) { }
87

98
public async executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise<void> {
109
const deviceIdentifier = device.deviceInfo.identifier;
@@ -19,33 +18,6 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
1918
this.$errors.failWithoutHelp(`The application ${projectId} does not appear to be running on ${deviceIdentifier} or is not built with debugging enabled. Try starting the application manually.`);
2019
}
2120
}
22-
23-
public async executeLaunchRequest(device: Mobile.IiOSDevice, timeout: number, readyForAttachTimeout: number, projectId: string, debugOptions: IDebugOptions): Promise<void> {
24-
const deviceIdentifier = device.deviceInfo.identifier;
25-
26-
try {
27-
if (!debugOptions.skipHandshake) {
28-
await this.executeHandshake(deviceIdentifier, projectId, timeout);
29-
}
30-
31-
if (debugOptions.debugBrk) {
32-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getWaitForDebug(projectId));
33-
}
34-
35-
await this.executeAttachRequest(device, readyForAttachTimeout, projectId);
36-
} catch (e) {
37-
this.$logger.trace("Launch request error: ", e);
38-
this.$errors.failWithoutHelp("Error while waiting for response from NativeScript runtime.");
39-
}
40-
}
41-
42-
private async executeHandshake(deviceIdentifier: string, projectId: string, timeout: number): Promise<void> {
43-
// This notification will be send only once by the runtime during application start.
44-
// In case app is already running, we'll fail here as we'll not receive it.
45-
const appLaunchingNotification = this.$iOSNotification.getAppLaunching(projectId);
46-
const appLaunchingSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, appLaunchingNotification, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
47-
await this.$iOSNotificationService.awaitNotification(deviceIdentifier, +appLaunchingSocket, timeout);
48-
}
4921
}
5022

5123
$injector.register("iOSSocketRequestExecutor", IOSSocketRequestExecutor);

lib/services/livesync/livesync-service.ts

-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
329329
} catch (err) {
330330
this.$logger.trace("Couldn't attach debugger, will modify options and try again.", err);
331331
attachDebuggerOptions.debugOptions.start = false;
332-
attachDebuggerOptions.debugOptions.skipHandshake = true;
333332
try {
334333
debugInformation = await this.attachDebugger(attachDebuggerOptions);
335334
} catch (innerErr) {

lib/services/user-settings-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import * as userSettingsServiceBaseLib from "../common/services/user-settings-se
44
export class UserSettingsService extends userSettingsServiceBaseLib.UserSettingsServiceBase {
55
constructor($fs: IFileSystem,
66
$settingsService: ISettingsService,
7-
$lockfile: ILockFile,
7+
$lockService: ILockService,
88
$logger: ILogger) {
99
const userSettingsFilePath = path.join($settingsService.getProfileDir(), "user-settings.json");
10-
super(userSettingsFilePath, $fs, $lockfile, $logger);
10+
super(userSettingsFilePath, $fs, $lockService, $logger);
1111
}
1212

1313
public async loadUserSettingsFile(): Promise<void> {

test/stubs.ts

-13
Original file line numberDiff line numberDiff line change
@@ -564,19 +564,6 @@ export class HooksServiceStub implements IHooksService {
564564
hookArgsName = "hookArgs";
565565
}
566566

567-
export class LockFile {
568-
569-
async check(): Promise<boolean> {
570-
return false;
571-
}
572-
573-
async lock(): Promise<void> {
574-
}
575-
576-
async unlock(): Promise<void> {
577-
}
578-
}
579-
580567
export class PrompterStub implements IPrompter {
581568
private strings: IDictionary<string> = {};
582569
private passwords: IDictionary<string> = {};

0 commit comments

Comments
 (0)