Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tyler Kellen
committed
Jan 31, 2014
0 parents
commit 7a512c7
Showing
13 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"strict": true, | ||
"undef": true, | ||
"unused": true, | ||
"node": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- "0.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2014 Tyler Kellen | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# liftoff [![Build Status](https://secure.travis-ci.org/tkellen/node-liftoff.png)](http://travis-ci.org/tkellen/node-liftoff) | ||
> Launch your command line tool with ease. | ||
[![NPM](https://nodei.co/npm/liftoff.png)](https://nodei.co/npm/liftoff/) | ||
|
||
### What? | ||
Say you're writing a CLI tool. Let's call it `hack`. You want to configure hack for your projects using a `Hackfile`. This is node, you install `hack` locally for each project you use it on. But, in order to get the `hack` command in your PATH, you also install it globally. | ||
|
||
Now, when you run the `hack` command, you want it to use the `Hackfile` in your current directory, and the local installation of `hack`, too. It'd be nice if it traversed up your folders until it found one, for those times when you're not in the root directory of your project. Heck, you might even want to launch it from a folder outside of your project by manually specifying a working directory. liftoff manages this for you. | ||
|
||
So, everything is working great. Now we can find your local `hack` and `Hackfile` with ease. Unfortunately, it turns out you authored your `Hackfile` in coffee-script, or some other js variant. In order to support *that*, you have to load the compiler for it, and then register the extension for it with node. Good news, liftoff can do that too. | ||
|
||
### Examples | ||
Check out [the example liftoff command](/blob/master/bin/liftoff.js) to see how you might use this. | ||
|
||
To see liftoff in action now, install it with `npm install -g liftoff`, then make a Hackfile.js with some arbitrary javascript it, and run `liftoff` while in the same parent folder. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
var Liftoff = require('../'); | ||
//var liftoff = require('liftoff'); // you want this, not the above | ||
|
||
var hack = new Liftoff({ | ||
moduleName: 'hack', // your npm module (installed locally to each project) | ||
configName: 'hackfile', // your module's configuration file name | ||
cwdOpt: 'cwd', // the cli option to change the cwd | ||
requireOpt: 'require' // the cli option for pre-requiring modules | ||
}).on('require', function (name, module) { | ||
// console.log('required:', name, module); | ||
}).on('requireFail', function (name, err) { | ||
// console.log('failed to require:', name, err); | ||
}).on('run', function () { | ||
console.log('CLI OPTIONS:', this.args); | ||
console.log('CWD:', this.cwd); | ||
console.log('LOCAL MODULES REQUIRED:', this.localRequires); | ||
console.log('EXTENSIONS RECOGNIZED:', this.validExtensions); | ||
console.log('SEARCHING FOR:', this.configNameRegex); | ||
console.log('FOUND CONFIG AT:', this.configPath); | ||
console.log('CONFIG BASE DIR:', this.configBase); | ||
console.log('YOUR LOCAL MODULE IS LOCATED AT:', this.modulePath); | ||
|
||
if(this.configPath) { | ||
require(this.configPath); | ||
} | ||
}); | ||
|
||
hack.launch(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
var util = require('util'); | ||
var path = require('path'); | ||
var EventEmitter = require('events').EventEmitter; | ||
|
||
var findup = require('findup-sync'); | ||
var findCwd = require('./lib/find_cwd'); | ||
var findLocal = require('./lib/find_local'); | ||
var validExtensions = require('./lib/valid_extensions'); | ||
|
||
function Liftoff (opts) { | ||
this.moduleName = opts.moduleName; | ||
this.configName = opts.configName; | ||
this.cwdOpt = opts.cwdOpt||'cwd'; | ||
this.requireOpt = opts.requireOpt||'require'; | ||
} | ||
util.inherits(Liftoff, EventEmitter); | ||
|
||
Liftoff.prototype.require = function (dep) { | ||
if(Array.isArray(dep)) { | ||
dep.forEach(this.require, this); | ||
} else { | ||
try { | ||
var module = require(findLocal(module, this.cwd)); | ||
this.emit('require', dep, module); | ||
return module; | ||
} catch (e) { | ||
this.emit('requireFail', dep, e); | ||
} | ||
} | ||
}; | ||
|
||
Liftoff.prototype.launch = function () { | ||
// parse cli | ||
this.args = require('optimist').argv; | ||
// get cwd | ||
this.cwd = findCwd(this.args[this.cwdOpt]); | ||
// load required modules | ||
this.localRequires = this.args[this.requireOpt]||null; | ||
if(this.localRequires) { | ||
this.require(this.localRequires); | ||
} | ||
// set valid extensions | ||
this.validExtensions = validExtensions(); | ||
// set the regex for finding a valid config file | ||
this.configNameRegex = this.configName+'{'+this.validExtensions+'}'; | ||
// set config path based on what we can require | ||
this.configPath = findup(this.configNameRegex, {cwd: this.cwd, nocase: true}); | ||
// if we found a config, load the requested module and matching package | ||
if(this.configPath) { | ||
this.configBase = path.dirname(this.configPath); | ||
this.modulePath = findLocal(this.moduleName, this.configBase); | ||
} | ||
// kick it off! | ||
this.emit('run'); | ||
}; | ||
|
||
module.exports = Liftoff; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
|
||
module.exports = function (base) { | ||
if (base) { | ||
return path.resolve(base); | ||
} else { | ||
return process.cwd(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
var resolve = require('resolve'); | ||
|
||
module.exports = function (module, basedir) { | ||
try { | ||
return resolve.sync(module, {basedir: basedir}); | ||
} catch (e) { | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
module.exports = function () { | ||
var extensions; | ||
if (require.extensions) { | ||
extensions = Object.keys(require.extensions).join(','); | ||
} else { | ||
extensions = ['.js']; | ||
} | ||
return extensions; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"name": "liftoff", | ||
"description": "Launch your command line tool with ease.", | ||
"version": "0.1.0", | ||
"homepage": "https://github.com/tkellen/node-liftoff", | ||
"author": { | ||
"name": "Tyler Kellen", | ||
"url": "http://goingslowly.com/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/tkellen/node-liftoff.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/tkellen/node-liftoff/issues" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/tkellen/node-liftoff/blob/master/LICENSE" | ||
} | ||
], | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
}, | ||
"bin": { | ||
"liftoff": "./bin/liftoff.js" | ||
}, | ||
"scripts": { | ||
"test": "tap ./test" | ||
}, | ||
"devDependencies": { | ||
"tap": "~0.4.8" | ||
}, | ||
"keywords": [ | ||
"command line" | ||
], | ||
"dependencies": { | ||
"findup-sync": "~0.1.2", | ||
"resolve": "~0.6.1", | ||
"optimist": "~0.6.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const test = require('tap').test; | ||
|