Skip to content

Commit

Permalink
Merge pull request #3622 from ethereum/fix_version_import
Browse files Browse the repository at this point in the history
Fix version import
  • Loading branch information
yann300 committed Apr 17, 2023
2 parents 44ef80b + f7bc9dc commit 6276cb5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
26 changes: 25 additions & 1 deletion apps/remix-ide-e2e/src/tests/solidityImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
}
}

Expand Down
18 changes: 14 additions & 4 deletions libs/remix-url-resolver/src/resolve.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -137,14 +138,16 @@ export class RemixURLResolver {
*/

async handleNpmImport(url: string): Promise<HandlerResponse> {
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
Expand All @@ -169,15 +172,17 @@ 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) {
console.log(e)
}
}


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
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6276cb5

Please sign in to comment.