|
| 1 | +import * as helpers from "../common/helpers"; |
| 2 | +import * as path from "path"; |
| 3 | +import { EOL } from "os"; |
| 4 | +import { PACKAGE_JSON_FILE_NAME, LoggerConfigData } from "../constants"; |
| 5 | + |
| 6 | +const enum MarkingMode { |
| 7 | + None = "none", |
| 8 | + Full = "full" |
| 9 | +} |
| 10 | + |
| 11 | +const MARKING_MODE_PROP = "markingMode"; |
| 12 | +const MARKING_MODE_FULL_DEPRECATION_MSG = `With the upcoming NativeScript 7.0 the "${MARKING_MODE_PROP}:${MarkingMode.None}" will become the only marking mode supported by the Android Runtime.`; |
| 13 | +const MARKING_MODE_NONE_CONFIRM_MSG = `Do you want to switch your app to the recommended "${MARKING_MODE_PROP}:${MarkingMode.None}"? |
| 14 | +More info about the reasons for this change can be found in the link below: |
| 15 | +https://www.nativescript.org/blog/markingmode-none-is-official-boost-android-performance-while-avoiding-memory-issues`; |
| 16 | + |
| 17 | +export class MarkingModeService implements IMarkingModeService { |
| 18 | + |
| 19 | + constructor(private $fs: IFileSystem, |
| 20 | + private $logger: ILogger, |
| 21 | + private $projectDataService: IProjectDataService, |
| 22 | + private $prompter: IPrompter |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public async handleMarkingModeFullDeprecation(options: IMarkingModeFullDeprecationOptions): Promise<void> { |
| 27 | + const { projectDir, skipWarnings, forceSwitch } = options; |
| 28 | + const projectData = this.$projectDataService.getProjectData(projectDir); |
| 29 | + const innerPackageJsonPath = path.join(projectData.getAppDirectoryPath(projectDir), PACKAGE_JSON_FILE_NAME); |
| 30 | + if (!this.$fs.exists(innerPackageJsonPath)) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const innerPackageJson = this.$fs.readJson(innerPackageJsonPath); |
| 35 | + let markingModeValue = (innerPackageJson && innerPackageJson.android |
| 36 | + && typeof (innerPackageJson.android[MARKING_MODE_PROP]) === "string" && innerPackageJson.android[MARKING_MODE_PROP]) || ""; |
| 37 | + |
| 38 | + if (forceSwitch) { |
| 39 | + this.setMarkingMode(innerPackageJsonPath, innerPackageJson, MarkingMode.None); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (!markingModeValue && helpers.isInteractive()) { |
| 44 | + this.$logger.info(); |
| 45 | + this.$logger.printMarkdown(` |
| 46 | +__Improve your app by switching to "${MARKING_MODE_PROP}:${MarkingMode.None}".__ |
| 47 | +
|
| 48 | +\`${MARKING_MODE_FULL_DEPRECATION_MSG}\``); |
| 49 | + const hasSwitched = await this.$prompter.confirm(MARKING_MODE_NONE_CONFIRM_MSG, () => true); |
| 50 | + |
| 51 | + markingModeValue = hasSwitched ? MarkingMode.None : MarkingMode.Full; |
| 52 | + this.setMarkingMode(innerPackageJsonPath, innerPackageJson, markingModeValue); |
| 53 | + } |
| 54 | + |
| 55 | + if (!skipWarnings && markingModeValue.toLowerCase() !== MarkingMode.None) { |
| 56 | + this.showMarkingModeFullWarning(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private setMarkingMode(packagePath: string, packageValue: any, newMode: string) { |
| 61 | + packageValue = packageValue || {}; |
| 62 | + packageValue.android = packageValue.android || {}; |
| 63 | + packageValue.android[MARKING_MODE_PROP] = newMode; |
| 64 | + this.$fs.writeJson(packagePath, packageValue); |
| 65 | + } |
| 66 | + |
| 67 | + private showMarkingModeFullWarning() { |
| 68 | + const markingModeFullWarning = `You are using the deprecated "${MARKING_MODE_PROP}:${MarkingMode.Full}".${EOL}${EOL}${MARKING_MODE_FULL_DEPRECATION_MSG}${EOL}${EOL}You should update your marking mode by executing 'tns update --markingMode'.`; |
| 69 | + |
| 70 | + this.$logger.warn(markingModeFullWarning, { [LoggerConfigData.wrapMessageWithBorders]: true }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +$injector.register("markingModeService", MarkingModeService); |
0 commit comments