diff --git a/remix-resolve/src/resolve.ts b/remix-resolve/src/resolve.ts index 4c05aec1b5..4c80662d0d 100644 --- a/remix-resolve/src/resolve.ts +++ b/remix-resolve/src/resolve.ts @@ -33,7 +33,7 @@ interface Handler { export class RemixResolveApi implements API { public readonly type = 'remix-resolve' - previouslyHandled: PreviouslyHandledImports + private previouslyHandled: PreviouslyHandledImports constructor() { this.previouslyHandled = {} } @@ -134,105 +134,3 @@ export class RemixResolveApi implements API { return imported } } - -export class ImportResolver { - previouslyHandled: PreviouslyHandledImports - constructor() { - this.previouslyHandled = {} - } - /** - * Handle an import statement based on github - * @params root The root of the github import statement - * @params filePath path of the file in github - */ - handleGithubCall(root: string, filePath: string) { - return - } - /** - * Handle an import statement based on http - * @params url The url of the import statement - * @params cleanURL - */ - handleHttp(url: string, cleanURL: string) { - return - } - /** - * Handle an import statement based on https - * @params url The url of the import statement - * @params cleanURL - */ - handleHttps(url: string, cleanURL: string) { - return - } - handleSwarm(url: string, cleanURL: string) { - return - } - /** - * Handle an import statement based on IPFS - * @params url The url of the IPFS import statement - */ - async handleIPFS(url: string) { - // replace ipfs:// with /ipfs/ - url = url.replace(/^ipfs:\/\/?/, 'ipfs/') - try { - const req = 'https://gateway.ipfs.io/' + url - // If you don't find greeter.sol on ipfs gateway use local - // const req = 'http://localhost:8080/' + url - const response = await axios.get(req) - return response.data - } catch (e) { - throw e - } - } - handleLocal(root: string, filePath: string) { - return - } - getHandlers(): Handler[] { - return [ - { - type: 'github', - match: (url) => { return /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/.exec(url) }, - handle: (match) => this.handleGithubCall(match[3], match[4]) - }, - { - type: 'http', - match: (url) => { return /^(http?:\/\/?(.*))$/.exec(url) }, - handle: (match) => this.handleHttp(match[1], match[2]) - }, - { - type: 'https', - match: (url) => { return /^(https?:\/\/?(.*))$/.exec(url) }, - handle: (match) => this.handleHttps(match[1], match[2]) - }, - { - type: 'swarm', - match: (url) => { return /^(bzz-raw?:\/\/?(.*))$/.exec(url) }, - handle: (match) => this.handleSwarm(match[1], match[2]) - }, - { - type: 'ipfs', - match: (url) => { return /^(ipfs:\/\/?.+)/.exec(url) }, - handle: (match) => this.handleIPFS(match[1]) - } - ] - } - async resolve(filePath: string, customHandlers?: Handler[]): Promise { - var imported: Imported = this.previouslyHandled[filePath] - if(imported) { - return imported - } - const builtinHandlers: Handler[] = this.getHandlers() - const handlers: Handler[] = customHandlers ? [...builtinHandlers, ...customHandlers] : [...builtinHandlers] - const matchedHandler = handlers.filter(handler => handler.match(filePath)) - const handler: Handler = matchedHandler[0] - const match = handler.match(filePath) - const content: string = await handler.handle(match) - imported = { - content, - cleanURL: filePath, - type: handler.type - } - this.previouslyHandled[filePath] = imported - return imported - } -} diff --git a/remix-resolve/tests/test.ts b/remix-resolve/tests/test.ts index ad528b6744..93808904e3 100644 --- a/remix-resolve/tests/test.ts +++ b/remix-resolve/tests/test.ts @@ -1,8 +1,8 @@ -import { RemixResolve, ImportResolver, RemixResolveApi } from '../src' +import { RemixResolve, RemixResolveApi } from '../src' import * as fs from 'fs' import * as path from 'path' import * as assert from 'assert' -import { Plugin, AppManager, PluginProfile } from 'remix-plugin' +import { AppManager, PluginProfile } from 'remix-plugin' const RemixResolveProfile: PluginProfile = { type: 'remix-resolve', @@ -19,70 +19,61 @@ interface IAppManager { describe('testRunner', () => { describe('#resolve', () => { describe('test example_1 [local imports]]', () => { - const fileName = '../remix-resolve/tests/example_1/greeter.sol' - let results = {} - - before(done => { - let resolver: ImportResolver = new ImportResolver() - - function handleLocal(pathString: string, filePath: string) { - // if no relative/absolute path given then search in node_modules folder - if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) { - // return handleNodeModulesImport(pathString, filePath, pathString) - return - } else { - const o = { encoding: 'UTF-8' } - const p = pathString ? path.resolve(pathString, filePath) : path.resolve(pathString, filePath) - const content = fs.readFileSync(p, o) - return content - } - } - const localFSHandler = [ - { - type: 'local', - match: (url: string) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) }, - handle: (match: Array) => { return handleLocal(match[2], match[3]) } - } - ] - resolver.resolve(fileName, localFSHandler) - .then(sources => { - results = sources - done() - }) - .catch(e => { - throw e - }) - }) - - it('should have 3 items', () => { - assert.equal(Object.keys(results).length, 3) - }) - - it('should returns contract content of given local path', () => { - const expt = { - content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n', - cleanURL: '../remix-resolve/tests/example_1/greeter.sol', - type: 'local' - } - assert.deepEqual(results, expt) - }) - - // test IPFShandle - // AppManager tests describe('test with AppManager', () => { let app: AppManager let api: RemixResolveApi - before(() => { + const fileName = '../remix-resolve/tests/example_1/greeter.sol' + let results: object = {} + + before(done => { api = new RemixResolveApi() app = new AppManager({ modules: [{ json: RemixResolveProfile, api }] }) + + function handleLocal(pathString: string, filePath: string) { + // if no relative/absolute path given then search in node_modules folder + if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) { + // return handleNodeModulesImport(pathString, filePath, pathString) + return + } else { + const o = { encoding: 'UTF-8' } + const p = pathString ? path.resolve(pathString, filePath) : path.resolve(pathString, filePath) + const content = fs.readFileSync(p, o) + return content + } + } + const localFSHandler = [ + { + type: 'local', + match: (url: string) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) }, + handle: (match: Array) => { return handleLocal(match[2], match[3]) } + } + ] + //app.calls[api.type].resolve(fileName, localFSHandler) + api.resolve(fileName, localFSHandler) + .then(sources => { + results = sources + done() + }) + .catch(e => { + throw e + }) }) it('Plugin should be added to app', () => { assert.equal(typeof(app.calls[api.type].resolve), 'function') }) + + it('should returns contract content of given local path', () => { + const expt = { + content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n', + cleanURL: '../remix-resolve/tests/example_1/greeter.sol', + type: 'local' + } + assert.deepEqual(results, expt) + }) }) }) })