diff --git a/Gruntfile.js b/Gruntfile.js index 3ecf8884..40f95634 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -76,8 +76,9 @@ module.exports = function(grunt) { rhino: { src: [ "<%= concat.core.dest %>", - "src/cli/common.js", - "src/cli/rhino.js" + "src/cli/cli.js", + "src/cli/api-rhino.js", + "src/cli/api-common.js" ], dest: "<%= build_dir %>/<%= pkg.name %>-rhino.js" }, @@ -97,8 +98,9 @@ module.exports = function(grunt) { banner: "#!/usr/bin/env node\n<%= banner.full %>" }, src: [ - "src/cli/common.js", - "src/cli/node.js" + "src/cli/cli.js", + "src/cli/api-node.js", + "src/cli/api-common.js" ], dest: "<%= build_dir %>/npm/cli.js" }, @@ -124,8 +126,9 @@ module.exports = function(grunt) { wsh: { src: [ "<%= concat.core.dest %>", - "src/cli/common.js", - "src/cli/wsh.js" + "src/cli/cli.js", + "src/cli/api-wsh.js", + "src/cli/api-common.js" ], dest: "<%= build_dir %>/<%= pkg.name %>-wsh.js" } diff --git a/src/cli/api-common.js b/src/cli/api-common.js new file mode 100644 index 00000000..6aeb3df4 --- /dev/null +++ b/src/cli/api-common.js @@ -0,0 +1,42 @@ +/*globals api, cli*/ +/*exported commonApi*/ + +var commonApi = { + lookUpFile: function (filename, base) { + var lookupd = base? this.getFullPath(base): this.getWorkingDirectory(), + data, + self = this; + + function isGoodToGoUp() { + var /*keep '==' because of rhino*/ + isUserhome = (lookupd == self.userhome), + _lookupd = self.getFullPath(lookupd + "/../"), + isTop = (lookupd == _lookupd), + gtg; + + gtg = (!data && !isUserhome && !isTop); + lookupd = _lookupd; + return gtg; + } + + (function traverseUp() { + var + fullpath = self.getFullPath(lookupd + "/" + filename); + + data = self.readFile(fullpath); + + if ( isGoodToGoUp() ) { + traverseUp(); + } + }()); + + return data; + } +}; + +/*api = */ +CSSLint.Util.mix(api, commonApi); + +if (typeof cli === "function") { + cli(api); +} diff --git a/src/cli/node.js b/src/cli/api-node.js similarity index 68% rename from src/cli/node.js rename to src/cli/api-node.js index e9499500..f068c3fe 100644 --- a/src/cli/node.js +++ b/src/cli/api-node.js @@ -3,7 +3,6 @@ */ /* jshint node:true */ -/* global cli */ /* exported CSSLint */ var fs = require("fs"), @@ -33,37 +32,6 @@ api = { } }, - lookUpFile: function (filename, base) { - var lookupd = base? this.getFullPath(base): this.getWorkingDirectory(), - data, - self = this; - - function isGoodToGoUp() { - var - isUserhome = (lookupd == self.userhome), - _lookupd = self.getFullPath(lookupd + "/../"), - isTop = (lookupd == _lookupd), - gtg; - - gtg = (!data && !isUserhome && !isTop); - lookupd = _lookupd; - return gtg; - } - - (function traverseUp() { - var - fullpath = self.getFullPath(lookupd + "/" + filename); - - data = self.readFile(fullpath); - - if ( isGoodToGoUp() ) { - traverseUp(); - } - }()); - - return data; - }, - getFiles: function(dir) { var files = []; @@ -111,5 +79,3 @@ api = { } } }; - -cli(api); diff --git a/src/cli/rhino.js b/src/cli/api-rhino.js similarity index 58% rename from src/cli/rhino.js rename to src/cli/api-rhino.js index cbc8eee5..5715f347 100644 --- a/src/cli/rhino.js +++ b/src/cli/api-rhino.js @@ -3,7 +3,8 @@ */ /* jshint rhino:true */ -/* global cli, File */ +/* global File*/ +/* exported api*/ importPackage(java.io); @@ -18,37 +19,6 @@ var api = { return dir.isDirectory(); }, - lookUpFile: function (filename, base) { - var lookupd = base? this.getFullPath(base): this.getWorkingDirectory(), - data, - self = this; - - function isGoodToGoUp() { - var - isUserhome = (lookupd == self.userhome), - _lookupd = self.getFullPath(lookupd + "/../"), - isTop = (lookupd == _lookupd), - gtg; - - gtg = (!data && !isUserhome && !isTop); - lookupd = _lookupd; - return gtg; - } - - (function traverseUp() { - var - fullpath = self.getFullPath(lookupd + "/" + filename); - - data = self.readFile(fullpath); - - if ( isGoodToGoUp() ) { - traverseUp(); - } - }()); - - return data; - }, - getFiles: function(dir){ var files = []; @@ -84,5 +54,3 @@ var api = { } } }; - -cli(api); diff --git a/src/cli/wsh.js b/src/cli/api-wsh.js similarity index 84% rename from src/cli/wsh.js rename to src/cli/api-wsh.js index 882d488b..ca058d17 100644 --- a/src/cli/wsh.js +++ b/src/cli/api-wsh.js @@ -5,9 +5,9 @@ //TODO: This file needs major cleanup!!! /* jshint wsh:true */ -/* global cli */ +/* exported api */ -var wshapi = (function(){ +var api = (function(){ var fso = new ActiveXObject("Scripting.FileSystemObject"); var shell = WScript.CreateObject("WScript.Shell"); var finalArgs = [], i, args = WScript.Arguments; @@ -142,37 +142,6 @@ var wshapi = (function(){ return fso.FolderExists(name); }, - lookUpFile: function (filename, base) { - var lookupd = base? this.getFullPath(base): this.getWorkingDirectory(), - data, - self = this; - - function isGoodToGoUp() { - var - isUserhome = (lookupd == self.userhome), - _lookupd = self.getFullPath(lookupd + "/../"), - isTop = (lookupd == _lookupd), - gtg; - - gtg = (!data && !isUserhome && !isTop); - lookupd = _lookupd; - return gtg; - } - - (function traverseUp() { - var - fullpath = self.getFullPath(lookupd + "/" + filename); - - data = self.readFile(fullpath); - - if ( isGoodToGoUp() ) { - traverseUp(); - } - }()); - - return data; - }, - getFiles: function(dir){ var files = []; traverseDir(files, dir); @@ -206,5 +175,3 @@ var wshapi = (function(){ }; }()); - -cli(wshapi); diff --git a/src/cli/common.js b/src/cli/cli.js similarity index 100% rename from src/cli/common.js rename to src/cli/cli.js diff --git a/tests/api/api-node.js b/tests/api/api-node.js index 5bb1efe1..6661bf67 100644 --- a/tests/api/api-node.js +++ b/tests/api/api-node.js @@ -14,11 +14,11 @@ function include(path, sandbox) { (function(){ var Assert = YUITest.Assert, sandbox = { - cli: function() {/*dummy cli to run from node.js api*/}, + CSSLint: CSSLint, require: function(req) { // monkey patched require if (req === "./lib/csslint-node") { - return {CSSLint: null}; + return {CSSLint: sandbox.CSSLint}; } return require(req); }, @@ -29,9 +29,10 @@ function include(path, sandbox) { ncwd, userhomeBackup; - include(__dirname + "/src/cli/node.js", sandbox); /* expose sandbox.api */ + include(__dirname + "/src/cli/api-node.js", sandbox); /* expose sandbox.api */ + include(__dirname + "/src/cli/api-common.js", sandbox); /* expose sandbox.commonApi */ - userhomeBackup = sandbox.api.userhome; // have no good idea hot to test if it was resolved or not + userhomeBackup = sandbox.api.userhome; // have no good idea how to test if it was resolved or not YUITest.TestRunner.add(new YUITest.TestCase({ name: "Node api helper",