Skip to content

Commit b30ab1e

Browse files
authored
Merge pull request #4710 from NativeScript/fatme/hooks
fix: add preview-sync and watchPatterns hooks
2 parents 3c2195f + 0b1d6cd commit b30ab1e

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

lib/controllers/prepare-controller.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,8 @@ export class PrepareController extends EventEmitter {
114114
return false;
115115
}
116116

117-
const patterns = [
118-
path.join(projectData.projectDir, PACKAGE_JSON_FILE_NAME),
119-
path.join(projectData.getAppDirectoryPath(), PACKAGE_JSON_FILE_NAME),
120-
path.join(projectData.getAppResourcesRelativeDirectoryPath(), platformData.normalizedPlatformName),
121-
`node_modules/**/platforms/${platformData.platformNameLowerCase}/`
122-
];
117+
const patterns = await this.getWatcherPatterns(platformData, projectData);
118+
123119
const watcherOptions: choki.WatchOptions = {
124120
ignoreInitial: true,
125121
cwd: projectData.projectDir,
@@ -143,6 +139,18 @@ export class PrepareController extends EventEmitter {
143139
return hasNativeChanges;
144140
}
145141

142+
@hook('watchPatterns')
143+
public async getWatcherPatterns(platformData: IPlatformData, projectData: IProjectData): Promise<string[]> {
144+
const patterns = [
145+
path.join(projectData.projectDir, PACKAGE_JSON_FILE_NAME),
146+
path.join(projectData.getAppDirectoryPath(), PACKAGE_JSON_FILE_NAME),
147+
path.join(projectData.getAppResourcesRelativeDirectoryPath(), platformData.normalizedPlatformName),
148+
`node_modules/**/platforms/${platformData.platformNameLowerCase}/`
149+
];
150+
151+
return patterns;
152+
}
153+
146154
private emitPrepareEvent(filesChangeEventData: IFilesChangeEventData) {
147155
if (this.isInitialPrepareReady) {
148156
this.emit(PREPARE_READY_EVENT_NAME, filesChangeEventData);

lib/controllers/preview-app-controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Device, FilesPayload } from "nativescript-preview-sdk";
22
import { TrackActionNames, PREPARE_READY_EVENT_NAME } from "../constants";
33
import { PrepareController } from "./prepare-controller";
44
import { performanceLog } from "../common/decorators";
5-
import { stringify } from "../common/helpers";
5+
import { stringify, hook } from "../common/helpers";
66
import { HmrConstants } from "../common/constants";
77
import { EventEmitter } from "events";
88
import { PrepareDataService } from "../services/prepare-data-service";
@@ -17,6 +17,7 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
1717
private $errors: IErrors,
1818
private $hmrStatusService: IHmrStatusService,
1919
private $logger: ILogger,
20+
public $hooksService: IHooksService,
2021
private $prepareController: PrepareController,
2122
private $previewAppFilesService: IPreviewAppFilesService,
2223
private $previewAppPluginsService: IPreviewAppPluginsService,
@@ -26,6 +27,7 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
2627
private $prepareDataService: PrepareDataService
2728
) { super(); }
2829

30+
@hook("preview-sync")
2931
public async startPreview(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData> {
3032
await this.previewCore(data);
3133

test/services/playground/preview-app-livesync-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ function createTestInjector(options?: {
180180
injector.register("analyticsService", {
181181
trackEventActionInGoogleAnalytics: () => ({})
182182
});
183+
injector.register("hooksService", {
184+
executeBeforeHooks: () => ({}),
185+
executeAfterHooks: () => ({})
186+
});
183187

184188
return injector;
185189
}

0 commit comments

Comments
 (0)