Building TypeScript scaffolding for cli

This commit is contained in:
Kasra Bigdeli
2019-01-02 23:33:07 -08:00
parent 574d490c9e
commit caf6e6b574
45 changed files with 2146 additions and 40 deletions
+164
View File
@@ -0,0 +1,164 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var MainApi = require("./MainApi");
var MachineHelper = require("../helpers/MachineHelper");
var _a = require("../utils/constants"), DEFAULT_BRANCH_TO_PUSH = _a.DEFAULT_BRANCH_TO_PUSH, DEFAULT_APP_NAME = _a.DEFAULT_APP_NAME;
var DeployApi = /** @class */ (function () {
function DeployApi() {
this.machineToDeploy = {};
this.branchToPush = DEFAULT_BRANCH_TO_PUSH;
this.appName = DEFAULT_APP_NAME;
this.appUrl = "";
}
DeployApi.prototype.setMachineToDeploy = function (machineToDeploy) {
this.machineToDeploy = machineToDeploy;
};
DeployApi.prototype.updateMachineToDeploy = function (machineToDeploy) {
var possibleMachine = {};
// Look machine by host
if (machineToDeploy.startsWith("http")) {
possibleMachine = MachineHelper.machines.find(function (machine) { return machine.baseUrl === machineToDeploy; });
}
else {
// Look machine by name
possibleMachine = MachineHelper.machines.find(function (machine) { return machine.name === machineToDeploy; });
}
this.machineToDeploy = possibleMachine;
};
DeployApi.prototype.setBranchToPush = function (branchToPush) {
this.branchToPush = branchToPush;
};
DeployApi.prototype.setAppName = function (appName) {
this.appName = appName;
this.setAppUrl();
};
DeployApi.prototype.setAppUrl = function () {
this.appUrl = this.machineToDeploy.baseUrl
.replace("//captain.", "//" + this.appName + ".")
.replace("https://", "http://");
};
DeployApi.prototype.fetchBuildLogs = function () {
return __awaiter(this, void 0, void 0, function () {
var _a, authToken, baseUrl, customOptions, data, e_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this.machineToDeploy, authToken = _a.authToken, baseUrl = _a.baseUrl;
customOptions = {
headers: {
"x-captain-auth": authToken
}
};
return [4 /*yield*/, MainApi.get(baseUrl + "/api/v1/user/appData/" + this.appName, customOptions)];
case 1:
data = _b.sent();
return [2 /*return*/, data];
case 2:
e_1 = _b.sent();
throw e_1;
case 3: return [2 /*return*/];
}
});
});
};
DeployApi.prototype.sendFile = function (sourceFile, gitHash) {
return __awaiter(this, void 0, void 0, function () {
var _a, authToken, baseUrl, url, form, options, data, e_2;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
_a = this.machineToDeploy, authToken = _a.authToken, baseUrl = _a.baseUrl;
url = baseUrl + "/api/v1/user/appData/" + this.appName + "/?detached=1";
form = {
sourceFile: sourceFile,
gitHash: gitHash
};
options = {
headers: {
"x-captain-auth": authToken
}
};
return [4 /*yield*/, MainApi.postWithFile(url, form, options)];
case 1:
data = _b.sent();
return [2 /*return*/, data];
case 2:
e_2 = _b.sent();
throw e_2;
case 3: return [2 /*return*/];
}
});
});
};
// This is not moved to LoginAPI since it's related only for machineToDeploy
DeployApi.prototype.isAuthTokenValid = function () {
return __awaiter(this, void 0, void 0, function () {
var url, currentToken, options, response, data, e_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
if (!this.machineToDeploy)
return [2 /*return*/, false];
url = this.machineToDeploy.baseUrl + "/api/v1/user/appDefinitions/";
currentToken = this.machineToDeploy.authToken;
options = {
headers: {
"x-captain-auth": currentToken
}
};
return [4 /*yield*/, MainApi.get(url, options)];
case 1:
response = _a.sent();
data = JSON.parse(response);
// Tolken is not valid
if (data.status === 1106 || data.status === 1105) {
return [2 /*return*/, false];
}
return [2 /*return*/, true];
case 2:
e_3 = _a.sent();
throw e_3;
case 3: return [2 /*return*/];
}
});
});
};
return DeployApi;
}());
module.exports = new DeployApi();
+105
View File
@@ -0,0 +1,105 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var MainApi = require("./MainApi");
var SystemApi = require("./SystemApi");
var DEFAULT_PASSWORD = require("../utils/constants").DEFAULT_PASSWORD;
var LoginApi = /** @class */ (function () {
function LoginApi() {
this.token = "";
this.oldPassword = DEFAULT_PASSWORD;
}
LoginApi.prototype.setOldPassword = function (newPassword) {
this.oldPassword = newPassword;
};
LoginApi.prototype.setToken = function (newToken) {
this.token = newToken;
};
LoginApi.prototype.loginMachine = function (baseUrl, password) {
return __awaiter(this, void 0, void 0, function () {
var data, dataAsObject, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, MainApi.post(baseUrl + "/api/v1/login", { password: password })];
case 1:
data = _a.sent();
dataAsObject = JSON.parse(data);
if (dataAsObject) {
this.setToken(dataAsObject.token);
}
return [2 /*return*/, data];
case 2:
e_1 = _a.sent();
throw e_1;
case 3: return [2 /*return*/];
}
});
});
};
LoginApi.prototype.changePass = function (baseUrl, newPassword) {
return __awaiter(this, void 0, void 0, function () {
var customOptions, form, data, e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
customOptions = {
headers: {
"x-captain-auth": LoginApi.token
}
};
form = {
oldPassword: this.oldPassword,
newPassword: newPassword
};
return [4 /*yield*/, MainApi.post(baseUrl + "/api/v1/user/changepassword/", form, customOptions)];
case 1:
data = _a.sent();
this.setToken(data.token);
SystemApi.setIpAddressOfServer(baseUrl);
return [2 /*return*/, data];
case 2:
e_2 = _a.sent();
throw e_2;
case 3: return [2 /*return*/];
}
});
});
};
return LoginApi;
}());
module.exports = new LoginApi();
+61
View File
@@ -0,0 +1,61 @@
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var Request = require("request-promise");
var MainApi = /** @class */ (function () {
function MainApi() {
this.sharedOptions = {
headers: {
"x-namespace": "captain"
}
};
}
MainApi.prototype._buildOptions = function (options) {
if (!options)
return this.sharedOptions;
if (options.headers) {
options.headers = Object.assign({}, this.sharedOptions.headers, options.headers);
}
return Object.assign({}, this.sharedOptions, options);
};
MainApi.prototype.get = function (url, options) {
var overrideOptions = this._buildOptions(options);
var optionsToSend = __assign({}, overrideOptions, { url: url, method: "GET" });
return Request(optionsToSend);
};
MainApi.prototype.post = function (url, form, options) {
var overrideOptions = this._buildOptions(options);
var optionsToSend = __assign({}, overrideOptions, { url: url, method: "POST", form: form });
return Request(optionsToSend);
};
MainApi.prototype.postWithFile = function (url, formData, options) {
var overrideOptions = this._buildOptions(options);
var optionsToSend = __assign({}, overrideOptions, { url: url, method: "POST", formData: formData });
return Request(optionsToSend);
};
MainApi.prototype.put = function (url, form, options) {
var overrideOptions = this._buildOptions(options);
var optionsToSend = __assign({}, overrideOptions, { url: url, method: "PUT", form: form });
return Request(optionsToSend);
};
MainApi.prototype.patch = function (url, form, options) {
var overrideOptions = this._buildOptions(options);
var optionsToSend = __assign({}, overrideOptions, { url: url, method: "PATCH", form: form });
return Request(optionsToSend);
};
MainApi.prototype.delete = function (url, options) {
var overrideOptions = this._buildOptions(options);
var optionsToSend = __assign({}, overrideOptions, { url: url, method: "DELETE" });
return Request(optionsToSend);
};
return MainApi;
}());
module.exports = new MainApi();
+134
View File
@@ -0,0 +1,134 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var MainApi = require("./MainApi");
var LoginApi = require("./LoginApi");
// const spinnerUtil = require("../../utils/spinner")
var SystemApi = /** @class */ (function () {
function SystemApi() {
this.ipAddressOfServer = "";
this.customDomainFromUser = "";
this.newPasswordFirstTry = "";
}
SystemApi.prototype.setCustomDomainFromUser = function (newCustomDomainFromUser) {
this.customDomainFromUser = newCustomDomainFromUser;
};
SystemApi.prototype.setIpAddressOfServer = function (newIpAddress) {
this.ipAddressOfServer = newIpAddress.trim();
};
SystemApi.prototype.setCustomDomain = function (baseUrl, rootDomain) {
return __awaiter(this, void 0, void 0, function () {
var customOptions, data, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
customOptions = {
headers: {
"x-captain-auth": LoginApi.token
}
};
return [4 /*yield*/, MainApi.post(baseUrl + "/api/v1/user/system/changerootdomain/", {
rootDomain: rootDomain
}, customOptions)];
case 1:
data = _a.sent();
return [2 /*return*/, data];
case 2:
e_1 = _a.sent();
throw e_1;
case 3: return [2 /*return*/];
}
});
});
};
SystemApi.prototype.enableHttps = function (baseUrl, emailAddress) {
return __awaiter(this, void 0, void 0, function () {
var customOptions, data, e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
customOptions = {
headers: {
"x-captain-auth": LoginApi.token
}
};
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, MainApi.post(baseUrl + "/api/v1/user/system/enablessl/", {
emailAddress: emailAddress
}, customOptions)];
case 2:
data = _a.sent();
return [2 /*return*/, data];
case 3:
e_2 = _a.sent();
throw e_2;
case 4: return [2 /*return*/];
}
});
});
};
SystemApi.prototype.forceHttps = function (baseUrl, isEnabled) {
if (isEnabled === void 0) { isEnabled = true; }
return __awaiter(this, void 0, void 0, function () {
var customOptions, data, e_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
customOptions = {
headers: {
"x-captain-auth": LoginApi.token
}
};
return [4 /*yield*/, MainApi.post(baseUrl + "/api/v1/user/system/forcessl/", {
isEnabled: isEnabled
}, customOptions)];
case 1:
data = _a.sent();
return [2 /*return*/, data];
case 2:
e_3 = _a.sent();
throw e_3;
case 3: return [2 /*return*/];
}
});
});
};
return SystemApi;
}());
module.exports = new SystemApi();