Skip to content

Commit 29db66b

Browse files
alaibeiurimatias
authored andcommitted
feat: import resolver
1 parent 0d8f233 commit 29db66b

File tree

31 files changed

+392
-426
lines changed

31 files changed

+392
-426
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
"@types/node": "10.11.7",
207207
"@types/os-locale": "2.1.0",
208208
"@types/pretty-ms": "3.2.0",
209+
"@types/request": "2.48.1",
209210
"@types/web3": "1.0.12",
210211
"babel-plugin-dynamic-import-node": "2.2.0",
211212
"chai": "4.1.2",

src/cmd/cmd_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ class EmbarkController {
652652
engine.startService("deployment", {
653653
trackContracts: false,
654654
compileOnceOnly: true,
655-
disableOptimizations: options.coverage
655+
isCoverage: options.coverage
656656
});
657657
engine.startService("storage");
658658
engine.startService("codeGenerator");

src/lib/core/config.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const fs = require('./fs.js');
2-
const File = require('./file.js');
32
const Plugins = require('./plugins.js');
43
const utils = require('../utils/utils.js');
54
const path = require('path');
@@ -11,6 +10,7 @@ const cloneDeep = require('lodash.clonedeep');
1110
import { replaceZeroAddressShorthand } from '../utils/addressUtils';
1211
import { unitRegex } from "../utils/regexConstants";
1312
import * as utilsContractsConfig from "../utils/contractsConfig";
13+
import { File, Types } from "./file";
1414

1515
const DEFAULT_CONFIG_PATH = 'config/';
1616

@@ -59,13 +59,13 @@ var Config = function(options) {
5959
resolver = resolver || function(callback) {
6060
callback(fs.readFileSync(filename).toString());
6161
};
62-
self.contractsFiles.push(new File({filename, type: File.types.custom, path: filename, resolver}));
62+
self.contractsFiles.push(new File({path: filename, type: Types.custom, resolver}));
6363
});
6464

6565
self.events.on('file-remove', (fileType, removedPath) => {
6666
if(fileType !== 'contract') return;
6767
const normalizedPath = path.normalize(removedPath);
68-
self.contractsFiles = self.contractsFiles.filter(file => path.normalize(file.filename) !== normalizedPath);
68+
self.contractsFiles = self.contractsFiles.filter(file => path.normalize(file.path) !== normalizedPath);
6969
});
7070
};
7171

@@ -127,7 +127,7 @@ Config.prototype.loadContractFiles = function() {
127127
if (!this.contractFiles || newContractsFiles.length !== this.contractFiles.length || !deepEqual(newContractsFiles, this.contractFiles)) {
128128
this.contractsFiles = this.contractsFiles.concat(newContractsFiles).filter((file, index, arr) => {
129129
return !arr.some((file2, index2) => {
130-
return file.filename === file2.filename && index < index2;
130+
return file.path === file2.path && index < index2;
131131
});
132132
});
133133
}
@@ -369,11 +369,11 @@ Config.prototype.loadExternalContractsFiles = function() {
369369
return this.logger.error(__("HTTP contract file not found") + ": " + contract.file);
370370
}
371371
const localFile = fileObj.filePath;
372-
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: fileObj.url, storageConfig: storageConfig}));
372+
this.contractsFiles.push(new File({path: localFile, type: Types.http, basedir: '', externalUrl: fileObj.url, storageConfig: storageConfig}));
373373
} else if (fs.existsSync(contract.file)) {
374-
this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file, storageConfig: storageConfig}));
374+
this.contractsFiles.push(new File({path: contract.file, type: Types.dappFile, basedir: '', storageConfig: storageConfig}));
375375
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
376-
this.contractsFiles.push(new File({filename: path.join('./node_modules/', contract.file), type: File.types.dapp_file, basedir: '', path: path.join('./node_modules/', contract.file), storageConfig: storageConfig}));
376+
this.contractsFiles.push(new File({path: path.join('./node_modules/', contract.file), type: Types.dappFile, basedir: '', storageConfig: storageConfig}));
377377
} else {
378378
this.logger.error(__("contract file not found") + ": " + contract.file);
379379
}
@@ -571,7 +571,7 @@ Config.prototype.loadFiles = function(files) {
571571
return (file[0] === '$' || file.indexOf('.') >= 0);
572572
}).filter(function(file) {
573573
let basedir = findMatchingExpression(file, files);
574-
readFiles.push(new File({filename: file, type: File.types.dapp_file, basedir: basedir, path: file, storageConfig: storageConfig}));
574+
readFiles.push(new File({path: file, type: Types.dappFile, basedir: basedir, storageConfig: storageConfig}));
575575
});
576576

