<commit_before>#!/usr/bin/env node
/*
 * npm-dview
 * http://skratchdot.com/projects/npm-dview
 *
 * Copyright (c) 2013 skratchdot
 * Licensed under the MIT license.
 */
'use strict';

var Table = require('cli-table'),
	async = require('async'),
	path = require('path'),
	program = require('commander'),
	exec = require('child_process').exec,
	fs = require('fs'),
	// state / misc variables
	appInfo = require('../package.json'),
	packageInfo,
	showDev = true,
	showRegular = true,
	// functions
	getDependencies,
	printTable,
	populateDependencies;

// setup command line options
program
	.version(appInfo.version, '-v, --version')
	.option('--dep-only', 'Only show regular dependencies')
	.option('--dev-only', 'Only show development dependencies')
	.option('--file <file>', 'The location of the package.json file',
			path.join(process.cwd(), path.sep, '/package.json'))
	.parse(process.argv);

// which tables do we show?
if (program.devOnly && !program.depOnly) {
	showRegular = false;
} else if (program.depOnly) {
	showDev = false;
}

// print processing message
console.log('Processing: ' + program.file);

// process our file
async.series({
	fnExists : function (callback) {
		fs.exists(program.file, function (exists) {
			// cannot find file
			if (!exists) {
				callback('Cannot find the given package.json file: ' + program.file);
			} else {
				callback(null, true);
			}
		});
	},
	fnIsFile : function (callback) {
		fs.stat(program.file, function (err, stats) {
			if (err) { // an error occurred
				callback(err);
			} else if (!stats.isFile()) { // not a file
				callback('This is not a file: ' + program.file);
			} else {
				callback(null, true);
			}
		});
	},
	fnIsJson : function (callback) {
		fs.readFile(program.file, function (err, data) {
			if (err) { // an error occurred
				callback(err);
			} else {
				try {
					packageInfo = JSON.parse(data.toString());
					callback(null, packageInfo);
				} catch (e) {
					callback(e);
				}
			}
		});
	},
	fnProcessDependenciesRegular : function (callback) {
		getDependencies('dependencies', showRegular, callback);
	},
	fnProcessDependenciesDev : function (callback) {
		getDependencies('devDependencies', showDev, callback);
	}
}, function (err, results) {
	if (err) {
		throw err;
	} else {
		printTable('Regular Dependencies', results.fnProcessDependenciesRegular);
		printTable('Dev Dependencies', results.fnProcessDependenciesDev);
	}
});

printTable = function (title, items) {
	var i,
		item,
		table = new Table({
			head: ['Module Name', 'Requested Version', 'Remote Version']<commit_msg>changing error handling<commit_after>#!/usr/bin/env node
/*
 * npm-dview
 * http://skratchdot.com/projects/npm-dview
 *
 * Copyright (c) 2013 skratchdot
 * Licensed under the MIT license.
 */
'use strict';

var Table = require('cli-table'),
	async = require('async'),
	path = require('path'),
	program = require('commander'),
	exec = require('child_process').exec,
	fs = require('fs'),
	// state / misc variables
	appInfo = require('../package.json'),
	packageInfo,
	showDev = true,
	showRegular = true,
	// functions
	getDependencies,
	printTable,
	populateDependencies;

// setup command line options
program
	.version(appInfo.version, '-v, --version')
	.option('--dep-only', 'Only show regular dependencies')
	.option('--dev-only', 'Only show development dependencies')
	.option('--file <file>', 'The location of the package.json file',
			path.join(process.cwd(), path.sep, '/package.json'))
	.parse(process.argv);

// which tables do we show?
if (program.devOnly && !program.depOnly) {
	showRegular = false;
} else if (program.depOnly) {
	showDev = false;
}

// print processing message
console.log('Processing: ' + program.file);

// process our file
async.series({
	fnExists : function (callback) {
		fs.exists(program.file, function (exists) {
			// cannot find file
			if (!exists) {
				callback('Cannot find the given package.json file: ' + program.file);
			} else {
				callback(null, true);
			}
		});
	},
	fnIsFile : function (callback) {
		fs.stat(program.file, function (err, stats) {
			if (err) { // an error occurred
				callback(err);
			} else if (!stats.isFile()) { // not a file
				callback('This is not a file: ' + program.file);
			} else {
				callback(null, true);
			}
		});
	},
	fnIsJson : function (callback) {
		fs.readFile(program.file, function (err, data) {
			if (err) { // an error occurred
				callback(err);
			} else {
				try {
					packageInfo = JSON.parse(data.toString());
					callback(null, packageInfo);
				} catch (e) {
					callback(e);
				}
			}
		});
	},
	fnProcessDependenciesRegular : function (callback) {
		getDependencies('dependencies', showRegular, callback);
	},
	fnProcessDependenciesDev : function (callback) {
		getDependencies('devDependencies', showDev, callback);
	}
}, function (err, results) {
	if (err) {
		console.error(err);
		process.exit(1);
	} else {
		printTable('Regular Dependencies', results.fnProcessDependenciesRegular);
		printTable('Dev Dependencies', results.fnProcessDependenciesDev);
	}
});

printTable = function (title, items) {
	var i,
		item,
		table = new Table({
			head: ['Module Name', 'Requested Version', 'Remote Version']
{'fraction_non_alphanumeric': 0.15865384615384615, 'fraction_numerical': 0.0017307692307692308, 'mean_word_length': 3.732484076433121, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7172964', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1643, 'n_words': 423}