Created
April 26, 2011 07:20
-
-
Save bennage/941936 to your computer and use it in GitHub Desktop.
a script for executing jslint on a command line in Windows. Execute with: cscript.exe jslint-helper-for-wsh.js file-to-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint evil: true, white: true, onevar: true, undef: true, nomen: true, regexp: true, plusplus: true, bitwise: true, newcap: true, maxerr: 10, indent: 4 */ | |
/*global JSLINT, WScript, ActiveXObject, Enumerator*/ | |
(function () { | |
var jslint_path = 'jslint.js', | |
jslint_source = '', | |
utf8 = '', | |
fso = new ActiveXObject('Scripting.FileSystemObject'), | |
files = [], | |
args = WScript.Arguments; | |
function echo(msg) { | |
WScript.Echo(msg); | |
} | |
function ltrim(s) { | |
return (s === undefined) ? '' : s.replace(/^\s+/, ''); | |
} | |
function readFile(fileName) { | |
var for_reading = 1, | |
default_encoding = -2, | |
lines = [], | |
content = '', | |
i = 0, | |
count = 0, | |
file = fso.GetFile(fileName), | |
stream = file.OpenAsTextStream(for_reading, default_encoding); | |
while (!stream.AtEndOfStream) { | |
lines[count] = stream.ReadLine(); | |
count += 1; | |
} | |
stream.Close(); | |
if (lines[0]) { | |
if(lines[0].substr(0,3) === utf8) { | |
lines[0] = lines[0].replace(utf8,''); | |
} | |
} | |
for (i = 0; i < lines.length; i += 1) { | |
content += lines[i] + '\n'; | |
} | |
return content; | |
} | |
function reportError(error, index) { | |
echo('\n error [' + index + ']'); | |
echo(' line ' + error.line + ' character ' + error.character); | |
echo(' ' + error.reason); | |
echo(' ' + ltrim(error.evidence)); | |
} | |
function processErrors(errors) { | |
var i, | |
error; | |
for (i = 0; i < errors.length; i += 1) { | |
error = errors[i]; | |
if (error) { | |
reportError(error, i); | |
} | |
} | |
} | |
function process(files) { | |
var i, | |
fileName, | |
script_source, | |
result, | |
error; | |
for (i = 0; i < files.length; i += 1) { | |
fileName = files[i]; | |
script_source = readFile(fileName); | |
result = JSLINT(script_source); | |
if (result) { | |
echo('OK: ' + fileName.Name); | |
} else { | |
echo('\nERRORS ' + fileName.Name); | |
processErrors(JSLINT.errors); | |
echo('\n'); | |
} | |
} | |
echo('\n' + i + ' files checked'); | |
} | |
function getFilesToProcess(args) { | |
var folder, | |
i, | |
file, | |
ext, | |
files = [], | |
enumerator, | |
directory = args.Named.Item('d') || args.Named.Item('directory'); | |
if (directory) { | |
echo('scanning files in'); | |
echo(directory); | |
folder = fso.GetFolder(directory); | |
enumerator = new Enumerator(folder.Files); | |
for (; !enumerator.atEnd(); enumerator.moveNext()) { | |
file = enumerator.item(); | |
ext = file.Name.substring(file.Name.length - 3, file.Name.length); | |
if(ext === '.js') { | |
files.push(file); | |
} else { | |
echo( 'skipping ' + file.Name + '; extension not .js'); | |
} | |
} | |
} | |
for (i = 0; i < args.Unnamed.length; i += 1) { | |
file = args.Unnamed.Item(i); | |
files.push(file); | |
} | |
return files; | |
} | |
jslint_source = readFile(jslint_path); | |
// Yes, this is evil. Is there a better way to load jslint with WSH? | |
eval(jslint_source); | |
if (args.Count() === 0) { | |
echo('You must supply at least one file to test.'); | |
} else { | |
files = getFilesToProcess(args); | |
process(files); | |
} | |
}());/*jslint evil: true, white: true, onevar: true, undef: true, nomen: true, regexp: true, plusplus: true, bitwise: true, newcap: true, maxerr: 10, indent: 4 */ | |
/*global JSLINT, WScript, ActiveXObject, Enumerator*/ | |
(function () { | |
var jslint_path = 'jslint.js', | |
jslint_source = '', | |
utf8 = '', | |
fso = new ActiveXObject('Scripting.FileSystemObject'), | |
files = [], | |
args = WScript.Arguments; | |
function echo(msg) { | |
WScript.Echo(msg); | |
} | |
function ltrim(s) { | |
return (s === undefined) ? '' : s.replace(/^\s+/, ''); | |
} | |
function readFile(fileName) { | |
var for_reading = 1, | |
default_encoding = -2, | |
lines = [], | |
content = '', | |
i = 0, | |
count = 0, | |
file = fso.GetFile(fileName), | |
stream = file.OpenAsTextStream(for_reading, default_encoding); | |
while (!stream.AtEndOfStream) { | |
lines[count] = stream.ReadLine(); | |
count += 1; | |
} | |
stream.Close(); | |
if (lines[0]) { | |
if(lines[0].substr(0,3) === utf8) { | |
lines[0] = lines[0].replace(utf8,''); | |
} | |
} | |
for (i = 0; i < lines.length; i += 1) { | |
content += lines[i] + '\n'; | |
} | |
return content; | |
} | |
function reportError(error, index) { | |
echo('\n error [' + index + ']'); | |
echo(' line ' + error.line + ' character ' + error.character); | |
echo(' ' + error.reason); | |
echo(' ' + ltrim(error.evidence)); | |
} | |
function processErrors(errors) { | |
var i, | |
error; | |
for (i = 0; i < errors.length; i += 1) { | |
error = errors[i]; | |
if (error) { | |
reportError(error, i); | |
} | |
} | |
} | |
function process(files) { | |
var i, | |
fileName, | |
script_source, | |
result, | |
error; | |
for (i = 0; i < files.length; i += 1) { | |
fileName = files[i]; | |
script_source = readFile(fileName); | |
result = JSLINT(script_source); | |
if (result) { | |
echo('OK: ' + fileName.Name); | |
} else { | |
echo('\nERRORS ' + fileName.Name); | |
processErrors(JSLINT.errors); | |
echo('\n'); | |
} | |
} | |
echo('\n' + i + ' files checked'); | |
} | |
function getFilesToProcess(args) { | |
var folder, | |
i, | |
file, | |
ext, | |
files = [], | |
enumerator, | |
directory = args.Named.Item('d') || args.Named.Item('directory'); | |
if (directory) { | |
echo('scanning files in'); | |
echo(directory); | |
folder = fso.GetFolder(directory); | |
enumerator = new Enumerator(folder.Files); | |
for (; !enumerator.atEnd(); enumerator.moveNext()) { | |
file = enumerator.item(); | |
ext = file.Name.substring(file.Name.length - 3, file.Name.length); | |
if(ext === '.js') { | |
files.push(file); | |
} else { | |
echo( 'skipping ' + file.Name + '; extension not .js'); | |
} | |
} | |
} | |
for (i = 0; i < args.Unnamed.length; i += 1) { | |
file = args.Unnamed.Item(i); | |
files.push(file); | |
} | |
return files; | |
} | |
jslint_source = readFile(jslint_path); | |
// Yes, this is evil. Is there a better way to load jslint with WSH? | |
eval(jslint_source); | |
if (args.Count() === 0) { | |
echo('You must supply at least one file to test.'); | |
} else { | |
files = getFilesToProcess(args); | |
process(files); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment