@nu-art/commando
Version:
Shell command execution framework with interactive sessions, CLI parameter resolution, and plugin system for building and executing shell scripts programmatically
68 lines (67 loc) • 2.57 kB
JavaScript
/*
* commando provides shell command execution framework with interactive sessions and plugin system
*
* Copyright (C) 2020 Adam van der Kruk aka TacB0sS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Merges multiple classes into a single class constructor.
*
* Creates a new class that combines all properties and methods from
* the provided plugin classes. Properties are copied from prototype
* descriptors during construction.
*
* **Use Case**: Plugin system for Commando - allows combining
* BaseCommando with plugin classes (e.g., Commando_Git, Commando_NVM).
*
* **Limitation**: Only copies own properties from prototypes, not
* inherited properties or static members.
*
* @template T - Array of constructor types
* @param plugins - Constructor classes to merge
* @returns New constructor that merges all plugin classes
*/
export function MergeClass(...plugins) {
class SuperClass {
/**
* Constructs an instance of SuperClass, merging properties from all plugins.
* @param {...any[]} args - The arguments to pass to the constructors.
*/
constructor(...args) {
plugins.forEach(plugin => {
Object.getOwnPropertyNames(plugin.prototype).forEach(name => {
const prop = Object.getOwnPropertyDescriptor(plugin.prototype, name);
if (prop) {
Object.defineProperty(this, name, prop);
}
});
});
}
}
return SuperClass;
}
/**
* Creates an instance of a merged class from multiple constructors.
*
* Convenience function that merges classes and immediately instantiates
* the result. Used by BaseCommando._create() to create plugin instances.
*
* @template T - Array of constructor types
* @param plugins - Constructor classes to merge and instantiate
* @returns Instance of the merged class
*/
export function CreateMergedInstance(...plugins) {
const SuperClass = MergeClass(...plugins);
return new SuperClass();
}