Skip to content

Commit 334d0ef

Browse files
author
Nicholas C. Zakas
committed
Added Windows Script Host CLI (fixes #198)
1 parent 4a1dc08 commit 334d0ef

6 files changed

Lines changed: 147 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ CSSLint is a tool to help point out problems with your CSS code. It does basic s
1717
1. Eric Wendelin, http://eriwen.com (Output formatters)
1818
1. Kasper Garnaes, http://reload.dk (Checkstyle XML format)
1919
1. Gord Tanner, http://www.tinyhippos.com (CLI quiet option)
20-
1. Hans-Peter Buniat, https://github.com/hpbuniat (Duplicate background image rule)
20+
1. Hans-Peter Buniat, https://github.com/hpbuniat (Duplicate background image rule)
21+
1. Dino Chiesa, https://github.com/DinoChiesa (Windows Script Host CLI)

build.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<property name="worker.build.file" value="csslint-worker.js"/>
3131
<property name="tests.build.file" value="csslint-tests.js"/>
3232
<property name="rhino.build.file" value="csslint-rhino.js"/>
33+
<property name="wsh.build.file" value="csslint-wsh.js"/>
3334

3435
<!-- embeddable license -->
3536
<loadfile property="license.text" srcfile="LICENSE" />
@@ -204,6 +205,14 @@
204205
</concat>
205206
</target>
206207

208+
<!-- build for WSH CLI integration -->
209+
<target name="build.wsh" depends="build.core">
210+
<concat destfile="${build.dir}/${wsh.build.file}" fixlastline="true">
211+
<filelist dir="${build.dir}" files="${core.build.file}" />
212+
<filelist dir="${src.dir}/cli" files="common.js,wsh.js" />
213+
</concat>
214+
</target>
215+
207216
<!-- Create a release with version number embedded -->
208217
<target name="release" depends="test,build.all,changelog.update">
209218
<delete dir="${release.dir}" />
@@ -228,6 +237,6 @@
228237
</target>
229238

230239
<!-- Build all files -->
231-
<target name="build.all" depends="clean,build.core,build.worker,build.node,build.tests,build.rhino"/>
240+
<target name="build.all" depends="clean,build.core,build.worker,build.node,build.tests,build.rhino,build.wsh"/>
232241

233242
</project>

src/cli/common.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,5 @@ function cli(api){
203203
api.quit(0);
204204
}
205205

206-
//files = api.fixFilenames(files);
207-
208206
api.quit(processFiles(files,options));
209207
}

src/cli/node.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@ cli({
6161
traverse(dir, []);
6262

6363
return files;
64-
},
65-
66-
fixFilenames: function(files){
67-
return files.map(function(filename){
68-
return path.resolve(process.cwd(), filename);
69-
});
70-
},
64+
},
7165

7266
getWorkingDirectory: function() {
7367
return process.cwd();

src/cli/rhino.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ cli({
3434

3535
return files;
3636
},
37-
38-
fixFilenames: function(files){
39-
return files;
40-
},
4137

4238
getWorkingDirectory: function() {
4339
return (new File(".")).getCanonicalPath();

src/cli/wsh.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Windows Script Host Command Line Interface
3+
*/
4+
/*global ActiveXObject, WScript, Enumerator, cli*/
5+
//TODO: This file needs major cleanup!!!
6+
7+
var wshapi = (function(){
8+
var fso = new ActiveXObject("Scripting.FileSystemObject");
9+
var shell = WScript.CreateObject("WScript.Shell");
10+
var finalArgs = [], i, args = WScript.Arguments;
11+
12+
if (typeof Array.prototype.forEach !== "function") {
13+
Array.prototype.forEach = function(f,m) {
14+
for (var i=0, L=this.length; i<L; ++i) {
15+
f(this[i], i, m);
16+
}
17+
};
18+
}
19+
20+
if (typeof Array.prototype.filter !== "function") {
21+
Array.prototype.filter = function(fn /*, thisp*/) {
22+
if (typeof fn != "function") {
23+
throw new Error("not a function");
24+
}
25+
var res = [], val, thisp = finalArgs[1];
26+
for (var i = 0, L = this.length; i < L; i++) {
27+
if (i in this) {
28+
val = this[i]; // in case fun mutates this
29+
if (fn.call(thisp, val, i, this)){
30+
res.push(val);
31+
}
32+
}
33+
}
34+
35+
return res;
36+
};
37+
}
38+
39+
if (!Array.prototype.indexOf) {
40+
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
41+
"use strict";
42+
if (this === void 0 || this === null) {
43+
throw new Error("unknown instance");
44+
}
45+
var t = this;
46+
var len = t.length >>> 0;
47+
if (len === 0) {
48+
return -1;
49+
}
50+
var n = 0;
51+
if (finalArgs.length > 0) {
52+
n = Number(finalArgs[1]);
53+
if (n !== n) { // shortcut for verifying if it's NaN
54+
n = 0;
55+
} else if (n !== 0 && n !== Infinity && n !== -Infinity) {
56+
n = (n > 0 || -1) * Math.floor(Math.abs(n));
57+
}
58+
}
59+
if (n >= len) {
60+
return -1;
61+
}
62+
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
63+
for (; k < len; k++) {
64+
if (k in t && t[k] === searchElement) {
65+
return k;
66+
}
67+
}
68+
return -1;
69+
};
70+
}
71+
72+
function traverseDir(files, path) {
73+
var filename, folder = fso.GetFolder(path),
74+
subFlds, fc = new Enumerator(folder.files);
75+
76+
for (; !fc.atEnd(); fc.moveNext()) {
77+
filename = fc.item();
78+
if (/\.css$/.test(filename)) {
79+
files.push(filename);
80+
}
81+
}
82+
83+
subFlds = new Enumerator(folder.SubFolders);
84+
for (; !subFlds.atEnd(); subFlds.moveNext()) {
85+
traverseDir(files, subFlds.item());
86+
}
87+
}
88+
89+
// turn the WScript.Arguments thing into a regular array
90+
if (args.Length > 0) {
91+
for (i = 0; i < args.Length; i++) {
92+
finalArgs.push(args(i));
93+
}
94+
}
95+
96+
return {
97+
args: finalArgs,
98+
print: function(s) { WScript.Echo(s);},
99+
quit: function (v) { WScript.Quit(v);},
100+
101+
isDirectory: function(name){
102+
return fso.FolderExists(name);
103+
},
104+
105+
getFiles: function(dir){
106+
var files = [];
107+
traverseDir(files, dir);
108+
return files;
109+
},
110+
111+
fixFilenames: function(files){
112+
return files;
113+
},
114+
115+
getWorkingDirectory: function() {
116+
return shell.CurrentDirectory;
117+
},
118+
119+
getFullPath: function(filename){
120+
return fso.GetAbsolutePathName(filename);
121+
},
122+
123+
readFile: function(path){
124+
var forReading = 1;
125+
var tf = fso.OpenTextFile(path, forReading);
126+
var allText = tf.ReadAll();
127+
tf.Close();
128+
return allText;
129+
}
130+
};
131+
132+
}());
133+
134+
cli(wshapi);

0 commit comments

Comments
 (0)