Skip to content

Commit

Permalink
Merge pull request #189 from opentable/safe-color
Browse files Browse the repository at this point in the history
Safe colors in order to avoid extending object and better testing
  • Loading branch information
matteofigus committed Feb 24, 2016
2 parents 46e2d36 + e5fb648 commit c8be21f
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 121 deletions.
52 changes: 29 additions & 23 deletions cli/facade/dev.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var async = require('async');
var colors = require('colors');
var colors = require('colors/safe');
var format = require('stringformat');
var path = require('path');
var _ = require('underscore');
Expand All @@ -18,6 +18,12 @@ module.exports = function(dependencies){
var local = dependencies.local,
logger = dependencies.logger;

var log = {
err: function(msg){ return logger.log(colors.red(msg)); },
ok: function(msg){ return logger.log(colors.green(msg)); },
warn: function(msg, noNewLine){ return logger[!!noNewLine ? 'logNoNewLine' : 'log'](colors.yellow(msg)); }
};

return function(opts){

var componentsDir = opts.dirName,
Expand All @@ -29,10 +35,10 @@ module.exports = function(dependencies){
var installMissingDeps = function(missing, cb){
if(_.isEmpty(missing)){ return cb(); }

logger.log(format(strings.messages.cli.INSTALLING_DEPS, missing.join(', ')).yellow);
log.warn(format(strings.messages.cli.INSTALLING_DEPS, missing.join(', ')));
npmInstaller(missing, componentsDir, function(err, result){
if(!!err){
logger.log(err.toString().red);
log.err(err.toString());
throw err;
}
cb();
Expand All @@ -42,9 +48,9 @@ module.exports = function(dependencies){
var watchForChanges = function(components, callback){
watch(components, componentsDir, function(err, changedFile){
if(!!err){
logger.log(format(strings.errors.generic.red, err));
log.err(format(strings.errors.generic, err));
} else {
logger.log(format(strings.messages.cli.CHANGES_DETECTED, changedFile).yellow);
log.warn(format(strings.messages.cli.CHANGES_DETECTED, changedFile));
callback(components);
}
});
Expand All @@ -57,7 +63,7 @@ module.exports = function(dependencies){

if(!packaging){
packaging = true;
logger.logNoNewLine(strings.messages.cli.PACKAGING_COMPONENTS.yellow);
log.warn(strings.messages.cli.PACKAGING_COMPONENTS, true);

async.eachSeries(componentsDirs, function(dir, cb){
local.package(dir, false, function(err){
Expand All @@ -67,33 +73,33 @@ module.exports = function(dependencies){
}, function(error){
if(!!error){
var errorDescription = ((error instanceof SyntaxError) || !!error.message) ? error.message : error;
logger.log(format(strings.errors.cli.PACKAGING_FAIL, componentsDirs[i], errorDescription.red));
logger.log(strings.messages.cli.RETRYING_10_SECONDS.yellow);
log.err(format(strings.errors.cli.PACKAGING_FAIL, componentsDirs[i], errorDescription));
log.warn(strings.messages.cli.RETRYING_10_SECONDS);
setTimeout(function(){
packaging = false;
packageComponents(componentsDirs);
}, 10000);
} else {
packaging = false;
logger.log('OK'.green);
log.ok('OK');
callback();
}
});
}
};

var loadDependencies = function(components, cb){
logger.logNoNewLine(strings.messages.cli.CHECKING_DEPENDENCIES.yellow);
log.warn(strings.messages.cli.CHECKING_DEPENDENCIES, true);

var dependencies = getComponentsDependencies(components),
missing = getMissingDeps(dependencies, components);

if(_.isEmpty(missing)){
logger.log('OK'.green);
log.ok('OK');
return cb(dependencies);
}

logger.log('FAIL'.red);
log.err('FAIL');
installMissingDeps(missing, function(){
loadDependencies(components, cb);
});
Expand All @@ -103,30 +109,30 @@ module.exports = function(dependencies){
var mockedPlugins = getMockedPlugins(logger);

mockedPlugins.forEach(function(p){
registry.register(p);
registry.register(p);
});

registry.on('request', function(data){
if(data.errorCode === 'PLUGIN_MISSING_FROM_REGISTRY'){
logger.log(format(strings.errors.cli.PLUGIN_MISSING_FROM_REGISTRY, data.errorDetails, strings.commands.cli.MOCK_PLUGIN.blue).red);
log.err(format(strings.errors.cli.PLUGIN_MISSING_FROM_REGISTRY, data.errorDetails, colors.blue(strings.commands.cli.MOCK_PLUGIN)));
} else if(data.errorCode === 'PLUGIN_MISSING_FROM_COMPONENT'){
logger.log(format(strings.errors.cli.PLUGIN_MISSING_FROM_COMPONENT, data.errorDetails).red);
log.err(format(strings.errors.cli.PLUGIN_MISSING_FROM_COMPONENT, data.errorDetails));
}
});
};

logger.logNoNewLine(strings.messages.cli.SCANNING_COMPONENTS.yellow);
log.warn(strings.messages.cli.SCANNING_COMPONENTS, true);
local.getComponentsByDir(componentsDir, function(err, components){

if(err){
return logger.log(err.red);
return log.err(err);
} else if(_.isEmpty(components)){
return logger.log(format(errors.DEV_FAIL, errors.COMPONENTS_NOT_FOUND).red);
return log.err(format(errors.DEV_FAIL, errors.COMPONENTS_NOT_FOUND));
}

logger.log('OK'.green);
log.ok('OK');
_.forEach(components, function(component){
logger.log('├── '.green + component);
logger.log(colors.green('├── ') + component);
});

loadDependencies(components, function(dependencies){
Expand All @@ -145,14 +151,14 @@ module.exports = function(dependencies){

registerPlugins(registry);

logger.logNoNewLine(format(strings.messages.cli.REGISTRY_STARTING, baseUrl).yellow);
log.warn(format(strings.messages.cli.REGISTRY_STARTING, baseUrl));
registry.start(function(err, app){

if(err){
if(err.code === 'EADDRINUSE'){
return logger.log(format(strings.errors.cli.PORT_IS_BUSY, port).red);
return log.err(format(strings.errors.cli.PORT_IS_BUSY, port));
} else {
logger.log(err.red);
log.err(err);
}
}

Expand Down
6 changes: 3 additions & 3 deletions cli/facade/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var colors = require('colors');
var colors = require('colors/safe');
var format = require('stringformat');

var strings = require('../../resources/index');
Expand All @@ -25,9 +25,9 @@ module.exports = function(dependencies){
err = errors.TEMPLATE_TYPE_NOT_VALID;
}

logger.log(format(errors.INIT_FAIL.red, err));
logger.log(format(colors.red(errors.INIT_FAIL), err));
} else {
logger.log(format(strings.messages.cli.COMPONENT_INITED.green, componentName));
logger.log(format(colors.green(strings.messages.cli.COMPONENT_INITED), componentName));
}
});
};
Expand Down
4 changes: 2 additions & 2 deletions cli/facade/mock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var colors = require('colors');
var colors = require('colors/safe');
var format = require('stringformat');

var strings = require('../../resources/index');
Expand All @@ -12,7 +12,7 @@ module.exports = function(dependencies){

return function(opts){
local.mock(opts, function(err, res){
return logger.log(format(strings.messages.cli.MOCKED_PLUGIN, opts.targetName, opts.targetValue).green);
return logger.log(colors.green(format(strings.messages.cli.MOCKED_PLUGIN, opts.targetName, opts.targetValue)));
});
};
};
93 changes: 43 additions & 50 deletions cli/facade/publish.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var async = require('async');
var colors = require('colors');
var colors = require('colors/safe');
var format = require('stringformat');
var path = require('path');
var read = require('read');
Expand All @@ -16,118 +16,111 @@ module.exports = function(dependencies){
local = dependencies.local,
logger = dependencies.logger;

return function(opts, reallyDoneThisTime){
var log = {
err: function(msg){ return logger.log(colors.red(msg)); },
ok: function(msg){ return logger.log(colors.green(msg)); },
warn: function(msg){ return logger.log(colors.yellow(msg)); }
};

return function(opts, callback){

reallyDoneThisTime = reallyDoneThisTime || _.noop;
callback = callback || _.noop;

var componentPath = opts.componentPath,
packageDir = path.resolve(componentPath, '_package'),
compressedPackagePath = path.resolve(componentPath, 'package.tar.gz');

var getCredentials = function(callback){
var getCredentials = function(cb){
if(opts.username && opts.password){
logger.log(strings.messages.cli.USING_CREDS.green);
return callback(null, {
username: opts.username,
password: opts.password
});
log.ok(strings.messages.cli.USING_CREDS);
return cb(null, _.pick(opts, 'username', 'password'));
}

logger.log(strings.messages.cli.ENTER_USERNAME.yellow);
log.warn(strings.messages.cli.ENTER_USERNAME);

read({}, function(err, username){

logger.log(strings.messages.cli.ENTER_PASSWORD.yellow);
log.warn(strings.messages.cli.ENTER_PASSWORD);

read({ silent: true }, function(err, password){
callback(null, { username: username, password: password});
cb(null, { username: username, password: password});
});
});
};

var packageAndCompress = function(callback){

logger.log(format(strings.messages.cli.PACKAGING.yellow, packageDir.green));
var packageAndCompress = function(cb){
log.warn(format(strings.messages.cli.PACKAGING, packageDir));

local.package(componentPath, function(err, component){
if(err){
return callback(err);
}
if(err){ return cb(err); }

logger.log(format(strings.messages.cli.COMPRESSING.yellow, compressedPackagePath.green));
log.warn(format(strings.messages.cli.COMPRESSING, compressedPackagePath));

local.compress(packageDir, compressedPackagePath, function(err){
if(err){
return callback(err);
}

callback(null, component);
if(err){ return cb(err); }
cb(null, component);
});
});
};

var putComponentToRegistry = function(options, callback){

logger.log(format(strings.messages.cli.PUBLISHING.yellow, options.route.green));
var putComponentToRegistry = function(options, cb){
log.warn(format(strings.messages.cli.PUBLISHING, options.route));

registry.putComponent(options, function(err, res){

if(!!err){

if(err === 'Unauthorized'){
if(!!options.username || !!options.password){
logger.log(format(strings.errors.cli.PUBLISHING_FAIL, strings.errors.cli.INVALID_CREDENTIALS).red);
return callback(err);
log.err(format(strings.errors.cli.PUBLISHING_FAIL, strings.errors.cli.INVALID_CREDENTIALS));
return cb(err);
}

logger.log(strings.messages.cli.REGISTRY_CREDENTIALS_REQUIRED.yellow);
log.warn(strings.messages.cli.REGISTRY_CREDENTIALS_REQUIRED);

return getCredentials(function(err, credentials){
putComponentToRegistry(_.extend(options, {
username: credentials.username,
password: credentials.password
}), callback);
putComponentToRegistry(_.extend(options, credentials), cb);
});

} else if(err.code === 'cli_version_not_valid') {
var upgradeCommand = format(strings.commands.cli.UPGRADE, err.details.suggestedVersion),
errorDetails = format(strings.errors.cli.OC_CLI_VERSION_NEEDS_UPGRADE, upgradeCommand.blue);
logger.log(format(strings.errors.cli.PUBLISHING_FAIL, errorDetails).red);
return callback();
errorDetails = format(strings.errors.cli.OC_CLI_VERSION_NEEDS_UPGRADE, colors.blue(upgradeCommand));

log.err(format(strings.errors.cli.PUBLISHING_FAIL, errorDetails));
return cb();
} else if(err.code === 'node_version_not_valid') {
var details = format(strings.errors.cli.NODE_CLI_VERSION_NEEDS_UPGRADE, err.details.suggestedVersion);
logger.log(format(strings.errors.cli.PUBLISHING_FAIL, details).red);
return callback();

log.err(format(strings.errors.cli.PUBLISHING_FAIL, details));
return cb();
} else {
logger.log(format(strings.errors.cli.PUBLISHING_FAIL, err).red);
return callback();
log.err(format(strings.errors.cli.PUBLISHING_FAIL, err));
return cb();
}
} else {
logger.log(format(strings.messages.cli.PUBLISHED, options.route.green).yellow);
return callback();
log.ok(format(strings.messages.cli.PUBLISHED, options.route.green));
return cb();
}
});
};

registry.get(function(err, registryLocations){
if(err){
return logger.log(err.red);
return log.err(err);
}

packageAndCompress(function(err, component){
if(err){
return logger.log(format(strings.errors.cli.PACKAGE_CREATION_FAIL, err).red);
return log.err(format(strings.errors.cli.PACKAGE_CREATION_FAIL, err));
}

async.eachSeries(registryLocations, function(l, done){
async.eachSeries(registryLocations, function(l, next){
var registryUrl = l,
registryLength = registryUrl.length,
registryNormalised = registryUrl.slice(registryLength - 1) === '/' ? registryUrl.slice(0, registryLength - 1) : registryUrl,
componentRoute = format('{0}/{1}/{2}', registryNormalised, component.name, component.version);

putComponentToRegistry({ route: componentRoute, path: compressedPackagePath}, done);
putComponentToRegistry({ route: componentRoute, path: compressedPackagePath}, next);
}, function(err){
local.cleanup(compressedPackagePath, reallyDoneThisTime);
local.cleanup(compressedPackagePath, callback);
});
});
});
Expand Down
Loading

0 comments on commit c8be21f

Please sign in to comment.