-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve error handling + add debug option
- Loading branch information
Showing
4 changed files
with
59 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |