Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
feat(cli): Add command 'list' to show list of browsers or sets from c…
Browse files Browse the repository at this point in the history
…onfig
  • Loading branch information
rostik404 committed Oct 14, 2016
1 parent b41d3fe commit b5e4b4a
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
@@ -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 <browser>', 'run test only in specified browser', collect)
Expand All @@ -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 <reporter>', 'test reporter', collect)
.option('-r, --reporter <reporter>', 'test reporter. Available reporters are: flat, vflat, html.', collect)
.option('-s, --set <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('');
Expand All @@ -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);
Expand All @@ -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'],
Expand All @@ -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;
}
Expand All @@ -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));
}

0 comments on commit b5e4b4a

Please sign in to comment.