UNPKG

@sasjs/cli

Version:

Command line interface for SASjs

215 lines (214 loc) 13.1 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.createAssetServices = void 0; var path_1 = __importDefault(require("path")); var utils_1 = require("@sasjs/utils"); var lodash_uniqby_1 = __importDefault(require("lodash.uniqby")); var getAssetPath_1 = require("./getAssetPath"); var sas9_1 = require("./sas9"); /** * Creates all the asset services for web streaming. * Also prepares an asset-path Map based on server type. * @param {Target} target the target to create asset service. * @param {string} destinationPath the location of web streaming files. * @param {StreamConfig} streamConfig stream configuration to be used. * @returns {AssetPathMap[]} list of all the sources specified along server based paths. */ var createAssetServices = function (target, destinationPath, _a) { var webSourcePath = _a.webSourcePath, streamWebFolder = _a.streamWebFolder, assetPaths = _a.assetPaths; return __awaiter(void 0, void 0, void 0, function () { var assetPathMap; return __generator(this, function (_b) { switch (_b.label) { case 0: assetPathMap = []; return [4 /*yield*/, (0, utils_1.asyncForEach)(__spreadArray([path_1.default.join(process.projectDir, webSourcePath)], __read((assetPaths !== null && assetPaths !== void 0 ? assetPaths : [])), false), function (assetPath) { return __awaiter(void 0, void 0, void 0, function () { var fullAssetPath, assetPathExists, assetPathMapNested; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: fullAssetPath = path_1.default.isAbsolute(assetPath) ? assetPath : path_1.default.isAbsolute(webSourcePath) ? path_1.default.join(webSourcePath, assetPath) : path_1.default.join(process.projectDir, webSourcePath, assetPath); return [4 /*yield*/, (0, utils_1.folderExists)(fullAssetPath)]; case 1: assetPathExists = _b.sent(); if (!assetPathExists) { (_a = process.logger) === null || _a === void 0 ? void 0 : _a.warn("Assets path '".concat(fullAssetPath, "' present in 'streamConfig' doesn't exist.")); return [2 /*return*/]; } return [4 /*yield*/, createAssetsServicesNested(target, streamWebFolder, fullAssetPath, destinationPath)]; case 2: assetPathMapNested = _b.sent(); assetPathMap.push.apply(assetPathMap, __spreadArray([], __read(assetPathMapNested), false)); return [2 /*return*/]; } }); }); })]; case 1: _b.sent(); return [2 /*return*/, (0, lodash_uniqby_1.default)(assetPathMap, 'source')]; } }); }); }; exports.createAssetServices = createAssetServices; /** * Creates all the asset services for web streaming for a specific folder. * Also prepares an asset-path Map based on server type. * @param {Target} target the target to create asset service. * @param {string} streamWebFolder the location of web streaming files. * @param {string} fullAssetPath path of the specific folder used to create all services for. * @param {string} destinationPath path of the destination folder for services. * @returns {AssetPathMap[]} list of all the sources specified along server based paths. */ var createAssetsServicesNested = function (target, streamWebFolder, fullAssetPath, destinationPath) { return __awaiter(void 0, void 0, void 0, function () { var assetPathMap, filePaths, folderNames; return __generator(this, function (_a) { switch (_a.label) { case 0: assetPathMap = []; return [4 /*yield*/, (0, utils_1.listFilesInFolder)(fullAssetPath)]; case 1: filePaths = _a.sent(); return [4 /*yield*/, (0, utils_1.asyncForEach)(filePaths, function (filePath) { return __awaiter(void 0, void 0, void 0, function () { var fullFileName, fileName, fileExtension, sourcePath, assetServiceUrl, _a, fileName_1; return __generator(this, function (_b) { switch (_b.label) { case 0: fullFileName = path_1.default.basename(filePath); fileName = fullFileName.substring(0, fullFileName.lastIndexOf('.')); fileExtension = path_1.default .basename(filePath) .substring(fullFileName.lastIndexOf('.') + 1, fullFileName.length); if (!(fileName && fileExtension)) return [3 /*break*/, 7]; sourcePath = path_1.default.join(fullAssetPath, filePath); assetServiceUrl = void 0; _a = target.serverType; switch (_a) { case utils_1.ServerType.SasViya: return [3 /*break*/, 1]; case utils_1.ServerType.Sas9: return [3 /*break*/, 3]; } return [3 /*break*/, 5]; case 1: return [4 /*yield*/, (0, utils_1.copy)(sourcePath, path_1.default.join(destinationPath, fullFileName))]; case 2: _b.sent(); assetServiceUrl = (0, getAssetPath_1.getAssetPath)(target.appLoc, target.serverType, streamWebFolder, fullFileName); return [3 /*break*/, 6]; case 3: return [4 /*yield*/, (0, sas9_1.generateAssetService)(sourcePath, destinationPath)]; case 4: fileName_1 = _b.sent(); assetServiceUrl = (0, getAssetPath_1.getAssetPath)(target.appLoc, target.serverType, streamWebFolder, fileName_1.replace('.sas', '')); return [3 /*break*/, 6]; case 5: throw new Error("Server Type: ".concat(target.serverType, " is not supported for processing streaming file.")); case 6: assetPathMap.push({ source: fullFileName, target: assetServiceUrl }); _b.label = 7; case 7: return [2 /*return*/]; } }); }); })]; case 2: _a.sent(); return [4 /*yield*/, (0, utils_1.listSubFoldersInFolder)(fullAssetPath)]; case 3: folderNames = _a.sent(); return [4 /*yield*/, (0, utils_1.asyncForEach)(folderNames, function (folderName) { return __awaiter(void 0, void 0, void 0, function () { var destinationPathNested, fullAssetPathNested, assetFolderMap; return __generator(this, function (_a) { switch (_a.label) { case 0: destinationPathNested = path_1.default.join(destinationPath, folderName); fullAssetPathNested = path_1.default.join(fullAssetPath, folderName); return [4 /*yield*/, (0, utils_1.createFolder)(destinationPathNested)]; case 1: _a.sent(); return [4 /*yield*/, createAssetsServicesNested(target, "".concat(streamWebFolder, "/").concat(folderName), fullAssetPathNested, destinationPathNested)]; case 2: assetFolderMap = _a.sent(); assetFolderMap.forEach(function (entry) { assetPathMap.push({ source: "".concat(folderName, "/").concat(entry.source), target: entry.target }); }); return [2 /*return*/]; } }); }); })]; case 4: _a.sent(); return [2 /*return*/, assetPathMap]; } }); }); };