hermione-gemini-migrator
Version:
Plugin for hermione for migration gemini tests.
133 lines (127 loc) • 4.69 kB
JavaScript
;
const _ = require('lodash');
const parseArgs = require('yargs-parser');
const {root, section, option} = require('gemini-configparser');
const ENV_PREFIX = 'hermione_gemini_migrator_';
const CLI_PREFIX = '--gemini-migrator-';
const getParser = () => {
return root(section({
enabled: option({
defaultValue: false,
parseEnv: JSON.parse,
parseCli: JSON.parse,
validate: (v) => {
if (!_.isBoolean(v)) {
throw new Error(`"enabled" option must be boolean, but got ${typeof v}`);
}
}
}),
geminiConfig: option({
defaultValue: '.gemini.js',
validate: (v) => {
if (!_.isString(v)) {
throw new Error(`"geminiConfig" option must be string, but got ${typeof v}`);
}
}
}),
inputPatterns: option({
defaultValue: '**/*.gemini.js',
parseEnv: JSON.parse,
parseCli: JSON.parse,
validate: (v) => {
if (!_.isString(v) && !(_.isArray(v) && v.every(_.isString))) {
throw new Error(`"inputPatterns" option must be string or array of string, but got ${typeof v}`);
}
}
}),
rootSuiteTitle: option({
defaultValue: 'Autogenerated',
parseEnv: JSON.parse,
parseCli: JSON.parse,
validate: (v) => {
if (!_.isString(v) && !(_.isArray(v) && v.every(_.isString))) {
throw new Error(`"rootSuiteTitle" option must be string or array of string, but got ${typeof v}`);
}
}
}),
filePathReplacer: option({
defaultValue: () => filePath => filePath.replace(/gemini/g, 'hermione'),
validate: (v) => {
if (!_.isFunction(v)) {
throw new Error(`"filePathReplacer" option must be function, but got ${typeof v}`);
}
}
}),
browserIdReplacer: option({
defaultValue: () => browserId => browserId,
validate: (v) => {
if (!_.isFunction(v)) {
throw new Error(`"browserIdReplacer" option must be function, but got ${typeof v}`);
}
}
}),
commandReplacers: option({
defaultValue: {},
validate: (v) => {
if (!_.isObject(v) && Object.keys(v).every(key => !_.isFunction(v[key]))) {
throw new Error(`"commandReplacer" option must be object with functions`);
}
}
}),
codeFormatter: option({
defaultValue: () => code => code,
validate: (v) => {
if (!_.isFunction(v)) {
throw new Error(`"codeFormatter" option must be function, but got ${typeof v}`);
}
}
}),
before: option({
defaultValue: () => () => {},
validate: (v) => {
if (!_.isFunction(v)) {
throw new Error(`"before" option must be function, but got ${typeof v}`);
}
}
}),
beforeEach: option({
defaultValue: () => () => {},
validate: (v) => {
if (!_.isFunction(v)) {
throw new Error(`"beforeEach" option must be function, but got ${typeof v}`);
}
}
}),
afterEach: option({
defaultValue: () => () => {},
validate: (v) => {
if (!_.isFunction(v)) {
throw new Error(`"afterEach" option must be function, but got ${typeof v}`);
}
}
}),
after: option({
defaultValue: () => () => {},
validate: (v) => {
if (!_.isFunction(v)) {
throw new Error(`"after" option must be function, but got ${typeof v}`);
}
}
})
}), {envPrefix: ENV_PREFIX, cliPrefix: CLI_PREFIX});
};
module.exports = {
parse(options) {
const { env, argv } = process;
const parsedOptions = getParser()({ options, env, argv });
const { geminiMigrate } = parseArgs(argv.slice(2), { boolean: ['gemini-migrate'] });
if (geminiMigrate) {
parsedOptions.enabled = true;
}
return parsedOptions;
},
extendCli(commander) {
commander
.option('--gemini-migrate', 'Enable hermione-gemini-migrator plugin');
}
};