nativescript
Version:
Command-line interface for building NativeScript projects
120 lines • 5.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonFileSettingsService = void 0;
const path = require("path");
const helpers_1 = require("../helpers");
const _ = require("lodash");
const yok_1 = require("../yok");
class JsonFileSettingsService {
get lockFilePath() {
return `${this.jsonSettingsFilePath}.lock`;
}
constructor(jsonFileSettingsPath, $fs, $lockService, $logger) {
this.$fs = $fs;
this.$lockService = $lockService;
this.$logger = $logger;
this.jsonSettingsFilePath = null;
this.jsonSettingsData = null;
this.jsonSettingsFilePath = jsonFileSettingsPath;
}
async getSettingValue(settingName, cacheOpts) {
const action = async () => {
await this.loadUserSettingsFile();
if (this.jsonSettingsData && _.has(this.jsonSettingsData, settingName)) {
const data = this.jsonSettingsData[settingName];
const dataToReturn = data.modifiedByCacheMechanism ? data.value : data;
if (cacheOpts && cacheOpts.cacheTimeout) {
if (!data.modifiedByCacheMechanism) {
// If data has no cache, but we want to check the timeout, consider the data as outdated.
// this should be a really rare case
return null;
}
const currentTime = Date.now();
if (currentTime - data.time > cacheOpts.cacheTimeout) {
return null;
}
}
return dataToReturn;
}
return null;
};
return this.$lockService.executeActionWithLock(action, this.lockFilePath);
}
async saveSetting(key, value, cacheOpts) {
const settingObject = {};
settingObject[key] = value;
return this.saveSettings(settingObject, cacheOpts);
}
async removeSetting(key) {
const action = async () => {
await this.loadUserSettingsFile();
delete this.jsonSettingsData[key];
await this.saveSettings();
};
return this.$lockService.executeActionWithLock(action, this.lockFilePath);
}
saveSettings(data, cacheOpts) {
const action = async () => {
await this.loadUserSettingsFile();
this.jsonSettingsData = this.jsonSettingsData || {};
_(data)
.keys()
.each((propertyName) => {
this.jsonSettingsData[propertyName] =
cacheOpts &&
cacheOpts.useCaching &&
!data[propertyName].modifiedByCacheMechanism
? {
time: Date.now(),
value: data[propertyName],
modifiedByCacheMechanism: true,
}
: data[propertyName];
});
this.$fs.writeJson(this.jsonSettingsFilePath, this.jsonSettingsData);
};
return this.$lockService.executeActionWithLock(action, this.lockFilePath);
}
async loadUserSettingsFile() {
if (!this.jsonSettingsData) {
await this.loadUserSettingsData();
}
}
async loadUserSettingsData() {
if (!this.$fs.exists(this.jsonSettingsFilePath)) {
const unexistingDirs = this.getUnexistingDirectories(this.jsonSettingsFilePath);
this.$fs.writeFile(this.jsonSettingsFilePath, null);
// when running under 'sudo' we create the <path to home dir>/.local/share/.nativescript-cli dir with root as owner
// and other Applications cannot access this directory anymore. (bower/heroku/etc)
if (process.env.SUDO_USER) {
for (const dir of unexistingDirs) {
await this.$fs.setCurrentUserAsOwner(dir, process.env.SUDO_USER);
}
}
}
const data = this.$fs.readText(this.jsonSettingsFilePath);
try {
this.jsonSettingsData = (0, helpers_1.parseJson)(data);
}
catch (err) {
this.$logger.trace(`Error while trying to parseJson ${data} data from ${this.jsonSettingsFilePath} file. Err is: ${err}`);
this.$fs.deleteFile(this.jsonSettingsFilePath);
}
}
getUnexistingDirectories(filePath) {
const unexistingDirs = [];
let currentDir = path.join(filePath, "..");
while (true) {
// this directory won't be created.
if (this.$fs.exists(currentDir)) {
break;
}
unexistingDirs.push(currentDir);
currentDir = path.join(currentDir, "..");
}
return unexistingDirs;
}
}
exports.JsonFileSettingsService = JsonFileSettingsService;
yok_1.injector.register("jsonFileSettingsService", JsonFileSettingsService, false);
//# sourceMappingURL=json-file-settings-service.js.map