Skip to content

Commit

Permalink
feat: Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Kellen authored and phated committed Nov 21, 2021
0 parents commit df54671
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 6 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"strict": true,
"undef": true,
"unused": true,
"node": true
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "0.10"
22 changes: 22 additions & 0 deletions LICENSE
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.
24 changes: 24 additions & 0 deletions README.md
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!
41 changes: 41 additions & 0 deletions bin/liftoff.js
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();
59 changes: 59 additions & 0 deletions index.js
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;
11 changes: 11 additions & 0 deletions lib/find_cwd.js
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();
}
};
11 changes: 11 additions & 0 deletions lib/find_local.js
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;
}
};
11 changes: 11 additions & 0 deletions lib/valid_extensions.js
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;
};
44 changes: 44 additions & 0 deletions package.json
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"
}
}
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const test = require('tap').test;

0 comments on commit df54671

Please sign in to comment.