Skip to content

Commit

Permalink
Fix: deep objectcopy
Browse files Browse the repository at this point in the history
Add: User Agent to function Stylus
Change: split test case files
Add: common lib
  • Loading branch information
fkei committed Feb 14, 2014
1 parent 8f6a99f commit 7c3b1d8
Show file tree
Hide file tree
Showing 16 changed files with 594 additions and 413 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sprite-logo.styl
sprite-logo@*
user.copy.json
.tern-port
test/hbs/local.develop.html
test/hbs/index.local.develop.html
test/hbs/require.beez.local.develop.js
test/image/sprite/.cache

35 changes: 35 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @name common.js
* @author Kei Funagayama <[email protected]>
* copyright (c) Cyberagent Inc.
* @overview common library
*/

var _ = require('underscore');
var ua = require('beez-ua');

var common = module.exports = {
/**
* Override judgment of UserAgent.
*
* @param {Object} config
* @param {Object} headers
* @return {boolean}
* @name isUAOverride
* @memberof common
*/
isUAOverride: function (config, headers) {
if (!config.extend.hasOwnProperty('condition') ||
!config.extend.condition.hasOwnProperty('ua')) {
return false;
}

// Get OS information from UserAgent
ua.setup(headers['user-agent'] || '-');

return _.some(config.extend.condition.ua, function(os) {
return !!ua.os[os];
});
}

};
57 changes: 44 additions & 13 deletions lib/css/stylus.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ var _ = require('underscore');

var logger = require('../logger');
var fsys = require('../fsys');
var obj = require('../obj');
var constant = require('../constant');
var common = require('../common');

var DEFAULT_OPTIONS = constant.CSS_STYLUS_DEFAULT_OPTIONS;

