From cf69a7d1d628f2e1a0dc2d519ad5542290390bb4 Mon Sep 17 00:00:00 2001 From: James Halliday Date: Thu, 30 May 2013 01:41:14 -0700 Subject: [PATCH] fix dotfiles, use fromargs --- bin/cmd.js | 51 ++++++++++++++++++++++++++++++-------------- example/files/one.js | 2 +- index.js | 7 ++---- 3 files changed, 38 insertions(+), 22 deletions(-) mode change 100644 => 100755 bin/cmd.js diff --git a/bin/cmd.js b/bin/cmd.js old mode 100644 new mode 100755 index ae54b5f..623044c --- a/bin/cmd.js +++ b/bin/cmd.js @@ -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); + } + }); + } +} diff --git a/example/files/one.js b/example/files/one.js index 80b26ea..75f7df6 100644 --- a/example/files/one.js +++ b/example/files/one.js @@ -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) }; diff --git a/index.js b/index.js index c03548c..6c91c7a 100644 --- a/index.js +++ b/index.js @@ -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 = {}; @@ -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]; @@ -22,8 +21,6 @@ module.exports = function (opts, cb) { }, opts.delay || 300); pending = true; - - console.log(Date.now()); }); });