Skip to content

Commit

Permalink
use jshint
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstallard committed May 23, 2013
1 parent 85a4795 commit bee198d
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 213 deletions.
6 changes: 2 additions & 4 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
"curly" : false,
"immed" : true,
"latedef" : true,
"newcap" : true,
"noarg" : true,
"sub" : true,
"undef" : true,
"node" : true,
"es5" : true,
"strict" : true,
"camelcase" : true,
"nonew" : true,
"unused" : true,
"trailing" : true,
"multistr" : true
"multistr" : true,
"expr" : true
}
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-vows-runner');

// default task
grunt.registerTask('default', ['vows']);
grunt.registerTask('default', ['jshint', 'vows']);

};
2 changes: 1 addition & 1 deletion bin/modest-bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

var fs = require('fs');
'use strict';

var _ = require('underscore');
var optimist = require('optimist');
Expand Down
182 changes: 94 additions & 88 deletions lib/ModestCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ function ModestCompiler(params){
//
this.params = _.defaults(params, ModestCompiler.options);
this.scripts = [];
if(this.params.jqueryPath)
if (this.params.jqueryPath) {
this.scripts.push(this.params.jqueryPath);
else
}
else {
$ = require('jquery');
}
_.bindAll(this);
}

Expand All @@ -38,135 +40,139 @@ ModestCompiler.prototype = {
resetModest : function(){
modest.reset();
},
compileFile : function(file,callback){
if(!this.params.quiet)
compileFile : function(file, callback){
if (!this.params.quiet) {
console.log('\tcompiling ' + file);
}
jsdom.env(file,
this.scripts,
function(errors, window) {
var script, output, $body, $document, $ssJs;

if(errors)
callback(errors);
else {
try{
modest.setWindow(window);
modest.loadModules(process.cwd());
modest.compileModules();

$document = $(document);
$body = $(document.body);

// Remove dummy elements

$document.find('[dummy="true"]').remove();

// Compile body

modest.compileNode($body);

// Remove 'uses' attributes

$body.find('[uses]').not('[uses=""]').removeAttr('uses');

// Remove any modest-preview script tags

$document.find('head script[src="' + this.params.previewScript + '"]').remove();

// Remove any jquery script tags inserted by jsdom

$document.find('script.jsdom').remove();

// Execute and then remove js marked for preprocessing

$ssJs = $document.find('script[pre="true"]');

$ssJs.each(function(){
require(process.cwd() + '/' + this.getAttribute('src'));
});

$ssJs.remove();

// Add a script tag with a reference to 'modest.js', if needed

if(!_.isEmpty(modest.saveAsJs)){
script = window.document.createElement('script');
script.src = 'modest.js';
insertAfter(window.document.body, script);
}

// Write the compiled xhtml out to a file (minus the '-pre')
this.scripts,
function(errors, window){
var script, output, $body, $document, $ssJs;

output = normalize(window.document.innerHTML);
fs.writeFileSync(file.replace(/-pre(\..+)?$/,'$1'),output);
if (errors) {
callback(errors);
}
catch(e){
callback(e);
else {
try {
modest.setWindow(window);
modest.loadModules(process.cwd());
modest.compileModules();

$document = $(document);
$body = $(document.body);

// Remove dummy elements

$document.find('[dummy="true"]').remove();

// Compile body

modest.compileNode($body);

// Remove 'uses' attributes

$body.find('[uses]').not('[uses=""]').removeAttr('uses');

// Remove any modest-preview script tags

$document.find('head script[src="' + this.params.previewScript + '"]').remove();

// Remove any jquery script tags inserted by jsdom

$document.find('script.jsdom').remove();

// Execute and then remove js marked for preprocessing

$ssJs = $document.find('script[pre="true"]');

$ssJs.each(function(){
require(process.cwd() + '/' + this.getAttribute('src'));
});

$ssJs.remove();

// Add a script tag with a reference to 'modest.js', if needed

if (!_.isEmpty(modest.saveAsJs)) {
script = window.document.createElement('script');
script.src = 'modest.js';
insertAfter(window.document.body, script);
}

// Write the compiled xhtml out to a file (minus the '-pre')

output = normalize(window.document.innerHTML);
fs.writeFileSync(file.replace(/-pre(\..+)?$/, '$1'), output);
}
catch (e) {
callback(e);
}
callback();
}
callback();
}
}.bind(this));
}.bind(this));
},
writeModestJs : function(){
// Save the modules requested for client-side use in 'modest.js'
// Save the modules requested for client-side use in 'modest.js'

var module, modestJs, moduleDefinition;
var savedModules = '';

for (module in modest.saveAsJs){
for (module in modest.saveAsJs) {
moduleDefinition = normalize(modest.modules[module]) + "';\n";
savedModules += 'modest.modules.' + module + " = '" + moduleDefinition;
}

if(savedModules !== ''){
if (savedModules !== '') {

modestJs = fs.readFileSync(this.params.previewScript, 'utf8');

modestJs = fs.readFileSync(this.params.previewScript,'utf8');

// Take out everything that isn't needed from the modest object
modestJs = modestJs.replace(/\/\/#REMOVE-POST-COMPILE[\s\S]*?\/\/#!REMOVE-POST-COMPILE/g,'');

modestJs = modestJs.replace(/\/\/#REMOVE-POST-COMPILE[\s\S]*?\/\/#!REMOVE-POST-COMPILE/g, '');

// Write the saved modules and the modest object to modest.js

fs.writeFileSync('modest.js', modestJs + savedModules);
}
}
},
compileFiles : function(callback){
// Compile all the files in the current directory
// Compile all the files in the current directory

var files = fs.readdirSync('.');
var toCompile = [];

// Clear out any existing modules

this.resetModest();

// Compile all the files that contain '-pre.' or end in '-pre'

_.each(files, function(f){
if(/-pre(?:\.|$)/.test(f))
if (/-pre(?:\.|$)/.test(f)) {
toCompile.push(async.apply(this.compileFile, f));
}
}.bind(this));

// Compile the files in series so dependency detection works
async.series(toCompile,function(err){
if(err)

async.series(toCompile, function(err){
if (err) {
throw(err);
else
}
else {
this.writeModestJs();
}
callback(err);
}.bind(this));
}
};

function insertAfter(referenceNode, newNode)
{
function insertAfter(referenceNode, newNode){
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function normalize(string){
return string.replace(/\s*[\r\n\t]\s*/g,'');
return string.replace(/\s*[\r\n\t]\s*/g, '');
}

module.exports = ModestCompiler;
Expand Down
Loading

0 comments on commit bee198d

Please sign in to comment.