Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add support for d8, frontent to auto-detect environment

  • Loading branch information...
commit 9fd166c7b5902096ebd62504e2b1c40b1a5f807b 1 parent 0ea9e5e
@bukzor bukzor authored
Showing with 81 additions and 99 deletions.
  1. +0 −58 env/d8.js
  2. +28 −41 env/jsc.js
  3. +53 −0 jshint
View
58 env/d8.js
@@ -1,58 +0,0 @@
-/*jshint boss:true, evil:true */
-
-// usage:
-// jsc ${env_home}/jsc.js -- ${file} "{option1:true,option2:false}" ${env_home}
-
-
-load("../jshint.js");
-
-if (typeof(JSHINT) === 'undefined') {
- print('jshint: Could not load jshint.js, tried "' + env_home + 'jshint.js".');
- quit();
-}
-
-(function(args){
-
- trim = function(str) {
- return str.replace(/^\s+/, '').replace(/\s+$/, '')
- }
-
- var name = args[0],
- opts = (function(arg){
- var opts = {};
- var item;
-
- switch (arg) {
- case undefined:
- case '':
- return opts;
- default:
- arg = arg.split(',');
- for (var i = 0, ii = arg.length; i < ii; i++) {
- item = arg[i].split(':');
- opts[item[0]] = eval(item[1]);
- }
- return opts;
- }
- })(args[1]);
-
- if (!name) {
- print('jshint: No file name was provided.');
- quit();
- } else if ( ! name.match(/^\//) ) {
- name = trim(os.system("readlink", ["-f", name]));
- }
-
- print("Reading file... >", name, "<");
- input = read(name);
-
- if (!JSHINT(input, opts)) {
- for (var i = 0, err; err = JSHINT.errors[i]; i++) {
- print(err.reason + ' (line: ' + err.line + ', character: ' + err.character + ')');
- print('> ' + (err.evidence || '').replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
- print('');
- }
- }
-
- quit();
-})(arguments);
View
69 env/jsc.js 100644 → 100755
@@ -1,12 +1,9 @@
/*jshint boss:true, evil:true */
// usage:
-// jsc ${env_home}/jsc.js -- ${file} "$(cat ${file})" "{option1:true,option2:false} ${env_home}"
-var env_home = '';
-if (arguments.length > 3) {
- env_home = arguments[3].toString().replace(/\/env$/, '/');
-}
-load(env_home + "jshint.js");
+// jsc ${env_home}/jsc.js -- "{option1:true,option2:false}" ${file} ...
+
+load("jshint.js");
if (typeof(JSHINT) === 'undefined') {
print('jshint: Could not load jshint.js, tried "' + env_home + 'jshint.js".');
@@ -14,44 +11,34 @@ if (typeof(JSHINT) === 'undefined') {
}
(function(args){
- var home = args[3],
- name = args[0],
- input = args[1],
- opts = (function(arg){
- var opts = {};
- var item;
-
- switch (arg) {
- case undefined:
- case '':
- return opts;
- default:
- arg = arg.split(',');
- for (var i = 0, ii = arg.length; i < ii; i++) {
- item = arg[i].split(':');
- opts[item[0]] = eval(item[1]);
- }
- return opts;
- }
- })(args[2]);
-
- if (!name) {
+ if (args.length < 1) {
print('jshint: No file name was provided.');
quit();
}
- if (!input) {
- print('jshint: ' + name + ' contents were not provided to jshint.');
- quit();
- }
-
- if (!JSHINT(input, opts)) {
- for (var i = 0, err; err = JSHINT.errors[i]; i++) {
- print(err.reason + ' (line: ' + err.line + ', character: ' + err.character + ')');
- print('> ' + (err.evidence || '').replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
- print('');
- }
- }
+ var err, opts;
+ try {
+ opts = JSON.parse(args[0]);
+ } catch(err) {
+ print( "Error while parsing arguments: '" + args[0] + "'" );
+ quit();
+ }
+
+ for (var i = 1; i < args.length; i++) {
+ try {
+ input = read(args[i]);
+ } catch(err) {
+ print("Error while reading:", args[i]);
+ quit();
+ }
+ if (!JSHINT(input, opts)) {
+ for (var j = 0; err = JSHINT.errors[j]; j++) {
+ print(err.reason + ' (line: ' + err.line + ', character: ' + err.character + ')');
+ print('> ' + (err.evidence || '').replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
+ print('');
+ }
+ }
+ }
quit();
-})(arguments);
+})(arguments);
View
53 jshint
@@ -0,0 +1,53 @@
+#!/bin/bash
+#
+# A command-line frontent to jshint.
+# We try to auto-detect a supported environment and use it.
+#
+# TODO: auto-detect the node jshint frontend
+#
+HERE=`readlink -f $0`
+HERE=`dirname $0`
+
+function quiet {
+ "$@" >/dev/null 2>&1
+}
+
+function getOpts() {
+ if [[ "$@" =~ (^| )--?h ]]; then
+ echo "usage: jshint ['{\"opt1\":true,\"opt2\":false}'] file1.js ..."
+ exit 0
+ fi
+ if [[ ! -f "$1" && "$1" =~ [a-z,:{}] ]]; then
+ jsopts="$1"
+ shift
+ else
+ jsopts="{}"
+ fi
+
+ # Make sure all input files are absolute paths.
+ index=0
+ for file in "$@"; do
+ files[$index]=`readlink -f "$file"`
+ let index+=1
+ done
+}
+
+function main {
+ getOpts "$@"
+
+ cd $HERE
+ if quiet which d8; then
+ d8 env/d8.js -- "$jsopts" "${files[@]}"
+ elif quiet which jsc; then
+ jsc env/jsc.js -- "$jsopts" "${files[@]}"
+ elif quiet which smjs; then
+ smjs env/jsc.js "$jsopts" "${files[@]}"
+ elif [ -e "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc" ]; then
+ /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc env/jsc.js -- "$jsopts" "${files[@]}"
+ else
+ echo "Could not find any supporte JavaScript environment. Looked for: d8 jsc smjs"
+ exit 1
+ fi
+}
+
+main "$@"
Please sign in to comment.
Something went wrong with that request. Please try again.