@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
67 lines (66 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createProgrammaticIO = exports.createProgrammaticContext = void 0;
const types_1 = require("../core/types");
const baseContext_1 = require("./baseContext");
class ProgrammaticContext extends baseContext_1.BaseContext {
constructor(command, options) {
var _a;
super(command);
this.inputs = (_a = options.inputs) !== null && _a !== void 0 ? _a : {};
this.onLog = options.onLog;
}
log(message) {
var _a;
(_a = this.onLog) === null || _a === void 0 ? void 0 : _a.call(this, message);
}
async confirm() {
return true; // programmatic mode always confirms
}
async resolveInput(name, spec) {
if (name in this.inputs && this.inputs[name] !== undefined) {
return this.inputs[name];
}
if ("default" in spec && spec.default !== undefined) {
return spec.default;
}
throw new types_1.MissingInputError(name, spec.message);
}
}
/**
* Create a CommandContext that resolves inputs from pre-supplied values.
* Used for non-interactive CLI mode and programmatic invocation.
*/
function createProgrammaticContext(command, options = {}) {
return new ProgrammaticContext(command, options);
}
exports.createProgrammaticContext = createProgrammaticContext;
/**
* Create a bare IO instance for helper functions that only need log + promptDirect.
*/
function createProgrammaticIO(options = {}) {
const {
inputs = {},
onLog
} = options;
return {
log(message) {
onLog === null || onLog === void 0 ? void 0 : onLog(message);
},
async confirm() {
return true;
},
async promptDirect(spec) {
if (spec.name in inputs && inputs[spec.name] !== undefined) {
return inputs[spec.name];
}
if ("default" in spec && spec.default !== undefined) {
return spec.default;
}
throw new types_1.MissingInputError(spec.name, spec.message);
}
};
}
exports.createProgrammaticIO = createProgrammaticIO;