@syntest/module
Advanced tools
Comparing version 0.1.0-beta.8 to 0.1.0-beta.9
@@ -8,18 +8,9 @@ import { Metric } from "@syntest/metric"; | ||
constructor(type: string, name: string, describe: string); | ||
} | ||
/** | ||
* We have defined both an abstract class and interface called Plugin here. | ||
* This is called 'merging' it allows an abstract class to have optional methods. | ||
*/ | ||
export interface Plugin { | ||
/** | ||
* Should return a list of metrics that are stored by this plugin | ||
*/ | ||
getMetrics?(): Promise<Metric[]> | Metric[]; | ||
/** | ||
* | ||
* @param tool the tool the plugin provides options for | ||
* @param command the command the tool provides options for | ||
* Should return a map of command -> yargsConfig | ||
*/ | ||
getToolOptions?(tool: string, labels: string[]): Promise<Map<string, Yargs.Options>> | Map<string, Yargs.Options>; | ||
abstract getOptions(tool: string, labels: string[], command?: string | undefined): Map<string, Yargs.Options>; | ||
/** | ||
@@ -30,17 +21,14 @@ * | ||
*/ | ||
getToolOptionChoices?(tool: string, labels: string[], option: string): Promise<string[]> | string[]; | ||
abstract getOptionChoices(option: string, tool: string, labels: string[], command?: string | undefined): string[]; | ||
} | ||
/** | ||
* We have defined both an abstract class and interface called Plugin here. | ||
* This is called 'merging' it allows an abstract class to have optional methods. | ||
*/ | ||
export interface Plugin { | ||
/** | ||
* @param tool the tool the plugin provides options for | ||
* @param command the command the tool provides options for | ||
* Should return a map of command -> yargsConfig | ||
* Should return a list of metrics that are stored by this plugin | ||
*/ | ||
getCommandOptions?(tool: string, labels: string[], command: string): Promise<Map<string, Yargs.Options>> | Map<string, Yargs.Options>; | ||
/** | ||
* | ||
* @param tool the tool the plugin provides additional choices for | ||
* @param command the command the plugin provides additional choices for | ||
* @param option the option the plugin provides additional choices for | ||
*/ | ||
getCommandOptionChoices?(tool: string, labels: string[], command: string, option: string): Promise<string[]> | string[]; | ||
getMetrics?(): Promise<Metric[]> | Metric[]; | ||
} | ||
//# sourceMappingURL=Plugin.d.ts.map |
@@ -6,4 +6,4 @@ import { Metric, MiddleWare } from "@syntest/metric"; | ||
abstract createMetricMiddleware(metrics: Metric[]): MiddleWare; | ||
getToolOptionChoices(tool: string, labels: string[], option: string): string[]; | ||
getOptionChoices(option: string): string[]; | ||
} | ||
//# sourceMappingURL=MetricMiddlewarePlugin.d.ts.map |
@@ -10,3 +10,3 @@ "use strict"; | ||
} | ||
getToolOptionChoices(tool, labels, option) { | ||
getOptionChoices(option) { | ||
// for every tool/label | ||
@@ -13,0 +13,0 @@ if (option === "metric-middleware-pipeline") { |
@@ -14,6 +14,9 @@ import { Metric } from "@syntest/metric"; | ||
constructor(name: string, labels: string[], describe: string, commands: Command[], toolOptions: Map<string, Yargs.Options>, handler?: (arguments_: Yargs.ArgumentsCamelCase) => void | Promise<void>); | ||
addPluginOptions(plugins: Plugin[]): Promise<void>; | ||
addCommandOptions(plugin: Plugin): Promise<void>; | ||
addPluginOptionChoices(plugins: Plugin[]): Promise<void>; | ||
addCommandOptionChoices(plugin: Plugin): Promise<void>; | ||
/** | ||
* These two functions are separated because we need to be able to add choices to options that are added by plugins. | ||
* If the two functions are combined, the choices will be added to the original options, not the options added by plugins. | ||
*/ | ||
addPluginOptions(plugins: Plugin[]): void; | ||
addPluginOptionChoices(plugins: Plugin[]): void; | ||
protected _addCommandOptionChoices(plugin: Plugin): void; | ||
builder: (yargs: Yargs.Argv) => Yargs.Argv<{}>; | ||
@@ -20,0 +23,0 @@ } |
@@ -30,53 +30,49 @@ "use strict"; | ||
} | ||
async addPluginOptions(plugins) { | ||
/** | ||
* These two functions are separated because we need to be able to add choices to options that are added by plugins. | ||
* If the two functions are combined, the choices will be added to the original options, not the options added by plugins. | ||
*/ | ||
addPluginOptions(plugins) { | ||
for (const plugin of plugins) { | ||
if (plugin.getToolOptions) { | ||
const toolOptions = await plugin.getToolOptions(this.name, this.labels); | ||
for (const option of toolOptions.keys()) { | ||
this.toolOptions.set(`${plugin.name}-${option}`, toolOptions.get(option)); | ||
const toolOptions = plugin.getOptions(this.name, this.labels); | ||
for (const option of toolOptions.keys()) { | ||
this.toolOptions.set(`${plugin.name}-${option}`, toolOptions.get(option)); | ||
} | ||
for (const command of this.commands) { | ||
const commandOptions = plugin.getOptions(this.name, this.labels, command.command); | ||
for (const option of commandOptions.keys()) { | ||
command.options.set(`${plugin.name}-${option}`, commandOptions.get(option)); | ||
} | ||
} | ||
if (!plugin.getCommandOptions) { | ||
continue; | ||
} | ||
await this.addCommandOptions(plugin); | ||
} | ||
/** | ||
* These two loops are separated because we need to be able to add choices to options that are added by plugins. | ||
* If the two loops are combined, the choices will be added to the original options, not the options added by plugins. | ||
*/ | ||
await this.addPluginOptionChoices(plugins); | ||
} | ||
async addCommandOptions(plugin) { | ||
for (const command of this.commands) { | ||
const commandOptions = await plugin.getCommandOptions(this.name, this.labels, command.command); | ||
for (const option of commandOptions.keys()) { | ||
command.options.set(`${plugin.name}-${option}`, commandOptions.get(option)); | ||
} | ||
} | ||
} | ||
async addPluginOptionChoices(plugins) { | ||
addPluginOptionChoices(plugins) { | ||
for (const plugin of plugins) { | ||
if (plugin.getToolOptionChoices) { | ||
for (const option of this.toolOptions.keys()) { | ||
const addedChoices = await plugin.getToolOptionChoices(this.name, this.labels, option); | ||
if (!this.toolOptions.get(option).choices) { | ||
throw new Error((0, diagnostics_1.cannotAddChoicesToOptionWithoutChoices)(option, plugin.name)); | ||
} | ||
this.toolOptions.get(option).choices = [ | ||
...this.toolOptions.get(option).choices, | ||
...addedChoices, | ||
]; | ||
for (const option of this.toolOptions.keys()) { | ||
const addedChoices = plugin.getOptionChoices(option, this.name, this.labels); | ||
if (addedChoices.length === 0) { | ||
continue; | ||
} | ||
if (!this.toolOptions.get(option).choices) { | ||
throw new Error((0, diagnostics_1.cannotAddChoicesToOptionWithoutChoices)(option, plugin.name)); | ||
} | ||
const newOption = { | ||
...this.toolOptions.get(option), | ||
}; | ||
newOption.choices = [ | ||
...this.toolOptions.get(option).choices, | ||
...addedChoices, | ||
]; | ||
this.toolOptions.set(option, newOption); | ||
} | ||
if (!plugin.getCommandOptionChoices) { | ||
continue; | ||
} | ||
await this.addCommandOptionChoices(plugin); | ||
this._addCommandOptionChoices(plugin); | ||
} | ||
} | ||
async addCommandOptionChoices(plugin) { | ||
_addCommandOptionChoices(plugin) { | ||
for (const command of this.commands) { | ||
for (const option of Object.keys(command.options)) { | ||
const addedChoices = await plugin.getCommandOptionChoices(this.name, this.labels, command.command, option); | ||
const addedChoices = plugin.getOptionChoices(option, this.name, this.labels, command.command); | ||
if (addedChoices.length === 0) { | ||
continue; | ||
} | ||
if (!command.options.get(option).choices) { | ||
@@ -89,2 +85,10 @@ throw new Error((0, diagnostics_1.cannotAddChoicesToOptionWithoutChoices)(option, plugin.name)); | ||
]; | ||
const newOption = { | ||
...command.options.get(option), | ||
}; | ||
newOption.choices = [ | ||
...command.options.get(option).choices, | ||
...addedChoices, | ||
]; | ||
command.options.set(option, newOption); | ||
} | ||
@@ -91,0 +95,0 @@ } |
@@ -41,5 +41,5 @@ import { UserInterface } from "@syntest/cli-graphics"; | ||
registerPlugin(module: string, plugin: Plugin): void; | ||
configureModules(yargs: Yargs.Argv, preset: string): Promise<Yargs.Argv<{}>>; | ||
configureModules(yargs: Yargs.Argv, presetChoice: string): Yargs.Argv<{}>; | ||
printModuleVersionTable(): void; | ||
} | ||
//# sourceMappingURL=ModuleManager.d.ts.map |
@@ -26,2 +26,3 @@ "use strict"; | ||
const PluginType_1 = require("./extension/plugins/PluginType"); | ||
const Configuration_1 = require("./util/Configuration"); | ||
const diagnostics_1 = require("./util/diagnostics"); | ||
@@ -233,23 +234,34 @@ class ModuleManager { | ||
} | ||
async configureModules(yargs, preset) { | ||
configureModules(yargs, presetChoice) { | ||
ModuleManager.LOGGER.info("Configuring modules"); | ||
// add presets options to yargs by overriding it | ||
yargs.option("preset", { | ||
alias: [], | ||
choices: [...this._presets.values()].map((preset) => preset.name), | ||
default: "none", | ||
description: "The preset you want to use", | ||
group: Configuration_1.OptionGroups.General, | ||
hidden: false, | ||
type: "string", | ||
}); | ||
const plugins = []; | ||
for (const pluginsOfType of this._plugins.values()) { | ||
for (const plugin of pluginsOfType.values()) { | ||
plugins.push(plugin); | ||
} | ||
} | ||
for (const tool of this._tools.values()) { | ||
const plugins = []; | ||
for (const pluginsOfType of this._plugins.values()) { | ||
for (const plugin of pluginsOfType.values()) { | ||
plugins.push(plugin); | ||
} | ||
} | ||
await tool.addPluginOptions(plugins); | ||
tool.addPluginOptions(plugins); | ||
tool.addPluginOptionChoices(plugins); | ||
yargs = yargs.command(tool); | ||
} | ||
ModuleManager.LOGGER.info("Setting preset"); | ||
if (preset === "none") { | ||
if (presetChoice === "none") { | ||
ModuleManager.LOGGER.info("No preset set"); | ||
return yargs; | ||
} | ||
if (!this._presets.has(preset)) { | ||
throw new Error((0, diagnostics_1.presetNotFound)(preset)); | ||
if (!this._presets.has(presetChoice)) { | ||
throw new Error((0, diagnostics_1.presetNotFound)(presetChoice)); | ||
} | ||
const presetObject = this._presets.get(preset); | ||
const presetObject = this._presets.get(presetChoice); | ||
yargs = yargs.middleware((arguments_) => presetObject.modifyArgs(arguments_), true); | ||
@@ -256,0 +268,0 @@ return yargs; |
{ | ||
"name": "@syntest/module", | ||
"version": "0.1.0-beta.8", | ||
"version": "0.1.0-beta.9", | ||
"description": "The module library of the SynTest Framework", | ||
@@ -61,3 +61,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "891ed9ca468401d8145524808d6a7d16ff861dd7" | ||
"gitHead": "d92e8dd8c1bb44407fe7a1a390e178fab90f8339" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71688
937