Skip to content

Commit

Permalink
use chalk instead of colors
Browse files Browse the repository at this point in the history
  • Loading branch information
fyockm committed Apr 28, 2014
1 parent 7f85d7b commit cba638a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 60 deletions.
80 changes: 43 additions & 37 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
var fs = require('fs'),
npm = require('npm'),
shell = require('shelljs'),
chalk = require('chalk'),
version = require('../package').version;

require('./colors');
var pkgType = {
contrib: 'Contrib',
custom: 'Custom'
};

function Progress() {
var interval, counter;
Expand Down Expand Up @@ -56,11 +60,16 @@ function ensureEmpty(path, force, callback) {
if (empty || force) {
callback();
} else {
console.log('Destination is not empty: '.warn + path);
console.log(chalk.yellow('Destination is not empty:'), path);
}
});
}

function getPackageInfo(type, data) {
var author = data.author ? chalk.green(' Author: ') + data.author.name : '';
return chalk.green(' ' + type + ': ') + data.name + '@' + data.version + author;
}

function loadPackageJson(path, callback) {
fs.readFile(path, function(err, data) {
if (err) return callback(err);
Expand All @@ -83,15 +92,15 @@ function checkVersion() {
npm.config.set('loglevel', 'warn');
npm.commands.outdated('meanio', true, function(err, list) {
if (err) {
console.log('Error: npm install failed'.error);
console.log(chalk.red('Error: npm install failed'));
return console.error(err);
}
var latest = list[2]; // list[2] holds the 'latest' value
if (latest < version) {
console.log(' meanio command line is out of date'.warn);
console.log(chalk.yellow(' meanio command line is out of date'));
console.log(' Current: ' + version + ' Latest: ' + latest);
} else {
console.log(' meanio command line at latest version: '.info + version);
console.log(chalk.green(' meanio command line at latest version:'), version);
}
});
});
Expand All @@ -100,7 +109,7 @@ function checkVersion() {
function requiresRoot(callback) {
loadPackageJson(process.cwd() + '/package.json', function(err, data) {
if (err || data.name !== 'mean') {
console.log('Invalid MEAN app or not in app root'.warn);
console.log(chalk.yellow('Invalid MEAN app or not in app root'));
} else {
callback();
}
Expand All @@ -111,18 +120,18 @@ function mongoConnect(env, callback) {
var config = require(process.cwd() + '/server/config/env/' + env + '.js');
require('mongodb').MongoClient.connect(config.db, function(err, db) {
if (err) {
console.log(' Error Connecting to database'.error);
console.log(chalk.red(' Error Connecting to database'));
console.log(err);
} else {
console.log(' DB connection successful!'.info);
console.log(chalk.green(' DB connection successful!'));
console.log();
callback(err, db);
}
});
}

exports.init = function(name, options) {
if (!shell.which('git')) return console.log(' Prerequisite not installed: git'.error);
if (!shell.which('git')) return console.log(chalk.red(' Prerequisite not installed: git'));

var source = (options.git ? 'git@github.com:linnovate/mean.git' : 'https://github.com/linnovate/mean.git');

Expand All @@ -131,26 +140,24 @@ exports.init = function(name, options) {
source = options.repo;
}

var message = 'Cloning branch: ' + options.branch + ' into destination folder: ' + name;
console.log(message.info);
console.log(chalk.green('Cloning branch: %s into destination folder:'), options.branch, name);

progress.start();
source = options.branch + ' ' + source + ' ' + name;
shell.exec('git clone -b ' + source, function(code) {
progress.stop();
if (code) return console.log('Error: git clone failed'.error);
if (code) return console.log(chalk.red('Error: git clone failed'));

loadPackageJson('./' + name + '/package.json', function(err, data) {
if (err) {
console.log('Something went wrong. Try again or use --git flag'.warn);
console.log('If the problem persists see past issues here: https://github.com/linnovate/mean/issues'.warn);
console.log('Or open a new issue here https://github.com/linnovate/mean/issues/new'.warn);
console.log(chalk.yellow('Something went wrong. Try again or use --git flag'));
console.log(chalk.yellow('If the problem persists see past issues here: https://github.com/linnovate/mean/issues'));
console.log(chalk.yellow('Or open a new issue here https://github.com/linnovate/mean/issues/new'));
//fallback code here
process.exit();
}

message = 'Version: ' + data.version + ' cloned';
console.log(message.info);
console.log(chalk.green('Version: %s cloned'), data.version);
console.log();
fs.readFile(__dirname + '/../img/logo.txt', function(err, data) {
console.log(data.toString());
Expand Down Expand Up @@ -181,26 +188,24 @@ exports.install = function(module, options) {
source = module;
}

var message = 'Installing module: ' + module + ' from branch (version): ' + options.branch;
console.log(message.info);
console.log(chalk.green('Installing module: %s from branch (version):'), module, options.branch);
console.log();

npm.load(function(err, npm) {
npm.commands.install([source], function(err) {
if (err) {
console.log('Error: npm install failed'.error);
console.log(chalk.red('Error: npm install failed'));
return console.error(err);
}

loadPackageJson('./node_modules/' + module + '/package.json', function(err, data) {
if (err) return console.error(err);

message = data.name + '@' + data.version + (data.author ? ' Author: ' + data.author.name : '');
console.log(message.info);
console.log();
console.log(getPackageInfo(pkgType.contrib, data));
if (!data.mean) {
console.log();
console.log('Warning: The module installed is not a valid MEAN module'.warn);
console.log(chalk.yellow('Warning: The module installed is not a valid MEAN module'));
}
});
});
Expand All @@ -210,15 +215,15 @@ exports.install = function(module, options) {

exports.uninstall = function(module) {
requiresRoot(function() {
console.log('Removing module: '.info + module);
console.log(chalk.yellow('Removing module:'), module);

npm.load(function(err, npm) {
npm.commands.uninstall([module], function(err) {
if (err) {
console.log('Error: npm install failed'.error);
console.log(chalk.red('Error: npm install failed'));
return console.error(err);
}
console.log(' npm uninstall complete'.info);
console.log(chalk.green(' npm uninstall complete'));
});
});
});
Expand All @@ -227,32 +232,33 @@ exports.uninstall = function(module) {
exports.list = function() {
requiresRoot(function() {

console.log(' MEAN Packages List:'.info);
console.log(chalk.green(' MEAN Packages List:'));
console.log(' -----------------');

function look(type) {
var path = type === 'Contrib' ? './node_modules/' : './packages/';
var path = type === pkgType.contrib ? './node_modules/' : './packages/';
fs.readdir(path, function(err, files) {
if (err || !files.length) return console.log(' No ' + type + ' Packages'.warn);
if (err || !files.length) return console.log(chalk.yellow(' No ' + type + ' Packages'));
files.forEach(function(file) {
loadPackageJson(path + file + '/package.json', function(err, data) {
if (!err && data.mean) console.log(' ' + type + ': '.info + data.name + '@' + data.version + (data.author ? ' Author: ' + data.author.name : ''));
if (!err && data.mean) console.log(getPackageInfo(data));
});
});
});
}

//look in node_modules for external packages
look('Contrib');
look(pkgType.contrib);

//look in packages for local modules
look('Custom');
look(pkgType.custom);
});
};

exports.status = function(options) {
requiresRoot(function() {
console.log();
console.log(' MEAN Status'.info);
console.log(chalk.green(' MEAN Status'));
console.log(' -----------');
console.log();
loadPackageJson('./package.json', function(err, data) {
Expand Down Expand Up @@ -293,13 +299,13 @@ exports.updateRole = function(email, options, type) {
var update = {};
switch (type) {
case 'a':
console.log(' Adding role `' + options.addRole + '` to user `' + email + '`'.info);
console.log(chalk.green(' Adding role `' + options.addRole + '` to user `' + email + '`'));
update.$push = {
roles: options.addRole
};
break;
case 'r':
console.log(' Removing role `' + options.removeRole + '` from user `' + email + '`'.info);
console.log(chalk.green(' Removing role `' + options.removeRole + '` from user `' + email + '`'));
update.$pull = {
roles: options.removeRole
};
Expand All @@ -315,8 +321,8 @@ exports.updateRole = function(email, options, type) {
upsert: false,
multi: false
}, function(err) {
if (err) console.log(err.message.warn);
else console.log('successfully updated'.info);
if (err) console.error(err);
else console.log(chalk.green('successfully updated'));
db.close();
});
});
Expand Down
16 changes: 0 additions & 16 deletions lib/colors.js

This file was deleted.

9 changes: 4 additions & 5 deletions lib/scaffold.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';

var fs = require('fs'),
shell = require('shelljs');

require('./colors');
shell = require('shelljs'),
chalk = require('chalk');

var data = {};

Expand All @@ -29,7 +28,7 @@ function camelCase(str) {
*/
function write(path, str) {
fs.writeFile(path, str);
console.log(' create : '.blue + path);
console.log(chalk.cyan(' create:'), path);
}

/**
Expand All @@ -54,7 +53,7 @@ function readTemplate(path) {
function mkdir(path, fn) {
shell.mkdir('-p', path);
shell.chmod(755, path);
console.log(' create : '.blue + path);
console.log(chalk.cyan(' create:'), path);
if (fn) fn();
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meanio",
"version": "0.4.10",
"version": "0.4.11",
"preferGlobal": true,
"description": "Simple command line interface for installing and managing MEAN apps",
"author": {
Expand Down Expand Up @@ -33,7 +33,7 @@
"npm": "1.4.x"
},
"dependencies": {
"colors": "^0.6.2",
"chalk": "^0.4.0",
"commander": "^2.2.0",
"lodash": "^2.4.1",
"mongodb": "^1.4.0",
Expand Down

1 comment on commit cba638a

@fyockm
Copy link
Contributor Author

@fyockm fyockm commented on cba638a Apr 28, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think chalk is much easier to understand and read in the code because it doesn't extend String.prototype.

Also, it seems like colors is no longer being maintained; see yeoman/yo#68.

Please sign in to comment.