diff --git a/apps/remix-ide-e2e/src/tests/solidityImport.test.ts b/apps/remix-ide-e2e/src/tests/solidityImport.test.ts index 55d4ba96bd..115119e996 100644 --- a/apps/remix-ide-e2e/src/tests/solidityImport.test.ts +++ b/apps/remix-ide-e2e/src/tests/solidityImport.test.ts @@ -112,7 +112,31 @@ module.exports = { timeout: 120000, }) .verifyContracts(['test13', 'ERC20'], { wait: 30000 }) - .end() + }, + + 'Test NPM Import (the version is specified in package.json) #group4': function (browser: NightwatchBrowser) { + browser + // clone https://github.com/yann300/remix-reward + .clickLaunchIcon('filePanel') + .waitForElementVisible('[data-id="workspaceMenuDropdown"]') + .click('[data-id="workspaceMenuDropdown"]') + .waitForElementVisible('[data-id="workspaceclone"]') + .click('[data-id="workspaceclone"]') + .waitForElementVisible('[data-id="fileSystemModalDialogModalBody-react"]') + .click('[data-id="fileSystemModalDialogModalBody-react"]') + .waitForElementVisible('[data-id="modalDialogCustomPromptTextClone"]') + .setValue('[data-id="modalDialogCustomPromptTextClone"]', 'https://github.com/yann300/remix-reward') + .click('[data-id="fileSystem-modal-footer-ok-react"]') + .waitForElementPresent('.fa-spinner') + .pause(5000) + .waitForElementNotPresent('.fa-spinner') + .waitForElementVisible('*[data-id="treeViewLitreeViewItem.git"]') + .waitForElementContainsText('[data-id="workspacesSelect"]', 'remix-reward') + .clickLaunchIcon('solidity') + // compile (this will be using the version specified in the package.json) + .openFile('contracts') + .openFile('contracts/RemixRewardUpgradable.sol') + .verifyContracts(['Remix']) } } diff --git a/libs/remix-url-resolver/src/resolve.ts b/libs/remix-url-resolver/src/resolve.ts index dbe1d09167..d0b08eeaf7 100644 --- a/libs/remix-url-resolver/src/resolve.ts +++ b/libs/remix-url-resolver/src/resolve.ts @@ -1,5 +1,6 @@ // eslint-disable-next-line no-unused-vars import axios, { AxiosResponse } from 'axios' +import semver from 'semver' import { BzzNode as Bzz } from '@erebos/bzz-node' export interface Imported { @@ -137,14 +138,16 @@ export class RemixURLResolver { */ async handleNpmImport(url: string): Promise { - if (this.getDependencies) { + if (!url) throw new Error('url is empty') + const isVersionned = semverRegex().exec(url.replace(/@/g, '@ ').replace(/\//g, ' /')) + if (this.getDependencies && !isVersionned) { try { const { deps, yarnLock, packageLock } = await this.getDependencies() let matchLength = 0 let pkg if (deps) { Object.keys(deps).map((dep) => { - const reg = new RegExp(dep, 'g') + const reg = new RegExp(dep + '/', 'g') const match = url.match(reg) if (match && match.length > 0 && matchLength < match[0].length) { matchLength = match[0].length @@ -169,7 +172,10 @@ export class RemixURLResolver { // package.json version = deps[pkg] } - if (version) url = url.replace(pkg, `${pkg}@${version}`) + if (version) { + const versionSemver = semver.minVersion(version) + url = url.replace(pkg, `${pkg}@${versionSemver.version}`) + } } } } catch (e) { @@ -177,7 +183,6 @@ export class RemixURLResolver { } } - const npm_urls = ["https://cdn.jsdelivr.net/npm/", "https://unpkg.com/"] process && process.env && process.env['NPM_URL'] && npm_urls.unshift(process.env['NPM_URL']) let content = null @@ -253,3 +258,8 @@ export class RemixURLResolver { return imported } } + +// see npm semver-regex +function semverRegex() { + return /(?<=^v?|\sv?)(?:(?:0|[1-9]\d{0,9}?)\.){2}(?:0|[1-9]\d{0,9})(?:-(?:--+)?(?:0|[1-9]\d*|\d*[a-z]+\d*)){0,100}(?=$| |\+|\.)(?:(?<=-\S+)(?:\.(?:--?|[\da-z-]*[a-z-]\d*|0|[1-9]\d*)){1,100}?)?(?!\.)(?:\+(?:[\da-z]\.?-?){1,100}?(?!\w))?(?!\+)/gi; +} diff --git a/package.json b/package.json index 3039f466e5..8d6abcb696 100644 --- a/package.json +++ b/package.json @@ -342,7 +342,7 @@ "request": "^2.83.0", "rimraf": "^2.6.1", "selenium-standalone": "^8.2.3", - "semver": "^6.3.0", + "semver": "^7.4.0", "solc": "0.7.4", "stream-browserify": "^3.0.0", "stream-http": "^3.2.0", diff --git a/yarn.lock b/yarn.lock index ba4025ff0f..b355a8d151 100644 --- a/yarn.lock +++ b/yarn.lock @@ -23663,6 +23663,13 @@ semver@^7.3.7: dependencies: lru-cache "^6.0.0" +semver@^7.4.0: + version "7.4.0" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.4.0.tgz#8481c92feffc531ab1e012a8ffc15bdd3a0f4318" + integrity sha512-RgOxM8Mw+7Zus0+zcLEUn8+JfoLpj/huFTItQy2hsM4khuC1HYRDp0cU482Ewn/Fcy6bCjufD8vAj7voC66KQw== + dependencies: + lru-cache "^6.0.0" + semver@~7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e"