Permalink
Browse files

reorganize to accomodate CLI

  • Loading branch information...
1 parent cc0f52e commit 78abd426bdbdf6f779a4a8a7cdf77d1f4da78c9f @jed committed Mar 2, 2011
Showing with 12 additions and 74 deletions.
  1. +1 −73 index.js
  2. +11 −1 package.json
View
@@ -1,73 +1 @@
-var fs = require( "fs" )
- , exec = require( "child_process" ).exec
- , Localhoses = function(){}
-
-if ( process.platform != "darwin" ) throw "Sorry, only tested on OS X."
-
-Localhoses.prototype = {
- hostsPath: "/private/etc/hosts",
- backupSuffix: ".localhoseBackup",
-
- domains: function(){ return Object.keys( this ) },
-
- set: function() {
- for ( var i = arguments.length; i--; ) this[ arguments[ i ] ] = true
-
- return this.save()
- },
-
- unset: function() {
- var domains = 0 in arguments ? arguments : this.domains()
-
- for ( var i = domains.length; i--; ) delete this[ domains[ i ] ]
-
- return this.save()
- },
-
- teardown: function() {
- var hostsPath = this.hostsPath
- , backupPath = hostsPath + this.backupSuffix
- , backup
-
- try {
- backup = fs.readFileSync( backupPath )
- fs.unlinkSync( backupPath )
- fs.writeFileSync( hostsPath, backup )
- }
-
- catch( e ){}
-
- return this
- },
-
- toString: function() {
- return [ "\n\n# The following were added by 'localhose':" ]
- .concat( this.domains() )
- .join( "\n127.0.0.1\t" )
- },
-
- save: function() {
- exec( "dscacheutil -flushcache" )
-
- if ( !this.domains().length ) return this.teardown()
-
- var hostsPath = this.hostsPath
- , backupPath = hostsPath + this.backupSuffix
- , hosts, backup
-
- try { hosts = fs.readFileSync( hostsPath ) }
- catch ( e ) { throw "/etc/hosts does not exist or cannot be read." }
-
- try { backup = fs.readFileSync( backupPath ) }
- catch ( e ) {
- try { fs.writeFileSync( backupPath, backup = hosts ) }
- catch ( e ){ throw backupPath + " is not writeable." }
- }
-
- fs.writeFileSync( hostsPath, backup + this.toString() )
-
- return this
- }
-}
-
-module.exports = new Localhoses
+module.exports = require( "./lib/localhose" )
View
@@ -1,6 +1,16 @@
{
"name": "localhose",
"version": "0.0.1",
- "description": "Local DNS hosing for easy development",
+ "description": "Hose your hosts file for easier local web development",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/jed/localhose.git"
+ },
+ "engines": [
+ "node"
+ ],
+ "directories": {
+ "lib": "./lib"
+ },
"main": "./index"
}

0 comments on commit 78abd42

Please sign in to comment.