Skip to content

Commit

Permalink
feat: support NCU_VERBOSITY=debug environment variable
Browse files Browse the repository at this point in the history
Support NCU_VERBOSITY=debug which tells ncu to log CLI commands
and HTTP requests to the command line for debugging.
  • Loading branch information
joyeecheung committed Sep 29, 2020
1 parent 55c780e commit 4f84166
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CLI tools for Node.js Core collaborators.
- [Install](#install)
- [Setting up credentials](#setting-up-credentials)
- [Make sure your credentials won't be committed](#make-sure-your-credentials-wont-be-committed)
- [Troubleshooting](#troubleshooting)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -99,6 +100,15 @@ serialized configurations.
If you ever accidentally commit your access token on GitHub, you can simply
revoke that token and use a new one.

### Troubleshooting

If you encounter an error that you cannot fix by yourself, please

1. Make sure you update NCU to the latest version
2. Try again with the `NCU_VERBOSITY=debug` environment variable set and
open an issue at https://github.com/nodejs/node-core-utils/issues with
detailed logs.

## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md).
Expand Down
2 changes: 2 additions & 0 deletions bin/get-metadata
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

const path = require('path');
const { runAsync } = require('../lib/run');
const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

const script = path.join(__dirname, 'git-node');
runAsync(process.execPath, [script, 'metadata', ...process.argv.slice(2)]);
2 changes: 2 additions & 0 deletions bin/git-node
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
const yargs = require('yargs');
const path = require('path');
const epilogue = require('../components/git/epilogue');
const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

const commandDir = path.join(__dirname, '..', 'components', 'git');

Expand Down
2 changes: 2 additions & 0 deletions bin/ncu-ci
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const {
DAILY_MASTER
}
} = require('../lib/ci/ci_type_parser');
const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

const { listBuilds } = require('../lib/ci/ci_utils');

Expand Down
3 changes: 3 additions & 0 deletions bin/ncu-config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const {
getConfig, updateConfig, GLOBAL_CONFIG, PROJECT_CONFIG, LOCAL_CONFIG
} = require('../lib/config');

const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

const yargs = require('yargs');
const argv = yargs
.command({
Expand Down
3 changes: 3 additions & 0 deletions bin/ncu-team
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const { runPromise } = require('../lib/run');
const CLI = require('../lib/cli');
const TeamInfo = require('../lib/team_info');

const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

require('yargs') // eslint-disable-line
.command({
command: 'list <team> [org]',
Expand Down
26 changes: 23 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ const fs = require('fs');
const path = require('path');
const { CI_DOMAIN } = require('./ci/ci_type_parser');
const proxy = require('./proxy');
const {
isDebugVerbosity,
debuglog
} = require('./verbosity');

function wrappedFetch(url, options, ...args) {
if (isDebugVerbosity()) {
debuglog('[fetch]', url);
}
return fetch(url, options, ...args);
}

class Request {
constructor(credentials) {
Expand All @@ -23,7 +34,7 @@ class Request {
options.headers = options.headers || {};
Object.assign(options.headers, this.getJenkinsHeaders());
}
return fetch(url, options);
return wrappedFetch(url, options);
}

async text(url, options = {}) {
Expand All @@ -32,7 +43,16 @@ class Request {

async json(url, options = {}) {
options.headers = options.headers || {};
return this.fetch(url, options).then(res => res.json());
const text = await this.text(url, options);
try {
return JSON.parse(text);
} catch (e) {
if (isDebugVerbosity()) {
debuglog('[Request] Cannot parse JSON response from',
url, ':\n', text);
}
throw e;
}
}

async gql(name, variables, path) {
Expand Down Expand Up @@ -81,7 +101,7 @@ class Request {
})
};

const result = await fetch(url, options).then(res => res.json());
const result = await this.json(url, options);
if (result.errors) {
const { type, message } = result.errors[0];
const err = new Error(`[${type}] GraphQL request Error: ${message}`);
Expand Down
21 changes: 16 additions & 5 deletions lib/run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';

const { spawn, spawnSync } = require('child_process');

const {
isDebugVerbosity,
debuglog
} = require('./verbosity');
const IGNORE = '__ignore__';

function runAsyncBase(cmd, args, {
Expand All @@ -10,10 +13,14 @@ function runAsyncBase(cmd, args, {
captureStdout = false
} = {}) {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, Object.assign({
const opt = Object.assign({
cwd: process.cwd(),
stdio: captureStdout ? ['inherit', 'pipe', 'inherit'] : 'inherit'
}, spawnArgs));
}, spawnArgs);
if (isDebugVerbosity()) {
debuglog('[Spawn]', `${cmd} ${(args || []).join(' ')}`, opt);
}
const child = spawn(cmd, args, opt);
let stdout;
if (captureStdout) {
stdout = '';
Expand Down Expand Up @@ -64,9 +71,13 @@ exports.runAsync = function(cmd, args, options) {
};

exports.runSync = function(cmd, args, options) {
const child = spawnSync(cmd, args, Object.assign({
const opt = Object.assign({
cwd: process.cwd()
}, options));
}, options);
if (isDebugVerbosity()) {
debuglog('[SpawnSync]', `${cmd} ${(args || []).join(' ')}`, opt);
}
const child = spawnSync(cmd, args, opt);
if (child.error) {
throw child.error;
} else if (child.status !== 0) {
Expand Down
29 changes: 29 additions & 0 deletions lib/verbosity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const chalk = require('chalk');
const util = require('util');

const VERBOSITY = {
NONE: 0,
DEBUG: 2
};

let verbosity = VERBOSITY.NONE;

exports.isDebugVerbosity = function() {
return verbosity === VERBOSITY.DEBUG;
};

exports.setVerbosityFromEnv = function() {
const env = (process.env.NCU_VERBOSITY || '').toUpperCase();
if (Object.keys(VERBOSITY).includes(env)) {
verbosity = VERBOSITY[env];
}
};

exports.debuglog = function(...args) {
// Prepend a line break in case it's logged while the spinner is running
console.error(chalk.green(util.format('\n[DEBUG]', ...args)));
};

exports.VERBOSITY = VERBOSITY;

0 comments on commit 4f84166

Please sign in to comment.