-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (86 loc) · 3.13 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
#!/usr/bin/env node
const { terminal } = require('terminal-kit');
const os = require('os');
const fs = require('fs/promises');
const path = require('path');
const childProcess = require('child_process');
const open = require('open');
async function readDir(basePath) {
let isTerminalMode = false;
terminal.reset();
terminal.removeAllListeners('key');
terminal.on('key', (key) => {
function renderCommandInput() {
terminal.inputField((error, command) => {
if (!command) return renderCommandInput();
if (error) {
terminal.clear();
console.error(error);
return;
}
childProcess.exec(command, { cwd: basePath }, (error, stdout) => {
if (error) terminal.red(`\n ${error}`);
else terminal.green(`\n ${stdout}`);
renderCommandInput();
});
});
}
const { username } = os.userInfo();
if (key.toLowerCase() === 't') {
if (isTerminalMode) return;
terminal.clear();
terminal.green(`${username}'s terminal:`)
renderCommandInput();
isTerminalMode = !isTerminalMode;
}
if (key === 'CTRL_T') {
terminal.clear();
return readDir(basePath);
}
if (key === 'CTRL_C') {
terminal.clear();
terminal.green(`${username}, Thanks for using the FileExp Explorer!`);
return setTimeout(() => {
terminal.clear();
terminal.processExit();
}, 1000);
}
});
const files = await fs.readdir(basePath, { withFileTypes: true });
const statsPromise = files.map(direct => fs.stat(path.join(basePath, direct.name)));
const stats = await Promise.all(statsPromise);
const entries = files.length ? files.map((dirent, i) => {
const prefix = dirent.isDirectory() ? 'D' : 'F';
const stat = stats[i];
return `${prefix}-${dirent.name} ${!dirent.isDirectory() ? `${stat.size}B` : ''}`;
}) : [''] ;
terminal.green(`${basePath} \n`);
terminal.gridMenu(entries, { exitOnUnexpectedKey: true }, (err, response) => {
const excludedKeys = [
't',
'esc',
'ctrl_c',
];
if (response.unexpectedKey === 'BACKSPACE') {
const pathSegments = basePath.split(path.sep);
pathSegments.pop();
const newPath = pathSegments.join(path.sep);
return readDir(newPath);
}
if (response.unexpectedKey) {
const lowerKey = response.unexpectedKey.toLowerCase();
if (excludedKeys.includes(lowerKey)) return;
return readDir(basePath);
}
const dirent = files[response.selectedIndex];
const pathToDirent = path.join(basePath, dirent.name);
if (dirent.isDirectory()) {
return readDir(pathToDirent);
}
open(pathToDirent);
terminal.clear();
readDir(basePath);
});
}
const { homedir } = os.userInfo();
readDir(homedir);