forked from hharnisc/hypercwd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setCwd.js
38 lines (33 loc) · 939 Bytes
/
setCwd.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
const { exec } = require('child_process');
const { promisify } = require('util');
const promiseExec = promisify(exec);
const setCwd = async ({ dispatch, action, tab }) => {
const newCwd = await promiseExec(
`lsof -p ${tab.pid} | grep cwd | tr -s ' ' | cut -d ' ' -f9-`);
const cwd = newCwd.trim();
dispatch({
type: 'SESSION_SET_CWD',
cwd,
});
};
// For Windows Support:
// the final excluded character () is an odd character
// that was added to the end of the line by posh-hg/posh-git
const directoryRegex = /([a-zA-Z]:[^\:\[\]\?\"\<\>\|]+)/mi;
const windowsSetCwd = ({ dispatch, action, tab, curTabId }) => {
const newCwd = directoryRegex.exec(action.data);
if (newCwd) {
const cwd = newCwd[0];
if (tab.cwd !== cwd && action.uid === curTabId) {
dispatch({
type: 'SESSION_SET_CWD',
cwd,
});
}
tab.cwd = cwd;
}
};
module.exports = {
setCwd,
windowsSetCwd,
}