Permalink
Browse files

Fix issue with trailing comma; many style fixes, jshintrc and other j…

…shint conf
  • Loading branch information...
1 parent 9dd8441 commit 75f54c79c4fdf17eb9741757deaedc4efbb930d1 @mixu committed Jan 9, 2014
View
@@ -0,0 +1,5 @@
+node_modules/
+test/node_modules
+test/tmp
+lib/runner/package-commonjs/resources
+test/command-integration
View
@@ -0,0 +1,7 @@
+{
+ // double !! is allowed
+ "-W018": true,
+ // and I don't care what jshint thinks is better written in dot notation, because using string notation
+ // is nice for things that are configuration options.
+ "-W069": true
+}
View
@@ -13,14 +13,23 @@ test:
.PHONY: test lint
+# Note: use latest gjslint e.g 2.3.13
lint:
- jshint . \
- --exclude="**/node_modules" \
- --exclude="lib/runner/package-commonjs/resources" \
- --exclude="test/command-integration"
+ jshint .
gjslint \
--nojsdoc \
--jslint_error=all \
--disable=6 \
--max_line_length=120 \
- --exclude_directories=node_modules,lib/runner/package-commonjs/resources,test/command-integration -r .
+ --custom_jsdoc_tags=api \
+ --exclude_directories=node_modules,lib/runner/package-commonjs/resources,test \
+ --max_line_length=120 --disable=0131,300,2,1,6 \
+ -r .
+
+test-lint:
+ ./bin/gluejs \
+ --no-cache \
+ --include ./lib \
+ --basepath ./ \
+ --out ./test/tmp/lint.js
+ gjslint --nojsdoc --custom_jsdoc_tags=api --max_line_length=120 --disable=0131,300,2,1,6 ./test/tmp/lint.js
View
@@ -130,7 +130,7 @@ API.prototype.exclude = function(path) {
// Express Middleware
API.middleware = function (opts) {
-
+
// -- Set some sane defaults
opts = opts || {};
opts.include = opts.include || './lib';
@@ -139,14 +139,14 @@ API.middleware = function (opts) {
// -- Create an instance of the API to use
var glue = new API()
- .include(opts.include) // Use API function to define
- .basepath(opts.basepath) // Use API function to define
+ .include(opts.include) // Use API function to define
+ .basepath(opts.basepath); // Use API function to define
// -- All other options are set by clobbering the glue.options hash
Object.keys(opts).forEach(function (key) {
glue.set(key, opts[key]);
});
-
+
// -- Middleware to return
return function (req, res, next) {
@@ -158,7 +158,7 @@ API.middleware = function (opts) {
// -- Render file and pipe to response
glue.render(res);
- }
+ };
};
API.prototype.handler = function(regex, fn) {};
@@ -9,12 +9,17 @@ module.exports = function(options) {
if(typeof options.task === 'string') {
// parse quoted string variants:
// 1) not whitespace, not quote followed by not whitespace OR
- // 2) double quote followed by not double quote or escaped double quote followed by double quote OR
- // 3) single quote followed by not single quote or escaped single quote followed by single quote
- options.task = options.task.match(/([^ \t\r\n"'][^ \t\r\n]*)|"(?:[\]["]|[^"])+"|'(?:[\][']|[^'])+'/g);
+ // 2) double quote followed by not double quote or escaped double quote followed by double OR
+ // 3) single quote followed by not single quote or escaped single quote followed by single
+ options.task = options.task.match(
+ /([^ \t\r\n"'][^ \t\r\n]*)|"(?:[\]["]|[^"])+"|'(?:[\][']|[^'])+'/g
+ );
// since we're using uv_spawn which calls out to exec(3), we need to exclude the beginning quotes
options.task = options.task.map(function(i, index) {
- if(index > 0 && i.charAt(0) == i.charAt(i.length-1) && (i.charAt(0) == '"' || i.charAt(0) == "'")) {
+ if (index > 0 &&
+ i.charAt(0) == i.charAt(i.length-1) &&
+ (i.charAt(0) == '"' || i.charAt(0) == "'")
+ ) {
return i.substring(1, i.length-1);
}
return i;
@@ -28,7 +33,8 @@ module.exports = function(options) {
task.on('exit', function(code) {
if(code !== 0) {
console.log('');
- console.log('spawn-task: "' +options.task.join(' ')+ '" on "'+ options.name+ '" exited with nonzero exit code: '+ code);
+ console.log('spawn-task: "' + options.task.join(' ') + '" on "' +
+ options.name+ '" exited with nonzero exit code: '+ code);
task.emit('error', new Error('Child process exited with nonzero exit code: '+ code));
}
});
@@ -29,7 +29,7 @@ WrapCJS.prototype._flush = function(done) {
this.writeFirst();
this.push('{};}');
} else {
- this.push(this.buffer.trim() + ';}');
+ this.push(this.buffer.trim() + ';\n}');
}
done();
};
@@ -1,13 +1,16 @@
// This task calculates the longest common substring among the file paths
+
+function check(obj) {
+ return (obj.name.charAt(index) == first.name.charAt(index));
+}
+
module.exports = function(list) {
var index = 0,
first = list.files[0];
if(!first) return;
- while(list.files.every(function(obj) {
- return (obj.name.charAt(index) == first.name.charAt(index));
- }) && index < first.name.length) {
+ while(list.files.every(check) && index < first.name.length) {
index++;
}
@@ -80,11 +80,6 @@ module.exports = function(list, options) {
}
function getPackage(root, currentPath, packageIndex) {
- var relPath = currentPath;
- // relative path excludes the package basepath for the current package
- if(packages[packageIndex].basepath) {
- relPath = currentPath.substr(packages[packageIndex].basepath.length);
- }
// handle files
if(root['.']) {
root['.'].forEach(function(file) {
@@ -106,7 +101,7 @@ module.exports = function(list, options) {
root['node_modules']['.'].forEach(function(file) {
// add single-file package
mainFile = path.basename(file.name);
- packageName = mainFile.replace(/(\.js|\.json)$/, ''),
+ packageName = mainFile.replace(/(\.js|\.json)$/, '');
packageBase = path.dirname(file.name) + '/';
index = packages.length;
@@ -169,7 +164,9 @@ module.exports = function(list, options) {
packages[0].main = getMainFile(packages[0].basepath, packages[0].files);
}
- // console.log(require('util').inspect(list.packages, null, 5, true), require('util').inspect(packages, null, 5, true));
+ // var util = require('util');
+ // console.log(util.inspect(list.packages, null, 5, true),
+ // util.inspect(packages, null, 5, true));
list.packages = packages;
};
@@ -9,7 +9,10 @@ module.exports = function(list, options) {
console.log('/* header */');
function next() {
- var last = runner({ stdout: fs.createReadStream(list.files[current].name) }, [ require('../file-tasks/wrap-commonjs-web.js')]);
+ var last = runner(
+ { stdout: fs.createReadStream(list.files[current].name) },
+ [ require('../file-tasks/wrap-commonjs-web.js')]
+ );
last.stdout.on('end', function() {
if(current == list.files.length){
console.log('/* footer */');
@@ -28,11 +28,12 @@ module.exports = function(list, options, out, onDone) {
basepath = (options.basepath ? path.normalize(options.basepath) : ''),
// replaced modules (e.g. jquery => window.jquery)
replaced = Object.keys(options.replaced || {}).map(function(key) {
- return JSON.stringify(key) + ': '+ '{ exports: ' + options.replaced[key] + ' }'
+ return JSON.stringify(key) + ': '+ '{ exports: ' + options.replaced[key] + ' }';
}).join(',\n'),
// remapped modules (e.g. assert => require('chai').assert
remapped = Object.keys(options.remap || {}).map(function(key) {
- return JSON.stringify(key) + ': '+ 'function(module, exports, require) { module.exports = ' + options.remap[key] + ' }';
+ return JSON.stringify(key) + ': '+
+ 'function(module, exports, require) { module.exports = ' + options.remap[key] + ' }';
}).join(',\n'),
// commands
commands = getCommands(options),
@@ -41,7 +42,7 @@ module.exports = function(list, options, out, onDone) {
optsHash = Cache.hash(JSON.stringify(options)),
progress;
- // console.log(util.inspect(list.files.map(function(item) { return item.name; }), false, 20, true));
+ // console.log(util.inspect(list.files.map(function(i) { return i.name; }), false, 20, true));
// exclude files using the npmjs defaults for file and path exclusions
filterNpm(list);
@@ -62,7 +63,8 @@ module.exports = function(list, options, out, onDone) {
// allow adding in expressions
if(options['exclude']) {
excludeList = excludeList.concat(
- (Array.isArray(options['exclude']) ? options['exclude'] : [ options['exclude'] ]).map(function(expr) {
+ (Array.isArray(options['exclude']) ? options['exclude'] : [ options['exclude'] ])
+ .map(function(expr) {
return new RegExp(expr);
})
);
@@ -74,7 +76,8 @@ module.exports = function(list, options, out, onDone) {
// run list level tasks
- // - generate `.packages` from `.files` (by grouping the set of `.files` into distinct dependencies)
+ // - generate `.packages` from `.files`
+ // (by grouping the set of `.files` into distinct dependencies)
// ... and infer the package main file
inferPackages(list, { main: options.main, basepath: basepath });
// - for each package, apply excludes (package.json.files, .npmignore, .gitignore)
@@ -141,41 +144,70 @@ module.exports = function(list, options, out, onDone) {
if(remapped) {
out.write(remapped + ',\n');
}
+
+ // store dependency references
+ Object.keys(packageObj.dependenciesById).forEach(function(name) {
+ var uid = packageObj.dependenciesById[name],
+ index;
+
+ // find the package in the (possibly altered) packages list by unique id
+ list.packages.some(function(item, itemIndex) {
+ var match = (item.uid == uid);
+ if(match) {
+ index = itemIndex;
+ }
+ return match;
+ });
+
+ // require.m[n]['foo'] = { c: 1, m: 'lib/index.js' }
+ out.write(
+ JSON.stringify(name) + ': ' + JSON.stringify({
+ c: index,
+ m: list.packages[index].main
+ }));
+ out.write(',\n');
+ });
+
done();
});
- // stream each file in serial order
- // Also does filtering: if no tasks match, a file is excluded
- packageObj.files = packageObj.files.filter(function(item, innerCurrent) {
- var fullpath = item.name,
- relname = path.relative(packageObj.basepath, item.name),
- tasks = getFileTasks(item, packageObj, commands);
- if(tasks.length == 0) {
- log.info('Excluded non-js/non-json file:', relname);
+ // filter files (and generate tasks)
+ packageObj.files = packageObj.files.filter(function(item) {
+ if(!fs.existsSync(item.name)) {
+ throw new Error('File not found: ' + item.name + ' Basepath = "' +
+ packageObj.basepath + '", filename="' + item.name + '"');
+ }
+
+ item.tasks = getFileTasks(item, packageObj, commands);
+ if(item.tasks.length === 0) {
+ log.info('Excluded non-js/non-json file:', path.relative(packageObj.basepath, item.name));
// also update list.files
removed.push(item.name);
return false; // exclude from package.files
}
+ return true; // do not filter out this file
+ });
+
+ // stream each file in serial order
+ var totalFiles = packageObj.files.length;
+ packageObj.files.forEach(function(item, index) {
+ var fullpath = item.name,
+ relname = path.relative(packageObj.basepath, item.name);
// all dependencies already have a basepath and the names are
// already relative to it, but this is not true for the main package
if(current === 0 && relname.substr(0, basepath.length) == basepath) {
relname = relname.substr(basepath.length);
}
- if(!fs.existsSync(fullpath)) {
- throw new Error('File not found: '+fullpath+' Basepath = "' +
- packageObj.basepath+'", filename="' + relname +'"');
- }
-
// add the first task
packageTasks.push(
function(out, done) {
out.write(JSON.stringify(relname) + ': ');
done();
});
- var flow = new Flow(tasks).input(fs.createReadStream(fullpath));
+ var flow = new Flow(item.tasks).input(fs.createReadStream(fullpath));
// these are used to disambiguate cached results
flow.inputFilePath = fullpath;
@@ -200,39 +232,19 @@ module.exports = function(list, options, out, onDone) {
packageTasks.push(
function(out, done) {
- out.write(',\n');
+ // determining when to write the last common becomes easy
+ // when files are processed last
+ if(index == totalFiles - 1) {
+ out.write('\n');
+ } else {
+ out.write(',\n');
+ }
done();
});
-
- return true; // do not filter out this file
});
// package footer
packageTasks.push(function(out, done) {
-
- // store dependency references
- Object.keys(packageObj.dependenciesById).forEach(function(name) {
- var uid = packageObj.dependenciesById[name],
- index;
-
- // find the package in the (possibly altered) packages list by unique id
- list.packages.some(function(item, itemIndex) {
- var match = (item.uid == uid);
- if(match) {
- index = itemIndex;
- }
- return match;
- });
-
- // require.m[n]['foo'] = { c: 1, m: 'lib/index.js' }
- out.write(
- JSON.stringify(name) + ': ' + JSON.stringify({
- c: index,
- m: list.packages[index].main
- }));
- out.write(',\n');
- });
-
out.write('};\n');
done();
});
@@ -250,7 +262,8 @@ module.exports = function(list, options, out, onDone) {
//
if(options['amd']) {
out.write('if (typeof define === "function" && define.amd) {');
- out.write('define('+JSON.stringify(exportVariableName)+', function() { return require(\'' + packageRootFileName + '\'); });\n');
+ out.write('define(' + JSON.stringify(exportVariableName) +
+ ', function() { return require(\'' + packageRootFileName + '\'); });\n');
out.write('}\n');
}
// export the package root into `window`
@@ -274,7 +287,8 @@ module.exports = function(list, options, out, onDone) {
// if any reporting is explicitly enabled
if(options.report || options.verbose || options.progress) {
if(cacheHits.length > 0) {
- console.log('Cache hits (' + options['cache-path'] + '):', cacheHits.length, '/', list.files.length, 'files');
+ console.log('Cache hits (' + options['cache-path'] + '):',
+ cacheHits.length, '/', list.files.length, 'files');
// exclude cached files
list.packages.forEach(function(pack, index) {
list.packages[index].files = list.packages[index].files.filter(function(item) {
@@ -315,7 +329,9 @@ module.exports = function(list, options, out, onDone) {
limit: options.jobs,
end: (out !== process.stdout ? true : false), // e.g. no "end" for process.stdout
onDone: function() {
- onDone && onDone();
+ if(typeof onDone === 'function') {
+ onDone();
+ }
}
});
Oops, something went wrong.

0 comments on commit 75f54c7

Please sign in to comment.