Skip to content

Commit 11c2263

Browse files
committed
chore: persist warnings per preview app's version
1 parent 76ff072 commit 11c2263

File tree

2 files changed

+82
-24
lines changed

2 files changed

+82
-24
lines changed

lib/services/livesync/playground/preview-app-plugins-service.ts

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,27 @@ import { Device } from "nativescript-preview-sdk";
55
import { PluginComparisonMessages } from "./preview-app-constants";
66

77
export class PreviewAppPluginsService implements IPreviewAppPluginsService {
8+
private previewAppVersionWarnings: IDictionary<string[]> = {};
9+
810
constructor(private $fs: IFileSystem,
911
private $logger: ILogger,
1012
private $projectData: IProjectData) { }
1113

1214
public async comparePluginsOnDevice(device: Device): Promise<void> {
13-
const devicePlugins = this.getDevicePlugins(device);
14-
const localPlugins = this.getLocalPlugins();
15-
16-
_.keys(localPlugins).forEach(localPlugin => {
17-
const localPluginVersion = localPlugins[localPlugin];
18-
const devicePluginVersion = devicePlugins[localPlugin];
19-
20-
this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`);
21-
22-
if (devicePluginVersion) {
23-
const localPluginVersionData = semver.coerce(localPluginVersion);
24-
const devicePluginVersionData = semver.coerce(devicePluginVersion);
25-
26-
if (localPluginVersionData.major !== devicePluginVersionData.major) {
27-
this.$logger.warn(util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion));
28-
}
15+
if (!this.previewAppVersionWarnings[device.previewAppVersion]) {
16+
const devicePlugins = this.getDevicePlugins(device);
17+
const localPlugins = this.getLocalPlugins();
18+
const warnings = _.keys(localPlugins)
19+
.map(localPlugin => {
20+
const localPluginVersion = localPlugins[localPlugin];
21+
const devicePluginVersion = devicePlugins[localPlugin];
22+
return this.getWarningForPlugin(localPlugin, localPluginVersion, devicePluginVersion, device.id);
23+
})
24+
.filter(item => !!item);
25+
this.previewAppVersionWarnings[device.previewAppVersion] = warnings;
26+
}
2927

30-
if (localPluginVersionData.major === devicePluginVersionData.major && localPluginVersionData.minor > devicePluginVersionData.minor) {
31-
this.$logger.warn(util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion));
32-
}
33-
} else {
34-
this.$logger.warn(util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, localPlugin, device.id));
35-
}
36-
});
28+
this.previewAppVersionWarnings[device.previewAppVersion].map(warning => this.$logger.warn(warning));
3729
}
3830

3931
private getDevicePlugins(device: Device): IStringDictionary {
@@ -54,5 +46,24 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService {
5446
return {};
5547
}
5648
}
49+
50+
private getWarningForPlugin(localPlugin: string, localPluginVersion: string, devicePluginVersion: string, deviceId: string): string {
51+
this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`);
52+
53+
if (devicePluginVersion) {
54+
const localPluginVersionData = semver.coerce(localPluginVersion);
55+
const devicePluginVersionData = semver.coerce(devicePluginVersion);
56+
57+
if (localPluginVersionData.major !== devicePluginVersionData.major) {
58+
return util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion);
59+
} else if (localPluginVersionData.minor > devicePluginVersionData.minor) {
60+
return util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion);
61+
}
62+
63+
return null;
64+
}
65+
66+
return util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, localPlugin, deviceId);
67+
}
5768
}
5869
$injector.register("previewAppPluginsService", PreviewAppPluginsService);

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function createDevice(plugins: string): Device {
3838
model: "myTestDeviceModel",
3939
name: "myTestDeviceName",
4040
osVersion: "10.0",
41-
previewAppVersion: "28.0",
41+
previewAppVersion: "28.0.0",
4242
runtimeVersion: "4.3.0",
4343
plugins,
4444
pluginsExpanded: false
@@ -58,6 +58,53 @@ function setup(localPlugins: IStringDictionary, previewAppPlugins: IStringDictio
5858

5959
describe("previewAppPluginsService", () => {
6060
describe("comparePluginsOnDevice", () => {
61+
it("should persist warnings per preview app's version", async () => {
62+
const localPlugins = {
63+
"nativescript-facebook": "2.2.3",
64+
"nativescript-theme-core": "1.0.4",
65+
"tns-core-modules": "4.2.0"
66+
};
67+
const previewAppPlugins = {
68+
"nativescript-theme-core": "2.0.4",
69+
"tns-core-modules": "4.2.0"
70+
};
71+
const injector = createTestInjector(localPlugins);
72+
const previewAppPluginsService = injector.resolve("previewAppPluginsService");
73+
74+
let isGetDevicePluginsCalled = false;
75+
const originalGetDevicePlugins = (<any>previewAppPluginsService).getDevicePlugins;
76+
(<any>previewAppPluginsService).getDevicePlugins = (device: Device) => {
77+
isGetDevicePluginsCalled = true;
78+
return originalGetDevicePlugins(device);
79+
};
80+
let isGetLocalPluginsCalled = false;
81+
const originalGetLocalPlugins = (<any>previewAppPluginsService).getLocalPlugins;
82+
(<any>previewAppPluginsService).getLocalPlugins = () => {
83+
isGetLocalPluginsCalled = true;
84+
return originalGetLocalPlugins.apply(previewAppPluginsService);
85+
};
86+
87+
await previewAppPluginsService.comparePluginsOnDevice(createDevice(JSON.stringify(previewAppPlugins)));
88+
89+
const expectedWarnings = [
90+
util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, "nativescript-facebook", deviceId),
91+
util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, "nativescript-theme-core", "1.0.4", "2.0.4")
92+
];
93+
assert.isTrue(isGetDevicePluginsCalled);
94+
assert.isTrue(isGetLocalPluginsCalled);
95+
assert.deepEqual(warnParams, expectedWarnings);
96+
97+
isGetDevicePluginsCalled = false;
98+
isGetLocalPluginsCalled = false;
99+
warnParams = [];
100+
101+
await previewAppPluginsService.comparePluginsOnDevice(createDevice(JSON.stringify(previewAppPlugins)));
102+
103+
assert.isFalse(isGetDevicePluginsCalled);
104+
assert.isFalse(isGetLocalPluginsCalled);
105+
assert.deepEqual(warnParams, expectedWarnings);
106+
});
107+
61108
const testCases = [
62109
{
63110
name: "should show warning for plugin not included in preview app",

0 commit comments

Comments
 (0)