Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Throw aggregate error for npmName.many() #38

Merged
merged 6 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const registryAuthToken = require('registry-auth-token');
const zip = require('lodash.zip');
const validate = require('validate-npm-package-name');
const organizationRegex = require('org-regex')({exact: true});
const pMap = require('p-map');

class InvalidNameError extends Error {}

Expand Down Expand Up @@ -90,7 +91,7 @@ module.exports.many = async (names, options = {}) => {
throw new Error('The `registryUrl` option must be a valid string URL');
}

const result = await Promise.all(names.map(name => request(name, options)));
const result = await pMap(names, name => request(name, options), {stopOnError: false});
return new Map(zip(names, result));
};

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
"is-scoped": "^2.1.0",
"is-url-superb": "^3.0.0",
"lodash.zip": "^4.2.0",
"org-regex": "^1.0.0",
"p-map": "^3.0.0",
"registry-auth-token": "^4.0.0",
"registry-url": "^5.1.0",
"validate-npm-package-name": "^3.0.0",
"org-regex": "^1.0.0"
"validate-npm-package-name": "^3.0.0"
},
"devDependencies": {
"aggregate-error": "^3.0.1",
"ava": "^2.1.0",
"tsd": "^0.7.1",
"unique-string": "^2.0.0",
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import uniqueString from 'unique-string';
import AggregateError from 'aggregate-error';
import npmName from '.';

const registryUrl = 'https://registry.yarnpkg.com/';
Expand Down Expand Up @@ -67,3 +68,19 @@ test('throws when package name is invalid', async t => {
- name cannot start with an underscore`
});
});

test('should return an iterable error capturing multiple errors when appropriate', async t => {
const name1 = 'chalk'; // False
const name2 = uniqueString(); // True
const name3 = '_ABC'; // Error
const name4 = 'CapitalsAreBad'; // Error

const aggregateError = await t.throwsAsync(npmName.many([name1, name2, name3, name4]), {
instanceOf: AggregateError
});

const errors = [...aggregateError];
t.is(errors.length, 2);
t.regex(errors[0].message, /Invalid package name: _ABC/);
t.regex(errors[1].message, /Invalid package name: CapitalsAreBad/);
});