Skip to content

Commit

Permalink
feat: Debugging improvements
Browse files Browse the repository at this point in the history
- Debugging can be enabled by passing -d or --debug option
  e.g. > jen status -d
- Added debug param usage to the help output
- Display project name in the config output #12
  • Loading branch information
m-sureshraj committed Sep 9, 2019
1 parent 3e65bcf commit b0cfd23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Jenkins personal assistant
Options:
-v, --version output the version number
-d, --debug Enable debug mode
-h, --help output usage information
Commands:
Expand All @@ -60,9 +61,13 @@ Commands:
| `jen config` \| `c` | `--username` \| `-n` <br> `--token` \| `-t` <br> `--url` \| `-u` <br> `--job` \| `-j` <br> <br> e.g. Reconfigure username & token <br> `jen config --username <foo> --token <bar>` | Overwrite project jenni config. Without any options it will print current config. |

## Debug
It's basic for the moment, but you can use `DEBUG_JEN=true` to log debug messages. E.g
It's basic for the moment, pass `-d` or `--debug` to log debug messages. Can also be enabled by setting the environment variable `DEBUG_JEN` to `true`. E.g.

```
> jen status -d
// OR
> DEBUG_JEN=true jen status
```

Expand Down
17 changes: 14 additions & 3 deletions bin/jen.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ const pkg = require('../package.json');
const { init, status, config, open } = require('../src');
const { debug } = require('../lib/log');

debug(`jen v${pkg.version}`);
const args = process.argv.slice(2);

jen.version(pkg.version, '-v, --version').description('Jenkins personal assistant');
// Why manual check instead of `jen.debug`?
// Because we have to enable the debug before initializing jen: `jen.parse(process.argv);`
if (args.includes('-d') || args.includes('--debug')) {
process.env.DEBUG_JEN = true;
}

debug(`Running jen v${pkg.version}`);

jen
.version(pkg.version, '-v, --version')
.description('Jenkins personal assistant')
.option('-d, --debug', 'Enable debug mode');

jen
.command('init')
Expand Down Expand Up @@ -49,7 +60,7 @@ jen
});

// print usage info if argument is empty.
if (!process.argv.slice(2).length) {
if (!args.length) {
jen.outputHelp();
process.exit(0);
}
Expand Down
4 changes: 1 addition & 3 deletions lib/log.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const { gray, red } = require('kleur');

const isDebuggingEnabled = process.env.DEBUG_JEN;

function debug(msg) {
isDebuggingEnabled && console.log(msg);
process.env.DEBUG_JEN && console.log(msg);
}

function logNetworkErrors(err) {
Expand Down
7 changes: 6 additions & 1 deletion src/commands/config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const path = require('path');

const Conf = require('conf');
const { blue, bold, yellow, gray, red, green } = require('kleur');

const { isGitRepository, getGitRootDirPath } = require('../../lib/git-cmd');
const { printConfig } = require('../../lib/cli-table');
const { isValidUrl } = require('../../lib/util');
const { debug } = require('../../lib/log');

// todo: Make store singleton
const store = new Conf();

function updateStoreWithNewConfiguration(storeKey, oldConfig, updatedConfig) {
Expand Down Expand Up @@ -51,7 +55,7 @@ module.exports = async function config(options = {}) {
if (!store.has(gitRootPath)) {
console.log(
yellow(
`Config is empty! Introduce jen to your project via ${bold(blue('> jen init'))}`
`Config Not Found! Introduce jen to your project via ${bold(blue('> jen init'))}`
)
);
return;
Expand All @@ -74,6 +78,7 @@ module.exports = async function config(options = {}) {
}

// else just print the current config
console.log(yellow(`Configuration of ${path.basename(gitRootPath)}`));
printConfig(oldConfig);
console.log(gray('Config path - ' + store.path));
};

0 comments on commit b0cfd23

Please sign in to comment.