Try to implement resolve function in TS

pull/7/head
0mkar 6 years ago
parent 7ba69b1a3b
commit 2fd155edab
  1. 1
      remix-resolve/package.json
  2. 5
      remix-resolve/src/getFile.js
  3. 9
      remix-resolve/src/getFile.ts
  4. 14
      remix-resolve/src/index.js
  5. 4
      remix-resolve/src/index.ts
  6. 95
      remix-resolve/src/resolve.ts
  7. 2
      remix-resolve/tests/test.js
  8. 4
      remix-resolve/tsconfig.json

@ -30,6 +30,7 @@
},
"dependencies": {
"axios": "^0.18.0",
"request": "^2.88.0",
"solc": "^0.5.0",
"url": "^0.11.0",
"valid-url": "^1.0.9"

@ -1,5 +0,0 @@
const getFile = function (path, sources) {
return sources[path].content
}
module.exports = getFile

@ -0,0 +1,9 @@
export interface Sources {
[contractPath: string]: {
content: string
}
}
export function getFile(path: string, sources: Sources) {
return sources[path].content
}

@ -1,14 +0,0 @@
/*
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')
const fileContent = rr.resolve('https://github.com/ethereum/greeter.sol')
const input = rr.combineSource({ 'greeter.sol': content })
*/
export * from './resolve.js'
export * from './resolve'
export * from './combineSource.js'
export * from './getFile.js'
export * from './getFile'

@ -0,0 +1,95 @@
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
}
}

@ -1,4 +1,4 @@
const rr = require('../src/index.js')
const rr = require('../dist/index.js')
const assert = require('assert')
const fs = require('fs')
const solc = require('solc')

@ -1,10 +1,10 @@
{
"compileOnSave": false,
"include": ["./src"],
"compileOptions": {
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./dist",
"sourceMap": false,
"sourceMap": true,
"declaration": false,
"module": "commonjs",
"strict": true,

Loading…
Cancel
Save