@hkvstore/taco-cli
Version:
taco-cli is a command-line interface for rapid Apache Cordova development (forked from Microsoft taco-cli)
103 lines (101 loc) • 3.98 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
/// <reference path="../../../typings/node.d.ts" />
/// <reference path="../../../typings/tacoUtils.d.ts" />
/// <reference path="../../../typings/Q.d.ts" />
/// <reference path="../../../typings/nopt.d.ts" />
;
var fs = require("fs");
var path = require("path");
var Q = require("q");
var util = require("util");
var TacoErrorCodes = require("../tacoErrorCodes");
var errorHelper = require("../tacoErrorHelper");
var tacoUtils = require("taco-utils");
var utils = tacoUtils.UtilHelper;
/*
* A static class which is responsible for dealing with the TacoSettings.json file
*/
var Settings = (function () {
function Settings() {
}
Object.defineProperty(Settings, "settingsFile", {
get: function () {
return path.join(utils.tacoHome, Settings.SETTINGS_FILENAME);
},
enumerable: true,
configurable: true
});
/*
* Load data from TACO_HOME/TacoSettings.json
*/
Settings.loadSettings = function () {
if (Settings.settings) {
return Q(Settings.settings);
}
try {
Settings.settings = JSON.parse(fs.readFileSync(Settings.settingsFile, "utf8"));
return Q(Settings.settings);
}
catch (e) {
if (e.code === "ENOENT") {
// File doesn't exist, no need for a stack trace.
// The error message will instruct the user to run "taco setup" to resolve the issue.
return Q.reject(errorHelper.get(TacoErrorCodes.TacoSettingsFileDoesNotExist));
}
else {
// Couldn't open the file, not sure why, so we'll keep the error around for the user
return Q.reject(errorHelper.wrap(TacoErrorCodes.CommandBuildTacoSettingsNotFound, e));
}
}
};
Settings.saveSettings = function (settings) {
// save to TACO_HOME/TacoSettings.json and store as the cached version
Settings.settings = settings;
utils.createDirectoryIfNecessary(utils.tacoHome);
fs.writeFileSync(Settings.settingsFile, JSON.stringify(settings));
return Q(settings);
};
/*
* Construct the base URL for the given build server
*/
Settings.getRemoteServerUrl = function (server) {
return util.format("http%s://%s:%d/%s", server.secure ? "s" : "", server.host, server.port, server.mountPoint);
};
/*
* Update settings from TACO_HOME/TacoSettings.json (It loads the settings, give you a chance to update them, and then it saves them again)
*/
Settings.updateSettings = function (updateFunction) {
var _this = this;
return this.loadSettings()
.fail(function (error) {
if (error.errorCode === TacoErrorCodes.TacoSettingsFileDoesNotExist) {
// If it fails because the file doesn't exist, we just return some empty settings and we continue...
return {};
}
else {
/* If it fails for other reason, we rethrow the exception
(we don't want to override a file if we are not sure about the contents) */
throw error;
}
})
.then(function (settings) {
updateFunction(settings);
return _this.saveSettings(settings);
});
};
/**
* Remove cached settings object, for use in tests
*/
Settings.forgetSettings = function () {
Settings.settings = null;
};
Settings.loadSettingsOrReturnEmpty = function () {
return Settings.loadSettings().fail(function () { return { remotePlatforms: {} }; });
};
Settings.settings = null;
Settings.SETTINGS_FILENAME = "TacoSettings.json";
return Settings;
}());
module.exports = Settings;
//# sourceMappingURL=settings.js.map