@contentstack/cli-cm-clone
Version:
Contentstack stack clone plugin
80 lines (79 loc) • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Clone = exports.CloneTypeSelectionCommand = exports.CreateNewStackCommand = exports.SetBranchCommand = exports.HandleExportCommand = exports.HandleDestinationStackCommand = exports.HandleBranchCommand = exports.HandleStackCommand = exports.HandleOrgCommand = exports.BaseCommand = void 0;
/**
* Base command class implementing the command pattern
*/
class BaseCommand {
constructor(executeFn, undoFn, params) {
this.executeFn = executeFn;
this.undoFn = undoFn;
this.params = params;
}
async execute(params) {
return this.executeFn(params || this.params);
}
async undo(params) {
if (this.undoFn) {
await this.undoFn(params || this.params);
}
}
}
exports.BaseCommand = BaseCommand;
/**
* Command factory functions
*/
function HandleOrgCommand(params, parentContext) {
return new BaseCommand(parentContext.handleOrgSelection.bind(parentContext), undefined, params);
}
exports.HandleOrgCommand = HandleOrgCommand;
function HandleStackCommand(params, parentContext) {
return new BaseCommand(parentContext.handleStackSelection.bind(parentContext), parentContext.execute.bind(parentContext), params);
}
exports.HandleStackCommand = HandleStackCommand;
function HandleBranchCommand(params, parentContext, backStepHandler) {
return new BaseCommand(parentContext.handleBranchSelection.bind(parentContext), backStepHandler, params);
}
exports.HandleBranchCommand = HandleBranchCommand;
function HandleDestinationStackCommand(params, parentContext) {
return new BaseCommand(parentContext.handleStackSelection.bind(parentContext), parentContext.executeDestination.bind(parentContext), params);
}
exports.HandleDestinationStackCommand = HandleDestinationStackCommand;
function HandleExportCommand(params, parentContext) {
return new BaseCommand(parentContext.cmdExport.bind(parentContext), undefined, params);
}
exports.HandleExportCommand = HandleExportCommand;
function SetBranchCommand(params, parentContext) {
return new BaseCommand(parentContext.setBranch.bind(parentContext), undefined, params);
}
exports.SetBranchCommand = SetBranchCommand;
function CreateNewStackCommand(params, parentContext) {
return new BaseCommand(parentContext.createNewStack.bind(parentContext), parentContext.executeDestination.bind(parentContext), params);
}
exports.CreateNewStackCommand = CreateNewStackCommand;
function CloneTypeSelectionCommand(params, parentContext) {
return new BaseCommand(parentContext.cloneTypeSelection.bind(parentContext), undefined, params);
}
exports.CloneTypeSelectionCommand = CloneTypeSelectionCommand;
/**
* Clone command executor class
*/
class Clone {
constructor() {
this.commands = [];
}
async execute(command) {
this.commands.push(command);
const result = await command.execute(command.params);
return result;
}
async undo() {
if (this.commands.length) {
const command = this.commands.pop();
if (command && command.undo) {
await command.undo(command.params);
}
}
}
}
exports.Clone = Clone;