Skip to content

Commit

Permalink
Allow for non bash profiles, rename/reformat files
Browse files Browse the repository at this point in the history
install.js now asks what your shell profile is called, defaulting
to .bash_profile. The profile scripts now set an environment var
that will be checked on install to prevent double adding to the
shell profile. The name of the shell profile is saved in a file in
~/.gotomark so it can be retrieved for uninstall.
  • Loading branch information
matthewess committed Jun 22, 2016
1 parent 38e51ba commit 5cbc7c9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 65 deletions.
5 changes: 3 additions & 2 deletions etc/bash_profile → etc/profile
Expand Up @@ -4,8 +4,9 @@
##### loads gotomark preferences if file exists #####
# #
if [ -f ~/.gotomark/profile.sh ]; then #
source ~/.gotomark/profile.sh #
fi #
source ~/.gotomark/profile.sh #
export GOTOMARK=INSTALLED #
fi #
# #
#####################################################
###### https://github.com/whtevn/gotomark ###########
Expand Down
29 changes: 29 additions & 0 deletions etc/profile_installer
@@ -0,0 +1,29 @@

################## added by gotomark ################
# #
###### attaches goto command to dogoto printer ######
# #
function __goto { #
local loc=`dogoto "$@"` #
if [ -d "$loc" ]; then #
cd $loc #
fi #
#
if [ -f "$loc" ]; then #
$EDITOR $loc #
fi #
} #
alias goto=__goto #
# #
########## bash completion for goto command #########
# #
function _goto { #
local cur=${COMP_WORDS[COMP_CWORD]} #
local opts=`dogoto -m` #
COMPREPLY=( $(compgen -W "$opts" -- $cur) ) #
} #
complete -F _goto goto #
# #
#####################################################
###### https://github.com/whtevn/gotomark ###########
#####################################################
29 changes: 0 additions & 29 deletions etc/profile_update

This file was deleted.

64 changes: 42 additions & 22 deletions install.js
@@ -1,35 +1,55 @@
#!/usr/bin/env node

var fs = require('fs');
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var homedir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var profile=homedir+"/.bash_profile";
var profile_update='./etc/bash_profile';
var installer='./etc/profile_update';
var markdir = homedir+"/.gotomark";
var uid, gid;
var profile_add='./etc/profile';
var installer='./etc/profile_installer';
var markdir = homedir+"/.gotomark";
var profile_loc = markdir + "/profile_loc"
var uid, gid, profile;

fs.mkdir(markdir, function(e){
copyFile(installer, markdir+"/profile.sh", function(){
fs.readFile(profile_update, {encoding: 'utf8'}, function(err, data){
fs.createWriteStream(profile, {'flags': 'a'}).write(data, 'utf8', function(err, data){
uid = process.env.SUDO_UID || process.getuid()
gid = process.env.SUDO_GID || process.getgid()
fs.chownSync(markdir, +(uid), +(gid));
fs.chownSync(markdir+"/profile.sh", +(uid), +(gid));
fs.exists(homedir+"/.marked_destinations", function(exists){
if(exists){
fs.rename(homedir+"/.marked_destinations", markdir+"/marked_destinations");
rl.question("What is your shell's profile called? (default: .bash_profile)\n", function(answer){
profile = answer || ".bash_profile";
profile = homedir+"/"+profile;
console.log("Using profile at " + profile);
fs.mkdir(markdir, function(e){
fs.createWriteStream(profile_loc, {'flags': 'a'}).write(profile, 'utf8', function(err, data){
if(err) throw(err);
copyFile(installer, markdir+"/profile.sh", function(){
fs.readFile(profile_add, {encoding: 'utf8'}, function(err, data){
if (process.env.GOTOMARK !== "INSTALLED") {
fs.createWriteStream(profile, {'flags': 'a'}).write(data, 'utf8', function(err, data){
uid = process.env.SUDO_UID || process.getuid()
gid = process.env.SUDO_GID || process.getgid()
fs.chownSync(markdir, +(uid), +(gid));
fs.chownSync(markdir+"/profile.sh", +(uid), +(gid));
fs.exists(homedir+"/.marked_destinations", function(exists){
if(exists){
fs.rename(homedir+"/.marked_destinations", markdir+"/marked_destinations");
}
});
if(err) throw(err);
console.log("\n#######################################\n#");
console.log("# Installation Success, I presume\n#");
console.log("# Now Run this command:");
console.log("# \tsource " + profile);
console.log("#\n#######################################\n");
});
} else {
console.log("\n#######################################\n#");
console.log("# Already installed!");
console.log("#\n#######################################\n");
}
});
if(err) throw(err);
console.log("\n#######################################\n#");
console.log("# Installation Success, I presume\n#");
console.log("# Now Run this command:");
console.log("# \tsource ~/.bash_profile");
console.log("#\n#######################################\n");
});
});
});
rl.close();
});

function copyFile(source, target, cb) {
Expand Down
30 changes: 18 additions & 12 deletions uninstall.js
Expand Up @@ -2,10 +2,10 @@

var fs = require('fs');
var homedir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var profile=homedir+"/.bash_profile";
var profile_update='./etc/bash_profile';
var installer='./etc/profile_update';
var markdir = homedir+"/.gotomark";
var profile_add='./etc/profile';
var installer='./etc/profile_installer';
var markdir = homedir+"/.gotomark";
var profile_loc=markdir+"/profile_loc"

/*
fs.exists(markdir, function (exists) {
Expand All @@ -18,15 +18,21 @@ fs.exists(markdir, function (exists) {
}
});
*/
fs.readFile(profile_loc, 'utf8', function (err, data) {
var profile = data;
console.log(data);
fs.readFile(profile, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}

fs.readFile(profile, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}

var result = data.replace(/(^\s*$\s)+#* added by gotomark #*$\s(^.*$\s)*^#*\shttps:.*gotomark\s#*$\s#*\s/mg, '');
fs.writeFile(profile, result, 'utf8', function (err) {
if (err) return console.log(err);
var result = data.replace(/(^\s*$\s)+#* added by gotomark #*$\s(^.*$\s)*^#*\shttps:.*gotomark\s#*$\s#*\s/mg, '');
fs.writeFile(profile, result, 'utf8', function (err) {
if (err) return console.log(err);
console.log("\n#######################################\n#");
console.log("# Please run \"export GOTOMARK=\" to reinstall in this shell");
console.log("#\n#######################################\n");
});
});
});
// http://stackoverflow.com/questions/14177087/replace-a-string-in-a-file-with-nodejs

0 comments on commit 5cbc7c9

Please sign in to comment.