-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmongonative.js
68 lines (60 loc) · 1.69 KB
/
mongonative.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
var cd = process.chdir;
var getHostName = os.hostname;
var hostname = os.hostname;
var pwd = process.cwd;
var _rand = Math.random;
var _isWindows = () => process.platform === 'win32';
function cat(filename, useBinaryMode) {
let contents = fs.readFileSync(filename, 'utf8');
if (!useBinaryMode && _isWindows()) {
contents = contents.replace(/(?<!\r)\n/g, '\r\n');
}
return contents;
}
function getMemInfo() {
return { resident: process.memoryUsage().rss,/* virtual: ? */ };
}
function isInteractive() {
const argv = process.argv.slice(2);
if (argv.includes('--shell')) return true;
const loadedFiles = argv.filter(arg => !arg.startsWith('-'));
if (argv.includes('--nodb')) {
return loadedFiles.length === 0;
} else {
return loadedFiles.length <= 1;
}
}
function listFiles(dir = '.') {
const files = fs.readdirSync(dir, { withFileTypes: true });
return files.map(dirent => {
const obj = { baseName: dirent.name, name: path.join(dir, dirent.name) };
obj.isDirectory = dirent.isDirectory();
if (dirent.isFile()) {
obj.size = fs.statSync(obj.name).size;
}
return obj;
});
}
function ls(dir) {
return listFiles(dir).map(file => file.name);
}
function md5sumFile(filename) {
return crypto.createHash('md5').update(fs.readFileSync(filename)).digest('hex');
}
function mkdir(path) {
const ret = fs.mkdirSync(path, { recursive: true });
if (ret === undefined) {
return { exists: true, created: false };
} else {
return { exists: true, created: true };
}
}
function removeFile(path) {
let existed = false;
try {
fs.statSync(path);
existed = true;
} catch {}
fs.rmSync(path, { recursive: true, force: true });
return existed;
}