Skip to content

Commit

Permalink
fix issue with automatic init
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Feb 10, 2017
1 parent b35e0a7 commit e7f0876
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 70 deletions.
3 changes: 1 addition & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ function cli(options) {
let searchTerm;

if (options.setup) {
require('./setup');
return Promise.resolve(null);
return require('./setup')();
}

if (_.isString(options.completions)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/getSourceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getSourceData(forceRefresh = false) {
fs.writeJSON(path.normalize(__dirname + '/../sources/.cache.json'), sourceData, null, 4);
fs.commit((err) => {
if (err) return reject(err);
return resolve({ repoDir: rootDir });
return resolve(sourceData);
});
});
});
Expand Down
140 changes: 73 additions & 67 deletions setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,88 @@ const path = require('path');
const inquirer = require('inquirer');
const os = require('os');

inquirer.prompt([
{
type: 'input',
name: 'alias',
message: 'Enter the bash command name you would like to use for bitcar:',
default: 'bit'
},
{
type: 'input',
name: 'rootDir',
message: 'Enter the root directory where bitcar should store your repos:',
default: path.normalize(process.env.HOME + '/bitcar-repos')
},
{
type: 'confirm',
name: 'addGithub',
message: 'Would you like to add github support?',
default: true
},
{
type: 'input',
name: 'githubUsernames',
message: 'Please type the github usernames for the repos you want bitcar to track (comma separated, no spaces):',
default: 'carsdotcom,machellerogden',
when: (answers) => answers.addGithub
},
{
type: 'confirm',
name: 'addBitbucketServer',
message: 'Would you like to add Bitbucket Server support?',
default: true
},
{
type: 'input',
name: 'bitbucketHost',
message: 'Please type the Bitbucket domain which contains repos you want bitcar to track:',
default: 'git.cars.com',
when: (answers) => answers.addBitbucketServer
}
]).then((answers) => {
const path = require('path');
module.exports = setup;

const profileContent = `
function setup() {
return inquirer.prompt([
{
type: 'input',
name: 'alias',
message: 'Enter the bash command name you would like to use for bitcar:',
default: 'bit'
},
{
type: 'input',
name: 'rootDir',
message: 'Enter the root directory where bitcar should store your repos:',
default: path.normalize(process.env.HOME + '/bitcar-repos')
},
{
type: 'confirm',
name: 'addGithub',
message: 'Would you like to add github support?',
default: true
},
{
type: 'input',
name: 'githubUsernames',
message: 'Please type the github usernames for the repos you want bitcar to track (comma separated, no spaces):',
default: 'carsdotcom,machellerogden',
when: (answers) => answers.addGithub
},
{
type: 'confirm',
name: 'addBitbucketServer',
message: 'Would you like to add Bitbucket Server support?',
default: true
},
{
type: 'input',
name: 'bitbucketHost',
message: 'Please type the Bitbucket domain which contains repos you want bitcar to track:',
default: 'git.cars.com',
when: (answers) => answers.addBitbucketServer
}
]).then((answers) => {
return new Promise((resolve, reject) => {
const path = require('path');

const profileContent = `
# begin bitcar
export BITCAR_ROOT_DIR='${answers.rootDir}'
source $HOME/.bitcar/cli.sh
source $HOME/.bitcar/completions.sh
# end bitcar
`
const configContent = {
sources: []
};

if (answers.addGithub) {
configContent.sources.push({ type: 'github', host: 'github.com', usernames: answers.githubUsernames.split(',') });
}
const configContent = {
sources: []
};

if (answers.addBitbucketServer) {
configContent.sources.push({ type: 'bitbucketServer', host: answers.bitbucketHost });
}
if (answers.addGithub) {
configContent.sources.push({ type: 'github', host: 'github.com', usernames: answers.githubUsernames.split(',') });
}

if (answers.addBitbucketServer) {
configContent.sources.push({ type: 'bitbucketServer', host: answers.bitbucketHost });
}

const mkdirp = require('mkdirp');
mkdirp.sync(path.normalize(process.env.HOME + '/.bitcar'));
fs.writeJSON(path.normalize(process.env.HOME + '/.bitcar/config'), configContent, null, 4);
fs.copyTpl(path.normalize(__dirname + '/dotfiles/cli.sh'), path.normalize(process.env.HOME + '/.bitcar/cli.sh'), answers);
fs.copyTpl(path.normalize(__dirname + '/dotfiles/completions.sh'), path.normalize(process.env.HOME + '/.bitcar/completions.sh'), answers);
fs.copy(path.normalize(__dirname + '/dotfiles/strip_codes'), path.normalize(process.env.HOME + '/.bitcar/strip_codes'));
fs.copy(path.normalize(process.env.HOME + '/.bash_profile'), path.normalize(process.env.HOME + '/.bash_profile.bkup'));
const bashProfile = fs.read(path.normalize(process.env.HOME + '/.bash_profile'));
let cleanedProfile = bashProfile.replace(/\n# begin bitcar[\s\S]+# end bitcar\n/gm, '');
cleanedProfile = cleanedProfile + os.EOL + profileContent;
fs.write(path.normalize(process.env.HOME + '/.bash_profile'), cleanedProfile);
const mkdirp = require('mkdirp');
mkdirp.sync(path.normalize(process.env.HOME + '/.bitcar'));
fs.writeJSON(path.normalize(process.env.HOME + '/.bitcar/config'), configContent, null, 4);
fs.copyTpl(path.normalize(__dirname + '/dotfiles/cli.sh'), path.normalize(process.env.HOME + '/.bitcar/cli.sh'), answers);
fs.copyTpl(path.normalize(__dirname + '/dotfiles/completions.sh'), path.normalize(process.env.HOME + '/.bitcar/completions.sh'), answers);
fs.copy(path.normalize(__dirname + '/dotfiles/strip_codes'), path.normalize(process.env.HOME + '/.bitcar/strip_codes'));
fs.copy(path.normalize(process.env.HOME + '/.bash_profile'), path.normalize(process.env.HOME + '/.bash_profile.bkup'));
const bashProfile = fs.read(path.normalize(process.env.HOME + '/.bash_profile'));
let cleanedProfile = bashProfile.replace(/\n# begin bitcar[\s\S]+# end bitcar\n/gm, '');
cleanedProfile = cleanedProfile + os.EOL + profileContent;
fs.write(path.normalize(process.env.HOME + '/.bash_profile'), cleanedProfile);

fs.commit(function (err) {
if (err) return console.error(err)
console.log("success!")
return fs.commit(function (err) {
if (err) return reject(err)
console.log('bitcar setup was successful');
return resolve();
});
});
});
});
}

0 comments on commit e7f0876

Please sign in to comment.