Permalink
Browse files

PhantomLint is now working within Node.js!

  • Loading branch information...
arthurakay committed Dec 11, 2012
1 parent f50fa07 commit e31347d9ad8dde4d434b61a4ef31a24e714e008f
Showing with 75 additions and 30 deletions.
  1. +2 −1 .gitignore
  2. +37 −27 PhantomLint.js
  3. +31 −0 package.json
  4. +5 −2 test.js
View
@@ -1,3 +1,4 @@
/.idea
*.log
-.DS_Store
+.DS_Store
+node_modules/
View
@@ -18,10 +18,11 @@
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-*/
+ */
-var filesystem = require('fs'),
- JSLINT, JSHINT;
+var fs = require('fs'),
+ JSLINT = require('jslint'),
+ JSHINT = require('jshint');
/**
* @class PhantomLint
@@ -62,15 +63,18 @@ PhantomLint = {
* - "options" is an object containing the optional lint flags.
*/
jsLint : {
- file : 'assets/jslint.js',
+ file : './assets/jslint.js',
options : {
nomen : true, //if names may have dangling _
plusplus : true, //if increment/decrement should be allowed
sloppy : true, //if the 'use strict'; pragma is optional
vars : true, //if multiple var statements per function should be allowed
white : true, //if sloppy whitespace is tolerated
- undef : true //if variables can be declared out of order
+ undef : true, //if variables can be declared out of order,
+ node : true, //if Node.js globals should be predefined
+ browser : true, //if the standard browser globals should be predefined
+ stupid : true //if really stupid practices are tolerated... namely blocking synchronous operations
}
},
@@ -83,7 +87,7 @@ PhantomLint = {
*
*/
jsHint : {
- file : 'assets/jshint-master/src/stable/jshint.js',
+ file : './assets/jshint-master/src/stable/jshint.js',
options : {
@@ -96,7 +100,6 @@ PhantomLint = {
*/
logFile : 'error_log.txt',
-
/**
* Call this method to de-lint your JavaScript codebase.
*/
@@ -105,17 +108,17 @@ PhantomLint = {
this.initConfigs(config);
if (this.jsLint) {
- this.log('Loading JSLint... ' + phantom.injectJs(this.jsLint.file), true);
+ this.log('Loading JSLint... ', true);
this.linters.push(JSLINT);
}
if (this.jsHint) {
- this.log('Loading JSHint... ' + phantom.injectJs(this.jsHint.file), true);
+ this.log('Loading JSHint... ', true);
this.linters.push(JSHINT);
}
if (!JSLINT && !JSHINT) {
- phantom.exit(1);
+ process.exit(1);
}
this.parseTree(config.filepaths);
@@ -126,7 +129,6 @@ PhantomLint = {
this.announceSuccess();
},
-
/**
* @private
*/
@@ -137,7 +139,6 @@ PhantomLint = {
*/
linters : [],
-
/**
* @private
*/
@@ -196,22 +197,22 @@ PhantomLint = {
}
this.log('\nFix Your Errors! Check the log file for more information.\n\n', true);
- phantom.exit(1);
+ process.exit(1);
},
/**
* @private
*/
announceSuccess : function () {
this.log('\nSuccessfully linted yo shit.\n\n', true);
- phantom.exit(0);
+ process.exit(0);
},
/**
* @private
*/
getFiles : function (path) {
- var tree = filesystem.list(path);
+ var tree = fs.readdirSync(path);
this.log('\nFILES FOUND AT PATH: ' + path);
this.log(tree);
@@ -262,7 +263,9 @@ PhantomLint = {
var spacer = ' ',
childPath, childTree;
- if (filesystem.isFile(currPath + list[x])) {
+ var stats = fs.statSync(currPath + list[x]);
+
+ if (stats.isFile()) {
this.log(spacer + list[x] + ' IS A FILE');
/*
* We only want JS files
@@ -338,7 +341,7 @@ PhantomLint = {
for (j; j < this.files.length; j++) {
file = this.files[j];
- js = filesystem.read(file);
+ js = fs.readFileSync(file, 'utf8');
var i = 0,
result = JSLINT(js, this.jsLint.options),
@@ -384,7 +387,7 @@ PhantomLint = {
for (j; j < this.files.length; j++) {
file = this.files[j];
- js = filesystem.read(file);
+ js = fs.readFileSync(file, 'utf8');
var i = 0,
result = JSHINT(js, this.jsHint.options),
@@ -424,17 +427,22 @@ PhantomLint = {
* @private
*/
logToFile : function (errorList) {
- this.log('\nWriting ' + (errorList.length / 6) + ' errors to log file.', true);
- filesystem.touch(this.logFile);
+ try {
+ this.log('\nDeleting old log file.', true);
+ fs.unlinkSync(this.logFile);
+ }
+ catch (err) {
+ this.log('\nNo log file currently exists.', true);
+ }
- var stream = filesystem.open(this.logFile, 'w');
+ this.log('\nWriting ' + (errorList.length / 6) + ' errors to new log file.', true);
- var i = 0;
- for (i; i < errorList.length; i++) {
- stream.writeLine(errorList[i]);
- }
+ var header = 'PhantomLint : Output for ' + new Date() + '\n\n';
+ errorList.splice(0, 0, header);
- stream.close();
+ var output = errorList.join().replace(/,/g, '\n');
+
+ fs.writeFileSync(this.logFile, output);
},
/**
@@ -445,4 +453,6 @@ PhantomLint = {
console.log(msg);
}
}
-};
+};
+
+module.exports = PhantomLint;
View
@@ -0,0 +1,31 @@
+{
+ "name" : "lint-roller",
+ "preferGlobal" : "true",
+ "version" : "2.0.0",
+
+ "author" : "Arthur Kay <art@akawebdesign.com>",
+ "description" : "Lint your JavaScript code and output errors to a log file. Convenient for pre-commit hooks and build systems to maintain code quality.",
+
+ "contributors" : [
+ {
+ "name" : "Arthur Kay",
+ "email" : "art@akawebdesign.com"
+ }
+ ],
+
+ "scripts" : {
+
+ },
+
+ "main" : "./PhantomLint.js",
+
+ "repository" : {
+ "type" : "git",
+ "url" : "https://github.com/arthurakay/PhantomLint"
+ },
+
+ "dependencies" : {
+ "jshint" : ">=0.9.1",
+ "jslint" : ">=0.1.9"
+ }
+}
View
@@ -1,3 +1,5 @@
+var PhantomLint = require('./PhantomLint.js');
+
var config = {
verbose : false,
stopOnFirstError : false,
@@ -10,7 +12,9 @@ var config = {
//but ignore anything in these folders
exclusions : [
- './assets/jshint-master/'
+ './node_modules/',
+ './assets/',
+ './docs/'
],
jsLint : {
@@ -22,5 +26,4 @@ var config = {
jsHint : false
};
-phantom.injectJs('PhantomLint.js');
PhantomLint.init(config);

0 comments on commit e31347d

Please sign in to comment.