Skip to content

Commit

Permalink
refactor(cli): using new way for working with plugins configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Feb 28, 2018
1 parent 87ec4d9 commit 7f7ae9a
Showing 1 changed file with 63 additions and 14 deletions.
77 changes: 63 additions & 14 deletions packages/baset-cli/src/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
import { utils } from 'baset-core';
import { ITestGroupOptions, utils } from 'baset-core';
import { Options } from 'yargs';

export interface IGlobalArgs {
plugins: utils.IDictionary<string[]>;
plugins: utils.IDictionary<ITestGroupOptions>;
// tslint:disable-next-line:no-any
options: utils.IDictionary<any>;
}
export interface ITestGroupPlugins {
baseliner: string;
environment?: string;
readers?: string[] | string;
resolvers?: string[] | string;
imports?: string[] | string;
}

function groupPlugins(plugins: string[]): ITestGroupOptions {
const firstModule = plugins.slice(0, 1)[0];
const environment = resolveModule((firstModule.includes('-env-') && firstModule) || undefined);
const firstImportIndex = Number(!!environment);
const firstReaderIndex = plugins.findIndex(plugin => plugin.includes('-reader-'));
const firstResolverIndex = plugins.findIndex(plugin => plugin.includes('-resolver-'));
const imports = plugins.slice(firstImportIndex, firstReaderIndex).map(resolveModule);
const readers = plugins.slice(firstReaderIndex, firstResolverIndex).map(resolveModule);
const resolvers = plugins.slice(firstResolverIndex, -1).map(resolveModule);
const baseliner = resolveModule(plugins.slice(-1)[0]);

function resolveBasetPlugins(name: string) {
return name.startsWith('baset')
return {
baseliner,
environment,
readers,
resolvers,
imports,
};
}
function getDefaultPlugins(plugins: ITestGroupPlugins): ITestGroupOptions {
return {
baseliner: resolveModule(plugins.baseliner),
environment: resolveModule(plugins.environment),
readers: (plugins.readers
? Array.isArray(plugins.readers)
? plugins.readers
: [plugins.readers]
: []).map(resolveModule),
resolvers: (plugins.resolvers
? Array.isArray(plugins.resolvers)
? plugins.resolvers
: [plugins.resolvers]
: []).map(resolveModule),
imports: (plugins.imports
? Array.isArray(plugins.imports)
? plugins.imports
: [plugins.imports]
: []).map(resolveModule),
};
}
function resolveModule<T extends string | undefined>(name: T) {
return (name && name.startsWith('baset'))
? `./node_modules/${name}`
: name;
}
Expand All @@ -18,27 +65,29 @@ export const options: utils.IDictionary<Options> = {
alias: 'p',
describe: 'Plugins used for your tests',
default: { '.spec.js$': 'baset-baseliner-json' },
coerce: (plugins: string[] | { [index: string]: string[] | string }) =>
(plugins instanceof Array)
coerce: (plugins: string[] | utils.IDictionary<string[] | string | ITestGroupPlugins>) =>
(Array.isArray(plugins))
// if plugins is Array, then we get this arg from cli
? plugins.reduce<{ [index: string]: string[] }>(
? plugins.reduce<utils.IDictionary<ITestGroupOptions>>(
(result, plugin) => ({
...result,
// regexp for spec // array of node modules
[plugin.split(':')[0]]: plugin.split(':').slice(1).map(resolveBasetPlugins),
// regexp for spec // array of plugins
[plugin.split(':')[0]]: groupPlugins(plugin.split(':').slice(1)),
}),
{})
// if not, then it defined in config
: Object.keys(plugins).reduce<{ [index: string]: string[] }>(
// we have to ensure that all values are arrays
: Object.keys(plugins).reduce<utils.IDictionary<ITestGroupOptions>>(
// we have to ensure that all values are in correct format
(result, key) => {
const plugin = plugins[key];

return {
...result,
[key]: (plugin instanceof Array)
? plugin.map(resolveBasetPlugins)
: [resolveBasetPlugins(plugin)],
[key]: (Array.isArray(plugin))
? groupPlugins(plugin)
: (typeof plugin === 'string')
? groupPlugins([plugin])
: getDefaultPlugins(plugin),
};
},
{}),
Expand Down

0 comments on commit 7f7ae9a

Please sign in to comment.