Skip to content

Commit

Permalink
Adapter refactor for async.
Browse files Browse the repository at this point in the history
  • Loading branch information
nporteschaikin committed Jun 19, 2014
1 parent 5dfb040 commit f1fbcb2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 63 deletions.
50 changes: 50 additions & 0 deletions lib/adapter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var map = require('./map')

, fs = require('fs')
, path = require('path')

, read = fs.readFileSync

This comment has been minimized.

Copy link
@jescalan

jescalan Jun 19, 2014

Still sync here...

, extname = path.extname;

var Adapter = function (source, options) {
this.source = source;
this.options = (options || {});
this.compiler = Adapter.compiler(options);
this.kind = Adapter.kind(options);
}

Adapter.prototype = {
render: function () {
if (this.compiler) {
if (!this.source && this.options.path)
this.source = read(this.options.path, 'utf8');
return this.compiler.render(this.source, this.options);
}
}
}

Adapter.compiler = function (options) {
if (options.path && !options.with)
options.with = map.extensions[extname(options.path)];
return map.compilers[options.with];
}

Adapter.kind = function (options) {
var compiler
, kind;

options = (options || {});
compiler = Adapter.compiler(options);

if (compiler) {
kind = map.kinds[compiler.kind];
kind.name = compiler.kind;
return kind;
}
}

Adapter.supports = function (options) {
return !!Adapter.compiler(options);
}

module.exports = Adapter;
74 changes: 28 additions & 46 deletions lib/compiler/adapter.js → lib/adapter/map.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
var path = require('path')

, extension = path.extname
, dirname = path.dirname;

