-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.mjs
165 lines (136 loc) · 3.95 KB
/
utils.mjs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
export function readAllContent(filename) {
if('fs' in globalThis) {
try {
return fs.readFileSync(filename, 'utf8');
} catch {
}
}
if('std' in globalThis) {
let f = std.open(filename, 'r');
if(f) {
let content = f.readAsString();
f.close();
return content;
}
}
}
export function basename(path) {
return path.split(/[\\/]/).pop();
}
export function related_path(pathA, pathB) {
pathA = pathA.split('/')
pathB = pathB.split('/')
let ret = [];
let i;
for(i=0; i<pathA.length && i<pathB.length && pathA[i]==pathB[i]; i++);
let j = i;
for(;i<pathB.length && pathB[i]!='';i++)
ret.push('..');
for(;j<pathA.length && pathA[i]!=''; j++)
ret.push(pathA[j]);
return ret.length==0?'.':ret.join('/');
}
export function simplify_path(path) {
let dpath = path.split('/');
let lst = [];
for(let entry of dpath) {
if(entry === '..' && lst.length && lst[lst.length-1]!='..' && lst[lst.length-1]!='') {
lst.pop();
} else if(entry === '.') {
// ignore
} else {
if(entry==='' && lst.length)
continue;
if(entry==='..' && lst.length && lst[lst.length-1]==='')
continue;
lst.push(entry);
}
}
if(lst.length) {
if(lst.length==1 && lst[0]==='')
return '/';
else
return lst.join('/');
}
// related path
return '.';
}
export function dirname(str) {
let ret = str.substring(0, str.lastIndexOf("/"));
return ret===''? '.': ret;
}
export async function polyfill() {
let fs = await import('fs').catch(() => {})
if(fs) {
// node
globalThis.fs = fs;
} else {
// qjs
globalThis.os = await import('os').catch(() => {})
globalThis.std = await import('std').catch(() => {})
}
if(!('setTimeout' in globalThis)) {
globalThis.setTimeout = os.setTimeout;
}
if(!('clearTimeout' in globalThis)) {
globalThis.clearTimeout = os.clearTimeout;
}
if(!('process' in globalThis)) {
globalThis.process = {};
process.cwd = function() { return os.getcwd()[0]; }
}
if(!process.argv && 'scriptArgs' in globalThis)
process.argv = ['qjs', ...scriptArgs];
if(!('info' in console))
console.info = console.log
}
export function polyfill2() {
if(!('setTimeout' in globalThis)) {
globalThis.setTimeout = os.setTimeout;
}
if(!('clearTimeout' in globalThis)) {
globalThis.clearTimeout = os.clearTimeout;
}
if(!('process' in globalThis)) {
globalThis.process = {};
process.cwd = function() { return os.getcwd()[0]; }
}
if(!process.argv && scriptArgs)
process.argv = ['qjs', ...scriptArgs];
}
export function fileExist(file) {
if('fs' in globalThis) {
return fs.existsSync(file);
}
if('os' in globalThis) {
return os.stat(file)[1] == 0;
}
}
export function compareMTime(afile, bfile) {
if('fs' in globalThis) {
let astat = fs.statSync(afile);
let bstat = fs.statSync(bfile);
return astat.mtimeMs < bstat.mtimeMs;
}
let [astat, _] = os.stat(afile);
let [bstat, __] = os.stat(bfile);
return astat.mtime < bstat.mtime;
}
export function writeFile(filename, content) {
if('fs' in globalThis) {
fs.writeFileSync(filename, content);
return;
}
let f = std.open(filename, 'w');
f.puts(content);
f.close();
}
export function getMethods(obj) {
return Object.getOwnPropertyNames(obj).filter(item => typeof obj[item] === 'function');
}
export function getClasses(obj) {
return Object.getOwnPropertyNames(obj).filter(item => typeof obj[item] === 'function' && obj[item].prototype);
}
export function appendSet(set, list) {
list.forEach(set.add, set);
}