Skip to content

Commit

Permalink
wip install / uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Apr 23, 2016
1 parent 3f92281 commit 16cdf73
Showing 1 changed file with 47 additions and 24 deletions.
71 changes: 47 additions & 24 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { spawn, exec } from 'child_process';
// pkg-config --variable=completionsdir bash-completion
// pkg-config --variable=compatdir bash-completion
export default class Installer {
get home() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

constructor(options, complete) {
this.options = options || {};
debug('init', this.options);
Expand All @@ -36,46 +40,65 @@ export default class Installer {

if (destination === 'stdout') return process.stdout.write('\n\n' + script + '\n');

var out = this.createStream(destination);
}

createStream(destination, options) {
if (destination === 'bashrc') destination = path.join(this.home, '.bashrc');
else if (destination === 'zshrc') destination = path.join(this.home, '.zshrc');
else destination = path.join(destination, this.options.name);

return new Promise((r, errback) => {
debug('Check %s destination', destination);

var flags = 'a';
fs.stat(destination, (err, stat) => {
if (err && err.code === 'ENOENT') flags = 'w';
else if (err) return errback(err);
return new Promise(this.createStream.bind(this, destination))
.then(this.installCompletion.bind(this, destination))

debug('Installing completion script to %s directory', destination);
debug('Writing to %s file in %s mode', destination, flags === 'a' ? 'append' : 'write');
}

var out = fs.createWriteStream(destination, { flags });
createStream(destination, r, errback) {
debug('Check %s destination', destination);
var flags = 'a';
fs.stat(destination, (err, stat) => {
if (err && err.code === 'ENOENT') flags = 'w';
else if (err) return errback(err);

out.on('error', (err) => {
if (err.code === 'EACCES') {
return console.error(`
Error: You don't have permission to write to ${destination}. Try running with sudo instead:
var out = fs.createWriteStream(destination, { flags });
out.on('error', (err) => {
if (err.code === 'EACCES') {
console.error(`
Error: You don't have permission to write to ${destination}.
Try running with sudo instead:
sudo ${process.argv.join(' ')}
sudo ${process.argv.join(' ')}
`);
}
}

throw err;
});
return errback(err);
});

out.on('open', () => {
debug('Installing completion script to %s directory', destination);
debug('Writing to %s file in %s mode', destination, flags === 'a' ? 'append' : 'write');
r(out);
});
});
}

get home() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
installCompletion(destination, out) {
console.log( Object.keys(out) );
var script = this.complete.script(this.options.name, this.options.name);
var filename = path.join(__dirname, '../.completions', this.options.name);
debug('Writing actual completion script to', filename);
fs.writeFile(filename, script, (err) => {
if (err) return errback(err);
console.error('tabtab: Adding source line to load %s in %s', filename, destination);

// fs.readFile(
// if (new RegExp('tabtab source for ' + this.options.name, 'i').test()) {
//
// }

out.write('\n');
debug('. %s > %s', filename, destination);
out.write('\n# tabtab source for ' + this.options.name + ' package');
out.write('\n# uninstall by removing these lines or running `tabtab uninstall ' + this.options.name + '`');
out.write('\n. ' + filename);
});
}

// Prompts user for installation location.
Expand Down

0 comments on commit 16cdf73

Please sign in to comment.