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

Commit

Permalink
Get dependency version with caret (#1146)
Browse files Browse the repository at this point in the history
Link a dependency version using the caret instead of exact version, and avoid relying on unix specific bash commands.

This allows linking a dependency like `@openzeppelin/contracts-ethereum-package` on version `^2.2.0`. This installs version 2.2.1, but allows using deployments made under version 2.2.0.
  • Loading branch information
spalladino committed Jul 22, 2019
1 parent bf6f813 commit eb9316c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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

0 comments on commit eb9316c

Please sign in to comment.