-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-fix-case.js
executable file
·50 lines (37 loc) · 1.13 KB
/
git-fix-case.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const git = require('simple-git/promise');
const path = require('path');
const trueCasePathSync = require('true-case-path');
const zip = require('lodash.zip');
const SEPARATOR = path.sep;
function components(file) {
return file.split(new RegExp(`\\${SEPARATOR}`, 'g'));
}
function checkFile(file) {
const realPath = fs.realpathSync(file);
const trueCasePath = trueCasePathSync(realPath);
if (realPath !== trueCasePath) {
const a = components(trueCasePath);
const b = components(realPath);
const aCurrent = [];
const bCurrent = [];
zip(a, b).forEach(([aPart, bPart]) => {
aCurrent.push(aPart);
bCurrent.push(bPart);
if (aPart !== bPart) {
const aString = aCurrent.join(SEPARATOR);
const bString = bCurrent.join(SEPARATOR);
console.log(`Renaming ${aString} to ${bString}`);
fs.renameSync(aString, bString);
}
});
}
}
function checkFiles(files) {
files.forEach(checkFile);
}
git().raw(['ls-files'])
.then(response => checkFiles(response.split(/[\r\n]+/g)))
.catch(error => console.error(error));