Skip to content

Commit

Permalink
Init command plumbing system
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Apr 21, 2016
1 parent 94369a0 commit 0361905
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.swp
src
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ babel:

lint:
eslint .
jscs .

env:
@echo $(PATH)

build: babel test lint

Expand Down
4 changes: 4 additions & 0 deletions bin/tabtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

process.env.DEBUG = process.env.DEBUG || 'tabtab*';
require('../src/cli');
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

var tabtab = module.exports = require('./src');


30 changes: 30 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

const debug = require('debug')('tabtab');
const minimist = require('minimist');
const npmlog = require('npmlog');
const commands = require('./commands');

let opts = minimist(process.argv.slice(2), {
alias: {
h: 'help',
v: 'version'
}
});

const cmd = opts._[0];


debug('Init tabtab with %s cmd', cmd);

if (opts.help) {
console.log(commands.help());
process.exit(0);
} else if (opts.version) {
console.log(commands.help());
process.exit(0);
} else if (commands[cmd]) {
debug('Run command %s with options', cmd, opts);
commands[cmd](opts);
} else {
console.log(commands.help());
}
50 changes: 50 additions & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const debug = require('debug')('tabtab:commands');
const { join } = require('path');

const {
readFileSync: read,
existsSync: exists
} = require('fs');

class Commands {

get completion() {
return read(join(__dirname, '../../scripts/completion.sh'), 'utf8')
}

// Fow now, just output to the console
install() {
var script = this.completion;
console.log(this.completion);
}

uninstall() {}
search() {}
list() {}
add() {}
rm() {}

help() {
return `
$ tabtab <command> [options]
Options:
-h, --help Show this help output
-v, --version Show package version
-s, --silent Silent mode for commands like install
-y, --yes Skips confirmation prompts
Commands:
install Install and enable completion file on user system
uninstall Undo the install command
list List the completion files managed by tabtab
search Search npm registry for tabtab completion files / dictionaries
add Install additional completion files / dictionaries
rm/remove Uninstall completion file / dictionnary
`;
}
}


module.exports = new Commands();
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@



// ok so how about staring it
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"author": "mklabs",
"name": "tabtab",
"description": "tab completion helpers, for node cli programs. Inspired by npm completion.",
"bin": "bin/tabtab",
"main": "index.js",
"scripts": {
"test": "bake test",
"prepublish": "bake babel",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"devDependencies": {
Expand All @@ -30,5 +30,10 @@
"repository": {
"type": "git",
"url": "https://github.com/mklabs/node-tabtab.git"
},
"dependencies": {
"debug": "^2.2.0",
"minimist": "^1.2.0",
"npmlog": "^2.0.3"
}
}
49 changes: 25 additions & 24 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Latest released version (when it'll get released)
> wip
- Binary to manage and discover completion
- Chainable API
- No API
- Automatic completion from help output
- `tabtab install` in package.json install script creates the completion file on user system
- Automatic completion with package.json `completion|tabtab` property

Expand Down Expand Up @@ -111,15 +110,31 @@ This command lets you source the completion script to a particular place.
Defaults is to use `/etc/bash_completion.d` dir if it exists, and fallback to
~/.bashrc or ~/.zshrc files.

#### npm install script
### tabtab uninstall

$ tabtab uninstall foobar

Attemps to uninstall a previous tabtab install. `tabtab install` adds an entry
to an internal registry of completions, to be able to undo the operation on
uninstall.

### tabtab ...

- tabtab list
- tabtab search
- tabtab add
- tabtab rm/remove

Using npm's install script, you can automatically install completion for your
program whenever it gets globally installed.
## npm scripts

Using npm's install/uninstall script, you can automatically manage completion
for your program whenever it gets globally installed or removed.

```json
{
"scripts": {
"install": "tabtab install"
"install": "tabtab install",
"uninstall": "tabtab uninstall"
}
}
```
Expand All @@ -134,7 +149,8 @@ Ex.
"name": "foobar",
"bin": "bin/foobar",
"scripts": {
"install": "tabtab install"
"install": "tabtab install",
"uninstall": "tabtab uninstall"
},
"dependencies": {
"tabtab": "^1.0.0"
Expand All @@ -144,23 +160,8 @@ Ex.

It will writes the output of `tabtab completion --name foobar` to
`/etc/bash_completion.d/foobar` and enable completion for your program
automatically whenever a user install your package.

### tabtab uninstall

$ tabtab uninstall foobar

Attemps to uninstall a previous tabtab install. `tabtab install` adds an entry
to an internal registry of completions, to be able to undo the operation on
uninstall.

### tabtab ...

- tabtab list
- tabtab search
- tabtab add
- tabtab rm/remove

automatically whenever a user install your package, and undo the operation if
removed.

## API

Expand Down
50 changes: 50 additions & 0 deletions scripts/completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) npm, Inc. and Contributors
# All rights reserved.

###-begin-{pkgname}-completion-###
### credits to npm, this file is coming directly from isaacs/npm repo
#
# Just testing for now. (trying to learn this cool stuff)
#
# npm command completion script
#
# Installation: {completer} completion >> ~/.bashrc (or ~/.zshrc)
#

COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}
COMP_WORDBREAKS=${COMP_WORDBREAKS/@/}
export COMP_WORDBREAKS

if type complete &>/dev/null; then
_{pkgname}_completion () {
local si="$IFS"
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
{completer} completion -- "${COMP_WORDS[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
complete -F _{pkgname}_completion {pkgname}
elif type compctl &>/dev/null; then
_{pkgname}_completion () {
local cword line point words si
read -Ac words
read -cn cword
let cword-=1
read -l line
read -ln point
si="$IFS"
IFS=$'\n' reply=($(COMP_CWORD="$cword" \
COMP_LINE="$line" \
COMP_POINT="$point" \
{completer} completion -- "${words[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
# if the completer function returns on matches, default
# to filesystem matching
compctl -K _{pkgname}_completion + -f + {pkgname}
fi
###-end-{pkgname}-completion-###

0 comments on commit 0361905

Please sign in to comment.