@nu-art/commando
Version:
79 lines (78 loc) • 2.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandBuilder = void 0;
/**
* Default options for CommandBuilder class instances.
*/
const defaultOptions = {
newlineDelimiter: '\n ',
indentation: 2,
};
class CommandBuilder {
/**
* Constructs a CommandBuilder instance with given options.
* @param {Partial<Options>} [options=defaultOptions] - Configuration options for the CommandBuilder instance.
*/
constructor(options = defaultOptions) {
this.commands = [];
this.indentation = 0;
this.option = defaultOptions;
/**
* Generates a string of spaces for indentation based on the current indentation level.
* @returns {string} - A string containing spaces for the current indentation level.
*/
this.getIndentation = () => {
return ' '.repeat(this.option.indentation * this.indentation);
};
/**
* Increases the current indentation level by one.
*/
this.indentIn = () => {
this.indentation++;
};
/**
* Decreases the current indentation level by one.
*/
this.indentOut = () => {
this.indentation--;
};
/**
* Appends a command to the command list with proper indentation.
* @param {string} command - The command to append.
* @returns {this} - The CommandBuilder instance for method chaining.
*/
this.append = (command) => {
const commands = command.split(this.option.newlineDelimiter);
for (const _command of commands) {
this.commands.push(`${this.getIndentation()}${_command.trim()}`);
}
return this;
};
this.option = options;
}
/**
* Appends an empty line to the command list for readability.
* @returns {this} - The CommandBuilder instance for method chaining.
*/
emptyLine() {
this.append('');
return this;
}
/**
* Retrieves the full command list as a single string.
* @returns {string} - The full command list.
*/
getCommand() {
return this.commands.join(this.option.newlineDelimiter);
}
/**
* Resets the command list and returns the previously accumulated commands.
* @returns {string} - The previously accumulated commands.
*/
reset() {
const command = this.getCommand();
this.commands.length = 0;
return command;
}
}
exports.CommandBuilder = CommandBuilder;