-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
137 lines (122 loc) · 2.78 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
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
const { spawn } = require('child_process');
/**
* Returns correspondence map between English and Russian layout
* @param {'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32'} platform
* @return {Record<string, string>}
*/
const getLayout = (platform) => {
const isMac = platform == 'darwin';
const layout = {
й: 'q',
ц: 'w',
у: 'e',
к: 'r',
е: 't',
н: 'y',
г: 'u',
ш: 'i',
щ: 'o',
з: 'p',
х: '[',
Х: '{',
ъ: ']',
ї: ']',
Ъ: '}',
Ї: '}',
ф: 'a',
ы: 's',
і: 's',
в: 'd',
а: 'f',
п: 'g',
р: 'h',
о: 'j',
л: 'k',
д: 'l',
ж: ';',
Ж: ':',
э: "'",
є: "'",
Э: '"',
Є: '"',
ё: isMac ? '\\' : '`',
Ё: isMac ? '|' : '~',
я: 'z',
ч: 'x',
с: 'c',
м: 'v',
и: 'b',
т: 'n',
ь: 'm',
б: ',',
Б: '<',
ю: '.',
Ю: '>',
'.': isMac ? '&' : '/',
',': isMac ? '^' : '?',
'"': '@',
'№': '#',
';': isMac ? '*' : '$',
':': isMac ? '%' : '^',
'?': isMac ? '?' : '&',
'/': isMac ? '|' : '/',
'%': isMac ? '$' : '%',
']': '`',
'[': '~',
'<': isMac ? '±' : '<',
'>': isMac ? '§' : '>',
};
const fullLayout = Object.keys(layout).reduce((acc, key) => {
return {
...acc,
[key.toUpperCase()]: layout[key].toUpperCase(),
[key]: layout[key],
};
}, {});
return fullLayout;
};
/**
* Creates a function to convert strings from Russian to English
* @param {Record<string, string>} layout
* @returns {(str: string) => string}
*/
const createConverter = (layout) => (str) => str.replace(/./g, (ch) => layout[ch] || ch);
const firstString = (...args) => {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (typeof arg === 'string') {
return arg;
}
}
};
/**
* Parses arguments string
* @param {string} value
* @return {string[]} Args list
*/
const parseArgs = (value) => {
const regexp = /([^\s'"]([^\s'"]*(['"])([^\3]*?)\3)+[^\s'"]*)|[^\s'"]+|(['"])([^\5]*?)\5/gi;
const out = [];
let match;
while (match !== null) {
match = regexp.exec(value);
if (match !== null) {
// Index 1 in the array is the captured group if it exists
// Index 0 is the matched text, which we use if no captured group exists
out.push(firstString(match[1], match[6], match[0]));
}
}
return out;
};
/**
* Runs cli
*/
module.exports.run = () => {
const [, , ...args] = process.argv;
const joinedArgs = args.join(' ');
const convert = createConverter(getLayout(process.platform));
const convertedArgs = convert(joinedArgs);
const parsedArgs = parseArgs(convertedArgs);
console.log('$ git ' + convertedArgs + '\n');
spawn('git', parsedArgs, { stdio: 'inherit' });
};