Skip to content

Commit

Permalink
lib: show stderr when getting project from npm fails (nodejs#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored and targos committed Oct 15, 2019
1 parent 27e267f commit 84187a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 11 additions & 5 deletions lib/grab-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ async function grabProject(context) {
`Downloading project: ${packageName}`
);
let options = createOptions(context.path, context);
options.stdio = ['ignore', 'pipe', 'ignore'];
options.stdio = ['ignore', 'pipe', 'pipe'];
const proc = spawn('npm', ['pack', packageName], options);

let filename = '';

// Default timeout to 10 minutes if not provided
const timeout = setTimeout(
cleanup,
Expand All @@ -56,10 +54,18 @@ async function grabProject(context) {
reject(new Error('Download Timed Out'));
}

let filename = '';
proc.stdout.setEncoding('utf8');
proc.stdout.on('data', (chunk) => {
filename += chunk;
});

let stderr = '';
proc.stderr.setEncoding('utf8');
proc.stderr.on('data', (chunk) => {
stderr += chunk;
});

proc.on('error', (err) => {
bailed = true;
clearTimeout(timeout);
Expand All @@ -70,15 +76,15 @@ async function grabProject(context) {
if (bailed) return;
clearTimeout(timeout);
if (code > 0) {
return reject(new Error('Failure getting project from npm'));
return reject(new Error(`Failure getting project from npm\n${stderr}`));
}
filename = filename.trim();
if (context.module.type === 'directory') {
filename = filename.trim().split('\n');
filename = filename.pop();
}
if (filename === '') {
return reject(new Error('No project downloaded'));
return reject(new Error(`No project downloaded\n${stderr}`));
}
context.emit(
'data',
Expand Down
5 changes: 3 additions & 2 deletions test/test-grab-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ test('grab-project: local', async (t) => {
});

test('grab-project: module does not exist', async (t) => {
t.plan(1);
t.plan(2);
const context = {
emit: function() {},
path: sandbox,
Expand All @@ -101,7 +101,8 @@ test('grab-project: module does not exist', async (t) => {
try {
await grabProject(context);
} catch (err) {
t.equals(err && err.message, 'Failure getting project from npm');
t.ok(err);
t.match(err.message, /^Failure getting project from npm/);
}
});

Expand Down

0 comments on commit 84187a6

Please sign in to comment.