Skip to content

Commit 60754f2

Browse files
committed
feat(commands): Adds autocompletion for all commands and subcommands
1 parent 3d2989a commit 60754f2

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The generated project has dependencies that require **Node 4 or greater**.
3232
* [Running End-to-End Tests](#running-end-to-end-tests)
3333
* [Deploying the App via GitHub Pages](#deploying-the-app-via-github-pages)
3434
* [Support for offline applications](#support-for-offline-applications)
35+
* [Commands autocompletion](#commands-autocompletion)
3536
* [Known Issues](#known-issues)
3637

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

206+
### Commands autocompletion
207+
208+
To turn on auto completion use the following commands:
209+
210+
For bash:
211+
```bash
212+
ng completion >> ~/.bashrc
213+
source ~/.bashrc
214+
```
215+
216+
For zsh:
217+
```bash
218+
ng completion >> ~/.zshrc
219+
source ~/.zshrc
220+
```
221+
222+
Windows users using gitbash:
223+
```bash
224+
ng completion >> ~/.bash_profile
225+
source ~/.bash_profile
226+
```
227+
205228
## Known issues
206229

207230
This project is currently a prototype so there are many known issues. Just to mention a few:

addon/ng2/commands/completion.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* jshint node: true */
2+
'use strict';
3+
4+
var Command = require('ember-cli/lib/models/command');
5+
var path = require('path');
6+
var fs = require('fs');
7+
8+
module.exports = Command.extend({
9+
name: 'completion',
10+
description: 'Adds autocomplete functionallity to `ng` commands and subcommands',
11+
works: 'everywhere',
12+
run: function() {
13+
var scriptPath = path.resolve(__dirname, '..', 'utilities', 'completion.sh');
14+
var scriptOutput = fs.readFileSync(scriptPath, 'utf8');
15+
16+
console.log(scriptOutput);
17+
}
18+
});

addon/ng2/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = {
1111
'e2e' : require('./commands/e2e'),
1212
'lint' : require('./commands/lint'),
1313
'format' : require('./commands/format'),
14-
'version' : require('./commands/version')
14+
'version' : require('./commands/version'),
15+
'completion': require('./commands/completion')
1516
};
1617
}
1718
};

addon/ng2/utilities/completion.sh

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###-begin-ng-completion###
2+
#
3+
# ng command completion script
4+
#
5+
# Installation: ng completion >> ~/.bashrc (or ~/.zshrc)
6+
#
7+
8+
ng_opts='new init build serve generate autocomplete e2e format lint test version'
9+
init_opts='--dry-run --verbose --blueprint --skip-npm --skip-bower --name'
10+
new_opts='--dry-run --verbose --blueprint --skip-npm --skip-bower --skip-git --directory'
11+
build_opts='--environment --output-path --watch --watcher'
12+
serve_opts='--port --host --proxy --insecure-proxy --watcher --live-reload --live-reload-host
13+
--live-reload-port --environment --output-path --ssl --ssl-key --ssl-cert'
14+
generate_opts='component directive pipe route service'
15+
test_opts='--watch --browsers --colors --log-level --port --reporters'
16+
17+
if type complete &>/dev/null; then
18+
_ng_completion() {
19+
local cword pword opts
20+
21+
COMPREPLY=()
22+
cword=${COMP_WORDS[COMP_CWORD]}
23+
pword=${COMP_WORDS[COMP_CWORD - 1]}
24+
25+
case ${pword} in
26+
ng) opts=$ng_opts ;;
27+
i|init) opts=$init_opts ;;
28+
new) opts=$new_opts ;;
29+
b|build) opts=$build_opts ;;
30+
s|serve|server) opts=$serve_opts ;;
31+
g|generate) opts=$generate_opts ;;
32+
test) opts=$test_opts ;;
33+
esac
34+
35+
COMPREPLY=( $(compgen -W '${opts}' -- $cword) )
36+
37+
return 0
38+
}
39+
complete -o default -F _ng_completion ng
40+
elif type compctl &>/dev/null; then
41+
_ng_completion () {
42+
local words cword opts
43+
read -Ac words
44+
read -cn cword
45+
let cword-=1
46+
47+
case $words[cword] in
48+
ng) opts=$ng_opts ;;
49+
i|init) opts=$init_opts ;;
50+
new) opts=$new_opts ;;
51+
b|build) opts=$build_opts ;;
52+
s|serve|server) opts=$serve_opts ;;
53+
g|generate) opts=$generate_opts ;;
54+
test) opts=$test_opts ;;
55+
esac
56+
57+
setopt shwordsplit
58+
reply=($opts)
59+
unset shwordsplit
60+
}
61+
compctl -K _ng_completion ng
62+
fi
63+
###-end-ng-completion###

0 commit comments

Comments
 (0)