forked from karabaja4/vscode-explorer-git-status
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.js
More file actions
177 lines (146 loc) · 8.16 KB
/
dev.js
File metadata and controls
177 lines (146 loc) · 8.16 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
function injectGitFileStatus()
{
const timeout = 5000;
const addedColor = "limegreen";
const modifiedColor = "darkorange";
const ignoredOpacity = "0.4";
const explorer = document.getElementById("workbench.view.explorer");
if (explorer)
{
const foldersView = explorer.getElementsByClassName("explorer-folders-view")[0];
if (foldersView)
{
const tree = foldersView.getElementsByClassName("monaco-tree")[0];
if (tree)
{
const firstRow = tree.getElementsByClassName("monaco-tree-row")[0];
if (firstRow)
{
const explorerItem = firstRow.getElementsByClassName("explorer-item")[0];
if (explorerItem)
{
const path = require("path");
const exec = require("child_process").exec;
const style = document.createElement("style");
document.body.appendChild(style);
const resolveHome = (filepath) =>
{
if (filepath[0] === "~")
{
return path.join(process.env.HOME, filepath.slice(1));
}
return filepath;
}
const unresolveHome = (filepath) =>
{
const home = process.env.HOME;
if (home && filepath.startsWith(home))
{
const regex = new RegExp(`^${home}`);
return filepath.replace(regex, "~");
}
else
{
return filepath;
}
}
const normalizePath = (name) =>
{
return path.normalize(name.substr(3)).replace(/\\+$/, "").replace(/\/+$/, "").replace(/\\/g, "\\\\");
};
const getAllSubdirectories = (name) =>
{
let i = 1;
const paths = [];
const sep = path.sep.replace(/\\/g, "\\\\");
while (i = name.indexOf(sep, i) + 1)
{
paths.push(name.substr(0, i - 1));
}
return paths;
};
const classPath = "#workbench\\.view\\.explorer .explorer-folders-view .monaco-tree .monaco-tree-rows .monaco-tree-row .explorer-item";
const getCssEntry = (root, file, cssEntry) =>
{
const filepath = unresolveHome(path.join(root, file).replace(/\\/g, "\\\\"));
return `${classPath}[title="${filepath}" i]{${cssEntry}}`;
}
const firstFileDir = path.normalize(path.dirname(explorerItem.getAttribute("title")));
const gitRootCommand = "git rev-parse --show-toplevel";
const gitRootOptions = { cwd: resolveHome(firstFileDir) };
const gitRootCallback = (error, stdout, stderr) =>
{
if (!error)
{
const gitRoot = stdout.trim();
const startGitStatusChecks = () =>
{
// run git status
const gitStatusCommand = "git status --short --ignored";
const gitStatusOptions = { cwd: resolveHome(gitRoot) }
const gitStatusCallback = (error, stdout, stderr) =>
{
if (!error)
{
const files = stdout.split("\n");
const added = files.filter(name => { return name.startsWith("?? "); }).map(name => { return normalizePath(name); });
const modified = files.filter(name => { return name.startsWith(" M "); }).map(name => { return normalizePath(name); });
const ignored = files.filter(name => { return name.startsWith("!! "); }).map(name => { return normalizePath(name); });
let html = "";
const addedFolders = new Set();
const modifiedFolders = new Set();
// files
added.forEach(addedFile =>
{
const subdirectories = getAllSubdirectories(addedFile);
subdirectories.forEach(subdirectory =>
{
addedFolders.add(subdirectory);
});
html += getCssEntry(gitRoot, addedFile, `color:${addedColor};`);
});
modified.forEach(modifiedFile =>
{
const subdirectories = getAllSubdirectories(modifiedFile);
subdirectories.forEach(subdirectory =>
{
modifiedFolders.add(subdirectory);
});
html += getCssEntry(gitRoot, modifiedFile, `color:${modifiedColor};`);
});
ignored.forEach(ignoredFile =>
{
html += getCssEntry(gitRoot, ignoredFile, `opacity:${ignoredOpacity};`);
});
// folders
addedFolders.forEach((addedFolder) =>
{
html += getCssEntry(gitRoot, addedFolder, `color:${addedColor};`);
});
modifiedFolders.forEach((modifiedFolder) =>
{
html += getCssEntry(gitRoot, modifiedFolder, `color:${modifiedColor};`);
});
if (style.innerHTML !== html)
{
style.innerHTML = html;
}
}
setTimeout(startGitStatusChecks, timeout);
}
exec(gitStatusCommand, gitStatusOptions, gitStatusCallback);
}
startGitStatusChecks();
}
}
exec(gitRootCommand, gitRootOptions, gitRootCallback);
// loaded
return;
};
}
}
}
}
setTimeout(injectGitFileStatus, timeout);
}
injectGitFileStatus();