-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashgen.js
108 lines (92 loc) · 2.67 KB
/
bashgen.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var config = require('./config.json');
var fs = require('fs');
// profile blocks
var aliases = '\n# Aliases\n';
var funcs = '# Helper functions\n';
var project_root = '';
var profile = '';
// helper functions
function addAlias(alias, cmd) {
aliases += 'alias ' + alias + '="' + cmd + '"\n';
}
function makeCd(path) {
return 'cd ' + path;
}
function makeProjectPath(project) {
return config['projects_root'] + '/' + project;
}
function addCd(alias, path) {
addAlias(alias, makeCd(path));
}
function addClass(pr) {
if ('div' in pr) {
var pFunc = 'cmd' + pr.alias + '() {\n' +
'\tif [ $# -eq 0 ]\n' +
'\t\tthen\n' +
'\t\t\t' + makeCd(makeProjectPath(pr.dir)) + ';\n' +
'\t\telse\n' +
'\t\t\t' + makeCd(makeProjectPath(pr.dir)) + '/' + pr.div + '$1;\n' +
'\tfi\n' +
'}\n\n';
funcs += pFunc;
addAlias(pr.alias, 'cmd' + pr.alias);
} else {
addCd(pr.alias, makeProjectPath(pr.dir));
}
}
function writeout() {
// write into bash_profile file
profile += funcs + aliases;
fs.open('bash_profile', 'w', function(err, fd) {
fs.write(fd, profile, function(err) {
if(err) {
return console.log(err);
}
console.log("The profile was saved!");
});
fs.close(fd, function(err){
if (err){
console.log(err);
}
});
});
}
// add shortcut to home directory
if ('home' in config) {
addCd(config.home, '~/');
}
// keep track of project root and add a shortcut to it
addCd('pro', config['projects_root']);
addCd('proj', config['projects_root']);
// add classes
for (var i = 0, len = config.classes.length; i < len; i++) {
addClass(config.classes[i]);
}
// add shortcuts to projects
for (var i = 0, len = config.projects.length; i < len; i++) {
addAlias(config.projects[i].alias, makeCd(makeProjectPath(config.projects[i].dir)));
}
// add other aliases
for (var i = 0, len = config.aliases.length; i < len; i++) {
addAlias(config.aliases[i].alias, config.aliases[i].cmd);
}
// add partials
var len = config.partials.length;
if (len > 0) {
var i = 0;
function partialCallback(err, data) {
if (err) {
return console.log(err);
}
profile = data + profile;
i++;
if (i < len) {
fs.readFile('./partials/' + config.partials[i], 'utf8', partialCallback);
} else {
writeout();
}
}
fs.readFile('./partials/' + config.partials[i], 'utf8', partialCallback);
} else {
writeout();
}