@nu-art/commando
Version:
98 lines (97 loc) • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Commando_Basic = void 0;
const BaseCommando_1 = require("../core/BaseCommando");
/**
* Represents a Command Line Interface (CLI) to build and execute shell commands.
*/
class Commando_Basic extends BaseCommando_1.BaseCommando {
/**
* Changes directory and optionally executes a block of commands in that directory.
* @param {string} folderName - Name of the directory to change to.
* @param {CliBlock} [toRun] - Optional block of commands to execute in the directory.
* @returns {this} - The Cli instance for method chaining.
*/
cd(folderName, toRun) {
this.append(`cd ${folderName}`);
this.indentIn();
if (toRun) {
toRun(this);
this.cd_();
}
return this;
}
custom(command) {
this.append(command);
return this;
}
/**
* Changes directory back to the previous directory.
* @returns {this} - The Cli instance for method chaining.
*/
cd_() {
this.indentOut();
this.append(`cd -`);
return this;
}
/**
* Appends an 'ls' command with optional parameters.
* @param {string} [params] - Optional parameters for the 'ls' command.
* @returns {this} - The Cli instance for method chaining.
*/
ls(params = '') {
this.append(`ls ${params}`);
return this;
}
mkdir(dirName) {
this.append(`mkdir -p ${dirName}`);
return this;
}
rmdir(dirPath, options) {
let command = 'rm';
if (options === null || options === void 0 ? void 0 : options.force)
command += ' -rf';
if (options === null || options === void 0 ? void 0 : options.recursive)
command += ' -r';
this.append(`${command} ${dirPath}`);
return this;
}
cpdir(srcPath, destPath, options) {
let command = `cp -r ${srcPath}`;
if (options === null || options === void 0 ? void 0 : options.contentOnly)
command += '/*';
command += ` ${destPath}`;
this.append(command);
return this;
}
cat(fileName) {
this.append(`cat ${fileName}`);
return this;
}
echo(log, options) {
const _escape = (options === null || options === void 0 ? void 0 : options.escape) ? '-e' : '';
const _toFile = (options === null || options === void 0 ? void 0 : options.toFile) ? `>${options.toFile.append ? '>' : ''} ${options.toFile.name}` : '';
const escapedLog = log.replace(/\\/g, '\\\\').replace(/\n/g, '\\\\n').replace(/\t/g, '\\\t');
this.append(`echo ${_escape} "${escapedLog}" ${_toFile}`);
return this;
}
/**
* Appends a 'pwd' command to print the current directory.
* @returns {this} - The Cli instance for method chaining.
*/
pwd() {
this.append('pwd');
return this;
}
/**
* Assigns a value to a variable in the script.
* @param {string} varName - The name of the variable.
* @param {string | string[]} value - The value to assign to the variable.
* @returns {this} - The Cli instance for method chaining.
*/
assignVar(varName, value) {
this.append(`${varName}=(${Array.isArray(value) ? value : [value].join(' ')})`);
return this;
}
}
exports.Commando_Basic = Commando_Basic;