UNPKG

@sasjs/cli

Version:

Command line interface for SASjs

350 lines (349 loc) 18.8 kB
"use strict"; 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 __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 (g && (g = 0, op[0] && (_ = 0)), _) 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 __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runSasCode = void 0; var path_1 = __importDefault(require("path")); var node_1 = require("@sasjs/adapter/node"); var config_1 = require("../../utils/config"); var utils_1 = require("@sasjs/utils"); var __1 = require("../"); var utils_2 = require("../../utils/"); var axios_1 = __importDefault(require("axios")); var internal_1 = require("../compile/internal"); var saveLog_1 = require("../../utils/saveLog"); var parseSourceFile_1 = require("../../utils/parseSourceFile"); /** * Runs SAS code from a given file on the specified target. * @param {Target} target - the target to run the SAS code on. * @param {string} filePath - the path to the file containing SAS code. * @param {boolean} compile - compiles sas file present at 'filePath' before running code. * @param {string} logFile - (Optional) Path to log file. */ function runSasCode(target, filePath, compile, logFile, source) { var _a; if (compile === void 0) { compile = false; } return __awaiter(this, void 0, void 0, function () { var isTempFile, sourcefilePathParts, sourceFolderPath, sasFilePath, sasFileContent, linesToExecute, macroVars, macroVarStatements, _b; var _c; return __generator(this, function (_d) { switch (_d.label) { case 0: isTempFile = false; if (!isUrl(filePath)) return [3 /*break*/, 2]; ; return [4 /*yield*/, createFileFromUrl(filePath)]; case 1: (_c = _d.sent(), isTempFile = _c.isTempFile, filePath = _c.tempFilePath); _d.label = 2; case 2: if (!compile) return [3 /*break*/, 5]; if (!/\.sas$/i.test(filePath)) return [3 /*break*/, 4]; sourcefilePathParts = path_1.default.normalize(filePath).split(path_1.default.sep); sourcefilePathParts.splice(-1, 1); sourceFolderPath = sourcefilePathParts.join(path_1.default.sep); return [4 /*yield*/, (0, __1.compileSingleFile)(target, 'identify', filePath, (0, internal_1.getDestinationServicePath)(sourceFolderPath), true)]; case 3: (filePath = (_d.sent()).destinationPath); (_a = process.logger) === null || _a === void 0 ? void 0 : _a.success("File Compiled and placed at: ".concat(filePath, " .")); return [3 /*break*/, 5]; case 4: process.logger.info('Compile flag has no effect on files with extension type other than sas'); _d.label = 5; case 5: sasFilePath = (0, utils_1.getAbsolutePath)(filePath, process.currentDir); return [4 /*yield*/, (0, utils_1.readFile)(sasFilePath)]; case 6: sasFileContent = _d.sent(); linesToExecute = sasFileContent.replace(/\r\n/g, '\n').split('\n'); if (!source) return [3 /*break*/, 8]; return [4 /*yield*/, (0, parseSourceFile_1.parseSourceFile)(source)]; case 7: macroVars = _d.sent(); macroVarStatements = (0, utils_2.convertToSASStatements)(macroVars).split('\n'); linesToExecute = __spreadArray(__spreadArray([], __read(macroVarStatements), false), __read(linesToExecute), false); _d.label = 8; case 8: if (!isTempFile) return [3 /*break*/, 10]; return [4 /*yield*/, (0, utils_1.deleteFile)(filePath)]; case 9: _d.sent(); _d.label = 10; case 10: _b = target.serverType; switch (_b) { case utils_1.ServerType.SasViya: return [3 /*break*/, 11]; case utils_1.ServerType.Sas9: return [3 /*break*/, 13]; case utils_1.ServerType.Sasjs: return [3 /*break*/, 15]; } return [3 /*break*/, 17]; case 11: return [4 /*yield*/, executeOnSasViya(filePath, target, linesToExecute, logFile)]; case 12: return [2 /*return*/, _d.sent()]; case 13: return [4 /*yield*/, executeOnSas9(target, linesToExecute, logFile)]; case 14: return [2 /*return*/, _d.sent()]; case 15: return [4 /*yield*/, executeOnSasJS(filePath, target, linesToExecute, logFile)]; case 16: return [2 /*return*/, _d.sent()]; case 17: return [2 /*return*/]; } }); }); } exports.runSasCode = runSasCode; function executeOnSasViya(filePath, target, linesToExecute, logFile) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var _e, sasjs, authConfig, contextName, log, buildDestinationResultsFolder, createdFilePath; var _this = this; return __generator(this, function (_f) { switch (_f.label) { case 0: (_a = process.logger) === null || _a === void 0 ? void 0 : _a.info("Sending ".concat(path_1.default.basename(filePath), " to SAS server for execution.")); return [4 /*yield*/, (0, config_1.getSASjsAndAuthConfig)(target)]; case 1: _e = _f.sent(), sasjs = _e.sasjs, authConfig = _e.authConfig; contextName = target.contextName || sasjs.getSasjsConfig().contextName; return [4 /*yield*/, sasjs .executeScript({ fileName: path_1.default.basename(filePath), linesOfCode: linesToExecute, contextName: contextName, authConfig: authConfig }) .catch(function (err) { return __awaiter(_this, void 0, void 0, function () { var log; return __generator(this, function (_a) { switch (_a.label) { case 0: log = err.log; if (!log) throw new node_1.ErrorResponse('We were not able to fetch the log this time.'); return [4 /*yield*/, createOutputFile(log, logFile)]; case 1: _a.sent(); throw new node_1.ErrorResponse('Find more error details in the log file.'); } }); }); })]; case 2: log = (_f.sent()).log; (_b = process.logger) === null || _b === void 0 ? void 0 : _b.success('Job execution completed!'); buildDestinationResultsFolder = process.sasjsConstants.buildDestinationResultsFolder; (_c = process.logger) === null || _c === void 0 ? void 0 : _c.info("Creating log file in ".concat(buildDestinationResultsFolder, " .")); return [4 /*yield*/, createOutputFile(log, logFile)]; case 3: createdFilePath = _f.sent(); (_d = process.logger) === null || _d === void 0 ? void 0 : _d.success("Log file has been created at ".concat(createdFilePath, " .")); return [2 /*return*/, { log: log }]; } }); }); } function executeOnSas9(target, linesToExecute, logFile) { var _a; return __awaiter(this, void 0, void 0, function () { var _b, sasjs, authConfigSas9, userName, password, executionResult; var _this = this; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, (0, config_1.getSASjsAndAuthConfig)(target)]; case 1: _b = _c.sent(), sasjs = _b.sasjs, authConfigSas9 = _b.authConfigSas9; userName = authConfigSas9.userName; password = (0, utils_1.decodeFromBase64)(authConfigSas9.password); return [4 /*yield*/, sasjs .executeScript({ linesOfCode: linesToExecute, authConfigSas9: { userName: userName, password: password } }) .catch(function (err) { return __awaiter(_this, void 0, void 0, function () { var log; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(err && err.payload && err.payload.log)) return [3 /*break*/, 2]; log = err.payload.log; return [4 /*yield*/, createOutputFile(log, logFile)]; case 1: _a.sent(); throw new node_1.ErrorResponse('Find more error details in the log file.'); case 2: if (err && err.errorCode === 404) { (0, utils_2.displaySasjsRunnerError)(userName); } throw err; } }); }); })]; case 2: executionResult = _c.sent(); (_a = process.logger) === null || _a === void 0 ? void 0 : _a.success('Job execution completed!'); return [4 /*yield*/, createOutputFile(executionResult || '', logFile)]; case 3: _c.sent(); return [2 /*return*/, { log: executionResult }]; } }); }); } function executeOnSasJS(filePath, target, linesToExecute, logFile) { var _a; return __awaiter(this, void 0, void 0, function () { var sasjs, authConfig, _b, fileExtension, log; return __generator(this, function (_c) { switch (_c.label) { case 0: sasjs = (0, config_1.getSASjs)(target); return [4 /*yield*/, (0, utils_2.isSasJsServerInServerMode)(target)]; case 1: if (!(_c.sent())) return [3 /*break*/, 3]; return [4 /*yield*/, (0, config_1.getAuthConfig)(target)]; case 2: _b = _c.sent(); return [3 /*break*/, 4]; case 3: _b = undefined; _c.label = 4; case 4: authConfig = _b; fileExtension = path_1.default.extname(filePath).slice(1); return [4 /*yield*/, sasjs.executeScript({ linesOfCode: linesToExecute, runTime: fileExtension, authConfig: authConfig })]; case 5: log = (_c.sent()).log; if (!log) { throw new node_1.ErrorResponse('We were not able to fetch the log this time.'); } (_a = process.logger) === null || _a === void 0 ? void 0 : _a.success('Job execution completed!'); return [4 /*yield*/, createOutputFile(log, logFile)]; case 6: _c.sent(); return [2 /*return*/, { log: log }]; } }); }); } function createOutputFile(log, logFilePath, silent) { return __awaiter(this, void 0, void 0, function () { var timestamp, buildDestinationResultsFolder; return __generator(this, function (_a) { switch (_a.label) { case 0: timestamp = (0, utils_1.generateTimestamp)(); buildDestinationResultsFolder = process.sasjsConstants.buildDestinationResultsFolder; if (!logFilePath) { logFilePath = path_1.default.join(buildDestinationResultsFolder, "sasjs-run-".concat(timestamp, ".log")); } return [4 /*yield*/, (0, saveLog_1.saveLog)(log || '', logFilePath, '', silent)]; case 1: _a.sent(); return [2 /*return*/, logFilePath]; } }); }); } function isUrl(filePath) { return filePath.startsWith('https://') || filePath.startsWith('http://'); } function createFileFromUrl(url) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, axios_1.default .get(url) .then(function (res) { return __awaiter(_this, void 0, void 0, function () { var invalidSasError, content, urlWithoutQueryParams, fileExtension, tempFilePath; return __generator(this, function (_a) { switch (_a.label) { case 0: invalidSasError = process.sasjsConstants.invalidSasError; if (typeof res.data !== 'string') { throw new Error(invalidSasError); } content = res.data.trim(); if (content && content.startsWith('<')) { throw new Error(invalidSasError); } urlWithoutQueryParams = url.split('?')[0]; fileExtension = path_1.default.extname(urlWithoutQueryParams); tempFilePath = path_1.default.join(process.projectDir, "temp-".concat(Date.now()).concat(fileExtension)); return [4 /*yield*/, (0, utils_1.createFile)(tempFilePath, content)]; case 1: _a.sent(); return [2 /*return*/, { isTempFile: true, tempFilePath: tempFilePath }]; } }); }); }) .catch(function (err) { throw new Error("".concat(err.message, "\nUrl: ").concat(url)); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }