Skip to content

Commit

Permalink
improve error handling + add debug option
Browse files Browse the repository at this point in the history
  • Loading branch information
ranyitz committed Mar 10, 2018
1 parent 1a40998 commit c1dc713
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 51 deletions.
11 changes: 0 additions & 11 deletions bin/qnm-list.js

This file was deleted.

39 changes: 1 addition & 38 deletions bin/qnm.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,3 @@
#! /usr/bin/env node

/* eslint-disable no-console */
const program = require('commander');
const pkg = require('../package.json');
const Workspace = require('../src/workspace/workspace');
const setupCompletions = require('../src/completions/setup-completions');

const matchAction = require('../src/actions/match');
const getAction = require('../src/actions/get');

program
.version(pkg.version)
.arguments('[module]', 'prints module version from the node_modules')
.option('-m, --match', 'works like grep, and prints modules which the provided string matches')
.command('list', 'list all node_modules with their versions');

program.parse(process.argv);

// if no arguments specified, show help
if (program.args.length === 0) {
program.help();
}

const preDefinedCommands = program.commands.map(c => c._name);

if (!preDefinedCommands.includes(program.args[0]) && program.args[0] !== 'completion') {
const arg = program.args[0];
const { match } = program;

const workspace = Workspace.loadSync();

if (match) {
console.log(matchAction(workspace, arg));
} else {
console.log(getAction(workspace, arg));
}
}

setupCompletions(preDefinedCommands);
require('../src/cli');
54 changes: 54 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable no-console */
const program = require('commander');
const pkg = require('../package.json');
const Workspace = require('./workspace/workspace');
const setupCompletions = require('./completions/setup-completions');

const matchAction = require('./actions/match');
const getAction = require('./actions/get');
const listAction = require('./actions/list');

const handleError = require('./handler-error');

try {
program
.version(pkg.version)
.command('list')
.description('list all node_modules with their versions')
.action(() => {
const workspace = Workspace.loadSync();
console.log(listAction(workspace));
});

program
.arguments('[module]', 'prints module version from the node_modules')
.option('-m, --match', 'works like grep, and prints modules which the provided string matches')
.option('-d, --debug', 'see full error messages, mostly for debugging');

program.parse(process.argv);

// if no arguments specified, show help
if (program.args.length === 0) {
program.help();
}

const preDefinedCommands = program.commands.map(c => c._name);
setupCompletions(preDefinedCommands);

const firstArg = program.rawArgs[2];

if (!preDefinedCommands.includes(firstArg) && firstArg !== 'completion') {
const arg = program.args[0];
const { match } = program;

const workspace = Workspace.loadSync();

if (match) {
console.log(matchAction(workspace, arg));
} else {
console.log(getAction(workspace, arg));
}
}
} catch (error) {
handleError(error, program.debug);
}
6 changes: 4 additions & 2 deletions src/handler-error.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable no-console */
const chalk = require('chalk');

module.exports = (error, verbose) => {
if (!verbose) {
module.exports = (error, debug) => {
if (!debug) {
console.error(chalk.red(error.toString()));
} else {
console.error(error.stack);
}

process.exit(1);
};

0 comments on commit c1dc713

Please sign in to comment.