diff --git a/index.js b/index.js index f66df9097cf5623458f78e28327db5a26963a3d2..607a1692930cf326a6faccb9d0366e8f29eb21f8 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 0000000000000000000000000000000000000000..a73aefa0cc98d2e209fd475bb2d8e34cc4b79157 --- /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 797143f5faf3ccc7cfdabcf9f2d885a79b892c42..3355194a2907b6fa74fb2d9d7d3089b5635124ab 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();