UNPKG

profoundjs

Version:

Profound.js Framework and Server

159 lines (139 loc) 4.13 kB
"use strict"; /* Utilities for PJS install/setup process. */ const child_process = require("child_process"); const fs = require("fs"); const os = require("os"); const path = require("path"); exports.getConfigPath = function() { // Get the path - maybe its relative let configPath = process.env.PROFOUNDJS_CONFIG_FILE || "./config.js"; if (configPath.startsWith(".")) { configPath = path.join(exports.getDeployDir(), configPath); } // Make sure directory exists fs.mkdirSync(path.dirname(configPath), { recursive: true }); return configPath; }; exports.getDeployDir = function() { const dirParts = __dirname.split(path.sep); // eslint-disable-next-line no-empty while (dirParts.length > 0 && dirParts.pop() !== "node_modules") {}; if (dirParts.length > 0) { return dirParts.join(path.sep); } }; exports.getPackageDir = function() { return path.resolve(__dirname, ".."); }; exports.getSetupDir = function() { return path.join(exports.getPackageDir(), "setup"); }; exports.getIBMiInstances = function(installDir) { const INSTANCES_DIR = "/profoundjs-base/instances"; if (!fs.existsSync(INSTANCES_DIR)) { return []; } const dirs = fs.readdirSync( INSTANCES_DIR, { withFileTypes: true } ) .filter(el => el.isDirectory()); let instances = []; for (const dir of dirs) { let conf; try { conf = fs.readFileSync(path.join(INSTANCES_DIR, dir.name, "conf"), "utf8"); } catch (error) { continue; } const instance = { name: dir.name.toUpperCase(), options: [] }; const lines = conf.split("\n"); for (const line of lines) { const parts = line.split("="); if (parts.length >= 2) { instance.options.push({ name: parts[0].trim(), value: parts.slice(1).join("=") }); } } instances.push(instance); } if (installDir) { instances = instances.filter(instance => { const startFile = instance.options.find(option => option.name === "path"); return startFile && startFile.value.trim() === path.join(installDir, "start.js"); }); } return instances; }; exports.validateIBMiCCSID = function(ccsid) { if (!Number.isInteger(ccsid) || ccsid < 1 || ccsid > 65535) { return "CCSID must be a number 1-65535"; } }; exports.validateIBMiIASP = function(name) { if (name.toUpperCase() !== "*SYSBAS") { return exports.validateIBMiName(name); } }; exports.validateIBMiName = function(name) { if (name.length < 1 || name.length > 10) { return "Name must be from 1-10 characters."; } const firstChar = name.substr(0, 1).toUpperCase(); if ((firstChar < "A" || firstChar > "Z") && firstChar != "#" && firstChar != "@" && firstChar != "$") { return "Name must start with either a character A-Z, #, @, or $"; } for (let i = 1; i < name.length; i++) { const chr = name.substr(i, 1).toUpperCase(); if ((chr < "A" || chr > "Z") && (chr < "0" || chr > "9") && chr != "#" && chr != "@" && chr != "$" && chr != "_" && chr != ".") { return "Name contains an invalid character: " + chr; } } }; exports.isIBMi = function() { // Node.js versions < 18 report os.platform() === "aix" on IBM i. return (os.platform() === "os400" || (os.platform() === "aix" && os.type() === "OS400")); }; exports.libraryExists = function(name) { if (typeof name !== "string" || name.trim() === "") { throw new Error("Library name is required."); } if (!exports.isIBMi()) { throw new Error("This function is not valid on this platform."); } let exists; try { child_process.execSync( `system 'qsys/chkobj obj(${name.trim()}) objtype(*lib) aut(*none)'`, { stdio: ["ignore", "pipe", "pipe"], env: {} } ); exists = true; } catch (error) { if (!Buffer.isBuffer(error.stderr) || !error.stderr.toString().startsWith("CPF9801")) { throw error; } exists = false; } return exists; }; if (exports.getDeployDir()) { require("dotenv").config( { path: path.join(exports.getDeployDir(), ".env") } ); }