From f6032d728d8e6324fdb312f7dc163dea833aaf64 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Wed, 24 May 2017 12:57:28 +0200
Subject: [PATCH] =?UTF-8?q?[enh]=C2=A0#989=20Add=20a=20`duniter.statics.qu?=
 =?UTF-8?q?ickRun()`=20facility=20for=20module=20development?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 index.js                                   | 42 ++++++++++++++++++----
 test/integration/scenarios/hello-plugin.js | 18 ++++++++++
 test/integration/v1.0-modules-api.js       |  9 +++++
 3 files changed, 62 insertions(+), 7 deletions(-)
 create mode 100644 test/integration/scenarios/hello-plugin.js

diff --git a/index.js b/index.js
index f66df9097..607a16929 100644
--- a/index.js
+++ b/index.js
@@ -65,13 +65,15 @@ module.exports.statics = {
    * Creates a new stack pre-registered with compliant modules found in package.json
    */
   autoStack: (priorityModules) => {
-    const pjson = require(path.resolve('./package.json'))
     const duniterModules = [];
-
-    // Look for compliant packages
-    const prodDeps = Object.keys(pjson.dependencies);
-    const devDeps = Object.keys(pjson.devDependencies);
-    const duniterDeps = prodDeps.concat(devDeps)
+    let duniterDeps = []
+    try {
+      const pjson = require(path.resolve('./package.json'))
+      // Look for compliant packages
+      const prodDeps = Object.keys(pjson.dependencies);
+      const devDeps = Object.keys(pjson.devDependencies);
+      duniterDeps = prodDeps.concat(devDeps)
+    } catch (e) { /* duniter as a dependency might not be run from an NPM project */ }
     for(const dep of duniterDeps) {
       try {
         const required = require(dep);
@@ -86,7 +88,33 @@ module.exports.statics = {
 
     // The final stack
     return new Stack((priorityModules || []).concat(PRODUCTION_DEPENDENCIES).concat(duniterModules));
-  }
+  },
+
+  quickRun: function() {
+    const deps = Array.from(arguments).map((f, index) => {
+      const canonicalPath = path.resolve(f)
+      console.log(canonicalPath)
+      return {
+        name: 'duniter-quick-module-' + index,
+        required: require(canonicalPath)
+      }
+    })
+    const that = this
+    const stack = this.autoStack(deps)
+    return co(function*() {
+      let res
+      try {
+        res = yield stack.executeStack(that.quickRunGetArgs())
+      } catch(e) {
+        console.error(e)
+      }
+      that.onRunDone()
+      return res
+    })
+  },
+
+  quickRunGetArgs: () => process.argv.slice(),
+  onRunDone: () => process.exit()
 };
 
 function Stack(dependencies) {
diff --git a/test/integration/scenarios/hello-plugin.js b/test/integration/scenarios/hello-plugin.js
new file mode 100644
index 000000000..a73aefa0c
--- /dev/null
+++ b/test/integration/scenarios/hello-plugin.js
@@ -0,0 +1,18 @@
+"use strict"
+
+const co = require('co')
+
+module.exports = {
+  duniter: {
+    cli: [{
+      name: 'hello-world',
+      desc: 'Says hello from \`duniter\` command.',
+      logs: false,
+      onDatabaseExecute: (server, conf, program, params) => co(function*() {
+        const msg = 'Hello world! from within Duniter.'
+        console.log(msg)
+        return msg
+      })
+    }]
+  }
+}
diff --git a/test/integration/v1.0-modules-api.js b/test/integration/v1.0-modules-api.js
index 797143f5f..3355194a2 100644
--- a/test/integration/v1.0-modules-api.js
+++ b/test/integration/v1.0-modules-api.js
@@ -4,6 +4,7 @@ const co      = require('co');
 const _       = require('underscore');
 const should  = require('should');
 const util    = require('util');
+const path    = require('path');
 const stream  = require('stream');
 const duniter = require('../../index');
 const parsers = require('duniter-common').parsers;
@@ -11,6 +12,14 @@ const querablep = require('querablep');
 
 describe("v1.0 Module API", () => {
 
+  it('should be able to execute `hello` command with quickRun', () => co(function*() {
+    duniter.statics.quickRunGetArgs = () => ['', '', 'hello-world']
+    duniter.statics.onRunDone = () => { /* Do not exit the process */ }
+    const absolutePath = path.join(__dirname, './scenarios/hello-plugin.js')
+    const res = yield duniter.statics.quickRun(absolutePath)
+    res.should.equal('Hello world! from within Duniter.')
+  }))
+
   it('should be able to execute `hello` command', () => co(function*() {
 
     const sStack = duniter.statics.simpleStack();
-- 
GitLab