@sap/adp-abap
Version:
abap service for all yeoman generators
223 lines • 11.3 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const https_1 = __importDefault(require("https"));
const path = __importStar(require("path"));
const fast_xml_parser_1 = require("fast-xml-parser");
const axios_1 = __importDefault(require("axios"));
const adp_tooling_1 = require("@sap-ux/adp-tooling");
const axios_extension_1 = require("@sap-ux/axios-extension");
const adp_common_1 = require("@sap/adp-common");
const UNDEPLOY_FEATURE_VERSION = "1.105";
class DeploymentManager {
constructor(projectData, targetDestination) {
this.isRunningInBAS = adp_common_1.EnvironmentUtils.isRunningInBAS();
this.projectData = projectData;
this.baseUrl = targetDestination.url;
this.client = targetDestination.client;
this.auth = targetDestination.auth;
this.isUsingProxy = targetDestination.isUsingProxy || false;
const config = this.getConfig();
const provider = (0, axios_extension_1.createForAbap)(Object.assign(Object.assign({}, config), { ignoreCertErrors: true }));
this.service = provider.getLayeredRepository();
}
getConfig() {
if (!this.isRunningInBAS || this.isUsingProxy) {
return {
baseURL: this.baseUrl,
auth: this.auth,
params: {
"sap-client": this.client
},
httpsAgent: new https_1.default.Agent({
rejectUnauthorized: false,
requestCert: false
})
};
}
return { baseURL: this.baseUrl, auth: this.auth };
}
deploy(packageName, transport) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const projectPath = this.projectData.path;
if ((0, adp_tooling_1.isTypescriptSupported)(projectPath)) {
yield (0, adp_tooling_1.runBuild)(projectPath);
}
yield adp_common_1.Workspace.archive(projectPath, path.join(projectPath, "webapp.zip"));
const response = yield this.service.deploy(path.join(projectPath, "webapp.zip"), {
namespace: this.projectData.namespace,
package: packageName,
transport
});
const result = response.data && JSON.parse(response.data);
this.logSystemResponse(result);
if (response.status !== 200) {
const text = ((_a = result.messages) === null || _a === void 0 ? void 0 : _a.pop().text) || "Error deploying project";
throw new Error(text);
}
adp_common_1.Workspace.deleteFile(this.projectData.path, "webapp.zip");
});
}
undeploy(transport) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.service.undeploy({
namespace: this.projectData.namespace,
transport
});
if (response.status !== 200) {
const result = response.data && JSON.parse(response.data);
this.logSystemResponse(result);
const text = ((_a = result.messages) === null || _a === void 0 ? void 0 : _a.pop().text) || "Error undeploying project";
throw new Error(text);
}
});
}
deploymentData() {
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.service.isExistingVariant(this.projectData.namespace);
if (![200, 404].includes(response.status)) {
throw new Error(`Error checking if already deployed: ${response.status} ${response.statusText}`);
}
const isDeployed = response.status === 200;
if (!isDeployed) {
return {
isDeployed
};
}
const xml = `
<?xml version="1.0" encoding="UTF-8" ?>
<asx:abap version="1.0" xmlns:asx="http://www.sap.com/abapxml">
<asx:values>
<DATA>
<OPERATION>I</OPERATION>
<URI>/sap/bc/adt/ui_flex_dta_folder?name=${this.projectData.namespace}&layer=CUSTOMER_BASE&package=undefined</URI>
</DATA>
</asx:values>
</asx:abap>`;
const config = {
method: "post",
url: `${this.baseUrl}/sap/bc/adt/cts/transportchecks`,
data: xml
};
if (this.isRunningInBAS) {
config.headers = {
"x-csrf-token": response.headers["x-csrf-token"],
"cookie": response.headers["set-cookie"][0].split(";")[0],
"Content-Type": "application/vnd.sap.as+xml; charset=UTF-8; dataname=com.sap.adt.transport.service.checkData"
};
}
else {
config.withCredentials = true;
config.auth = this.auth;
config.headers = {
"Accept": "application/vnd.sap.as+xml; dataname=com.sap.adt.transport.service.checkData",
"Content-Type": "application/vnd.sap.as+xml; charset=UTF-8; dataname=com.sap.adt.transport.service.checkData",
"X-sap-adt-profiling": "server-time",
"cookie": response.headers["set-cookie"],
"x-csrf-token": response.headers["x-csrf-token"]
};
config.params = {
"sap-client": this.client
};
config.httpsAgent = new https_1.default.Agent({ rejectUnauthorized: false, requestCert: false, keepAlive: true });
}
const axiosResponse = yield axios_1.default.request(config);
if (axiosResponse.status !== 200) {
throw new Error(`Error getting transport request number: ${axiosResponse.status} ${axiosResponse.statusText}`);
}
const parser = new fast_xml_parser_1.XMLParser();
const xmlResult = parser.parse(axiosResponse.data);
return {
isDeployed,
isUndeploymentSupported: yield this.isUndeploymentSupported(),
transport: ((_a = xmlResult["asx:abap"]["asx:values"].DATA.LOCKS.CTS_OBJECT_LOCK) === null || _a === void 0 ? void 0 : _a.LOCK_HOLDER.REQ_HEADER.TRKORR) || "",
package: xmlResult["asx:abap"]["asx:values"].DATA.DEVCLASS || "$TMP"
};
}
catch (error) {
let errorMessage = "";
if (error.cause) {
errorMessage = `Failed to connect to backend system to get deployment data: ${(_b = error.cause) === null || _b === void 0 ? void 0 : _b.status} ${(_c = error.cause) === null || _c === void 0 ? void 0 : _c.statusText}`;
}
else if (error.response) {
errorMessage = `Failed to connect to backend system to get deployment data: ${(_d = error.response) === null || _d === void 0 ? void 0 : _d.status} ${(_e = error.response) === null || _e === void 0 ? void 0 : _e.statusText}`;
}
else {
errorMessage = `Failed to connect to backend system to get deployment data. ${error === null || error === void 0 ? void 0 : error.message}`;
}
throw new Error(errorMessage);
}
});
}
isUndeploymentSupported() {
return __awaiter(this, void 0, void 0, function* () {
const version = yield this.getSystemUI5Version();
return version.toLowerCase().includes("snapshot") || adp_common_1.UI5VersionsUtils.isFeatureSupportedVersion(UNDEPLOY_FEATURE_VERSION, version);
});
}
getSystemUI5Version() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.service.get(`${this.baseUrl}/sap/bc/adt/filestore/ui5-bsp/ui5-rt-version`);
if (response.status !== 200) {
throw new Error(`Unable to get the UI5 version of the system. ${response.status} ${response.statusText}`);
}
if (/^1\.\d{2,3}\.\d{1,2}\.*/.test(response === null || response === void 0 ? void 0 : response.data)) {
return response.data;
}
throw new Error(`Unable to parse the UI5 version of the system: ${response.data}`);
});
}
logSystemResponse(result) {
var _a, _b;
if (!result) {
return;
}
const allMessages = JSON.stringify(result === null || result === void 0 ? void 0 : result.messages) ? JSON.stringify(result === null || result === void 0 ? void 0 : result.messages) : "";
(_a = adp_common_1.Logger.getLogger) === null || _a === void 0 ? void 0 : _a.log("\nSystem response result:");
allMessages.split(",").forEach((element) => {
var _a;
(_a = adp_common_1.Logger.getLogger) === null || _a === void 0 ? void 0 : _a.log(element);
});
(_b = adp_common_1.Logger.getLogger) === null || _b === void 0 ? void 0 : _b.log("END - system response result.\n");
}
}
exports.default = DeploymentManager;
//# sourceMappingURL=DeploymentManager.js.map