Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Kellen committed Jan 31, 2014
0 parents commit 7a512c7
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions .jshintrc
@@ -0,0 +1,6 @@
{
"strict": true,
"undef": true,
"unused": true,
"node": true
}
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
test
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
22 changes: 22 additions & 0 deletions LICENSE
@@ -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.
16 changes: 16 additions & 0 deletions README.md
@@ -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.
31 changes: 31 additions & 0 deletions bin/liftoff.js
@@ -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();
59 changes: 59 additions & 0 deletions index.js
@@ -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;
11 changes: 11 additions & 0 deletions lib/find_cwd.js
@@ -0,0 +1,11 @@
'use strict';

var path = require('path');

module.exports = function (base) {
if (base) {
return path.resolve(base);
} else {
return process.cwd();
}
};
11 changes: 11 additions & 0 deletions lib/find_local.js
@@ -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;
}
};
11 changes: 11 additions & 0 deletions lib/valid_extensions.js
@@ -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;
};
44 changes: 44 additions & 0 deletions package.json
@@ -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"
}
}
2 changes: 2 additions & 0 deletions test/index.js
@@ -0,0 +1,2 @@
const test = require('tap').test;

0 comments on commit 7a512c7

Please sign in to comment.