UNPKG

nativescript

Version:

Command-line interface for building NativeScript projects

92 lines 4.35 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); const helpers_1 = require("../common/helpers"); const decorators_1 = require("../common/decorators"); const _ = require("lodash"); const yok_1 = require("../common/yok"); const color_1 = require("../color"); class AnalyticsSettingsService { constructor($userSettingsService, $staticConfig, $hostInfo, $osInfo, $logger) { this.$userSettingsService = $userSettingsService; this.$staticConfig = $staticConfig; this.$hostInfo = $hostInfo; this.$osInfo = $osInfo; this.$logger = $logger; } async canDoRequest() { return true; } getUserId() { return this.getSettingValueOrDefault("USER_ID"); } getClientId() { return this.getSettingValueOrDefault(this.$staticConfig.ANALYTICS_INSTALLATION_ID_SETTING_NAME); } getClientName() { return "" + color_1.color.cyan.bold(this.$staticConfig.CLIENT_NAME_ALIAS); } async getUserSessionsCount(projectName) { const sessionsCountForProject = await this.$userSettingsService.getSettingValue(this.getSessionsProjectKey(projectName)); return sessionsCountForProject || 0; } async setUserSessionsCount(count, projectName) { return this.$userSettingsService.saveSetting(this.getSessionsProjectKey(projectName), count); } getUserAgentString(identifier) { let osString = ""; const osRelease = this.$osInfo.release(); if (this.$hostInfo.isWindows) { osString = `Windows NT ${osRelease}`; } else if (this.$hostInfo.isDarwin) { osString = `Macintosh`; const macRelease = this.getMacOSReleaseVersion(osRelease); if (macRelease) { osString += `; Intel Mac OS X ${macRelease}`; } } else { osString = `Linux x86`; if (this.$osInfo.arch() === "x64") { osString += "_64"; } } const userAgent = `${identifier} (${osString}; ${this.$osInfo.arch()})`; return userAgent; } getMacOSReleaseVersion(osRelease) { // https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history // Each macOS version is labeled 10.<version>, where it looks like <versions> is taken from the major version returned by os.release() (16.x.x for example) and subtracting 4 from it. // So the version becomes "10.12" in this case. // Could be improved by spawning `system_profiler SPSoftwareDataType` and getting the System Version line from the result. const majorVersion = osRelease && _.first(osRelease.split(".")); return majorVersion && `10.${+majorVersion - 4}`; } getSessionsProjectKey(projectName) { return `${AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX}${projectName}`; } async getSettingValueOrDefault(settingName) { let guid = await this.$userSettingsService.getSettingValue(settingName); if (!guid) { guid = (0, helpers_1.createGUID)(false); this.$logger.trace(`Setting new ${settingName}: ${guid}.`); await this.$userSettingsService.saveSetting(settingName, guid); } return guid; } } AnalyticsSettingsService.SESSIONS_STARTED_KEY_PREFIX = "SESSIONS_STARTED_"; __decorate([ (0, decorators_1.exported)("analyticsSettingsService") ], AnalyticsSettingsService.prototype, "getClientId", null); __decorate([ (0, decorators_1.exported)("analyticsSettingsService") ], AnalyticsSettingsService.prototype, "getUserAgentString", null); yok_1.injector.register("analyticsSettingsService", AnalyticsSettingsService); //# sourceMappingURL=analytics-settings-service.js.map