UNPKG

2.94 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 + 1;
20 var filename = result.filename + ".js";
21
22 var exampleContract = fs.readFileSync(path.join(__dirname, "..", contract-example.js), "utf8");
23 exampleContract = exampleContract.replace(/ExampleContract/g, name);
24 exampleContract = exampleContract.replace("//self.type = null;", "self.type = " + type);
25 fs.writeFileSync(path.join(contractsPath, filename), exampleContract, "utf8");
26
27 console.log("New contract created: " + ("./contracts/" + filename));
28 console.log("Updating contracts list");
29
30 var text = fs.readFileSync(path.join(".", "modules.full.json"), "utf8");
31 var modules = JSON.parse(text);
32 var contractName = "contracts/" + name;
33 var dappPathConfig = "./" + path.join(contractsPath, filename);
34
35 modules[contractName] = dappPathConfig;
36 modules = JSON.stringify(modules, false, 2);
37
38 fs.writeFileSync(path.join(".", "modules.full.json"), modules, "utf8");
39 console.log("Done");
40 });
41 } catch (e) {
42 console.log(e);
43 }
44}
45
46function deleteContract() {
47 inquirer.prompt([
48 {
49 type: "input",
50 name: "filename",
51 message: "Contract file name (without .js)"
52 }
53 ], function (result) {
54 var name = result.filename;
55 var type = filenames.length + 1;
56 var filename = result.filename + ".js";
57
58 var contractPath = path.join(contractsPath, filename);
59 var exists = fs.existsSync(contractPath);
60 if (!exists) {
61 return console.log("Contract not found: " + contractPath);
62 }
63 try {
64 fs.unlinkSync(contractPath);
65 console.log("Contract removed");
66 console.log("Updating contracts list");
67
68 var text = fs.readFileSync(path.join(".", "modules.full.json"), "utf8");
69 var modules = JSON.parse(text);
70 var name = "contracts/" + name;
71 delete modules[name];
72 modules = JSON.stringify(modules, false, 2);
73 fs.writeFileSync(path.join(".", "modules.full.json"), modules, "utf8");
74 console.log("Done");
75 } catch (e) {
76 console.log(e);
77 }
78 });
79}
80
81module.exports = function (program) {
82 program
83 .command("contract")
84 .description("contract operations")
85 .option("-a, --add", "add new contract")
86 .option("-d, --delete", "delete contract")
87 .action(function (options) {
88 var exist = fs.exists(contractsPath);
89 if (exist) {
90 if (options.add) {
91 addContract();
92 } else if (options.delete) {
93 deleteContract();
94 } else {
95 console.log("'node contract -h' to get help");
96 }
97 } else {
98 return console.log("./modules/contracts path not found, please change directory to your dapp folder");
99 }
100 });
101}
\No newline at end of file