Comparing version 1.3.2 to 2.0.0
@@ -6,23 +6,22 @@ #!/usr/bin/env node | ||
require('bole').output({ | ||
stream: process.stdout, | ||
level: 'debug' | ||
}) | ||
var args = process.argv.slice(2) | ||
var opts = require('minimist')(args) | ||
var wtch = require('wtch') | ||
var portfinder = require('portfinder') | ||
var defaultGlob = '**/*.{html,css}' | ||
var entries = opts._ | ||
opts.stream = process.stdout | ||
delete opts._ | ||
require('../')(args, function(budo) { | ||
var watcher | ||
if (opts.live || opts['live-plugin']) { | ||
watcher = wtch([defaultGlob, budo.output.glob]) | ||
portfinder.basePort = opts.port || opts.p || 9966 | ||
portfinder.getPort(function(err, port) { | ||
if (err) { | ||
console.error("Could not find port", err) | ||
process.exit(1) | ||
} | ||
budo.on('exit', function() { | ||
if (watcher) | ||
watcher.close() | ||
}) | ||
}) | ||
opts.port = port | ||
require('../')(entries, opts) | ||
.on('error', function(err2) { | ||
console.error(err2.message) | ||
process.exit(1) | ||
}) | ||
}) |
109
index.js
@@ -1,71 +0,90 @@ | ||
var log = require('bole')('budo') | ||
var minimist = require('minimist') | ||
var portfinder = require('portfinder') | ||
var bole = require('bole') | ||
var log = bole('budo') | ||
var xtend = require('xtend') | ||
var assign = require('xtend/mutable') | ||
var Emitter = require('events/') | ||
var getOutput = require('./lib/get-output') | ||
var wtch = require('wtch') | ||
var defaultGlob = '**/*.{html,css}' | ||
var budo = require('./lib/budo') | ||
var noop = function(){} | ||
module.exports = function(args, cb) { | ||
cb = cb||noop | ||
module.exports = function(entry, opts) { | ||
var argv = assign({}, opts) | ||
var argv = minimist(args) | ||
if (argv._.length === 0) { | ||
console.error("No entry scripts specified!") | ||
process.exit(1) | ||
if (argv.stream) { | ||
bole.output({ | ||
stream: argv.stream, | ||
level: 'debug' | ||
}) | ||
} | ||
argv.port = argv.port || 9966 | ||
var emitter = new Emitter() | ||
var watcher | ||
var entries = Array.isArray(entry) ? entry : [ entry ] | ||
entries = entries.filter(Boolean) | ||
if (entries.length === 0) { | ||
bail("No entry scripts specified!") | ||
return emitter | ||
} | ||
argv.port = typeof argv.port === 'number' ? argv.port : 9966 | ||
argv.dir = argv.dir || process.cwd() | ||
var outOpts = xtend(argv, { __to: entryMapping() }) | ||
var outOpts = xtend(argv, { __to: entryMapping() }) | ||
getOutput(outOpts, function(err, output) { | ||
if (err) { | ||
console.error("Error: Could not create temp bundle.js directory") | ||
process.exit(1) | ||
bail("Error: Could not create temp bundle.js directory") | ||
return emitter | ||
} | ||
//determine next port | ||
portfinder.basePort = argv.port | ||
portfinder.getPort(function(err, port) { | ||
if (err) { | ||
console.error("Error: Could not get available port") | ||
process.exit(1) | ||
} | ||
//run watchify server | ||
var emitter = budo(args, xtend(argv, { | ||
port: port, | ||
output: output | ||
})).on('error', function(err) { | ||
console.error("Error running budo on", port, err) | ||
process.exit(1) | ||
}).on('exit', function() { | ||
//run watchify server | ||
var app = budo(entries, output, argv) | ||
.on('error', function(err2) { | ||
var msg = "Error running budo on " + argv.port + ': ' + err2 | ||
bail(msg) | ||
}) | ||
.on('exit', function() { | ||
log.info('closing') | ||
emitter.emit('exit') | ||
if (watcher) | ||
watcher.close() | ||
}) | ||
emitter.on('connect', function(result) { | ||
cb(result) | ||
.on('connect', function() { | ||
//setup live reload | ||
if (argv.live || argv['live-plugin']) { | ||
var liveOptions = { | ||
port: argv['live-port'] | ||
} | ||
//dispatches watch and LiveReload events | ||
watcher = wtch([defaultGlob, app.glob], liveOptions) | ||
watcher.on('watch', emitter.emit.bind(emitter, 'watch')) | ||
watcher.on('reload', emitter.emit.bind(emitter, 'reload')) | ||
} | ||
emitter.emit('connect', app) | ||
}) | ||
}) | ||
}) | ||
return emitter | ||
function entryMapping() { | ||
var mapTo | ||
var first = argv._[0] | ||
var to | ||
var first = entries[0] | ||
var parts = first.split(':') | ||
if (parts.length > 1 && parts[1].length > 0) { | ||
var from = parts[0] | ||
var to = parts[1] | ||
argv._[0] = from | ||
//clean up original arguments for watchify | ||
var idx = args.indexOf(first) | ||
if (idx>=0) | ||
args[idx] = from | ||
mapTo = to | ||
to = parts[1] | ||
entries[0] = from | ||
} | ||
return mapTo | ||
return to | ||
} | ||
function bail(msg) { | ||
process.nextTick(function() { | ||
emitter.emit('error', new Error(msg)) | ||
}) | ||
} | ||
} |
135
lib/budo.js
var path = require('path') | ||
var Emitter = require('events/') | ||
var watchify = require('./watchify') | ||
var minimist = require('minimist') | ||
var xtend = require('xtend') | ||
@@ -9,18 +8,18 @@ var assign = require('xtend/mutable') | ||
var log = require('bole')('budo') | ||
var dargs = require('dargs') | ||
module.exports = function(watchifyArgs, opt) { | ||
var output = opt.output | ||
module.exports = function(entries, output, opt) { | ||
opt = opt||{} | ||
var port = opt.port || 9966 | ||
var host = opt.host | ||
var serverOpt = xtend(opt, { output: output }) | ||
var emitter = new Emitter() | ||
//patch watchify args with new outfile and default debug | ||
setOutfile(watchifyArgs, output.from) | ||
setDebug(watchifyArgs, opt.d || opt.debug) | ||
var closed = false | ||
//spin up watchify instance | ||
var watchifyArgs = getWatchifyArgs(entries, output, opt) | ||
var watchProc = watchify(watchifyArgs) | ||
var serverOpt = xtend(opt, { output: output }) | ||
var server = http(serverOpt) | ||
@@ -30,26 +29,7 @@ .on('error', function(err) { | ||
}) | ||
.listen(port, host, function(err) { | ||
if (err) { | ||
emitter.emit('error', new Error("Could not connect to server:", err)) | ||
return | ||
} | ||
var uri = "http://"+(host||'localhost')+":"+port+"/" | ||
log.info("Server running at", uri) | ||
//bug with chokidar@1.0.0-rc3 | ||
//anything in OSX tmp dirs need a wildcard | ||
//to work with fsevents | ||
var glob = output.tmp | ||
? path.join(output.dir, '**.js') | ||
: output.from | ||
//add the uri / output to budo instance | ||
assign(emitter, { | ||
uri: uri, | ||
output: xtend(output, { glob: glob }) | ||
}) | ||
emitter.emit('connect', emitter) | ||
}) | ||
.listen(port, host, handler) | ||
emitter.close = function() { | ||
if (closed) return | ||
closed = true | ||
watchProc.kill() | ||
@@ -66,48 +46,57 @@ server.close() | ||
function setOutfile(arglist, file) { | ||
var idx = arglist.indexOf('-o') | ||
if (idx === -1) | ||
idx = arglist.indexOf('--outfile') | ||
if (idx === -1) { | ||
arglist.push('-o') | ||
arglist.push(file) | ||
} else | ||
arglist[idx+1] = file | ||
function handler(err) { | ||
if (err) { | ||
emitter.emit('error', new Error("Could not connect to server: " + err)) | ||
return | ||
} | ||
var hostname = (host||'localhost') | ||
var uri = "http://"+hostname+":"+port+"/" | ||
log.info("Server running at", uri) | ||
//bug with chokidar@1.0.0-rc3 | ||
//anything in OSX tmp dirs need a wildcard | ||
//to work with fsevents | ||
var glob = output.tmp | ||
? path.join(output.dir, '**.js') | ||
: output.from | ||
//add the uri / output to budo instance | ||
assign(emitter, { | ||
uri: uri, | ||
port: port, | ||
host: hostname, | ||
server: server | ||
}, output, { glob: glob }) | ||
emitter.emit('connect', emitter) | ||
} | ||
} | ||
//Supports various debug features | ||
// ['--no-debug'] (from minimist parsing, which browserify uses) | ||
// ['--debug=false'] | ||
// ['--debug', 'false'] | ||
// ['-d', 'false'] | ||
//Need to remove the debug flag entirely from args otherwise browserify | ||
//will use source maps | ||
function setDebug(arglist, enabled) { | ||
enabled = enabled !== false && enabled !== 'false' | ||
//user can disable debug | ||
if (enabled === false) { | ||
removeDebug(arglist) | ||
} | ||
//by default, we want to enable debug | ||
else { | ||
var idx1 = arglist.indexOf('-d') | ||
var idx2 = arglist.indexOf('--debug') | ||
if (idx1 === -1 && idx2 === -1) | ||
arglist.push('-d') | ||
} | ||
function getWatchifyArgs(entries, output, opt) { | ||
//do not mutate original opts | ||
opt = assign({}, opt) | ||
//set output file | ||
opt.outfile = output.from | ||
delete opt.o | ||
//enable debug by default | ||
if (opt.d !== false && opt.debug !== false) { | ||
delete opt.d | ||
opt.debug = true | ||
} | ||
//if user explicitly disabled debug... | ||
else if (opt.d === false || opt.debug === false) { | ||
delete opt.d | ||
delete opt.debug | ||
} | ||
function removeDebug(arglist) { | ||
var args = ['-d', '--debug', '--debug=false'] | ||
args.forEach(function(arg) { | ||
var idx = arglist.indexOf(arg) | ||
if (idx === -1 || String(arglist[idx+1]) === 'true') | ||
return | ||
if (String(arglist[idx+1]) === 'false') | ||
arglist.splice(idx, 2) | ||
else | ||
arglist.splice(idx, 1) | ||
}) | ||
} | ||
//clean up some possible collisions | ||
delete opt.dir | ||
delete opt.port | ||
delete opt.host | ||
delete opt.live | ||
delete opt['live-port'] | ||
delete opt['live-script'] | ||
delete opt['live-plugin'] | ||
return entries.concat(dargs(opt)) | ||
} |
@@ -21,6 +21,3 @@ var ecstatic = require('ecstatic') | ||
var live = opts.live | ||
var host = opts.host | ||
var livePort = opts['live-port'] | ||
var live = opts.live || opts['live-script'] | ||
var liveOpts = { | ||
@@ -45,4 +42,2 @@ host: opts.host, | ||
router.addRoute('*', function(req, res, params) { | ||
// if (live) | ||
// res = inject(res, liveOpts) | ||
log.info({ url: req.url, type: 'static' }) | ||
@@ -49,0 +44,0 @@ staticHandler(req, res) |
@@ -9,3 +9,3 @@ var spawn = require('npm-execspawn') | ||
.join(' ') | ||
var proc = spawn(cmd) | ||
@@ -12,0 +12,0 @@ proc.stderr.on('data', function(err) { |
{ | ||
"name": "budo", | ||
"version": "1.3.2", | ||
"version": "2.0.0", | ||
"description": "a browserify server for rapid prototyping", | ||
@@ -17,2 +17,3 @@ "main": "index.js", | ||
"bole": "^2.0.0", | ||
"dargs": "^4.0.0", | ||
"ecstatic": "^0.5.8", | ||
@@ -30,3 +31,3 @@ "events": "^1.0.2", | ||
"tmp": "0.0.24", | ||
"wtch": "^3.1.0", | ||
"wtch": "^3.2.0", | ||
"xtend": "^4.0.0" | ||
@@ -51,7 +52,7 @@ }, | ||
"tree-kill": "0.0.6", | ||
"watchify": "^2.3.0", | ||
"watchify": "^2.6.2", | ||
"win-spawn": "^2.0.0" | ||
}, | ||
"scripts": { | ||
"test": "node test/test-simple.js | tap-spec", | ||
"test": "tape test/test*.js | tap-spec", | ||
"start": "./bin/cmd.js example/app.js --dir example --verbose | garnish", | ||
@@ -58,0 +59,0 @@ "live": "./bin/cmd.js example/app.js --dir example --live | garnish -v", |
@@ -45,2 +45,3 @@ # budō | ||
- [script injection with budo-chrome](https://github.com/mattdesl/budo-chrome) | ||
- [programmatic usage (Gulp, Grunt)](docs/programmatic-usage.md) | ||
- [rapid prototyping with budō and wzrd](http://mattdesl.svbtle.com/rapid-prototyping) | ||
@@ -52,2 +53,4 @@ | ||
### CLI | ||
Details for `budo` command-line interface. Other options like `--verbose` and `--transform` are sent to browserify/watchify. | ||
@@ -71,2 +74,23 @@ | ||
*Note:* The `--outfile` is relative to the specified `--dir`. | ||
### API | ||
The API mirrors the CLI except you must provide a `stream` for logging, and it does not attempt to auto-portfind. | ||
```js | ||
var budo = require('budo') | ||
budo('./src/index.js', { | ||
live: true, //live reload | ||
stream: process.stdout, //log to stdout | ||
port: 8000 //use this port | ||
}).on('connnect', function(ev) { | ||
//... | ||
}) | ||
``` | ||
See [API usage](docs/programmatic-usage.md) for more details. | ||
## Script Injection | ||
@@ -73,0 +97,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16150
329
104
16
+ Addeddargs@^4.0.0
+ Addeddargs@4.1.0(transitive)
+ Addednumber-is-nan@1.0.1(transitive)
Updatedwtch@^3.2.0