UNPKG

@sasjs/cli

Version:

Command line interface for SASjs

171 lines (170 loc) 10.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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileSingleFile = exports.CompileSingleFileSubCommands = void 0; var path_1 = __importDefault(require("path")); var config_1 = require("../../utils/config"); var utils_1 = require("@sasjs/utils"); var error_1 = require("@sasjs/utils/error"); var internal_1 = require("./internal"); var CompileSingleFileSubCommands; (function (CompileSingleFileSubCommands) { CompileSingleFileSubCommands["Job"] = "job"; CompileSingleFileSubCommands["Service"] = "service"; CompileSingleFileSubCommands["Test"] = "test"; })(CompileSingleFileSubCommands = exports.CompileSingleFileSubCommands || (exports.CompileSingleFileSubCommands = {})); var isCompileSingleFileSubCommands = function (command) { return Object.values(CompileSingleFileSubCommands).includes(command); }; function compileSingleFile(target, subCommand, source, output, insertProgramVar, currentFolder) { var _a; if (subCommand === void 0) { subCommand = 'identify'; } if (insertProgramVar === void 0) { insertProgramVar = false; } return __awaiter(this, void 0, void 0, function () { var commandExample, sourcePath, outputPathParts, leafFolderName, parentOutputFolder, pathExists, sourceFileName, destinationPath, sourceFileNameWithoutExt, macroFolders, programFolders, psMaxOption, programVar, compileTree, sourceFolder, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!isCompileSingleFileSubCommands(subCommand) && subCommand !== 'identify') { throw new Error("Unsupported context command. Supported commands are:\n".concat(Object.values(CompileSingleFileSubCommands).join('\n'))); } commandExample = 'sasjs compile <command> --source myjob.sas --target targetName -output /some/folder'; if (!source) { throw new Error("'--source' flag is missing (eg '".concat(commandExample, "')")); } source = source.split('/').join(path_1.default.sep); sourcePath = (0, utils_1.getAbsolutePath)(source, currentFolder || process.currentDir); return [4 /*yield*/, validateSourcePath(sourcePath)]; case 1: if (!(_c.sent())) { throw new Error("Provide a valid path to source file (eg '".concat(commandExample, "')")); } if (!(subCommand === 'identify')) return [3 /*break*/, 3]; return [4 /*yield*/, (0, internal_1.identifySasFile)(target, sourcePath).catch(function (err) { throw (0, error_1.prefixMessage)(err, 'Single file compilation failed. '); })]; case 2: subCommand = _c.sent(); _c.label = 3; case 3: (_a = process.logger) === null || _a === void 0 ? void 0 : _a.info("Compiling source file:\n- ".concat(sourcePath)); outputPathParts = output.split(path_1.default.sep); outputPathParts.pop(), outputPathParts.pop(); leafFolderName = source.split(path_1.default.sep).pop(); parentOutputFolder = outputPathParts.join(path_1.default.sep); return [4 /*yield*/, (0, utils_1.fileExists)(parentOutputFolder)]; case 4: pathExists = _c.sent(); if (!pathExists) return [3 /*break*/, 6]; return [4 /*yield*/, (0, utils_1.deleteFolder)(parentOutputFolder)]; case 5: _c.sent(); _c.label = 6; case 6: return [4 /*yield*/, (0, utils_1.createFolder)(output)]; case 7: _c.sent(); sourceFileName = sourcePath.split(path_1.default.sep).pop(); destinationPath = path_1.default.join(output, sourceFileName); return [4 /*yield*/, (0, utils_1.copy)(sourcePath, destinationPath)]; case 8: _c.sent(); sourceFileNameWithoutExt = sourceFileName.split('.')[0]; return [4 /*yield*/, (0, config_1.getMacroFolders)(target)]; case 9: macroFolders = _c.sent(); return [4 /*yield*/, (0, config_1.getProgramFolders)(target)]; case 10: programFolders = _c.sent(); psMaxOption = 'options ps=max;'; programVar = insertProgramVar ? "%let _program=".concat(target.appLoc, "/").concat(subCommand, "s/").concat(leafFolderName, "/").concat(sourceFileNameWithoutExt, ";\n").concat(psMaxOption) : "".concat(psMaxOption); compileTree = (0, internal_1.getCompileTree)(target); sourceFolder = sourcePath .split(path_1.default.sep) .slice(0, sourcePath.split(path_1.default.sep).length - 1) .join(path_1.default.sep); _b = subCommand; switch (_b) { case CompileSingleFileSubCommands.Service: return [3 /*break*/, 11]; case CompileSingleFileSubCommands.Job: return [3 /*break*/, 13]; case CompileSingleFileSubCommands.Test: return [3 /*break*/, 15]; } return [3 /*break*/, 17]; case 11: return [4 /*yield*/, (0, internal_1.compileFile)(target, destinationPath, macroFolders, programFolders, programVar, compileTree, utils_1.SASJsFileType.service, sourceFolder)]; case 12: _c.sent(); return [3 /*break*/, 17]; case 13: return [4 /*yield*/, (0, internal_1.compileFile)(target, destinationPath, macroFolders, programFolders, programVar, compileTree, utils_1.SASJsFileType.job, sourceFolder)]; case 14: _c.sent(); return [3 /*break*/, 17]; case 15: return [4 /*yield*/, (0, internal_1.compileTestFile)(target, sourcePath, programVar, undefined, false, compileTree, destinationPath)]; case 16: _c.sent(); return [3 /*break*/, 17]; case 17: return [4 /*yield*/, compileTree.saveTree()]; case 18: _c.sent(); return [2 /*return*/, { destinationPath: destinationPath }]; } }); }); } exports.compileSingleFile = compileSingleFile; function validateSourcePath(path) { return __awaiter(this, void 0, void 0, function () { var isSourceFile; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!path) return [2 /*return*/, false]; isSourceFile = /\.sas$/i.test(path); if (!isSourceFile) return [2 /*return*/, false]; return [4 /*yield*/, (0, utils_1.fileExists)(path)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }