Permalink
Browse files

watch stubs

  • Loading branch information...
1 parent aeffd15 commit 657738c6adaefd28cb6110f19f0a9c59ee531288 @substack committed Jul 9, 2011
Showing with 70 additions and 38 deletions.
  1. +69 −37 index.js
  2. +1 −1 package.json
View
106 index.js
@@ -3,50 +3,82 @@ var path = require('path');
var findit = require('findit');
var Seq = require('seq');
-module.exports = function (target, dir, ext) {
+module.exports = function (target, dir, optsOrEx) {
if (!target) throw new Error('Target name required');
if (!dir) throw new Error('Directory or files required');
+ var opts = typeof optsOrEx === 'object' && !Array.isArray(optsOrEx)
+ ? optsOrEx
+ : { extension : optsOrEx }
+ ;
+
+ var filter = function (file) {
+ var ext = path.extname(file);
+ if (!opts.extension) {
+ return true;
+ }
+ else if (typeof opts.extension === 'function') {
+ return opts.extension(file);
+ }
+ else if (typeof opts.extension === 'string') {
+ return opts.extension === ext;
+ }
+ else if (Array.isArray(opts.extension)) {
+ return opts.extension.some(function (e) { return e === ext });
+ }
+ else {
+ return true;
+ }
+ };
+
return function (bundle) {
- var files = findit.sync(dir).filter(function (absfile) {
- if (fs.statSync(absfile).isDirectory()) return false;
-
- var file = absfile.replace(dir.match(/\/$/) ? dir : dir + '/', '');
- var e = (file.match(/\.([^\/.]+)$/) || [,''])[1];
-
- if (typeof ext === 'function' && ext(file, e)) {
- return true;
- }
- else if (file.split('/').some(function (p) {
- return p.match(/^\./)
- })) {
- // hidden files and directories
- return false;
- }
- else if (Array.isArray(ext) && ext.indexOf(e) >= 0) {
- return true;
- }
- else if (typeof ext === 'string' && e === ext) {
- return true;
+ var files = [];
+
+ findit.sync(dir, function (file, stat) {
+ if (stat.isDirectory()) {
+ if (opts.watch) {
+ fs.watchFile(file, function (curr, prev) {
+
+ });
+ }
}
- else if (!ext) {
- return true;
+ else if (filter(file)) {
+ var i = files.length;
+ files.push(file);
+
+ if (opts.watch) {
+ fs.watchFile(file, function (curr, prev) {
+
+ });
+ }
}
});
- var bodies = files.reduce(function (acc, file) {
- var rel = file.slice(dir.length + 1);
- acc[rel] = fs.readFileSync(file, 'utf8');
- return acc;
- }, {});
-
- var file = __dirname + '/browser/files.js';
- var body = fs.readFileSync(file, 'utf8')
- .replace(/\$bodies/, function () {
- return JSON.stringify(bodies);
- })
- ;
-
- bundle.include(null, path.normalize('/node_modules/' + target), body);
+ var include = function (files) {
+ var dst = path.normalize('/node_modules/' + target);
+
+ Object.keys(bundle.files).forEach(function (key) {
+ if (bundle.files[key].target === dst) {
+ delete bundle.files[key];
+ }
+ });
+
+ var bodies = files.reduce(function (acc, file) {
+ var rel = file.slice(dir.length + 1);
+ acc[rel] = fs.readFileSync(file, 'utf8');
+ return acc;
+ }, {});
+
+ var file = __dirname + '/browser/files.js';
+ var body = fs.readFileSync(file, 'utf8')
+ .replace(/\$bodies/, function () {
+ return JSON.stringify(bodies);
+ })
+ ;
+
+ bundle.include(null, dst, body);
+ };
+ include(files);
};
};
+
View
@@ -8,7 +8,7 @@
"example" : "example"
},
"dependencies" : {
- "findit" : ">=0.0.4 <0.1",
+ "findit" : ">=0.1.1 <0.2",
"seq" : ">=0.2.5 <0.3"
},
"devDependencies" : {

0 comments on commit 657738c

Please sign in to comment.