-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
110 lines (96 loc) · 2.01 KB
/
utils.ts
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
import * as path from 'path';
import { execSync } from 'child_process';
const tools = '/usr/bin/nspire-tools';
const DIR_RW = 0o40755;
const FILE_RW = 0o100644;
const DIR_SIZE = 4096;
var errMsg = '';
export function error() {
if (process.getuid() != 0) {
errMsg = 'Error.\nPlease check USB connection or run this program as root.\n';
} else {
errMsg = 'Error.\nPlease check USB connection.\n';
}
}
export function clearError() {
errMsg = '';
}
export function getError() {
return errMsg;
}
export function exec(...command: string[]): { exit: number, stdout: string } {
var p: Buffer;
try {
p = execSync(tools + ' ' + command.map(x => x.replace(/ /g, '\\ ')).join(' '));
}
catch (e) {
return {
exit: e.status,
stdout: e.stdout.toString()
};
}
return {
exit: 0,
stdout: p.toString()
}
}
export function splitPath(p: string): string[] {
return path.resolve(p).split('/').filter(x => x);
}
export function joinPath(p: string[]): string {
return '/' + p.join('/');
}
export interface Stat {
mtime: Date,
atime: Date,
ctime: Date,
size: number,
mode: number,
uid: number,
gid: number
}
export function createStat(date: Date | string, size: number, dir=false): Stat {
const d = typeof date === 'string' ? new Date(date) : date;
const stat = {
mtime: d,
atime: d,
ctime: d,
size: dir ? DIR_SIZE : size,
mode: dir ? DIR_RW : FILE_RW,
uid: process.getuid(),
gid: process.getgid()
};
return stat;
}
export interface Cache {
list?: { [f: string]: Cache },
stat?: Stat,
exp?: number,
lexp?: number
}
const cache: Cache = {
stat: createStat(new Date(), DIR_SIZE, true),
list: {}
};
export function getLocalCache(path: string, add=true): Cache | undefined {
var e = true;
var localCache = cache;
splitPath(path).forEach(f => {
if (!e || !localCache.list) {
e = false;
return;
}
if (!localCache.list[f]) {
if (add) {
localCache.list[f] = { list: {} };
} else {
e = false;
return;
}
}
localCache = localCache.list[f];
});
if (e) {
return localCache;
}
}