From 5592a6b37304cc311bc24eb38cbf72e815b232b3 Mon Sep 17 00:00:00 2001 From: jkuri Date: Fri, 26 Feb 2016 09:08:29 +0100 Subject: [PATCH] feat(commands): Adds autocompletion for all commands and subcommands --- README.md | 23 +++++++++++ addon/ng2/commands/completion.js | 18 +++++++++ addon/ng2/index.js | 3 +- addon/ng2/utilities/completion.sh | 63 +++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 addon/ng2/commands/completion.js create mode 100644 addon/ng2/utilities/completion.sh diff --git a/README.md b/README.md index f12afd97cf34..e1db87493856 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/addon/ng2/commands/completion.js b/addon/ng2/commands/completion.js new file mode 100644 index 000000000000..bbddefbfafb9 --- /dev/null +++ b/addon/ng2/commands/completion.js @@ -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); + } +}); diff --git a/addon/ng2/index.js b/addon/ng2/index.js index 787b042c6a63..6a34562e106f 100644 --- a/addon/ng2/index.js +++ b/addon/ng2/index.js @@ -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') }; } }; diff --git a/addon/ng2/utilities/completion.sh b/addon/ng2/utilities/completion.sh new file mode 100644 index 000000000000..9237b64fa8ad --- /dev/null +++ b/addon/ng2/utilities/completion.sh @@ -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### \ No newline at end of file