-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
133 lines (109 loc) · 4.23 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* External Module Dependencies */
var TruffleContractCompiler = require('truffle/lib/contracts')
var TruffleContractMigrator = require('truffle/lib/migrate')
var SolidityParser = require('solidity-parser')
var Web3 = require('web3')
/* Internal Module Dependencies */
var Logger = require('./lib/logger_decorator')
var BuildOptionNormalizer = require('./lib/build_option_normalizer')
var ScratchDir = require('./lib/scratch_dir')
/* Native Node Imports */
var path = require('path')
var fs = require('fs')
// Synchronus file existence check helper
function compiledContractExists (filePath) {
try {
fs.statSync(filePath)
} catch (err) {
if (err.code === 'ENOENT') return false
}
return true
}
// Read the contract source file and pass it to the `compilationFinished` callback
function returnContractAsSource (filePath, compilationFinished) {
fs.readFile(filePath, 'utf8', function (err, solJsFile) {
if (err) {
Logger.error(err)
return compilationFinished(err, null)
}
compilationFinished(err, solJsFile)
})
}
// This acts as a mutex to prevent multiple compilation runs
var isCompilingContracts = false
module.exports = function (source) {
this.cacheable && this.cacheable()
var scratchPath = new ScratchDir()
scratchPath.createIfMissing()
var buildPath = scratchPath.path()
var compilationFinished = this.async()
var contractPath = this.context
var contractFilePath = this.resourcePath
var contractFileName = path.basename(contractFilePath)
var contractName = contractFileName.charAt(0).toUpperCase() + contractFileName.slice(1, contractFileName.length - 4)
var compiledContractPath = path.resolve(buildPath, contractFileName + '.js')
var imports = SolidityParser.parseFile(contractFilePath, 'imports')
imports.forEach(function (solidityImport) {
var dependencyPath = path.resolve(contractPath, solidityImport)
this.addDependency(dependencyPath)
if (compiledContractExists(compiledContractPath)) {
fs.unlinkSync(compiledContractPath)
}
}.bind(this))
var buildOpts = {}
buildOpts.logger = Logger
buildOpts = BuildOptionNormalizer.normalize(buildOpts, this.query)
function waitForContractCompilation () {
setTimeout(function () {
if (compiledContractExists(compiledContractPath)) {
returnContractAsSource(compiledContractPath, compilationFinished)
} else {
waitForContractCompilation()
}
}.bind(this), 500)
}
if (!isCompilingContracts) {
Logger.log(`Writing temporary contract build artifacts to ${buildPath}`)
isCompilingContracts = true
var compilerOpts = buildOpts
compilerOpts.contracts_directory = contractPath
compilerOpts.contracts_build_directory = buildPath
compilerOpts.logger = Logger
compilerOpts.all = false
var provisionOpts = {}
provisionOpts.provider = new Web3.providers.HttpProvider(buildOpts.web3_rpc_uri)
provisionOpts.contracts_build_directory = buildPath
TruffleContractCompiler.compile(compilerOpts, function (err, contracts) {
if (err) {
Logger.error(err)
return compilationFinished(err, null)
}
isCompilingContracts = false
Logger.log('COMPILATION FINISHED')
Logger.log('RUNNING MIGRATIONS')
var migrationOpts = compilerOpts
migrationOpts.migrations_directory = buildOpts.migrations_directory
migrationOpts.contracts_build_directory = buildPath
migrationOpts.provider = provisionOpts.provider
migrationOpts.logger = Logger
migrationOpts.reset = true // Force the migrations to re-run
// Once all of the contracts have been compiled, we know we can immediately
// try to run the migrations safely.
TruffleContractMigrator.run(migrationOpts, function (err, result) {
if (err) {
Logger.error(err)
return compilationFinished(err, null)
}
console.log('here', result)
// Finally return the contract source we were originally asked for.
returnContractAsSource(compiledContractPath, compilationFinished)
})
})
return
}
if (compiledContractExists(compiledContractPath)) {
returnContractAsSource(compiledContractPath, compilationFinished)
} else {
waitForContractCompilation()
}
}