From 7ef0c6dbfefc98b89efbfd058d476cccbd01ccc6 Mon Sep 17 00:00:00 2001 From: alwx Date: Thu, 2 Feb 2017 17:48:56 +0300 Subject: [PATCH] Watching for DApp changes --- README.md | 26 +++++++++++++--- devtools.js | 2 +- index.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++-- package.json | 3 +- 4 files changed, 107 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c127eb4..e7fdebb 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ Additional tools for DApps developers. These tools allows to speed up the proces ## Requirements 1. Geth (https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum); -2. Node.js -3. NPM +2. Node.js; +3. NPM; +4. Watchman (https://facebook.github.io/watchman/docs/install.html). ## Installing standalone @@ -39,7 +40,7 @@ Example: ### Removing DApp -`./status-dev-cli add-dapp ` +`./status-dev-cli remove-dapp ` * `attach_to` — Geth endpoint; * `public_key` — Public key of your user (you can find it in your profile); @@ -49,4 +50,21 @@ Example: ``` ./status-dev-cli remove-dapp http://localhost:8545 "0x04..." "dapp-test" -``` \ No newline at end of file +``` + +### Watching for DApp changes and updating it automatically + +`./status-dev-cli watch-dapp ` + +* `attach_to` — Geth endpoint; +* `public_key` — Public key of your user (you can find it in your profile); +* `dapp_identity` — `whisper-identity` of your DApp; +* `dapp_dir` — dir that should be observed. + +Example: + +``` +./status-dev-cli watch-dapp http://localhost:8545 "0x04..." "dapp-test" ~/Documents/DApps/dapp-test/ +``` + + diff --git a/devtools.js b/devtools.js index 8f0b23f..74db2fe 100644 --- a/devtools.js +++ b/devtools.js @@ -27,7 +27,7 @@ var status = { "ttl": 2, "payload": web3.fromAscii("{:type :debug :action :dapp-changed :args \"" + argsHex + "\"}\n") }); - return "DApp has been removed"; + return "Notification sent"; } }; diff --git a/index.js b/index.js index 54cc9f8..786f2ba 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,14 @@ #!/usr/bin/env node const cli = require("commander") const child = require('child_process') -const pkgJson = require(__dirname + '/package.json'); +const watchman = require('fb-watchman'); +const pkgJson = require(__dirname + '/package.json'); const devtoolsPath = __dirname + '/devtools.js'; -var fromAscii = function(str) { +var client = new watchman.Client(); + +function fromAscii(str) { var hex = ""; for(var i = 0; i < str.length; i++) { var code = str.charCodeAt(i); @@ -16,6 +19,42 @@ var fromAscii = function(str) { return "0x" + hex; }; +function makeSubscription(client, watch, relativePath, attachTo, publicKey, dapp) { + sub = { + expression: ["allof", ["match", "*.*"]], + fields: ["name"] + }; + if (relativePath) { + sub.relative_root = relativePath; + } + + client.command(['subscribe', watch, 'dapp-subscription', sub], + function (error, resp) { + if (error) { + console.error('Failed to subscribe: ', error); + return; + } + console.log('Subscription established'); + } + ); + + client.on('subscription', function (resp) { + if (resp.subscription !== 'dapp-subscription') return; + + resp.files.forEach(function (file) { + console.log('File changed: ' + file); + }); + + child.execSync( + "geth --exec '" + + "loadScript(\"" + devtoolsPath + "\");" + + "status.notifyDAppChanged(\"" + publicKey + "\", \"" + dapp + "\");'" + + " " + + "attach " + attachTo + ); + }); +} + cli.version(pkgJson.version); cli.command("add-dapp ") @@ -32,7 +71,7 @@ cli.command("add-dapp ") }); cli.command("remove-dapp ") - .description("Adds a DApp to contacts and chats") + .description("Removes a debuggable DApp") .action(function (attachTo, publicKey, dappIdentity) { dapp = fromAscii(JSON.stringify({"whisper-identity": dappIdentity})); child.execSync( @@ -44,6 +83,46 @@ cli.command("remove-dapp ") ); }); +cli.command("watch-dapp ") + .description("Starts watching for DApp changes") + .action(function (attachTo, publicKey, dappIdentity, dappDir) { + dapp = fromAscii(JSON.stringify({"whisper-identity": dappIdentity})); + + client.capabilityCheck( + {optional:[], required:['relative_root']}, + function (error, resp) { + if (error) { + console.log(error); + client.end(); + return; + } + + client.command( + ['watch-project', dappDir], + function (error, resp) { + if (error) { + console.error('Error initiating watch:', error); + return; + } + + if ('warning' in resp) { + console.log('Warning: ', resp.warning); + } + + makeSubscription( + client, + resp.watch, + resp.relative_path, + attachTo, + publicKey, + dapp + ); + } + ); + } + ); + }); + cli.on("*", function(command) { console.error("Unknown command " + command[0] + ". See --help for valid commands") }); diff --git a/package.json b/package.json index 4714abf..d91175d 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "Status.im", "license": "MPL-2.0", "dependencies": { - "commander": "^2.9.0" + "commander": "^2.9.0", + "fb-watchman": "^2.0.0" } }