var map = {
module.exports = {

compilers: {

'coffee-script': {
kind: 'javascript',
render: function (str, path) {
return require('coffee-script').compile(str);
render: function (source, options) {
var coffee = require('coffee-script')
return coffee.compile(source, options);
}
},

'css': {
kind: 'css',
render: function (str, path) {
return str;
render: function (source, options) {
return source;
}
},

'haml': {
kind: 'html',
render: function (str, path) {
return require('hamljs').render(str, { filename: path });
render: function (source, options) {
var haml = require('hamljs')
return haml.render(source, options);
}
},

'html': {
kind: 'html',
render: function (str, path) {
return str;
render: function (source, options) {
return source;
}
},

'jade': {
kind: 'html',
render: function (str, path) {
return require('jade').render(str, { filename: path });
render: function (source, options) {
var jade = require('jade');
return jade.render(source, options);
}
},

'js': {
kind: 'javascript',
render: function (str, path) {
return str;
render: function (source) {
return source;
}
},

'less': {
kind: 'css',
render: function (str, path) {
var css;
require('less').render(str, { filename: path }, function (error, output) {
render: function (source, options) {
var less = require('less')
, output;
less.render(source, options, function (error, css) {
if (error) throw error;
css = output;
output = css;
});
return css;
return output;
}
},

'sneak': {
kind: 'html',
render: function (str, path) {
return require('sneak').render(str, { basepath: dirname(path) });
render: function (source, options) {
var sneak = require('sneak');
return sneak.render(source, { basepath: dirname(options.path) });
}
},

'stylus': {
kind: 'css',
render: function (str, path) {
return require('stylus').render(str, { filename: path });
render: function (source, options) {
var stylus = require('stylus');
return stylus.render(source, options);
}
}

Expand Down Expand Up @@ -132,22 +133,3 @@ var map = {
}

}

module.exports = function (str, options) {

if (!options.with && options.path) {
options.with = map.extensions[extension(options.path)];
}

var compiler = map.compilers[options.with];
if (compiler) {
var kind = map.kinds[compiler.kind];
kind.name = compiler.kind;
return {
source: compiler.render(str, options.path)
, kind: kind
}
};

return false;
}
2 changes: 1 addition & 1 deletion lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Commands.prototype = {
this.bind();
}
return this.exec()
.catch(this.error.bind(this));
// .catch(this.error.bind(this));

This comment has been minimized.

Copy link
@jescalan

jescalan Jun 19, 2014

?

},

exec: function () {
Expand Down
15 changes: 8 additions & 7 deletions lib/compiler/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Lexer = require('./lexer')
, Adapter = require('./adapter')
, Adapter = require('../adapter')

, fs = require('fs')
, path = require('path')
Expand All @@ -9,12 +9,13 @@ var Lexer = require('./lexer')
, dirname = path.dirname;

var Parser = function (source, options) {
var adapter
, lexer
var lexer
, options = options || {};
if (adapter = Adapter(source, options)) {

if (Adapter.supports(options)) {
this.adapter = adapter = new Adapter(source, options);
this.kind = adapter.kind;
this.lexer = new Lexer(adapter.source, adapter.kind);
this.lexer = new Lexer(adapter.render(), adapter.kind);
this.tokens = this.lexer.exec();
this.path = options.path;
this.nodes = [];
Expand Down Expand Up @@ -87,9 +88,9 @@ Parser.prototype = {
}

if (include.value.src) {

options = {
in: this.kind
, path: include.value.src
path: include.value.src
}

parser = new Parser(read(join(dirname(this.path), options.path), 'utf8'), options);
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,4 @@ Peak.prototype = {

}

Peak.Compiler = Compiler;
module.exports = Peak;
16 changes: 8 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ var Peak = require('../')
, Commands = require('../lib/commands')
, Lexer = require('../lib/compiler/lexer')
, Parser = require('../lib/compiler/parser')
, Adapter = require('../lib/compiler/adapter')
, Compiler = require('../lib/compiler')
, Adapter = require('../lib/adapter')

, chai = require('chai')
, fs = require('fs')
Expand Down Expand Up @@ -314,32 +314,32 @@ describe('parser', function () {
describe('adapter', function () {

it('should return js kind if ext is .coffee', function () {
var adapter = Adapter('$ ->\nconsole.log \'test\'', { path: 'test.coffee' });
var adapter = new Adapter('$ ->\nconsole.log \'test\'', { path: 'test.coffee' });
adapter.kind.name.should.eq('javascript');
})

it('should return html kind if ext is .jade', function () {
var adapter = Adapter('html\n head', { path: 'test.jade' });
var adapter = new Adapter('html\n head', { path: 'test.jade' });
adapter.kind.name.should.eq('html');
})

it('should return html kind if ext is .sneak', function () {
var adapter = Adapter('html\n head', { path: 'test.sneak' });
var adapter = new Adapter('html\n head', { path: 'test.sneak' });
adapter.kind.name.should.eq('html');
})

it('should return html kind if ext is .haml', function () {
var adapter = Adapter('%html', { path: 'test.haml' });
var adapter = new Adapter('%html', { path: 'test.haml' });
adapter.kind.name.should.eq('html');
})

it('should return css kind if ext is .styl', function () {
var adapter = Adapter('body\n background: black', { path: 'test.styl' });
var adapter = new Adapter('body\n background: black', { path: 'test.styl' });
adapter.kind.name.should.eq('css');
})

it('should return css kind if ext is .less', function () {
var adapter = Adapter('body { &.index { background: blue; } }', { path: 'test.less' });
var adapter = new Adapter('body { &.index { background: blue; } }', { path: 'test.less' });
adapter.kind.name.should.eq('css');
})

Expand Down Expand Up @@ -528,7 +528,7 @@ describe('watcher', function (){

describe('deployer', function () {

this.timeout(10000);
this.timeout(20000);

it('should throw exception if credentials are invalid', function (done) {
var peak = new Peak(join(fixtures, 'deployer', 'invalid'), {email: "[email protected]", password: "!@#$%^&"});
Expand Down

0 comments on commit f1fbcb2

Please sign in to comment.