-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·93 lines (86 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var inquirer = require('inquirer');
var program = require('commander');
var sh = require('shelljs');
var fs = require('fs');
var chalk = require('chalk');
var expander = require('expand-home-dir');
function getProfiles(fname) {
try {
return JSON.parse(fs.readFileSync(expander(fname)));
} catch (err) {
if (err.code === 'ENOENT') {
return null;
} else {
message = 'Error parsing ' + fname + '. Is is valid JSON? ' + err.message;
err.message = message;
console.log(chalk.red(message));
throw err;
}
}
}
function quoteForShell(value) {
return "'" + value.replace("'", "\\'") + "'";
}
function selectProfile() {
var profiles = getProfiles('~/.gitprofiles');
var profileNames = profiles === null ? [] : Object.keys(profiles);
if (profileNames.length === 0) {
console.log(chalk.yellow('~/.gitprofiles is empty or not found'));
return -1;
}
profileNames.push('exit');
return inquirer.prompt({
type: 'list',
message: 'Pick a git profile to switch to:',
name: 'profile',
choices: profileNames
}).then(function proc(answers) {
if (answers.profile === 'exit') {
return 0;
}
var picked = profiles[answers.profile];
var globalFlag = (answers.profile === 'global') ? '--global' : '';
Object.keys(picked).map(function(key) {
var value = picked[key];
sh.exec('git config --unset-all ' + key);
sh.exec('git config ' + globalFlag + ' ' + key + ' ' + quoteForShell(value));
});
sh.exec('git config -l ' + globalFlag);
return 0;
});
}
/*
function createProfile() {
var profiles = [];
try {
profiles = JSON.parse(fs.readFileSync(expander(fname)));
} catch (err) {
// ignore error
}
var profileList = Object.keys(profiles);
profileList.append('other');
var picked = null;
var questions = [{
type: 'list',
choices: profileList,
name: 'picked'
when: function when(answers) {
return 1 < profileList.length;
}
}, {
type: 'input',
name: 'profileName',
when: function when(answers) {
return ( profileList.length <= 1 ) ||
( answers.picked === 'other' );
}
}]
}
function updateProfile() {
}
function deleteProfile() {
}
*/
module.exports = {
selectProfile: selectProfile
};