|
| 1 | +import * as path from "path"; |
| 2 | +import * as semver from "semver"; |
| 3 | +import { Device } from "nativescript-preview-sdk"; |
| 4 | + |
| 5 | +export class PreviewAppPluginsService implements IPreviewAppPluginsService { |
| 6 | + constructor(private $fs: IFileSystem, |
| 7 | + private $logger: ILogger, |
| 8 | + private $projectData: IProjectData) { } |
| 9 | + |
| 10 | + public async comparePluginsOnDevice(device: Device): Promise<void> { |
| 11 | + const devicePlugins = this.getDevicePlugins(device); |
| 12 | + const localPlugins = this.getLocalPlugins(); |
| 13 | + |
| 14 | + _.keys(localPlugins).forEach(localPlugin => { |
| 15 | + const localPluginVersion = localPlugins[localPlugin]; |
| 16 | + const devicePluginVersion = devicePlugins[localPlugin]; |
| 17 | + |
| 18 | + this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`); |
| 19 | + |
| 20 | + if (!devicePluginVersion) { |
| 21 | + this.$logger.warn(`Plugin ${localPlugin} is not included in preview app and will not work.`); |
| 22 | + } |
| 23 | + |
| 24 | + if (devicePluginVersion && semver.gt(semver.coerce(localPluginVersion), semver.coerce(devicePluginVersion))) { |
| 25 | + this.$logger.warn(`Plugin ${localPlugin} has local version ${localPluginVersion} but preview app has ${devicePluginVersion} version of plugin. Some functionalities may not work.`); |
| 26 | + } |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + private getDevicePlugins(device: Device): IStringDictionary { |
| 31 | + try { |
| 32 | + return JSON.parse(device.plugins); |
| 33 | + } catch (err) { |
| 34 | + this.$logger.trace(`Error while parsing plugins from device ${device.id}. Error is ${err.message}`); |
| 35 | + return {}; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private getLocalPlugins(): IStringDictionary { |
| 40 | + const projectFilePath = path.join(this.$projectData.projectDir, "package.json"); |
| 41 | + try { |
| 42 | + return this.$fs.readJson(projectFilePath).dependencies; |
| 43 | + } catch (err) { |
| 44 | + this.$logger.trace(`Error while parsing ${projectFilePath}. Error is ${err.message}`); |
| 45 | + return {}; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +$injector.register("previewAppPluginsService", PreviewAppPluginsService); |
0 commit comments