-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
118 lines (89 loc) · 3.29 KB
/
index.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
// Required Modules
var sol = require("solc"),
path = require('path'),
fs = require('fs');
// Function that finds files in directory
function findFilesInDir(startPath,filter){
// Results storage
var results = [];
// If the folder doesn't exist, exit
if (!fs.existsSync(startPath)){
console.log("no dir ", startPath);
return;
}
// All the files in the directory
var files = fs.readdirSync(startPath);
// Go through all the files
for(var i=0;i<files.length;i++){
// The current item name in the loop
var filename = path.join(startPath,files[i]);
// Get stats on the item
var stat = fs.lstatSync(filename);
// If the current item is a directory
if (stat.isDirectory()){
// Recurse down through this directory and add the files
results = results.concat(findFilesInDir(filename, filter));
} else {
// If the current item is a file
if (filename.indexOf(filter) >= 0) {
console.log('-- found: ',filename);
results.push(filename);
}
}
}
return results;
}
// Main exportable logic
module.exports = function(source) {
// Basic setup
var files = findFilesInDir(this.context, '.sol'),
sourceMap = {},
self = this,
exportJS = false;
// Webpack caching for hot module reloading
this.cacheable();
// If there is a query attached in the webpack config
if (typeof this.query !== "undefined") {
// Setup for queryString
var queryString = {};
// Populate the regex rules that apply
this.query.replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) { queryString[$1] = $3; }
);
// Set exporting if based on query string
if(queryString.export && queryString.export === 'false') { exportJS = false; }
if(queryString.export && queryString.export === 'true' ) { exportJS = true; }
}
// For each file in the source directory
files.forEach(function(filePath, index){
// Strip out the filename
var fileName = filePath.split("/").pop();
// Add to a container for source maps
sourceMap[fileName] = fs.readFileSync(filePath, 'utf8');
// Webpack - add dependecy
self.addDependency(filePath);
});
// Compiled contracts in one object
var compiled = sol.compile({sources: sourceMap}, 1);
// If there is a query present then we are exporting
if (exportJS) {
// Set the file string and sources as specificied by solidity compiler
var fileString = "module.exports = {'contracts': " + JSON.stringify(compiled.contracts) + ", ";
fileString += "'sources': " + JSON.stringify(compiled.sources) + ", ";
// Sifts through the compiled contracts and makes web3.eth.contracts out of them
// Ether-Pudding normally does this for you but we can do it here too
Object.keys(compiled.contracts)
.forEach(function(contractName){
fileString += "'" +
contractName + "': " +
'function (web3) { var contract = web3.eth.contract(' +
compiled.contracts[contractName].interface +"); contract.bytecode = '" + compiled.contracts[contractName].bytecode + "'; return contract; },";
});
// Cleans up spaces for conciseness
fileString = fileString.replace(/,\s*$/, "") + "}; ";
return fileString;
} else {
return compiled;
}
}