-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
0 parents
commit df54671
Showing
13 changed files
with
236 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,24 @@ | ||
# 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, so you install `hack` locally for each project you use it in. 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` next to it. It'd be nice if it traversed up your folders until it found a `Hackfile`, 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 you can find your local `hack` and `Hackfile` with ease. Unfortunately, it turns out you've 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](/bin/liftoff.js) to see how you might use this. | ||
|
||
|
||
### Try it now | ||
Want to see how the above example works? | ||
|
||
1. Install liftoff with `npm install -g liftoff` | ||
2. Make a `Hackfile.js` with some arbitrary javascript it. | ||
3. Run `liftoff` while in the same parent folder. | ||
|
||
For extra credit, try writing your `Hackfile` in coffeescript. Then, run `takeoff --require coffee-script`. Make sure you install coffee-script locally, though! |
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,41 @@ | ||
#!/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) { | ||
// called each time a module is pre-required | ||
if (name === 'coffee-script') { | ||
module.register(); | ||
} | ||
console.log('required:', name); | ||
}).on('requireFail', function (name, err) { | ||
// called each time a pre-required module can't be loaded | ||
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) { | ||
process.chdir(this.configBase); // set the current working directory of | ||
// this process to the folder the config | ||
// is in. now when you read or write | ||
// files, they'll be relative to it. | ||
require(this.configPath); // load your local config | ||
|
||
} | ||
}); | ||
|
||
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(dep, 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.0.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; | ||
|