This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
compiler-imports.js
148 lines (140 loc) · 5.48 KB
/
compiler-imports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use babel'
// Copyright 2018 Etheratom Authors
// This file is part of Etheratom.
// Etheratom is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Etheratom is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Etheratom. If not, see <http://www.gnu.org/licenses/>.
/*eslint no-useless-escape: "warn"*/
/*eslint node/no-deprecated-api: "warn" */
import axios from 'axios';
import path from 'path';
import url from 'url';
import validUrl from 'valid-url';
import fs from 'fs';
async function handleGithubCall(fullpath, repoPath, path, filename, fileRoot) {
return await axios({
method: 'get',
url: 'https://api.github.com/repos/' + repoPath + '/contents/' + path,
responseType: 'json'
}).then(function(response) {
if('content' in response.data) {
const buf = Buffer.from(response.data.content, 'base64');
fileRoot = fullpath.substring(0, fullpath.lastIndexOf('/'));
fileRoot = fileRoot + '/';
const resp = { filename, content: buf.toString('UTF-8'), fileRoot };
return resp;
} else {
throw 'Content not received!';
}
});
}
async function handleNodeModulesImport(pathString, filename, fileRoot) {
const o = { encoding: 'UTF-8' };
var modulesDir = fileRoot;
while(true) {
var p = path.join(modulesDir, 'node_modules', pathString, filename);
try {
const content = fs.readFileSync(p, o);
fileRoot = path.join(modulesDir, 'node_modules', pathString);
const response = { filename, content, fileRoot };
return response;
}
catch(err) {
console.log(err);
}
// Recurse outwards until impossible
var oldModulesDir = modulesDir;
modulesDir = path.join(modulesDir, '..');
if (modulesDir === oldModulesDir) {
break;
}
}
}
async function handleLocalImport(pathString, filename, fileRoot) {
// if no relative/absolute path given then search in node_modules folder
if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) {
return handleNodeModulesImport(pathString, filename, fileRoot);
}
else{
const o = { encoding: 'UTF-8' };
const p = pathString ? path.resolve(fileRoot, pathString, filename) : path.resolve(fileRoot, filename);
const content = fs.readFileSync(p, o);
fileRoot = pathString ? path.resolve(fileRoot, pathString) : fileRoot;
const response = { filename, content, fileRoot };
return response;
}
}
async function getHandlers() {
return [
{
type: 'local',
match: /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g,
handle: async(match, fileRoot) => { return await handleLocalImport(match[2], match[3], fileRoot); }
},
{
type: 'github',
match: /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)(.*\/(\w+\.sol))/g,
handle: async(match, fileRoot) => {
return await handleGithubCall(match[0], match[3], match[4], match[5], fileRoot);
}
}
];
}
async function resolveImports(fileRoot, sourcePath) {
const handlers = await getHandlers();
let response = {};
for(const handler of handlers) {
try {
// here we are trying to find type of import path github/swarm/ipfs/local
const match = handler.match.exec(sourcePath);
if(match) {
response = await handler.handle(match, fileRoot);
break;
}
} catch(e) {
throw e;
}
}
return response;
}
export async function combineSource(fileRoot, sources) {
let fn, importLine, ir;
var matches = [];
ir = /^(?:import){1}(.+){0,1}\s['"](.+)['"];/gm;
let match = null;
for (const fileName of Object.keys(sources)) {
const source = sources[fileName].content;
while((match = ir.exec(source))) {
matches.push(match);
}
for(let match of matches) {
importLine = match[0];
const extra = match[1] ? match[1] : '';
if(validUrl.isUri(fileRoot)) {
fn = url.resolve(fileRoot, match[2]);
} else {
fn = match[2];
}
try {
// resolve anything other than remix_tests.sol & tests.sol
if(fn.localeCompare('remix_tests.sol') != 0 && fn.localeCompare('tests.sol') != 0) {
let subSorce = {};
const response = await resolveImports(fileRoot, fn);
sources[fileName].content = sources[fileName].content.replace(importLine, 'import' + extra + ' \'' + response.filename + '\';');
subSorce[response.filename] = { content: response.content };
sources = Object.assign(await combineSource(response.fileRoot, subSorce), sources);
}
} catch (e) {
throw e;
}
}
}
return sources;
}