Skip to content
Snippets Groups Projects
Commit f6032d72 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[enh] #989 Add a `duniter.statics.quickRun()` facility for module development

parent e2cc4264
No related branches found
No related tags found
No related merge requests found
......@@ -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 = [];
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);
const duniterDeps = prodDeps.concat(devDeps)
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) {
......
"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
})
}]
}
}
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment