Skip to content

Commit 4f84166

Browse files
committed
feat: support NCU_VERBOSITY=debug environment variable
Support NCU_VERBOSITY=debug which tells ncu to log CLI commands and HTTP requests to the command line for debugging.
1 parent 55c780e commit 4f84166

File tree

9 files changed

+90
-8
lines changed

9 files changed

+90
-8
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CLI tools for Node.js Core collaborators.
1313
- [Install](#install)
1414
- [Setting up credentials](#setting-up-credentials)
1515
- [Make sure your credentials won't be committed](#make-sure-your-credentials-wont-be-committed)
16+
- [Troubleshooting](#troubleshooting)
1617
- [Contributing](#contributing)
1718
- [License](#license)
1819

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

103+
### Troubleshooting
104+
105+
If you encounter an error that you cannot fix by yourself, please
106+
107+
1. Make sure you update NCU to the latest version
108+
2. Try again with the `NCU_VERBOSITY=debug` environment variable set and
109+
open an issue at https://github.com/nodejs/node-core-utils/issues with
110+
detailed logs.
111+
102112
## Contributing
103113

104114
See [CONTRIBUTING.md](./CONTRIBUTING.md).

bin/get-metadata

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
const path = require('path');
55
const { runAsync } = require('../lib/run');
6+
const { setVerbosityFromEnv } = require('../lib/verbosity');
7+
setVerbosityFromEnv();
68

79
const script = path.join(__dirname, 'git-node');
810
runAsync(process.execPath, [script, 'metadata', ...process.argv.slice(2)]);

bin/git-node

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
const yargs = require('yargs');
55
const path = require('path');
66
const epilogue = require('../components/git/epilogue');
7+
const { setVerbosityFromEnv } = require('../lib/verbosity');
8+
setVerbosityFromEnv();
79

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

bin/ncu-ci

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const {
1414
DAILY_MASTER
1515
}
1616
} = require('../lib/ci/ci_type_parser');
17+
const { setVerbosityFromEnv } = require('../lib/verbosity');
18+
setVerbosityFromEnv();
1719

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

bin/ncu-config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const {
66
getConfig, updateConfig, GLOBAL_CONFIG, PROJECT_CONFIG, LOCAL_CONFIG
77
} = require('../lib/config');
88

9+
const { setVerbosityFromEnv } = require('../lib/verbosity');
10+
setVerbosityFromEnv();
11+
912
const yargs = require('yargs');
1013
const argv = yargs
1114
.command({

bin/ncu-team

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const { runPromise } = require('../lib/run');
77
const CLI = require('../lib/cli');
88
const TeamInfo = require('../lib/team_info');
99

10+
const { setVerbosityFromEnv } = require('../lib/verbosity');
11+
setVerbosityFromEnv();
12+
1013
require('yargs') // eslint-disable-line
1114
.command({
1215
command: 'list <team> [org]',

lib/request.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ const fs = require('fs');
55
const path = require('path');
66
const { CI_DOMAIN } = require('./ci/ci_type_parser');
77
const proxy = require('./proxy');
8+
const {
9+
isDebugVerbosity,
10+
debuglog
11+
} = require('./verbosity');
12+
13+
function wrappedFetch(url, options, ...args) {
14+
if (isDebugVerbosity()) {
15+
debuglog('[fetch]', url);
16+
}
17+
return fetch(url, options, ...args);
18+
}
819

920
class Request {
1021
constructor(credentials) {
@@ -23,7 +34,7 @@ class Request {
2334
options.headers = options.headers || {};
2435
Object.assign(options.headers, this.getJenkinsHeaders());
2536
}
26-
return fetch(url, options);
37+
return wrappedFetch(url, options);
2738
}
2839

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

3344
async json(url, options = {}) {
3445
options.headers = options.headers || {};
35-
return this.fetch(url, options).then(res => res.json());
46+
const text = await this.text(url, options);
47+
try {
48+
return JSON.parse(text);
49+
} catch (e) {
50+
if (isDebugVerbosity()) {
51+
debuglog('[Request] Cannot parse JSON response from',
52+
url, ':\n', text);
53+
}
54+
throw e;
55+
}
3656
}
3757

3858
async gql(name, variables, path) {
@@ -81,7 +101,7 @@ class Request {
81101
})
82102
};
83103

84-
const result = await fetch(url, options).then(res => res.json());
104+
const result = await this.json(url, options);
85105
if (result.errors) {
86106
const { type, message } = result.errors[0];
87107
const err = new Error(`[${type}] GraphQL request Error: ${message}`);

lib/run.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

33
const { spawn, spawnSync } = require('child_process');
4-
4+
const {
5+
isDebugVerbosity,
6+
debuglog
7+
} = require('./verbosity');
58
const IGNORE = '__ignore__';
69

710
function runAsyncBase(cmd, args, {
@@ -10,10 +13,14 @@ function runAsyncBase(cmd, args, {
1013
captureStdout = false
1114
} = {}) {
1215
return new Promise((resolve, reject) => {
13-
const child = spawn(cmd, args, Object.assign({
16+
const opt = Object.assign({
1417
cwd: process.cwd(),
1518
stdio: captureStdout ? ['inherit', 'pipe', 'inherit'] : 'inherit'
16-
}, spawnArgs));
19+
}, spawnArgs);
20+
if (isDebugVerbosity()) {
21+
debuglog('[Spawn]', `${cmd} ${(args || []).join(' ')}`, opt);
22+
}
23+
const child = spawn(cmd, args, opt);
1724
let stdout;
1825
if (captureStdout) {
1926
stdout = '';
@@ -64,9 +71,13 @@ exports.runAsync = function(cmd, args, options) {
6471
};
6572

6673
exports.runSync = function(cmd, args, options) {
67-
const child = spawnSync(cmd, args, Object.assign({
74+
const opt = Object.assign({
6875
cwd: process.cwd()
69-
}, options));
76+
}, options);
77+
if (isDebugVerbosity()) {
78+
debuglog('[SpawnSync]', `${cmd} ${(args || []).join(' ')}`, opt);
79+
}
80+
const child = spawnSync(cmd, args, opt);
7081
if (child.error) {
7182
throw child.error;
7283
} else if (child.status !== 0) {

lib/verbosity.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const chalk = require('chalk');
4+
const util = require('util');
5+
6+
const VERBOSITY = {
7+
NONE: 0,
8+
DEBUG: 2
9+
};
10+
11+
let verbosity = VERBOSITY.NONE;
12+
13+
exports.isDebugVerbosity = function() {
14+
return verbosity === VERBOSITY.DEBUG;
15+
};
16+
17+
exports.setVerbosityFromEnv = function() {
18+
const env = (process.env.NCU_VERBOSITY || '').toUpperCase();
19+
if (Object.keys(VERBOSITY).includes(env)) {
20+
verbosity = VERBOSITY[env];
21+
}
22+
};
23+
24+
exports.debuglog = function(...args) {
25+
// Prepend a line break in case it's logged while the spinner is running
26+
console.error(chalk.green(util.format('\n[DEBUG]', ...args)));
27+
};
28+
29+
exports.VERBOSITY = VERBOSITY;

0 commit comments

Comments
 (0)