Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiple errors in Sidekick #4888

Merged
merged 8 commits into from
Jul 19, 2019
11 changes: 8 additions & 3 deletions lib/controllers/prepare-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class PrepareController extends EventEmitter {
private watchersData: IDictionary<IDictionary<IPlatformWatcherData>> = {};
private isInitialPrepareReady = false;
private persistedData: IFilesChangeEventData[] = [];
private webpackCompilerHandler: any = null;

constructor(
private $platformController: IPlatformController,
Expand Down Expand Up @@ -46,7 +47,8 @@ export class PrepareController extends EventEmitter {
}

if (this.watchersData && this.watchersData[projectDir] && this.watchersData[projectDir][platformLowerCase] && this.watchersData[projectDir][platformLowerCase].hasWebpackCompilerProcess) {
await this.$webpackCompilerService.stopWebpackCompiler(platform);
await this.$webpackCompilerService.stopWebpackCompiler(platformLowerCase);
this.$webpackCompilerService.removeListener(WEBPACK_COMPILATION_COMPLETE, this.webpackCompilerHandler);
this.watchersData[projectDir][platformLowerCase].hasWebpackCompilerProcess = false;
}
}
Expand Down Expand Up @@ -110,11 +112,14 @@ export class PrepareController extends EventEmitter {

private async startJSWatcherWithPrepare(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData): Promise<void> {
if (!this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].hasWebpackCompilerProcess) {
this.$webpackCompilerService.on(WEBPACK_COMPILATION_COMPLETE, data => {
const handler = (data: any) => {
if (data.platform.toLowerCase() === platformData.platformNameLowerCase) {
this.emitPrepareEvent({ ...data, hasNativeChanges: false });
}
});
};

this.webpackCompilerHandler = handler.bind(this);
this.$webpackCompilerService.on(WEBPACK_COMPILATION_COMPLETE, this.webpackCompilerHandler);

this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].hasWebpackCompilerProcess = true;
await this.$webpackCompilerService.compileWithWatch(platformData, projectData, prepareData);
Expand Down
15 changes: 11 additions & 4 deletions lib/controllers/preview-app-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon

constructor(
private $analyticsService: IAnalyticsService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $errors: IErrors,
private $hmrStatusService: IHmrStatusService,
private $logger: ILogger,
Expand All @@ -39,11 +40,15 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
return result;
}

public async stopPreview(): Promise<void> {
public async stopPreview(data: IProjectDir): Promise<void> {
this.$previewSdkService.stop();
this.$previewDevicesService.updateConnectedDevices([]);

await this.$prepareController.stopWatchers(data.projectDir, this.$devicePlatformsConstants.Android);
await this.$prepareController.stopWatchers(data.projectDir, this.$devicePlatformsConstants.iOS);

if (this.prepareReadyEventHandler) {
this.removeListener(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
this.$prepareController.removeListener(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
this.prepareReadyEventHandler = null;
}
}
Expand Down Expand Up @@ -83,10 +88,12 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
await this.$previewAppPluginsService.comparePluginsOnDevice(data, device);

if (!this.prepareReadyEventHandler) {
this.prepareReadyEventHandler = async (currentPrepareData: IFilesChangeEventData) => {
const handler = async (currentPrepareData: IFilesChangeEventData) => {
await this.handlePrepareReadyEvent(data, currentPrepareData);
};
this.$prepareController.on(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler.bind(this));

this.prepareReadyEventHandler = handler.bind(this);
this.$prepareController.on(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
}

data.env = data.env || {};
Expand Down
10 changes: 6 additions & 4 deletions lib/controllers/run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class RunController extends EventEmitter implements IRunController {
}

if (!this.prepareReadyEventHandler) {
this.prepareReadyEventHandler = async (data: IFilesChangeEventData) => {
const handler = async (data: IFilesChangeEventData) => {
if (data.hasNativeChanges) {
const platformData = this.$platformsDataService.getPlatformData(data.platform, projectData);
const prepareData = this.$prepareDataService.getPrepareData(liveSyncInfo.projectDir, data.platform, { ...liveSyncInfo, watch: !liveSyncInfo.skipWatcher });
Expand All @@ -63,7 +63,9 @@ export class RunController extends EventEmitter implements IRunController {
await this.syncChangedDataOnDevices(data, projectData, liveSyncInfo);
}
};
this.$prepareController.on(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler.bind(this));

this.prepareReadyEventHandler = handler.bind(this);
this.$prepareController.on(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
}

await this.syncInitialDataOnDevices(projectData, liveSyncInfo, deviceDescriptorsForInitialSync);
Expand Down Expand Up @@ -113,7 +115,7 @@ export class RunController extends EventEmitter implements IRunController {
liveSyncProcessInfo.deviceDescriptors = [];

if (this.prepareReadyEventHandler) {
this.removeListener(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
this.$prepareController.removeListener(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
this.prepareReadyEventHandler = null;
}

Expand Down Expand Up @@ -377,7 +379,7 @@ export class RunController extends EventEmitter implements IRunController {

await this.$deviceInstallAppService.installOnDevice(device, deviceDescriptor.buildData, rebuiltInformation[platformData.platformNameLowerCase].packageFilePath);
await platformLiveSyncService.syncAfterInstall(device, watchInfo);
await platformLiveSyncService.restartApplication(projectData, { deviceAppData, modifiedFilesData: [], isFullSync: false, useHotModuleReload: liveSyncInfo.useHotModuleReload });
await this.refreshApplication(projectData, { deviceAppData, modifiedFilesData: [], isFullSync: false, useHotModuleReload: liveSyncInfo.useHotModuleReload }, data, deviceDescriptor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only change that bothers me a little, but refreshApplication seems to be the only method that can handle missing DDI.

} else {
const isInHMRMode = liveSyncInfo.useHotModuleReload && data.hmrData && data.hmrData.hash;
if (isInHMRMode) {
Expand Down
2 changes: 1 addition & 1 deletion lib/definitions/preview-app-livesync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ declare global {

interface IPreviewAppController {
startPreview(data: IPreviewAppLiveSyncData): Promise<IQrCodeImageData>;
stopPreview(): Promise<void>;
stopPreview(data: IProjectDir): Promise<void>;
}
}
4 changes: 4 additions & 0 deletions lib/services/webpack/webpack-compiler-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp

childProcess.on("error", (err) => {
this.$logger.trace(`Unable to start webpack process in watch mode. Error is: ${err}`);
delete this.webpackProcesses[platformData.platformNameLowerCase];
reject(err);
});

Expand All @@ -82,6 +83,7 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
this.$logger.trace(`Webpack process exited with code ${exitCode} when we expected it to be long living with watch.`);
const error = new Error(`Executing webpack failed with exit code ${exitCode}.`);
error.code = exitCode;
delete this.webpackProcesses[platformData.platformNameLowerCase];
reject(error);
});
});
Expand All @@ -97,12 +99,14 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
childProcess.on("error", (err) => {
this.$logger.trace(`Unable to start webpack process in non-watch mode. Error is: ${err}`);
delete this.webpackProcesses[platformData.platformNameLowerCase];
reject(err);
});

childProcess.on("close", async (arg: any) => {
await this.$cleanupService.removeKillProcess(childProcess.pid.toString());

delete this.webpackProcesses[platformData.platformNameLowerCase];
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
if (exitCode === 0) {
resolve();
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nativescript",
"preferGlobal": true,
"version": "6.0.1",
"version": "6.0.2",
"author": "Telerik <support@telerik.com>",
"description": "Command-line interface for building NativeScript projects",
"bin": {
Expand Down
3 changes: 2 additions & 1 deletion test/controllers/run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ function createTestInjector() {
prepareData = currentPrepareData;
return { platform: prepareData.platform, hasNativeChanges: false };
},
on: () => ({})
on: () => ({}),
removeListener: (): void => undefined
});
injector.register("prepareNativePlatformService", {});
injector.register("projectChangesService", {});
Expand Down