This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.js
208 lines (166 loc) · 5.46 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const dir = require("node-dir");
const path = require("path");
const async = require("async");
const expect = require("truffle-expect");
const util = require('util');
const Reporter = require("truffle-reporters").migrationsV5;
const Migration = require('./migration.js');
/**
* This API is consumed by `truffle-core` at the `migrate` and `test` commands via
* the `.runMigrations` method.
*/
const Migrate = {
Migration: Migration,
reporter: null,
launchReporter: function(){
Migrate.reporter = new Reporter();
},
acceptDryRun: async function(){
return Migrate.reporter.acceptDryRun();
},
assemble: function(options, callback) {
dir.files(options.migrations_directory, function(err, files) {
if (err) return callback(err);
options.allowed_extensions = options.allowed_extensions || /^\.(js|es6?)$/;
let migrations = files
.filter(file => isNaN(parseInt(path.basename(file))) == false)
.filter(file => path.extname(file).match(options.allowed_extensions) != null)
.map(file => new Migration(file, Migrate.reporter, options));
// Make sure to sort the prefixes as numbers and not strings.
migrations = migrations.sort((a, b) => {
if (a.number > b.number) return 1;
if (a.number < b.number) return -1;
return 0;
});
callback(null, migrations);
});
},
run: function(options, callback) {
const self = this;
expect.options(options, [
"working_directory",
"migrations_directory",
"contracts_build_directory",
"provider",
"artifactor",
"resolver",
"network",
"network_id",
"logger",
"from", // address doing deployment
]);
if (options.reset == true) {
return this.runAll(options, callback);
}
self.lastCompletedMigration(options, function(err, last_migration) {
if (err) return callback(err);
// Don't rerun the last completed migration.
self.runFrom(last_migration + 1, options, callback);
});
},
runFrom: function(number, options, callback) {
const self = this;
this.assemble(options, function(err, migrations) {
if (err) return callback(err);
while (migrations.length > 0) {
if (migrations[0].number >= number) break;
migrations.shift();
}
if (options.to) {
migrations = migrations.filter(migration => migration.number <= options.to);
}
self.runMigrations(migrations, options, callback);
});
},
runAll: function(options, callback) {
this.runFrom(0, options, callback);
},
runMigrations: function(migrations, options, callback) {
// Perform a shallow clone of the options object
// so that we can override the provider option without
// changing the original options object passed in.
const clone = {};
Object.keys(options).forEach(function(key) {
clone[key] = options[key];
});
if (options.quiet) {
clone.logger = {
log: function() {}
}
};
clone.provider = this.wrapProvider(options.provider, clone.logger);
clone.resolver = this.wrapResolver(options.resolver, clone.provider);
// Make migrations aware of their position in sequence
const total = migrations.length;
if(total){
migrations[0].isFirst = true;
migrations[total - 1].isLast = true;
}
async.eachSeries(migrations, function(migration, finished) {
migration.run(clone, function(err) {
if (err) return finished(err);
finished();
});
}, callback);
},
wrapProvider: function(provider) {
return {
send: function(payload, callback) {
provider.send(payload, function(err, result) {
(err)
? callback(err)
: callback(err, result);
});
}
};
},
wrapResolver: function(resolver, provider) {
return {
require: function(import_path, search_path) {
const abstraction = resolver.require(import_path, search_path);
abstraction.setProvider(provider);
return abstraction;
},
resolve: resolver.resolve
}
},
lastCompletedMigration: function(options, callback) {
let Migrations;
try {
Migrations = options.resolver.require("Migrations");
} catch (e) {
const message = `Could not find built Migrations contract: ${e.message}`;
return callback(new Error());
}
if (Migrations.isDeployed() == false) {
return callback(null, 0);
}
const migrations = Migrations.deployed();
Migrations.deployed().then(function(migrations) {
// Two possible Migrations.sol's (lintable/unlintable)
return (migrations.last_completed_migration)
? migrations.last_completed_migration.call()
: migrations.lastCompletedMigration.call();
}).then(function(completed_migration) {
callback(null, parseInt(completed_migration));
}).catch(callback);
},
needsMigrating: function(options, callback) {
const self = this;
if (options.reset == true) {
return callback(null, true);
}
this.lastCompletedMigration(options, function(err, number) {
if (err) return callback(err);
self.assemble(options, function(err, migrations) {
if (err) return callback(err);
while (migrations.length > 0) {
if (migrations[0].number >= number) break;
migrations.shift();
}
callback(null, migrations.length > 1 || (migrations.length && number === 0));
});
});
}
};
module.exports = Migrate;