Skip to content

Commit

Permalink
When not passing version to resolvePackage, it should return latest.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Mar 13, 2021
1 parent b07e2b0 commit b65334d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/composability.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const assert = require('assert');
const semver = require('semver');
const pacote = require('pacote');

Expand Down Expand Up @@ -54,19 +55,24 @@ composability.lookupLocalPackages = function (packagesToLookup = 'generator-*')
/**
* Resolve a package name with version.
*
* @param {string} [packageName] - package to resolve.
* @param {string} packageName - package to resolve.
* @param {string} [packageVersion] - version or range to resolve.
* @param {string[]} Array of key, value pairs.
*/
composability.resolvePackage = async function (packageName, packageVersion) {
assert(packageName, 'Parameter packageName is required');
if (packageVersion) {
packageName = `${packageName}@${packageVersion}`;
}
const manifest = await pacote.manifest(packageName);
if (!manifest) {
return undefined;
}
const from = manifest._from;
const index = from.lastIndexOf('@');
if (index > 1) {
return [from.slice(0, Math.max(0, index)), from.slice(index + 1, from.length)];
const resolvedVersion = from.slice(index + 1, from.length) || manifest.version;
return [from.slice(0, Math.max(0, index)), resolvedVersion];
}
return [manifest.name, from];
return [manifest.name, from || manifest.version];
};
9 changes: 9 additions & 0 deletions test/composability.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ describe('composability', () => {
});

describe('resolvePackage()', () => {
it('should throw on missing packageName', async function () {
assert.rejects(async () => this.env.resolvePackage());
});

it('should lookup for latest package version', async function () {
const entry = await this.env.resolvePackage('yeoman-generator');
assert.ok(entry[1], 'must provide an version');
});

it('should return package entry', async function () {
assert.deepStrictEqual(
await this.env.resolvePackage('yeoman-generator', '^2'),
Expand Down

0 comments on commit b65334d

Please sign in to comment.