577577
var filesFromPlugins = [];
@@ -605,7 +605,7 @@ Config.prototype.loadPluginContractFiles = function() {
605605
contractsPlugins.forEach(function(plugin) {
606606
plugin.contractsFiles.forEach(function(file) {
607607
var filename = file.replace('./','');
608-
self.contractsFiles.push(new File({filename: filename, pluginPath: plugin.pluginPath, type: File.types.custom, path: filename, storageConfig: storageConfig, resolver: function(callback) {
608+
self.contractsFiles.push(new File({path: filename, pluginPath: plugin.pluginPath, type: Types.custom, storageConfig: storageConfig, resolver: function(callback) {
609609
callback(plugin.loadPluginFile(file));
610610
}}));
611611
});

src/lib/core/engine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class Engine {
212212
}
213213

214214
setupCompilerAndContractsManagerService(options) {
215-
this.registerModule('compiler', {plugins: this.plugins, disableOptimizations: options.disableOptimizations});
215+
this.registerModule('compiler', {plugins: this.plugins, isCoverage: options.isCoverage});
216216
this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard});
217217
this.registerModule('vyper');
218218
this.registerModule('contracts_manager', {plugins: this.plugins, compileOnceOnly: options.compileOnceOnly});

src/lib/core/file.js

Lines changed: 0 additions & 210 deletions
This file was deleted.

src/lib/core/file.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as path from "path";
2+
3+
const fs = require("./fs.js");
4+
const utils = require("../utils/utils");
5+
6+
export enum Types {
7+
embarkInternal = "embark_internal",
8+
dappFile = "dapp_file",
9+
custom = "custom",
10+
http = "http",
11+
}
12+
13+
interface ImportRemapping {
14+
prefix: string;
15+
target: string;
16+
}
17+
18+
export class File {
19+
public type: Types;
20+
public externalUrl: string = "";
21+
public path: string;
22+
public basedir: string;
23+
public resolver: (callback: (content: string) => void) => void;
24+
public pluginPath: string;
25+
public storageConfig: any;
26+
public providerUrl: string;
27+
public importRemappings: ImportRemapping[] = [];
28+
29+
constructor(options: any) {
30+
this.type = options.type;
31+
32+
this.basedir = options.basedir;
33+
this.resolver = options.resolver;
34+
this.pluginPath = options.pluginPath ? options.pluginPath : "";
35+
this.storageConfig = options.storageConfig;
36+
this.providerUrl = "";
37+
38+
if (this.type === Types.http) {
39+
const external = utils.getExternalContractUrl(options.externalUrl, this.providerUrl);
40+
this.externalUrl = external.url;
41+
this.path = external.filePath;
42+
} else {
43+
this.path = options.path.replace(/\\/g, "/");
44+
}
45+
}
46+
47+
public get content(): Promise<string> {
48+
return new Promise<string>((resolve) => {
49+
switch (this.type) {
50+
case Types.embarkInternal: {
51+
const content = fs.readFileSync(fs.embarkPath(path.join("dist", this.path)), "utf-8");
52+
return resolve(content);
53+
}
54+
55+
case Types.dappFile: {
56+
const content = fs.readFileSync(this.path, "utf-8").toString();
57+
return resolve(content);
58+
}
59+
60+
case Types.custom: {
61+
return this.resolver((content: string) => {
62+
resolve(content);
63+
});
64+
}
65+
66+
case Types.http: {
67+
fs.ensureFileSync(this.path);
68+
return utils.downloadFile(this.externalUrl, this.path, () => {
69+
const content = fs.readFileSync(this.path, "utf-8");
70+
resolve(content);
71+
});
72+
}
73+
}
74+
});
75+
}
76+
77+
}

0 commit comments

Comments
 (0)