From b5e4b4a4a18db94f8370cf27d017f8f9793f59fc Mon Sep 17 00:00:00 2001 From: Rostislav Shtanko Date: Mon, 10 Oct 2016 14:40:04 +0300 Subject: [PATCH] feat(cli): Add command 'list' to show list of browsers or sets from config --- lib/cli/index.js | 62 +++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/lib/cli/index.js b/lib/cli/index.js index 9f91aa7d3..2402fa2a2 100644 --- a/lib/cli/index.js +++ b/lib/cli/index.js @@ -1,14 +1,17 @@ 'use strict'; -var pkg = require('../../package.json'), - program = require('commander'), - Promise = require('bluebird'), - handleErrors = require('./errors').handleErrors, - handleUncaughtExceptions = require('./errors').handleUncaughtExceptions, - Gemini = require('../gemini'), - checkForDeprecations = require('./deprecations').checkForDeprecations; - -exports.run = function() { +const _ = require('lodash'); +const program = require('commander'); +const Promise = require('bluebird'); + +const Config = require('../config'); +const Gemini = require('../gemini'); +const pkg = require('../../package.json'); +const handleErrors = require('./errors').handleErrors; +const checkForDeprecations = require('./deprecations').checkForDeprecations; +const handleUncaughtExceptions = require('./errors').handleUncaughtExceptions; + +exports.run = () => { program .version(pkg.version) .option('-b, --browser ', 'run test only in specified browser', collect) @@ -20,21 +23,16 @@ exports.run = function() { .option('--diff', 'update only screenshots with diff') .option('--new', 'save only new screenshots') .description('update the changed screenshots or gather if they doesn\'t exist') - .action(function(paths, options) { - runGemini('update', paths, options).done(); - }); + .action((paths, options) => runGemini('update', paths, options).done()); program.command('test [paths...]') .allowUnknownOption(true) - .option('-r, --reporter ', 'test reporter', collect) + .option('-r, --reporter ', 'test reporter. Available reporters are: flat, vflat, html.', collect) .option('-s, --set ', 'set to run', collect) .description('run tests') - .action(function(paths, options) { - runGemini('test', paths, options) - .done(); - }); + .action((paths, options) => runGemini('test', paths, options).done()); - program.on('--help', function() { + program.on('--help', () => { console.log(' Overriding config'); console.log(' To override any config option use full option path converted to --kebab-case'); console.log(''); @@ -55,9 +53,27 @@ exports.run = function() { console.log(' If both cli flag and env var are used, cli flag takes precedence') ; }); + program.option('list', 'Use \'list browsers\' or \'list sets\' to get all available browsers or sets.') + .action((option) => logOptionFromConfig(option)); + program.parse(process.argv); }; +function logOptionFromConfig(option) { + const config = parseConfig(program.config); + + console.log(config[option] || `Cannot list option ${option}. Available options are: sets, browsers`); +} + +function parseConfig(configPath) { + const config = new Config(configPath); + + return { + sets: _.keys(config.sets).join(', '), + browsers: config.getBrowserIds().join(', ') + }; +} + function collect(newValue, array) { array = array || []; return array.concat(newValue); @@ -68,11 +84,11 @@ function runGemini(method, paths, options) { handleUncaughtExceptions(); - return Promise.try(function() { + return Promise.try(() => { checkForDeprecations(); return new Gemini(program.config, {cli: true, env: true}); }) - .then(function(gemini) { + .then((gemini) => { return gemini[method](paths, { sets: options.set, reporters: options.reporter || ['flat'], @@ -82,7 +98,7 @@ function runGemini(method, paths, options) { new: options.new }); }) - .then(function(stats) { + .then((stats) => { if (stats.failed > 0 || stats.errored > 0) { return 2; } @@ -93,7 +109,5 @@ function runGemini(method, paths, options) { } function exit(code) { - process.on('exit', function() { - process.exit(code); - }); + process.on('exit', () => process.exit(code)); }