Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Get dependency version with caret #1146

Merged
merged 1 commit into from
Jul 22, 2019
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
6 changes: 3 additions & 3 deletions packages/cli/src/models/dependency/Dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export default class Dependency {
public static async fetchVersionFromNpm(name: string): Promise<string> {
const execAsync = promisify(exec);
try {
const { stdout } = await execAsync(`npm view ${name} | grep latest`);
const versionMatch = stdout.match(/([0-9]+\.){2}[0-9]+/);
return Array.isArray(versionMatch) && versionMatch.length > 0 ? `${name}@${versionMatch[0]}` : name;
const { stdout } = await execAsync(`npm view ${name}@latest version`);
const version = new semver.SemVer(stdout.trim());
return `${name}@^${version.major}.${version.minor}.0`;
} catch (error) {
return name;
}
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/test/models/Dependency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ contract('Dependency', function([_, from]) {
});
});

describe('#fetchVersionFromNpm', function() {
it('fetches version from npm', async function () {
const actual = await Dependency.fetchVersionFromNpm('zos');
actual.should.match(/^zos@\^2\.\d+\.0$/);
});

it('fetches version from npm for org package', async function () {
const actual = await Dependency.fetchVersionFromNpm('@openzeppelin/cli');
actual.should.match(/^@openzeppelin\/cli@\^\d+\.\d+\.0$/);
});
});

describe('#fromNameAndVersion', function() {
describe('with invalid nameAndVersion', function() {
it('throws error', function() {
Expand Down