UNPKG

@mountainpass/hooked-cli

Version:
64 lines (63 loc) 3.29 kB
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()); }); }; import { fetchGlobalEnvVars, findScript } from '../config.js'; import { executeScriptsSequentially, resolveScripts, verifyScriptsAreExecutable } from '../scriptExecutors/ScriptExecutor.js'; import { HttpError } from '../server/router.js'; import { isJobsSerialScript } from '../types.js'; import { mergeEnvVars } from '../utils/envVarUtils.js'; import logger from '../utils/logger.js'; import loaders from './loaders.js'; /** * Invokes a script with the provided environment names. * @param providedEnvNames * @param scriptPath * @returns */ const invoke = (user, systemProcessEnvs, options, config, providedEnvNames, scriptPath, stdin, isFinalScript, storeResultAsEnv) => __awaiter(void 0, void 0, void 0, function* () { var _a; const { env, envVars } = yield loaders.initialiseEnvironment(systemProcessEnvs, options, config); logger.debug(`Running script: env=${providedEnvNames.join(',')} script=${scriptPath.join(',')}`); // find the script to execute... const rootScriptAndPaths = yield findScript(config, scriptPath, options); const [script, paths] = rootScriptAndPaths; options.scriptPath = paths; // verify user has access to invoke script if (user !== null) { const scriptAccessRoles = (_a = script.accessRoles) !== null && _a !== void 0 ? _a : ['admin']; const canInvoke = scriptAccessRoles.some(role => user.accessRoles.includes(role)); if (!canInvoke) { throw new HttpError(403, `User '${user.username}' does not have required role '${scriptAccessRoles.join(',')}' #1`); } } // merge in the stdin... mergeEnvVars(envVars, stdin); // fetch the environment variables... const [, resolvedEnvNames] = yield fetchGlobalEnvVars(config, providedEnvNames, options, envVars); logger.debug(`Resolved environment: ${JSON.stringify(envVars)}`); // executable scripts let executableScriptsAndPaths = [rootScriptAndPaths]; if (isJobsSerialScript(script)) { // resolve job definitions executableScriptsAndPaths = yield resolveScripts(paths, script, config, options); } // check executable scripts are actually executable verifyScriptsAreExecutable(executableScriptsAndPaths); // execute scripts sequentially const outputs = yield executeScriptsSequentially(executableScriptsAndPaths, stdin, env, config, options, envVars, isFinalScript, storeResultAsEnv); return { success: true, finishedAt: Date.now(), envNames: resolvedEnvNames, paths, envVars, outputs }; }); export default { invoke };