-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
109 lines (93 loc) · 2.72 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Cross-shell robbyrussell theme written in JavaScript
*
* @author: Denys Dovhan, denysdovhan.com
* @license: MIT
* @link: https://github.com/denysdovhan/robbyrussell-node
*/
const path = require('path');
const colors = require('./utils/colors');
const git = require('./utils/git');
/**
* Paint arrow in success or failure colors depending on
* status code of the last executed command
* @param {object} config Configuration of prompt
* @return {string} Propmpt character with color
*/
function status(config) {
const { char, success, failure } = config.status;
return `${config.code === 0 ? success : failure}${char} `;
}
/**
* Current working directory
* @param {object} config Configuration of prompt
* @return {string} Formated string with current folder
*/
function directory(config) {
const { color } = config.dir;
const dir = path.basename(config.cwd);
return `${color}${dir === process.env.USER ? '~' : dir}`;
}
/**
* Synchronous method
* Get current branch and check if repo is dirty
* @param {object} config Configuration of prompt
* @return {string} Fromated string with git status
*/
function gitRepo(config) {
const { indicator, branch, dirty, dirtyChar } = config.git;
let gitStatus = '';
if (git.isGitSync(config.cwd)) {
gitStatus += ` ${indicator}git:(${branch}${git.branchSync()}${indicator})`;
if (git.dirtySync(config.cwd) || git.untrackedSync(config.cwd)) {
gitStatus += `${dirty} ${dirtyChar}`;
}
}
return gitStatus;
}
/**
* Asynchronous method
* Get current branch and check if repo is dirty
* @param {object} config Configuration of prompt
* @return {string} Formated string with git status
*/
async function gitRepoAsync(config) {
const { indicator, branch, dirty, dirtyChar } = config.git;
let gitStatus = '';
if (await git.isGit(config.cwd)) {
gitStatus += ` ${indicator}git:(${branch}${await git.branch()}${indicator})`;
if (await git.dirty(config.cwd) || await git.untracked(config.cwd)) {
gitStatus += `${dirty} ${dirtyChar}`;
}
}
return gitStatus;
}
/**
* Compose whole prompt
* @param {object} config Configuration of prompt
* @return {string}
*/
function prompt(config) {
const { open, close } = config.prompt;
return `${open}${status(config)}${directory(config)}${gitRepo(config)}${close} `;
}
/**
* Compose whole prompt
* @param {object} config Configuration of prompt
* @return {string}
*/
async function promptAsync(config) {
const { open, close } = config.prompt;
return `${open}${status(config)}${directory(config)}${await gitRepo(config)}${close} `;
}
/**
* Export all parts for prompt
*/
module.exports = {
status,
directory,
gitRepo,
gitRepoAsync,
prompt,
promptAsync,
};