-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
197 lines (177 loc) · 3.7 KB
/
util.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import fs = require('fs');
import path = require('path');
import child_process = require('child_process');
import globby = require('globby');
export const unaccessedFileMap = new Set<string>();
export function wait<T>(fn:(cb:(arg?:T)=>void)=>void):Promise<T>
{
return new Promise<T>((resolve, reject)=>fn((data)=>{
resolve(data);
}));
}
export function prom<T>(fn:(cb:(err:Error, arg?:T)=>void)=>void):Promise<T>
{
return new Promise<T>((resolve, reject)=>fn((err, data)=>{
if (err) reject(err);
else resolve(data);
}));
}
export function extFilter(ext:string):(str:string)=>boolean
{
ext = '.'+ext;
return function(file){
return file.endsWith(ext);
}
}
export function checkSame(output:string, input:string):boolean
{
try
{
const stat = fs.statSync(output);
var outsize = stat.size;
var outmtime = +stat.mtime;
}
catch(err)
{
if (err.code !== 'ENOENT')
throw err;
return false;
}
const istat = fs.statSync(input);
if (+istat.mtime != outmtime) return false;
if (istat.size != outsize) return false;
return true;
}
export function checkModified(output:string, inputs:string[]):boolean
{
try
{
var outmtime = +fs.statSync(output).mtime;
}
catch(err)
{
if (err.code !== 'ENOENT')
throw err;
return true;
}
for(var input of inputs)
{
var mtime = +fs.statSync(input).mtime;
if (mtime > outmtime) return true;
}
return false;
}
export function mkdir(dir:string):boolean
{
access(dir);
try
{
fs.mkdirSync(dir);
console.log("mkdir " + dir);
return true;
}
catch (err)
{
switch (err.code)
{
case 'EEXIST':
return false;
case 'ENOENT':
if (!mkdir(path.dirname(dir)))
throw err;
fs.mkdirSync(dir);
console.log("mkdir " + dir);
return true;
default: throw err;
}
}
}
export function copy(src:string, dest:string):void
{
access(dest);
if (checkSame(dest, src)) return;
console.log("copy " + src + " " + dest);
try
{
fs.copyFileSync(src, dest);
}
catch(err)
{
if (err.code === 'ENOENT')
{
mkdir(path.dirname(dest));
fs.copyFileSync(src, dest);
}
else
{
throw err;
}
}
}
export async function unaccess(dir:string|string[]):Promise<void>
{
const files = await globby(dir);
for(const file of files)
{
unaccessedFileMap.add(path.resolve(file));
}
}
export function access(file:string):void
{
file = path.resolve(file);
unaccessedFileMap.delete(file);
var idx = file.length;
while ((idx = file.lastIndexOf(path.sep, idx)) !== -1)
{
unaccessedFileMap.delete(file.substr(0, idx));
idx --;
}
}
export function accessChild(file:string):void
{
file = path.resolve(file);
unaccessedFileMap.delete(file);
var idx = file.length;
while ((idx = file.lastIndexOf(path.sep, idx)) !== -1)
{
unaccessedFileMap.delete(file.substr(0, idx));
idx --;
}
file += path.sep;
for (const item of unaccessedFileMap)
{
if (item.startsWith(file))
{
unaccessedFileMap.delete(item);
}
}
}
export function copyAll(src:string, dest:string, patterns:string[]|string):void
{
console.log("copyall " + src + " " + dest);
if (!(patterns instanceof Array)) patterns =[patterns];
for (var i = 0; i < patterns.length; i++)
{
var pattern = patterns[i];
if (pattern.charAt(0) === '!')
pattern = '!' + src + '/' + pattern.substr(1);
else
pattern = src + '/' + pattern;
patterns[i] = pattern;
}
const files = globby.sync(patterns);
for (let file of files)
{
if (!file.startsWith(src)) continue;
const destpath = dest + file.substr(src.length);
access(destpath);
const stat = fs.statSync(file);
if (stat.isDirectory()) continue;
copy(file, destpath);
}
}
export function copyAuto(from:string, to:string, pattern?:string[]|string):void
{
if (pattern) return copyAll(from ,to, pattern);
return copy(from, to);
}