Skip to content

Commit 8b65d78

Browse files
committed
chore: don't show warnings for {N} plugins without native code when bundle option is provided
1 parent b5e0aa0 commit 8b65d78

File tree

4 files changed

+227
-17
lines changed

4 files changed

+227
-17
lines changed

lib/definitions/preview-app-livesync.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ declare global {
1818
}
1919

2020
interface IPreviewAppPluginsService {
21-
comparePluginsOnDevice(device: Device): Promise<void>;
21+
comparePluginsOnDevice(data: IPreviewAppLiveSyncData, device: Device): Promise<void>;
2222
getExternalPlugins(device: Device): string[];
2323
}
2424

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
4949
startSyncFilesTimeout: startSyncFilesTimeout.bind(this)
5050
}
5151
});
52-
await this.$previewAppPluginsService.comparePluginsOnDevice(device);
52+
await this.$previewAppPluginsService.comparePluginsOnDevice(data, device);
5353
const payloads = await this.syncFilesForPlatformSafe(data, device.platform);
5454

5555
return payloads;
@@ -60,7 +60,7 @@ export class PreviewAppLiveSyncService implements IPreviewAppLiveSyncService {
6060
this.showWarningsForNativeFiles(files);
6161

6262
for (const device of this.$previewSdkService.connectedDevices) {
63-
await this.$previewAppPluginsService.comparePluginsOnDevice(device);
63+
await this.$previewAppPluginsService.comparePluginsOnDevice(data, device);
6464
}
6565

6666
const platforms = _(this.$previewSdkService.connectedDevices)

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as semver from "semver";
33
import * as util from "util";
44
import { Device } from "nativescript-preview-sdk";
55
import { PluginComparisonMessages } from "./preview-app-constants";
6+
import { NODE_MODULES_DIR_NAME } from "../../../common/constants";
67

78
export class PreviewAppPluginsService implements IPreviewAppPluginsService {
89
private previewAppVersionWarnings: IDictionary<string[]> = {};
@@ -11,15 +12,15 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService {
1112
private $logger: ILogger,
1213
private $projectData: IProjectData) { }
1314

14-
public async comparePluginsOnDevice(device: Device): Promise<void> {
15+
public async comparePluginsOnDevice(data: IPreviewAppLiveSyncData, device: Device): Promise<void> {
1516
if (!this.previewAppVersionWarnings[device.previewAppVersion]) {
1617
const devicePlugins = this.getDevicePlugins(device);
1718
const localPlugins = this.getLocalPlugins();
1819
const warnings = _.keys(localPlugins)
1920
.map(localPlugin => {
2021
const localPluginVersion = localPlugins[localPlugin];
2122
const devicePluginVersion = devicePlugins[localPlugin];
22-
return this.getWarningForPlugin(localPlugin, localPluginVersion, devicePluginVersion, device.id);
23+
return this.getWarningForPlugin(data, localPlugin, localPluginVersion, devicePluginVersion, device);
2324
})
2425
.filter(item => !!item);
2526
this.previewAppVersionWarnings[device.previewAppVersion] = warnings;
@@ -62,7 +63,15 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService {
6263
}
6364
}
6465

65-
private getWarningForPlugin(localPlugin: string, localPluginVersion: string, devicePluginVersion: string, deviceId: string): string {
66+
private getWarningForPlugin(data: IPreviewAppLiveSyncData, localPlugin: string, localPluginVersion: string, devicePluginVersion: string, device: Device): string {
67+
if (data && data.appFilesUpdaterOptions && data.appFilesUpdaterOptions.bundle && this.isNativeScriptPluginWithoutNativeCode(localPlugin, device.platform)) {
68+
return null;
69+
}
70+
71+
return this.getWarningForPluginCore(localPlugin, localPluginVersion, devicePluginVersion, device.id);
72+
}
73+
74+
private getWarningForPluginCore(localPlugin: string, localPluginVersion: string, devicePluginVersion: string, deviceId: string): string {
6675
this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`);
6776

6877
if (devicePluginVersion) {
@@ -80,5 +89,20 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService {
8089

8190
return util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, localPlugin, deviceId);
8291
}
92+
93+
private isNativeScriptPluginWithoutNativeCode(localPlugin: string, platform: string): boolean {
94+
return this.isNativeScriptPlugin(localPlugin) && !this.hasNativeCode(localPlugin, platform);
95+
}
96+
97+
private isNativeScriptPlugin(localPlugin: string): boolean {
98+
const pluginPackageJsonPath = path.join(this.$projectData.projectDir, NODE_MODULES_DIR_NAME, localPlugin, "package.json");
99+
const pluginPackageJsonContent = this.$fs.readJson(pluginPackageJsonPath);
100+
return pluginPackageJsonContent && pluginPackageJsonContent.nativescript;
101+
}
102+
103+
private hasNativeCode(localPlugin: string, platform: string): boolean {
104+
const nativeFolderPath = path.join(this.$projectData.projectDir, NODE_MODULES_DIR_NAME, localPlugin, "platforms", platform.toLowerCase());
105+
return this.$fs.exists(nativeFolderPath) && !this.$fs.isEmptyDir(nativeFolderPath);
106+
}
83107
}
84108
$injector.register("previewAppPluginsService", PreviewAppPluginsService);

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

+197-11
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,43 @@ import { PluginComparisonMessages } from "../../../lib/services/livesync/playgro
88
let readJsonParams: string[] = [];
99
let warnParams: string[] = [];
1010

11-
function createTestInjector(localPlugins: IStringDictionary): IInjector {
11+
const deviceId = "myTestDeviceId";
12+
const projectDir = "testProjectDir";
13+
14+
function createTestInjector(localPlugins: IStringDictionary, options?: { isNativeScriptPlugin?: boolean, hasPluginNativeCode?: boolean }): IInjector {
15+
options = options || {};
1216
const injector = new Yok();
1317
injector.register("fs", {
1418
readJson: (filePath: string) => {
1519
readJsonParams.push(filePath);
16-
return {
20+
const result: any = {
1721
dependencies: localPlugins
1822
};
23+
24+
if (options.isNativeScriptPlugin) {
25+
result.nativescript = {};
26+
}
27+
28+
return result;
29+
},
30+
exists: (filePath: string) => {
31+
return options.hasPluginNativeCode;
32+
},
33+
isEmptyDir: (filePath: string) => {
34+
return !options.hasPluginNativeCode;
1935
}
2036
});
2137
injector.register("logger", {
2238
trace: () => ({}),
2339
warn: (message: string) => warnParams.push(message)
2440
});
2541
injector.register("projectData", {
26-
projectDir: "testProjectDir"
42+
projectDir
2743
});
2844
injector.register("previewAppPluginsService", PreviewAppPluginsService);
2945
return injector;
3046
}
3147

32-
const deviceId = "myTestDeviceId";
33-
3448
function createDevice(plugins: string): Device {
3549
return {
3650
id: deviceId,
@@ -45,8 +59,20 @@ function createDevice(plugins: string): Device {
4559
};
4660
}
4761

48-
function setup(localPlugins: IStringDictionary, previewAppPlugins: IStringDictionary): any {
49-
const injector = createTestInjector(localPlugins);
62+
function createPreviewLiveSyncData(options?: { bundle: boolean }) {
63+
return {
64+
projectDir,
65+
appFilesUpdaterOptions: {
66+
release: false,
67+
bundle: options.bundle
68+
},
69+
env: {}
70+
};
71+
}
72+
73+
function setup(localPlugins: IStringDictionary, previewAppPlugins: IStringDictionary,
74+
options?: { isNativeScriptPlugin: boolean, hasPluginNativeCode: boolean }): any {
75+
const injector = createTestInjector(localPlugins, options);
5076
const previewAppPluginsService = injector.resolve("previewAppPluginsService");
5177
const device = createDevice(JSON.stringify(previewAppPlugins));
5278

@@ -57,7 +83,7 @@ function setup(localPlugins: IStringDictionary, previewAppPlugins: IStringDictio
5783
}
5884

5985
describe("previewAppPluginsService", () => {
60-
describe("comparePluginsOnDevice", () => {
86+
describe("comparePluginsOnDevice without bundle", () => {
6187
it("should persist warnings per preview app's version", async () => {
6288
const localPlugins = {
6389
"nativescript-facebook": "2.2.3",
@@ -84,7 +110,9 @@ describe("previewAppPluginsService", () => {
84110
return originalGetLocalPlugins.apply(previewAppPluginsService);
85111
};
86112

87-
await previewAppPluginsService.comparePluginsOnDevice(createDevice(JSON.stringify(previewAppPlugins)));
113+
const previewLiveSyncData = createPreviewLiveSyncData({ bundle: false });
114+
115+
await previewAppPluginsService.comparePluginsOnDevice(previewLiveSyncData, createDevice(JSON.stringify(previewAppPlugins)));
88116

89117
const expectedWarnings = [
90118
util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, "nativescript-facebook", deviceId),
@@ -98,7 +126,7 @@ describe("previewAppPluginsService", () => {
98126
isGetLocalPluginsCalled = false;
99127
warnParams = [];
100128

101-
await previewAppPluginsService.comparePluginsOnDevice(createDevice(JSON.stringify(previewAppPlugins)));
129+
await previewAppPluginsService.comparePluginsOnDevice(previewLiveSyncData, createDevice(JSON.stringify(previewAppPlugins)));
102130

103131
assert.isFalse(isGetDevicePluginsCalled);
104132
assert.isFalse(isGetLocalPluginsCalled);
@@ -225,13 +253,171 @@ describe("previewAppPluginsService", () => {
225253
it(`${testCase.name}`, async () => {
226254
const { previewAppPluginsService, device } = setup(testCase.localPlugins, testCase.previewAppPlugins);
227255

228-
await previewAppPluginsService.comparePluginsOnDevice(device);
256+
await previewAppPluginsService.comparePluginsOnDevice(createPreviewLiveSyncData({ bundle: false }), device);
229257

230258
assert.equal(warnParams.length, testCase.expectedWarnings.length);
231259
testCase.expectedWarnings.forEach(warning => assert.include(warnParams, warning));
232260
});
233261
}
234262
});
263+
describe("comparePluginsOnDevice with bundle", () => {
264+
const testCases = [
265+
{
266+
name: "should show warning for non nativescript plugin that has lower major version",
267+
localPlugins: {
268+
lodash: "1.2.3"
269+
},
270+
previewAppPlugins: {
271+
lodash: "2.3.3"
272+
},
273+
isNativeScriptPlugin: false,
274+
hasPluginNativeCode: false,
275+
expectedWarnings: [
276+
util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, "lodash", "1.2.3", "2.3.3")
277+
]
278+
},
279+
{
280+
name: "should show warning for non nativescript plugin that has greather major version",
281+
localPlugins: {
282+
lodash: "3.2.3"
283+
},
284+
previewAppPlugins: {
285+
lodash: "2.3.3"
286+
},
287+
isNativeScriptPlugin: false,
288+
hasPluginNativeCode: false,
289+
expectedWarnings: [
290+
util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, "lodash", "3.2.3", "2.3.3")
291+
]
292+
},
293+
{
294+
name: "should show warning for non nativescript plugin that has greather minor version",
295+
localPlugins: {
296+
lodash: "3.4.5"
297+
},
298+
previewAppPlugins: {
299+
lodash: "3.3.0"
300+
},
301+
isNativeScriptPlugin: false,
302+
hasPluginNativeCode: false,
303+
expectedWarnings: [
304+
util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION, "lodash", "3.4.5", "3.3.0")
305+
]
306+
},
307+
{
308+
name: "should not show warning for non nativescript plugin that has the same version",
309+
localPlugins: {
310+
lodash: "3.4.5"
311+
},
312+
previewAppPlugins: {
313+
lodash: "3.4.5"
314+
},
315+
isNativeScriptPlugin: false,
316+
hasPluginNativeCode: false,
317+
expectedWarnings: []
318+
},
319+
{
320+
name: "should not show warning for nativescript plugin without native code that has lower major version",
321+
localPlugins: {
322+
"nativescript-theme-core": "2.4.5"
323+
},
324+
previewAppPlugins: {
325+
"nativescript-theme-core": "3.4.5"
326+
},
327+
isNativeScriptPlugin: true,
328+
hasPluginNativeCode: false,
329+
expectedWarnings: <string[]>[]
330+
},
331+
{
332+
name: "should not show warning for nativescript plugin without native code that has greather major version",
333+
localPlugins: {
334+
"nativescript-theme-core": "4.4.5"
335+
},
336+
previewAppPlugins: {
337+
"nativescript-theme-core": "3.4.5"
338+
},
339+
isNativeScriptPlugin: true,
340+
hasPluginNativeCode: false,
341+
expectedWarnings: []
342+
},
343+
{
344+
name: "should not show warning for nativescript plugin without native code that has greather minor version",
345+
localPlugins: {
346+
"nativescript-theme-core": "4.6.5"
347+
},
348+
previewAppPlugins: {
349+
"nativescript-theme-core": "4.4.5"
350+
},
351+
isNativeScriptPlugin: true,
352+
hasPluginNativeCode: false,
353+
expectedWarnings: []
354+
},
355+
{
356+
name: "should not show warning for nativescript plugin without native code that has the same version",
357+
localPlugins: {
358+
"nativescript-theme-core": "4.6.5"
359+
},
360+
previewAppPlugins: {
361+
"nativescript-theme-core": "4.6.5"
362+
},
363+
isNativeScriptPlugin: true,
364+
hasPluginNativeCode: false,
365+
expectedWarnings: []
366+
},
367+
{
368+
name: "should show warning for nativescript plugin with native code that has lower major version",
369+
localPlugins: {
370+
"nativescript-theme-core": "2.4.5"
371+
},
372+
previewAppPlugins: {
373+
"nativescript-theme-core": "3.4.5"
374+
},
375+
isNativeScriptPlugin: true,
376+
hasPluginNativeCode: true,
377+
expectedWarnings: [util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, "nativescript-theme-core", "2.4.5", "3.4.5")]
378+
},
379+
{
380+
name: "should show warning for nativescript plugin with native code that has greather major version",
381+
localPlugins: {
382+
"nativescript-theme-core": "4.4.5"
383+
},
384+
previewAppPlugins: {
385+
"nativescript-theme-core": "3.4.5"
386+
},
387+
isNativeScriptPlugin: true,
388+
hasPluginNativeCode: true,
389+
expectedWarnings: [util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, "nativescript-theme-core", "4.4.5", "3.4.5")]
390+
},
391+
{
392+
name: "should show warning for nativescript plugin with native code that has greather minor version",
393+
localPlugins: {
394+
"nativescript-theme-core": "4.4.5"
395+
},
396+
previewAppPlugins: {
397+
"nativescript-theme-core": "4.3.5"
398+
},
399+
isNativeScriptPlugin: true,
400+
hasPluginNativeCode: true,
401+
expectedWarnings: [util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION, "nativescript-theme-core", "4.4.5", "4.3.5")]
402+
},
403+
];
404+
405+
afterEach(() => {
406+
warnParams = [];
407+
readJsonParams = [];
408+
});
409+
410+
_.each(testCases, testCase => {
411+
it(`${testCase.name}`, async () => {
412+
const { previewAppPluginsService, device } = setup(testCase.localPlugins, testCase.previewAppPlugins, { isNativeScriptPlugin: testCase.isNativeScriptPlugin, hasPluginNativeCode: testCase.hasPluginNativeCode });
413+
414+
await previewAppPluginsService.comparePluginsOnDevice(createPreviewLiveSyncData({ bundle: true }), device);
415+
416+
assert.equal(warnParams.length, testCase.expectedWarnings.length);
417+
testCase.expectedWarnings.forEach(warning => assert.include(warnParams, warning));
418+
});
419+
});
420+
});
235421
describe("getExternalPlugins", () => {
236422
const testCases = [
237423
{

0 commit comments

Comments
 (0)