Skip to content

Commit

Permalink
feat(uninstall): Implement uninstall command and --auto flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed May 21, 2016
1 parent 8107a54 commit de37993
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
23 changes: 22 additions & 1 deletion lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class Commands {
this.options = options || {};
this.complete = new Complete(this.options);
this.installer = new Installer(this.options, this.complete);
this.shell = process.env.SHELL.split('/').slice(-1)[0];
}

// Commands
Expand Down Expand Up @@ -65,7 +66,27 @@ export default class Commands {
}

// Public: to be implemented.
uninstall() {}
uninstall(options) {
debug('Trigger uninstall command', options);
if (!options.auto) throw new Error('Uninstall only available with --auto flag');

var dest = this.shell === 'zsh' ? '~/.zshhrc' :
this.shell === 'bash' ? '~/.bashrc' :
'~/.config/fish/config.fish';

// win32 ?
dest = dest.replace('~', process.env.HOME);

debug('Destination:', dest);
this.installer.uninstallCompletion(dest)
.catch(e => {
throw e
})
.then(() => {
console.log('uninstall end');
});

}

// Public: to be implemented.
search() {}
Expand Down
52 changes: 47 additions & 5 deletions lib/installer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const debug = require('./debug')('tabtab:installer')


import fs from 'fs'
import path from 'path'
import fs from 'fs';
import path from 'path';
import inquirer from 'inquirer';
import { spawn, exec } from 'child_process';
import mkdirp from 'mkdirp';
Expand All @@ -26,6 +25,13 @@ export default class Installer {
constructor(options, complete) {
this.options = options || {};
this.complete = complete;

this._shell = process.env.SHELL.split('/').slice(-1)[0];
this.dest = this._shell === 'zsh' ? 'zshrc' :
this._shell === 'bash' ? 'bashrc' :
'fish';

this.dest = this.dest.replace('~', process.env.HOME);
}

// Called on install command.
Expand All @@ -40,8 +46,12 @@ export default class Installer {
});
});

return this.prompt()
.then(this.writeTo.bind(this));
if (options.auto) {
this.template = this._shell;
return this.writeTo({ destination: this.dest });
}

return this.prompt().then(this.writeTo.bind(this));
}

writeTo(data) {
Expand Down Expand Up @@ -129,6 +139,38 @@ export default class Installer {
});
}

uninstallCompletion(destination) {
return new Promise((r, errback) => {
fs.readFile(destination, 'utf8', (err, body) => {
if (err) return errback(err);
r(body);
});
})

.then((body) => {
var lines = body.split(/\r?\n/);

debug('Uninstall', this.options);
var name = this.options.name;
var reg = new RegExp('(tabtab source for ' + name + ' package|`tabtab uninstall ' + name + '`|node-tabtab/.completions/' + name + '.' + this.template + ')');
debug('reg', reg);
lines = lines.filter((line) => {
return !reg.test(line);
});

return lines.join('\n');
})

.then((content) => {
return new Promise((r, errback) => {
fs.writeFile(destination, content, (err) => {
if (err) return errback(err);
debug('%s sucesfully updated to remove tabtab', destination);
});
});
});
}

// Prompts user for installation location.
prompt() {
var choices = [{
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ should do:
![zsh](./docs/img/zsh-install.png)
![fish](./docs/img/fish-install.png)

#### tabtab install --auto

The `--auto` flag can be used to bypass prompts and use the SHELL configuration file options by default:

- bash: Will use `~/.bashrc`
- zsh: Will use `~/.zshrc`
- bash: Will use `~/.config/fish/config.fish`

This way, you can silently install / uninstall completion for a specific command without asking user to do so.

### tabtab uninstall --auto

The uninstall command can be used to undo what has been done by `tabtab install --auto` command.

### Completion description

> todo: zsh / fish offers the ability to define description with each
Expand Down

0 comments on commit de37993

Please sign in to comment.