Skip to content

Commit

Permalink
fix dotfiles, use fromargs
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed May 30, 2013
1 parent 838bebd commit cf69a7d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
51 changes: 35 additions & 16 deletions bin/cmd.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
#!/bin/bash
#!/usr/bin/env node

var watchify = require('../');
var through = require('through');
var fs = require('fs');
var argv = require('optimist').argv;
var outfile = argv.o || argv.outfile;
var path = require('path');
var fromArgs = require('browserify/bin/args');

var w = watchify(argv._);
w.on('update', function () {
console.log('UPDATE');
var s = w.bundle();
s.pipe(fs.createWriteStream('.' + outfile));
s.on('data', function () {});
s.on('end', function () {
fs.rename('.' + outfile, outfile, function (err) {
if (err) console.error(err)
else console.log(outfile + ' written')
});
var w = watchify(fromArgs(process.argv.slice(2)));
var outfile = w.argv.o || w.argv.outfile;
var verbose = w.argv.v || w.argv.verbose;

if (!outfile) {
console.error('You MUST specify an outfile with -o.');
process.exit(1);
}
var dotfile = path.join(path.dirname(outfile), '.' + path.basename(outfile));

w.on('update', bundle);
bundle();

function bundle () {
var wb = w.bundle();
wb.on('error', function (err) {
console.error(String(err));
});
});
w.bundle().pipe(fs.createWriteStream(outfile));
wb.pipe(fs.createWriteStream(dotfile));
var bytes = 0;
wb.pipe(through(write, end));

function write (buf) { bytes += buf.length }

function end () {
fs.rename(dotfile, outfile, function (err) {
if (err) return console.error(err);
if (verbose) {
console.error(bytes + ' bytes written to ' + outfile);
}
});
}
}
2 changes: 1 addition & 1 deletion example/files/one.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var two = require('./two');

module.exports = function (x) { return x * two(x + 6) };
module.exports = function (x) { return x * two(x + 5) };
7 changes: 2 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var fs = require('fs');
var browserify = require('browserify');
var mdeps = require('module-deps');
var through = require('through');
var browserify = require('browserify');

module.exports = function (opts, cb) {
if (!opts) opts = {};
Expand All @@ -10,7 +9,7 @@ module.exports = function (opts, cb) {
var pending = false;

b.on('dep', function (dep) {
cache[dep.id] = dep;
cache[dep.id] = dep.source;

fs.watch(dep.id, function (type) {
delete cache[dep.id];
Expand All @@ -22,8 +21,6 @@ module.exports = function (opts, cb) {
}, opts.delay || 300);

pending = true;

console.log(Date.now());
});
});

Expand Down

0 comments on commit cf69a7d

Please sign in to comment.