UNPKG

armpit

Version:

Another resource manager programming interface toolkit.

152 lines 5.56 kB
var _a; import { $ as Execa$ } from "execa"; import { CallableClassBase, isTemplateStringArray, isPromiseLike, isStringy as isStringy, isThrowableAbortSignal, } from "./tsUtils.js"; import { shallowMergeDefinedValues } from "./optionsUtils.js"; import { adjustCliResultObject, ensureAzPrefix, } from "./azCliInvoker.js"; function isExecaResult(value) { // eslint-disable-next-line @typescript-eslint/no-explicit-any return !!(value && (value.command || value.stdout || value.stderr)); } function isExecaAbortSignal(value) { return value != null && Object.prototype.toString.call(value) === "[object AbortSignal]"; } async function prepareExecaExpressionArg(e) { if (isPromiseLike(e)) { e = (await e); } if (e == null) { return ""; } switch (typeof e) { case "number": case "string": return e; case "boolean": case "symbol": return e.toString(); case "bigint": return e.toString(10); } if (Array.isArray(e)) { return prepareExecaExpressionArgs(e); } if (isExecaResult(e)) { return e; } if (isStringy(e)) { return e.toString(); } return e; } function prepareExecaExpressionArgs(azExpressions) { return Promise.all(azExpressions.map(prepareExecaExpressionArg)); } // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging export class AzCliExecaInvoker extends CallableClassBase { #options; constructor(options) { super(); this.#options = { forceAzCommandPrefix: true, allowBlanks: false, unwrapResults: true, simplifyContainerAppResults: true, }; if (options) { this.#options = shallowMergeDefinedValues(this.#options, options); } } fnImpl(...args // eslint-disable-next-line @typescript-eslint/no-explicit-any ) { if (args.length > 0 && args[0] != null) { if (isTemplateStringArray(args[0])) { return this.#templateFn(...args); } if (typeof args[0] === "object") { return this.#withOptions(args[0]); } } throw new Error("An option or template is required"); } async #templateFn(templates, ...expressions) { if (isThrowableAbortSignal(this.#options.abortSignal)) { this.#options.abortSignal.throwIfAborted(); } const execaEnv = { ...this.#options.env, AZURE_CORE_OUTPUT: "json", // request json by default AZURE_CORE_ONLY_SHOW_ERRORS: "true", // the tools aren't always consistent so this is just simpler AZURE_CORE_DISABLE_PROGRESS_BAR: "true", // avoid progress bars and spinners AZURE_CORE_NO_COLOR: "true", // hopefully this reduces some noise in stderr and stdout AZURE_CORE_LOGIN_EXPERIENCE_V2: "off", // these tools have their own way to select accounts }; if (this.#options.defaultResourceGroup != null) { execaEnv.AZURE_DEFAULTS_GROUP = this.#options.defaultResourceGroup; } if (this.#options.defaultLocation != null) { execaEnv.AZURE_DEFAULTS_LOCATION = this.#options.defaultLocation; } const execaExpressions = await prepareExecaExpressionArgs(expressions); // TODO: fix the inputs! if (this.#options.forceAzCommandPrefix) { templates = ensureAzPrefix(templates); } let abortSignal = this.#options.abortSignal; if (abortSignal != null && !isExecaAbortSignal(abortSignal)) { if (isThrowableAbortSignal(abortSignal)) { abortSignal.throwIfAborted(); } abortSignal = undefined; // don't forward it to execa if it may be rejected } const execaFn = Execa$({ env: execaEnv, stdin: "inherit", stdout: "pipe", stderr: "pipe", cancelSignal: abortSignal, }); let invocationResult; try { invocationResult = await execaFn(templates, ...execaExpressions); } catch (invocationError) { if (this.#options.allowBlanks) { const stderr = invocationError?.stderr; if (stderr && typeof stderr === "string" && /not\s*found/i.test(stderr)) { return null; } } throw invocationError; } const { stdout, stderr } = invocationResult; if (stderr != null && stderr !== "") { console.warn(stderr); } if (stdout == null || stdout === "") { if (this.#options.allowBlanks) { return null; } else { throw new Error("Result was blank"); } } else if (typeof stdout === "string") { return this.#parseJsonResponse(stdout); } else if (Array.isArray(stdout)) { return this.#parseJsonResponse(stdout.join("")); } throw new Error("Failed to parse invocation result"); } #parseJsonResponse(value) { if (value == null || value === "") { return null; } return adjustCliResultObject(JSON.parse(value), this.#options); } #withOptions(options) { return new _a(shallowMergeDefinedValues(this.#options, options)); } } _a = AzCliExecaInvoker; //# sourceMappingURL=azCliExecaInvoker.js.map