UNPKG

3.37 kBJavaScriptView Raw
1
2var inquirer = require("inquirer");
3var fs = require("fs");
4var path = require("path");
5
6var contractsPath = path.join(".", "modules", "contracts");
7
8function addContract() {
9 try {
10 var filenames = fs.readdirSync(contractsPath);
11 inquirer.prompt([
12 {
13 type: "input",
14 name: "filename",
15 message: "Contract file name (without .js)"
16 }
17 ], function (result) {
18 var name = result.filename;
19 var type = filenames.length;
20 var filename = result.filename + ".js";
21
22 var className = '';
23 for (var i = 0; i < name.length; ++i) {
24 className += (i==0 ? name[i].toUpperCase() : name[i]);
25 }
26 var exampleContract = fs.readFileSync(path.join(__dirname, "..", "contract-example.js"), "utf8");
27 exampleContract = exampleContract.replace(/ExampleContract/g, className);
28 exampleContract = exampleContract.replace(/__TYPE__/g, 'TransactionTypes.' + name.toUpperCase());
29 fs.writeFileSync(path.join(contractsPath, filename), exampleContract, "utf8");
30
31 var typesFile = path.resolve("./modules/helpers/transaction-types.js");
32 var transactionTypes = require(typesFile);
33 transactionTypes[name.toUpperCase()] = type;
34 fs.writeFileSync(typesFile, 'module.exports = ' + JSON.stringify(transactionTypes, null, 2), "utf8");
35
36 console.log("New contract created: " + ("./contracts/" + filename));
37 console.log("Updating contracts list");
38
39 var text = fs.readFileSync(path.join(".", "modules.full.json"), "utf8");
40 var modules = JSON.parse(text);
41 var contractName = "contracts/" + name;
42 var dappPathConfig = "./" + path.join(contractsPath, filename);
43
44 modules[contractName] = dappPathConfig;
45 modules = JSON.stringify(modules, false, 2);
46
47 fs.writeFileSync(path.join(".", "modules.full.json"), modules, "utf8");
48 console.log("Done");
49 });
50 } catch (e) {
51 console.log(e);
52 }
53}
54
55function deleteContract() {
56 inquirer.prompt([
57 {
58 type: "input",
59 name: "filename",
60 message: "Contract file name (without .js)"
61 }
62 ], function (result) {
63 var name = result.filename;
64 var type = filenames.length + 1;
65 var filename = result.filename + ".js";
66
67 var contractPath = path.join(contractsPath, filename);
68 var exists = fs.existsSync(contractPath);
69 if (!exists) {
70 return console.log("Contract not found: " + contractPath);
71 }
72 try {
73 fs.unlinkSync(contractPath);
74 console.log("Contract removed");
75 console.log("Updating contracts list");
76
77 var text = fs.readFileSync(path.join(".", "modules.full.json"), "utf8");
78 var modules = JSON.parse(text);
79 var name = "contracts/" + name;
80 delete modules[name];
81 modules = JSON.stringify(modules, false, 2);
82 fs.writeFileSync(path.join(".", "modules.full.json"), modules, "utf8");
83 console.log("Done");
84 } catch (e) {
85 console.log(e);
86 }
87 });
88}
89
90module.exports = function (program) {
91 program
92 .command("contract")
93 .description("contract operations")
94 .option("-a, --add", "add new contract")
95 .option("-d, --delete", "delete contract")
96 .action(function (options) {
97 var exist = fs.existsSync(contractsPath);
98 if (exist) {
99 if (options.add) {
100 addContract();
101 } else if (options.delete) {
102 deleteContract();
103 } else {
104 console.log("'node contract -h' to get help");
105 }
106 } else {
107 return console.log("./modules/contracts path not found, please change directory to your dapp folder");
108 }
109 });
110}