Commit 2fd155ed authored by 0mkar's avatar 0mkar

Try to implement resolve function in TS

parent 7ba69b1a
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
}, },
"dependencies": { "dependencies": {
"axios": "^0.18.0", "axios": "^0.18.0",
"request": "^2.88.0",
"solc": "^0.5.0", "solc": "^0.5.0",
"url": "^0.11.0", "url": "^0.11.0",
"valid-url": "^1.0.9" "valid-url": "^1.0.9"
......
const getFile = function (path, sources) {
return sources[path].content
}
module.exports = getFile
export interface Sources {
[contractPath: string]: {
content: string
}
}
export function getFile(path: string, sources: Sources) {
return sources[path].content
}
/*
const rr = require('remix-resolve')
const fileContent = rr.resolve('https://github.com/ethereum/greeter.sol')
const input = rr.combineSource({ 'greeter.sol': content })
*/
const resolve = require('./resolve.js')
const combineSource = require('./combineSource.js')
const getFile = require('./getFile.js')
module.exports = {
resolve: resolve,
combineSource: combineSource,
getFile: getFile
}
...@@ -3,6 +3,6 @@ const rr = require('remix-resolve') ...@@ -3,6 +3,6 @@ const rr = require('remix-resolve')
const fileContent = rr.resolve('https://github.com/ethereum/greeter.sol') const fileContent = rr.resolve('https://github.com/ethereum/greeter.sol')
const input = rr.combineSource({ 'greeter.sol': content }) const input = rr.combineSource({ 'greeter.sol': content })
*/ */
export * from './resolve.js' export * from './resolve'
export * from './combineSource.js' export * from './combineSource.js'
export * from './getFile.js' export * from './getFile'
interface Imported {
content: string;
cleanURL: string;
type: string;
}
interface PrvHandld {
[filePath: string]: Imported
}
interface Handler {
type: string;
match(url: string): any;
handle(match: any): any;
}
export class ImportResolver {
previouslyHandled: PrvHandld[]
constructor() {
this.previouslyHandled = []
}
handleGithubCall(root: string, filePath: string) {
return
}
handleHttp(url: string, cleanURL: string) {
return
}
handleHttps(url: string, cleanURL: string) {
return
}
handleSwarm(url: string, cleanURL: string) {
return
}
handleIPFS(url: string) {
return
}
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]) }
},
{
type: 'local',
match: (url) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) },
handle: (match) => { this.handleLocal(match[2], match[3]) }
}
]
}
resolve(filePath: string, customHandlers: Handler[]) {
var imported: Imported = this.previouslyHandled[filePath]
if(imported) {
return imported
}
const builtinHandlers: Handler[] = this.getHandlers()
const handlers: Handler[] = [...builtinHandlers, ...customHandlers]
handlers.forEach(handler => {
const match = handler.match(filePath)
if(match) {
const content: any = handler.handle(match)
imported = {
content,
cleanURL: filePath,
type: handler.type
}
this.previouslyHandled[filePath] = imported
}
})
return imported
}
}
const rr = require('../src/index.js') const rr = require('../dist/index.js')
const assert = require('assert') const assert = require('assert')
const fs = require('fs') const fs = require('fs')
const solc = require('solc') const solc = require('solc')
......
{ {
"compileOnSave": false, "compileOnSave": false,
"include": ["./src"], "include": ["./src"],
"compileOptions": { "compilerOptions": {
"baseUrl": "./src", "baseUrl": "./src",
"outDir": "./dist", "outDir": "./dist",
"sourceMap": false, "sourceMap": true,
"declaration": false, "declaration": false,
"module": "commonjs", "module": "commonjs",
"strict": true, "strict": true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment