Skip to content

feat(commands): Adds autocompletion for all commands and subcommands #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The generated project has dependencies that require **Node 4 or greater**.
* [Running End-to-End Tests](#running-end-to-end-tests)
* [Deploying the App via GitHub Pages](#deploying-the-app-via-github-pages)
* [Support for offline applications](#support-for-offline-applications)
* [Commands autocompletion](#commands-autocompletion)
* [Known Issues](#known-issues)

## Installation
Expand Down Expand Up @@ -202,6 +203,28 @@ cp node_modules/angular2-service-worker/dist/worker.js src/
Then, the commented snippet in `index.html` must be uncommented to register the worker script
as a service worker.

### Commands autocompletion

To turn on auto completion use the following commands:

For bash:
```bash
ng completion >> ~/.bashrc
source ~/.bashrc
```

For zsh:
```bash
ng completion >> ~/.zshrc
source ~/.zshrc
```

Windows users using gitbash:
```bash
ng completion >> ~/.bash_profile
source ~/.bash_profile
```

## Known issues

This project is currently a prototype so there are many known issues. Just to mention a few:
Expand Down
18 changes: 18 additions & 0 deletions addon/ng2/commands/completion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* jshint node: true */
'use strict';

var Command = require('ember-cli/lib/models/command');
var path = require('path');
var fs = require('fs');

module.exports = Command.extend({
name: 'completion',
description: 'Adds autocomplete functionality to `ng` commands and subcommands',
works: 'everywhere',
run: function() {
var scriptPath = path.resolve(__dirname, '..', 'utilities', 'completion.sh');
var scriptOutput = fs.readFileSync(scriptPath, 'utf8');

console.log(scriptOutput);
}
});
3 changes: 2 additions & 1 deletion addon/ng2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module.exports = {
'e2e' : require('./commands/e2e'),
'lint' : require('./commands/lint'),
'format' : require('./commands/format'),
'version' : require('./commands/version')
'version' : require('./commands/version'),
'completion': require('./commands/completion')
};
}
};
63 changes: 63 additions & 0 deletions addon/ng2/utilities/completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###-begin-ng-completion###
#
# ng command completion script
#
# Installation: ng completion >> ~/.bashrc (or ~/.zshrc)
#

ng_opts='new init build serve generate autocomplete e2e format lint test version'
init_opts='--dry-run --verbose --blueprint --skip-npm --skip-bower --name'
new_opts='--dry-run --verbose --blueprint --skip-npm --skip-bower --skip-git --directory'
build_opts='--environment --output-path --watch --watcher'
serve_opts='--port --host --proxy --insecure-proxy --watcher --live-reload --live-reload-host
--live-reload-port --environment --output-path --ssl --ssl-key --ssl-cert'
generate_opts='component directive pipe route service'
test_opts='--watch --browsers --colors --log-level --port --reporters'

if type complete &>/dev/null; then
_ng_completion() {
local cword pword opts

COMPREPLY=()
cword=${COMP_WORDS[COMP_CWORD]}
pword=${COMP_WORDS[COMP_CWORD - 1]}

case ${pword} in
ng) opts=$ng_opts ;;
i|init) opts=$init_opts ;;
new) opts=$new_opts ;;
b|build) opts=$build_opts ;;
s|serve|server) opts=$serve_opts ;;
g|generate) opts=$generate_opts ;;
test) opts=$test_opts ;;
esac

COMPREPLY=( $(compgen -W '${opts}' -- $cword) )

return 0
}
complete -o default -F _ng_completion ng
elif type compctl &>/dev/null; then
_ng_completion () {
local words cword opts
read -Ac words
read -cn cword
let cword-=1

case $words[cword] in
ng) opts=$ng_opts ;;
i|init) opts=$init_opts ;;
new) opts=$new_opts ;;
b|build) opts=$build_opts ;;
s|serve|server) opts=$serve_opts ;;
g|generate) opts=$generate_opts ;;
test) opts=$test_opts ;;
esac

setopt shwordsplit
reply=($opts)
unset shwordsplit
}
compctl -K _ng_completion ng
fi
###-end-ng-completion###