Skip to content

Commit

Permalink
Merged branch addprocesses into master
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Sep 29, 2016
2 parents 6eff603 + e9468ec commit 6bfb678
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/cli/helpers/abe-plugins.js
Expand Up @@ -51,6 +51,18 @@ class Plugins {
plugin.templates = null
}

// has process
var plugProcess = path.join(plugin.path, 'process')
if(folderUtils.isFolder(plugProcess)) {
plugin.process = {}
var processFiles = FileParser.getFiles(plugProcess, true, 0)
Array.prototype.forEach.call(processFiles, (processFile) => {
plugin.process[processFile.cleanNameNoExt] = processFile.path
})
}else {
plugin.process = null
}

// has routes
var plugRoutes = path.join(plugin.path, 'routes')
if(folderUtils.isFolder(plugRoutes)) {
Expand Down Expand Up @@ -87,6 +99,20 @@ class Plugins {
return this[singleton]
}

getProcess(fn) {
var proc = null
if(typeof this._plugins !== 'undefined' && this._plugins !== null) {
Array.prototype.forEach.call(this._plugins, (plugin) => {
if(typeof plugin.process !== 'undefined' && plugin.process !== null
&& typeof plugin.process[fn] !== 'undefined' && plugin.process[fn] !== null) {
proc = plugin.process[fn]
}
})
}

return proc
}

hooks() {
if(arguments.length > 0) {
var args = [].slice.call(arguments)
Expand Down
24 changes: 22 additions & 2 deletions src/cli/helpers/abe-process.js
@@ -1,7 +1,9 @@
import process from 'child_process'
import fse from 'fs-extra'

import {
config
,Plugins
} from '../'

function prepend(value, array) {
Expand All @@ -10,9 +12,27 @@ function prepend(value, array) {
return newArray
}

var abeProcess = function(name, args) {
var abeProcess = function(name, args = []) {
args = prepend(`ABE_WEBSITE=${config.root}`, args)
var publishAll = process.fork(`${__dirname}/../../cli/process/${name}.js`, args)
args = prepend(`ABEJS_PATH=${__dirname}/../../../dist`, args)

var file = `${__dirname}/../../cli/process/${name}.js`
try {
var stats = fse.statSync(file)
if (stats.isFile()) {
process.fork(file, args)
}
}catch(err) {
try {
file = Plugins.instance.getProcess(name)
var stats = fse.statSync(file)
if (stats.isFile()) {
process.fork(file, args)
}
}catch(err) {

}
}
}

export default abeProcess
9 changes: 9 additions & 0 deletions test/fixtures/plugins/test/hooks/hooks.js
@@ -0,0 +1,9 @@
'use strict';

var hooks = {
afterEditorInput: function() {
return 'test';
}
};

exports.default = hooks;
1 change: 1 addition & 0 deletions test/fixtures/plugins/test/process/test.js
@@ -0,0 +1 @@
console.log('test')
8 changes: 8 additions & 0 deletions test/fixtures/plugins/test/routes/get/test.js
@@ -0,0 +1,8 @@
'use strict';

var route = function route(req, res, next, abe) {
res.set('Content-Type', 'text/html');
return res.send('test');
}

exports.default = route
38 changes: 38 additions & 0 deletions test/plugin.js
@@ -0,0 +1,38 @@
var chai = require('chai');

var config = require('../src/cli').config
config.set({root: __dirname + '/fixtures'})

var Hooks = require('../src/cli').Hooks;
var Plugins = require('../src/cli').Plugins;
var Manager = require('../src/cli').Manager;
var fse = require('fs-extra');

describe('Plugin', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
this.fixture = {}
done()

}.bind(this))
});

/**
* getRoutes
*
*/
it('getRoutes()', function() {
var routes = Plugins.instance.getRoutes()
chai.expect(routes[0].get).to.have.length(1);
});

/**
* Hooks.instance.trigger
*
*/
it('Hooks.instance.trigger', function() {
var res = Hooks.instance.trigger('afterEditorInput')
chai.assert.equal(res, 'test', 'Hook test failed !')
});
});
38 changes: 38 additions & 0 deletions test/process.js
@@ -0,0 +1,38 @@
var chai = require('chai');

var config = require('../src/cli').config
config.set({root: __dirname + '/fixtures'})

var Manager = require('../src/cli').Manager;
var Plugins = require('../src/cli').Plugins;
var abeProcess = require('../src/cli/helpers/abe-process').default

describe('Process', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
this.fixture = {}
done()

}.bind(this))
});

/**
* getRoutes
*
*/
it('getProcess()', function() {
var file = Plugins.instance.getProcess('test')
chai.expect(file).to.not.be.null;
});


/**
* Hooks.instance.trigger
*
*/
it('abeProcess', function() {
var res = abeProcess('test', [])
// chai.assert.equal(res, 'test', 'Hook test failed !')
});
});

0 comments on commit 6bfb678

Please sign in to comment.