Skip to content

Commit

Permalink
Started using eson for more JSON parsing, this is a step towards supp…
Browse files Browse the repository at this point in the history
…orting #30 as eson

already supports including of files. A nice addition to this is that it also supports glob
for files, so no need to make that happen for us as well.
  • Loading branch information
3rd-Eden committed Jul 17, 2012
1 parent 4bb58e1 commit 9f73f61
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 66 deletions.
201 changes: 139 additions & 62 deletions lib/square.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ require('sugar');
*/

var Logger = require('devnull')
, _ = require('lodash')
, async = require('async')
, exec = require('shelljs').exec
, Codesurgeon = require('codesurgeon').Codesurgeon;
, eson = require('eson')
, _ = require('lodash')
, exec = require('shelljs').exec;

/**
* Pre-process filters.
Expand Down Expand Up @@ -66,55 +66,123 @@ var reqparse = /(\/\*|\/\/)\s*\[square\]\s*\@(require|import|include)\s*\"([^"]+
*/

var Square = module.exports = function Square(options) {
var self = this
, stdout = false;

this.env = process.env.NODE_ENV || 'development';
this.home = process.env.HOME || process.env.USERPROFILE;
this.reqparse = reqparse;
this.cli = false;
this.logger = new Logger({
timestamp: false
, namespacing: 0
, notification: 0
});
this.commentStyles = {
js: {
header: '/*!'
, body: ' *'
, footer: ' */'
}
, css: {
header: '/*!'
, body: ' *'
, footer: ' */'
}
};
// create a new dev/null logger instance
this.logger = new Logger({ timestamp: false, namespacing: 0 });

// setup our eson parser, which will pre-parse our configuration files
this.eson = eson()
.use(eson.include)
.use(eson.bools)
.use(eson.glob);

// When the stdout properly is set we need to see if we should enable logging
// or not.
Object.defineProperty(this, 'stdout', {
get: function get() {
return stdout;
}

, set: function set(bool) {
// check if we need to silence the logger
self.logger.set('level', bool ? 0 : 8);
stdout = !!bool;
}
get: this.getSTDOUT
, set: this.setSTDOUT
});

// the extend should happen after we have set all the Object.define's so they
// can get triggered once our options are updated.
_.extend(this, options || {});

// should not be overridden
// these values should never be overriden by the _.extend
this.config = require('../static');
this.middleware = [];
this.package = {};
};

Square.prototype.__proto__ = EventEmitter.prototype;

/**
* The current process env.
*
* @type {String}
*/

Square.prototype.env = process.env.NODE_ENV || 'development';

/**
* Path to the $HOME directory of the user, so we replace ./~ paths with this
* value.
*
* @type {String}
*/

Square.prototype.home = process.env.HOME || process.env.USERPROFILE;

/**
* Regular Expression for parsing [square] import/require direcives.
*
* @type {RegExp}
*/

Square.prototype.reqparse = reqparse;

/**
* Boolean flag so we know if we run as an API or as a CLI application.
*
* @type {Boolean}
*/

Square.prototype.cli = false;

/**
* Boolean flag that indicates if we need output data to stdout.
*
* @type {Boolean}
* @api private
*/

Square.prototype.standardOutput = false;

/**
* Small helper object that allows us to wrap files in the correct comment
* syntax based on the file extension. The key is the file extension and the
* value is an Object that shows everything should be wrapped.
*
* @type {Object}
*/

Square.prototype.commentStyles = {
js: {
header: '/*!'
, body: ' *'
, footer: ' */'
}
, css: {
header: '/*!'
, body: ' *'
, footer: ' */'
}
};

/**
* Setup our STDOUT get handler.
*
* @returns {Boolean}
* @api private
*/

Square.prototype.getSTDOUT = function getSTDOUT() {
return this.standardOutput;
};

/**
* When STDOUT is to true we need to silence our logger, so it doesn't output
* any information as we will be writing our own data to stdout.
*
* @param {Boolean} bool
* @returns {Boolean}
* @api private
*/

Square.prototype.setSTDOUT = function setSTDOUT(bool) {
this.logger.set('level', bool ? 0 : 8);

return this.standardOutput = !!bool;
};

/**
* Middleware layer. Use different middleware layers for compiling your bundles.
* The order of definition is respected.
Expand Down Expand Up @@ -235,6 +303,32 @@ Square.prototype.forEach = function forEach(collection, cb) {
);
};

/**
* Runs the supplied configuration function only for the set env. This allows
* you to use different middleware stacks for development and production. You
* can specify as many environments as you wish. It only requires a callback
* function as last argument. All other arguments are seen as environment
* variables where it should be toggled.
*
* @param {String} env environment
* @param {Function} fn callback
* @api public
*/

Square.prototype.configure = function configure(evn, fn) {
var envs = 'all'
, args = slice.call(arguments);

// setup the correct argument structure
fn = args.pop();

if (args.length) envs = args;
if (envs === 'all' || ~envs.indexOf(this.env)) fn.call(this);

return this;
};


/**
* Pre-process a single bundle.
*
Expand Down Expand Up @@ -291,31 +385,6 @@ Square.prototype.preprocess = function preprocess(bundle, details, fn) {
return this;
};

/**
* Runs the supplied configuration function only for the set env. This allows
* you to use different middleware stacks for development and production. You
* can specify as many environments as you wish. It only requires a callback
* function as last argument. All other arguments are seen as environment
* variables where it should be toggled.
*
* @param {String} env environment
* @param {Function} fn callback
* @api public
*/

Square.prototype.configure = function configure(evn, fn) {
var envs = 'all'
, args = slice.call(arguments);

// setup the correct argument structure
fn = args.pop();

if (args.length) envs = args;
if (envs === 'all' || ~envs.indexOf(this.env)) fn.call(this);

return this;
};

/**
* Parse the given bundle, this function accepts multiple argument formats.
*
Expand Down Expand Up @@ -384,13 +453,21 @@ Square.prototype.read = function read(args) {
args.package = fs.readFileSync(args.string, 'utf8');
}

// apply extra JSON transformation that are done by eson, such as file
// glob's, JSON includes etc
this.package = this.eson.parse(JSON.stringify(this.package));

this.package.path = path.dirname(args.string); // folder of the bundle
this.package.source = args.package; // stringified content
this.package.location = args.string; // location of the bundle
} else if (args.object) {
this.package = args.object;
args.package = JSON.stringify(this.package);

// apply extra JSON transformation that are done by eson, such as file
// glob's, JSON includes etc
this.package = this.eson.parse(args.package);

this.package.path = path.dirname(process.env.PWD); // folder of the bundl
this.package.source = args.package; // stringified content
this.package.location = process.env.PWD; // location of the bundle
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
},
"dependencies": {
"async": "0.1.22",
"eson": "0.2.0"
"canihaz": "0.0.x",
"codesurgeon": "0.3.2",
"colors": "0.6.0-1",
"commander": "0.6.1",
"devnull": "0.0.9",
"eventreactor": "0.0.6",
"findit": "0.1.2",
"fs.notify": "0.0.x",
"glob": "3.1.10",
"lodash": "0.4.0",
"shelljs": "0.0.6pre2",
"sugar": "1.2.5"
},
"canihaz": {
"active-x-obfuscator": "0.0.1",
"codesurgeon": "0.3.2",
"coffee-script": "1.3.3",
"csslint": "0.9.8",
"fs.notify": "0.0.x",
"github": "0.1.3",
"jade": "0.26.3",
"jsdom": "0.2.14",
Expand Down

0 comments on commit 9f73f61

Please sign in to comment.