forked from trufflesuite/truffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
truffle.es6
executable file
·366 lines (295 loc) · 10.1 KB
/
truffle.es6
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env ./node_modules/.bin/babel-node
require("coffee-script/register");
var web3 = require("web3");
var path = require("path");
var fs = require("fs");
var gaze = require('gaze');
var deasync = require("deasync");
var colors = require('colors/safe');
var Init = require("./lib/init");
var Create = require("./lib/create");
var Config = require("./lib/config");
var Contracts = require("./lib/contracts");
var Build = require("./lib/build");
var Test = require("./lib/test");
var Exec = require("./lib/exec");
var Repl = require("./lib/repl");
var Serve = require("./lib/serve");
var ConfigurationError = require("./lib/errors/configurationerror");
var ExtendableError = require("./lib/errors/extendableerror");
var argv = require('yargs').argv;
var truffle_dir = process.env.TRUFFLE_NPM_LOCATION || argv.n || argv.npm_directory;
var working_dir = process.env.TRUFFLE_WORKING_DIRECTORY || argv.w || argv.working_directory;
if (working_dir[working_dir.length - 1] != "/") {
working_dir += "/";
}
var pkg = JSON.parse(fs.readFileSync(path.join(truffle_dir, "package.json"), {encoding: "utf8"}));
var tasks = {};
var registerTask = function(name, description, fn) {
tasks[name] = {
name: name,
description: description,
fn: fn
};
}
var printSuccess = function() {
console.log(colors.green(`Completed without errors on ${new Date().toString()}`));
};
var printFailure = function() {
console.log(colors.red(`Completed with errors on ${new Date().toString()}`));
};
var runTask = function(name) {
try {
var fn = deasync(tasks[name].fn);
fn();
return 0;
} catch (e) {
if (e instanceof ExtendableError) {
console.log(e.message);
if (argv.stack != null) {
console.log(e.stack);
}
} else {
// Bubble up all other unexpected errors.
console.log(e.stack || e.toString());
}
return 1;
}
};
registerTask('watch', "Watch filesystem for changes and rebuild the project automatically", function(done) {
var needs_rebuild = true;
var needs_redeploy = false;
gaze(["app/**/*", "environments/*/contracts/**/*", "contracts/**/*"], {cwd: working_dir, interval: 1000, debounceDelay: 500}, function() {
// On changed/added/deleted
this.on('all', function(event, filePath) {
var display_path = path.join("./", filePath.replace(working_dir, ""));
console.log(colors.cyan(`>> File ${display_path} changed.`));
needs_rebuild = true;
if (display_path.indexOf("contracts/") == 0) {
needs_redeploy = true;
} else {
needs_rebuild = true;
}
});
});
var check_rebuild = function() {
if (needs_redeploy == true) {
needs_redeploy = false;
needs_rebuild = false;
console.log("Redeploying...");
if (runTask("deploy") != 0) {
printFailure();
}
}
if (needs_rebuild == true) {
needs_rebuild = false;
console.log("Rebuilding...");
if (runTask("build") != 0) {
printFailure();
}
}
setTimeout(check_rebuild, 500);
};
setInterval(check_rebuild, 500);
});
registerTask('list', "List all available tasks", function(done) {
console.log("Truffle v" + pkg.version + " - a development framework for Ethereum");
console.log("");
console.log("Usage: truffle [command] [options]");
console.log("");
console.log("Commands:");
console.log("");
var sorted = Object.keys(tasks).sort();
var longestTask = sorted.reduce(function(a, b) {
var first = typeof a == "string" ? a.length : a;
return Math.max(first, b.length);
});
for (var i = 0; i < sorted.length; i++) {
var task = tasks[sorted[i]];
var heading = task.name;
while (heading.length < longestTask) {
heading += " ";
}
console.log(" " + heading + " => " + task.description)
}
console.log("");
done();
});
registerTask('version', "Show version number and exit", function(done) {
console.log("Truffle v" + pkg.version);
done();
});
registerTask('init', "Initialize new Ethereum project, including example contracts and tests", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv);
Init.all(config, done);
});
registerTask('create:contract', "Create a basic contract", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv);
var name = argv.name;
if (name == null && argv._.length > 1) {
name = argv._[1];
}
if (name == null) {
throw new ConfigurationError("Please specify a name. Example: truffle create:contract MyContract");
} else {
Create.contract(config, name, done);
}
});
registerTask('create:test', "Create a basic test", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv);
var name = argv.name;
if (name == null && argv._.length > 1) {
name = argv._[1];
}
if (name == null) {
throw new ConfigurationError("Please specify a name. Example: truffle create:test MyTest");
} else {
Create.test(config, name, done);
}
});
registerTask('resolve', "Resolve dependencies in contract file and print result", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "development");
var file = argv.file;
if (file == null && argv._.length > 1) {
file = argv._[1];
}
if (file == null) {
throw new ConfigurationError("Please specify a contract file. Example: truffle resolve ./contracts/Example.sol");
} else {
file = path.resolve(file);
var code;
try {
code = Contracts.resolve(file);
} catch (e) {
done(e);
return;
}
console.log(code);
done();
}
});
registerTask('compile', "Compile contracts", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "development");
Contracts.compile(config, done);
});
registerTask('deploy', "Deploy contracts to the network", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "development");
console.log("Using environment " + config.environment + ".");
var compile = true;
if (argv.compile === false) {
compile = false;
}
// Compile and deploy.
Contracts.deploy(config, compile, function(err) {
if (err != null) {
done(err);
} else {
// console.log("Rebuilding app with new contracts...");
// runTask("build");
done();
}
});
});
registerTask('build', "Build development version of app", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "development");
Build.build(config, function(err) {
done(err);
if (err == null) {
printSuccess();
}
});
});
registerTask('dist', "Create distributable version of app (minified)", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "production");
console.log("Using environment " + config.environment + ".");
Build.dist(config, function(err) {
done(err);
if (err == null) {
printSuccess();
}
});
});
registerTask('exec', "Execute a Coffee/JS file within truffle environment. Script *must* call process.exit() when finished.", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "development");
var file = argv.file;
if (file == null && argv._.length > 1) {
file = argv._[1];
}
if (file == null) {
console.log("Please specify a file, passing the path of the script you'd like the run. Note that all scripts *must* call process.exit() when finished.");
done();
return;
}
Exec.file(config, file, done);
});
// Supported options:
// --no-color: Disable color
// More to come.
registerTask('test', "Run tests", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "test");
console.log("Using environment " + config.environment + ".");
// Ensure we're quiet about deploys during tests.
config.argv.quietDeploy = true;
var file = argv.file;
if (file == null && argv._.length > 1) {
file = argv._[1];
}
if (file == null) {
Test.run(config, done);
} else {
Test.run(config, file, done);
}
});
registerTask('console', "Run a console with deployed contracts instanciated and available (REPL)", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "development");
Repl.run(config, done);
});
registerTask('serve', "Serve app on http://localhost:8080 and rebuild changes as needed", function(done) {
var config = Config.gather(truffle_dir, working_dir, argv, "development");
console.log("Using environment " + config.environment + ".");
Serve.start(config, function() {
runTask("watch");
});
});
// registerTask('watch:tests', "Watch filesystem for changes and rerun tests automatically", function(done) {
//
// gaze(["app/**/*", "config/**/*", "contracts/**/*", "test/**/*"], {cwd: working_dir, interval: 1000, debounceDelay: 500}, function() {
// // On changed/added/deleted
// this.on('all', function(event, filePath) {
// if (filePath.match(/\/config\/.*?\/.*?\.sol\.js$/)) {
// // ignore changes to /config/*/*.sol.js since these changes every time
// // tests are run (when contracts are compiled)
// return;
// }
// process.stdout.write("\u001b[2J\u001b[0;0H"); // clear screen
// var display_path = "./" + filePath.replace(working_dir, "");
// console.log(colors.cyan(`>> File ${display_path} changed.`));
// run_tests();
// });
// });
//
// var run_tests = function() {
// console.log("Running tests...");
//
// process.chdir(working_dir);
// var config = Config.gather(truffle_dir, working_dir, argv, "test");
// config.argv.quietDeploy = true; // Ensure we're quiet about deploys during tests
//
// Test.run(config, function() { console.log("> test run complete; watching for changes..."); });
// };
// run_tests(); // run once immediately
//
// });
// Default to listing available commands.
var current_task = argv._[0];
if (current_task == null) {
current_task = "list";
}
if (tasks[current_task] == null) {
console.log(colors.red("Unknown command: " + current_task));
process.exit(1);
}
// Something is keeping the process open. I'm not sure what.
// Let's explicitly kill it until I can figure it out.
process.exit(runTask(current_task));
//runTask(current_task);