Expand All @@ -36,16 +38,32 @@ module.exports = {
* @param {function} callback
* @example
* beezlib.css.stylus.write('./test.styl', './test.css', {
* compress: true,
* firebug: false,
* linenos: false,
* nib: true,
* fn : function (styl) {
* styl.define('body-padding', function (data) {
* var rate = data.val || 1;
* var base = 10;
* return (rate * base) + 'px';
* });
* options: {
* compress: true,
* firebug: false,
* linenos: false,
* nib: true,
* fn : function (styl) {
* styl.define('body-padding', function (data) {
* var rate = data.val || 1;
* var base = 10;
* return (rate * base) + 'px';
* });
* }
* },
* {
* extend: {
* "condition": {
* "ua": [ "android", "ios" ]
* },
* "content": {
* "options": {
* "fn": {
* "STAT_URL": "http://stat.example.com"
* }
* }
* }
* }
* }
* }, function (err, css) {
* if (err) {
Expand All @@ -55,7 +73,14 @@ module.exports = {
* });
*
*/
write: function (src, dst, options, callback) {
write: function (src, dst, config, headers, callback) {

if (_.isFunction(headers)) {
callback = headers;
headers = undefined;
}

var options = config.options;

if (!fsys.isFileSync(src)) {
return callback(new Error('source file not found. path:', src));
Expand All @@ -79,10 +104,16 @@ module.exports = {
.define('b64', stylus.url(urlopt))
;

if(options.nib) { // use nib library
if (options.nib) { // use nib library
styl.use(nib());
}

// extend options
if (config.hasOwnProperty('extend') && headers && common.isUAOverride(config, headers) && config.extend.hasOwnProperty('content')) {
options = obj.copy(config.extend.content, config).options;
logger.debug('Override stylus options:', options);
}

// define functions and constant values
if (options.fn && Object.keys(options.fn).length) {
styl.use(function (styl) {
Expand All @@ -107,4 +138,4 @@ module.exports = {
callback(null, css);
});
}
};
};
8 changes: 7 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var colors = require('colors');

var logger = require('./logger');

var common = require('./common');
var obj = require('./obj');
var fsys = require('./fsys');
var cmd = require('./cmd');
Expand Down Expand Up @@ -88,6 +89,12 @@ beezlib = {
* @memberof beezlib
*/
constant: constant,
/**
* common
* @name common
* @memberof beezlib
*/
common: common,
/**
* empty function
* @name none
Expand Down Expand Up @@ -125,7 +132,6 @@ beezlib = {

return colors.setTheme(theme);
}

};

module.exports = beezlib;
26 changes: 3 additions & 23 deletions lib/obj/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

var _ = require('underscore');
var copy = require('copy');

/**
* @name obj
Expand All @@ -14,30 +15,9 @@ var _ = require('underscore');
module.exports = {

/**
* for each props in src
* Warnning: !! destructive !!
*
* @see https://github.com/evlun/copy
* @memberof obj
* @method
* @param {Object} dst destination
* @param {Object} src source
* @return {Object}
*/
copyr: function copyr(dst, src) {
var dstProp, srcProp;

for (var k in src) {
// avoid unnecessary traversing prototype
if (src.hasOwnProperty(k)) {
dstProp = dst[k];
srcProp = src[k];
if (_.isObject(dstProp) && !_.isArray(srcProp)){
copyr(dst[k], src[k]); // cp recursively
} else {
dst[k] = src[k]; // override/add property 'k'
}
}
}
return dst;
}
copy: copy
};
47 changes: 18 additions & 29 deletions lib/template/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,14 @@ var fs = require('fs');

var _ = require('underscore');
var handlebars = require('handlebars');
var ua = require('beez-ua');

var logger = require('../logger');
var obj = require('../obj');
var constant = require('../constant');
var logger = require('../logger');
var common = require('../common');

var DEFAULT_ENCODE = constant.DEFAULT_ENCODE;

/**
* Override judgment of UserAgent.
*
* @param {Object} config
* @param {Object} headers
* @return {boolean}
*/
var isUAOverride = function isUAOverride(config, headers) {
if (!config.extend.hasOwnProperty('condition') ||
!config.extend.condition.hasOwnProperty('ua')) {
return false;
}

// Get OS information from UserAgent
ua.setup(headers['user-agent'] || '-');

return _.some(config.extend.condition.ua, function(os) {
return !!ua.os[os];
});
};

/**
* @name hbs
* @namespace hbs
Expand Down Expand Up @@ -82,6 +61,7 @@ module.exports = {
out.push(' return templates[\''+template+'\'];');
out.push('});');
out = out.join('');

fs.writeFileSync(dstpath, out, encode);

return dstpath;
Expand Down Expand Up @@ -166,18 +146,22 @@ module.exports = {
entrypoint = clientconf.entrypoint;
}

if (clientconf.hasOwnProperty('extend') && headers && isUAOverride(clientconf, headers) && clientconf.extend.hasOwnProperty('content')) {
clientconf.requirejs = obj.copyr(clientconf.requirejs, clientconf.extend.content);
var out = obj.copy(clientconf.requirejs, {});
if (clientconf.hasOwnProperty('extend') && headers && common.isUAOverride(clientconf, headers) && clientconf.extend.hasOwnProperty('content')) {
out = obj.copy(clientconf.extend.content, out);
logger.debug('Override requirehbs2hbsc out:', out);
}

var context = {
config: new handlebars.SafeString(JSON.stringify(clientconf.requirejs, null, ' ')),
config: new handlebars.SafeString(JSON.stringify(out, null, ' ')),
entrypoint: entrypoint,
name: env,
key: key
};

var html = template(context);

///
fs.writeFileSync(dstpath, html, encode);

return dstpath;
Expand Down Expand Up @@ -211,10 +195,13 @@ module.exports = {

var locals = {};
if (store.stat.data.hasOwnProperty(key)) {
locals = JSON.parse(JSON.stringify(store.stat.data[key])); // deep clone
if (locals.hasOwnProperty('extend') && headers && isUAOverride(locals, headers) && locals.extend.hasOwnProperty('content')) {
locals.requirejs = obj.copyr(locals.requirejs, locals.extend.content);
locals = obj.copy( store.stat.data[key], {});

if (locals.hasOwnProperty('extend') && headers && common.isUAOverride(locals, headers) && locals.extend.hasOwnProperty('content')) {
locals.requirejs = obj.copy(locals.extend.content, locals.requirejs);
logger.debug('Override hbs2hbsc2html requirejs:', locals.requirejs);
}

for (var k in locals.requirejs.config) {
var newkey = k.replace('.', '_'); // data.requirejs.config key copy . -> _
locals.requirejs.config[newkey] = locals.requirejs.config[k];
Expand All @@ -225,6 +212,8 @@ module.exports = {
}

var output = template(locals);

///
fs.writeFileSync(dstpath, output, encode);

return dstpath;
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"handlebars",
"csssprite"
],
"scripts": {
"test": "mocha -r should test/test-*.js"
},
"homepage": "https://github.com/CyberAgent/beezlib",
"licenses": [
{
Expand Down Expand Up @@ -56,6 +59,7 @@
"colors": "~0.6.0",
"jsonlint": "~1.6.0",
"beez-ua": "~1.0.3",
"copy": "~0.0.1",
"node-spritesheet": "git+https://github.com/shibucafe/node-spritesheet.git"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion test/hbs/index.html.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1>index.html.hbs</h1>
<h2>config</h2>
<pre>
{{requirejs.config.test}}
{{requirejs}}
</pre>
<hr>

Expand All @@ -20,3 +20,7 @@
<h2>key</h2>
<pre>
{{key}}

<h2>extend</h2>
<pre>
{{extend}}
12 changes: 11 additions & 1 deletion test/store/local/develop.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
{
"develop": { // enviroment name
"entrypoint": "index/index",
"extend": {
"condition": {
"ua": [ "android", "ios" ]
},
"content": {
"config": {
"test": true
}
}
},
"requirejs": {
"test": true,
"config": {
"test": true
"test": false
}
}
// ....
Expand Down
3 changes: 2 additions & 1 deletion test/stylus/test.styl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
body
padding body-padding(3)
font 14px "Lucida Grande", Helvetica, Arial, sans-serif
font 14px "Lucida Grande", Helvetica, Arial, sans-serif
color color()
Loading

0 comments on commit 7c3b1d8

Please sign in to comment.