From b0cfd23385374013b675c8ca81dbd5e8a81628e4 Mon Sep 17 00:00:00 2001 From: m-sureshraj Date: Mon, 9 Sep 2019 09:26:19 +0530 Subject: [PATCH] feat: Debugging improvements - 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 --- README.md | 7 ++++++- bin/jen.js | 17 ++++++++++++++--- lib/log.js | 4 +--- src/commands/config.js | 7 ++++++- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ca41ddf..d3d4b93 100644 --- a/README.md +++ b/README.md @@ -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: @@ -60,9 +61,13 @@ Commands: | `jen config` \| `c` | `--username` \| `-n`
`--token` \| `-t`
`--url` \| `-u`
`--job` \| `-j`

e.g. Reconfigure username & token
`jen config --username --token ` | 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 ``` diff --git a/bin/jen.js b/bin/jen.js index de6f674..fd8fbd0 100755 --- a/bin/jen.js +++ b/bin/jen.js @@ -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') @@ -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); } diff --git a/lib/log.js b/lib/log.js index c9ae991..bc4f181 100644 --- a/lib/log.js +++ b/lib/log.js @@ -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) { diff --git a/src/commands/config.js b/src/commands/config.js index 8dcec49..05d1534 100644 --- a/src/commands/config.js +++ b/src/commands/config.js @@ -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) { @@ -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; @@ -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)); };