FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TypeScript/scripts/build/utils.js at master · lindner/TypeScript · GitHub
lindner
/
TypeScript
Public
forked from
microsoft/TypeScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TypeScript
/
scripts
/
build
/
utils.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
435 lines (402 loc) · 14.3 KB
Breadcrumbs
TypeScript
/
scripts
/
build
/
utils.js
Copy path
File metadata and controls
435 lines (402 loc) · 14.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//
@ts
-check
/// <reference path="../types/ambient.d.ts" />
const
fs
=
require
(
"fs"
)
;
const
path
=
require
(
"path"
)
;
const
log
=
require
(
"fancy-log"
)
;
const
mkdirp
=
require
(
"mkdirp"
)
;
const
del
=
require
(
"del"
)
;
const
File
=
require
(
"vinyl"
)
;
const
ts
=
require
(
"../../lib/typescript"
)
;
const
{
default
:
chalk
}
=
require
(
"chalk"
)
;
const
{
spawn
}
=
require
(
"child_process"
)
;
const
{
CancellationToken
,
CancelError
,
Deferred
}
=
require
(
"prex"
)
;
const
{
Readable
,
Duplex
}
=
require
(
"stream"
)
;
const
isWindows
=
/
^
w
i
n
/
.
test
(
process
.
platform
)
;
/**
* Executes the provided command once with the supplied arguments.
*
@param
{
string
} cmd
*
@param
{
string[]
} args
*
@param
{
ExecOptions
} [options]
*
*
@typedef
ExecOptions
*
@property
{
boolean
} [ignoreExitCode]
*
@property
{
import("prex").CancellationToken
} [cancelToken]
*
@property
{
boolean
} [hidePrompt]
*/
function
exec
(
cmd
,
args
,
options
=
{
}
)
{
return
/**
@type
{
Promise<{exitCode: number}>
}*/
(
new
Promise
(
(
resolve
,
reject
)
=>
{
const
{
ignoreExitCode
,
cancelToken
=
CancellationToken
.
none
}
=
options
;
cancelToken
.
throwIfCancellationRequested
(
)
;
// TODO (weswig): Update child_process types to add windowsVerbatimArguments to the type definition
const
subshellFlag
=
isWindows
?
"/c"
:
"-c"
;
const
command
=
isWindows
?
[
possiblyQuote
(
cmd
)
,
...
args
]
:
[
`
${
cmd
}
${
args
.
join
(
" "
)
}
`
]
;
if
(
!
options
.
hidePrompt
)
log
(
`>
${
chalk
.
green
(
cmd
)
}
${
args
.
join
(
" "
)
}
`
)
;
const
proc
=
spawn
(
isWindows
?
"cmd"
:
"/bin/sh"
,
[
subshellFlag
,
...
command
]
,
{
stdio
:
"inherit"
,
windowsVerbatimArguments
:
true
}
)
;
const
registration
=
cancelToken
.
register
(
(
)
=>
{
log
(
`
${
chalk
.
red
(
"killing"
)
}
'
${
chalk
.
green
(
cmd
)
}
${
args
.
join
(
" "
)
}
'...`
)
;
proc
.
kill
(
"SIGINT"
)
;
proc
.
kill
(
"SIGTERM"
)
;
reject
(
new
CancelError
(
)
)
;
}
)
;
proc
.
on
(
"exit"
,
exitCode
=>
{
registration
.
unregister
(
)
;
if
(
exitCode
===
0
||
ignoreExitCode
)
{
resolve
(
{
exitCode
}
)
;
}
else
{
reject
(
new
Error
(
`Process exited with code:
${
exitCode
}
`
)
)
;
}
}
)
;
proc
.
on
(
"error"
,
error
=>
{
registration
.
unregister
(
)
;
reject
(
error
)
;
}
)
;
}
)
)
;
}
exports
.
exec
=
exec
;
/**
*
@param
{
string
} cmd
*/
function
possiblyQuote
(
cmd
)
{
return
cmd
.
indexOf
(
" "
)
>=
0
?
`"
${
cmd
}
"`
:
cmd
;
}
/**
*
@param
{
ts.Diagnostic[]
} diagnostics
*
@param
{
{ cwd?: string, pretty?: boolean }
} [options]
*/
function
formatDiagnostics
(
diagnostics
,
options
)
{
return
options
&&
options
.
pretty
?
ts
.
formatDiagnosticsWithColorAndContext
(
diagnostics
,
getFormatDiagnosticsHost
(
options
&&
options
.
cwd
)
)
:
ts
.
formatDiagnostics
(
diagnostics
,
getFormatDiagnosticsHost
(
options
&&
options
.
cwd
)
)
;
}
exports
.
formatDiagnostics
=
formatDiagnostics
;
/**
*
@param
{
ts.Diagnostic[]
} diagnostics
*
@param
{
{ cwd?: string }
} [options]
*/
function
reportDiagnostics
(
diagnostics
,
options
)
{
log
(
formatDiagnostics
(
diagnostics
,
{
cwd
:
options
&&
options
.
cwd
,
pretty
:
process
.
stdout
.
isTTY
}
)
)
;
}
exports
.
reportDiagnostics
=
reportDiagnostics
;
/**
*
@param
{
string | undefined
} cwd
*
@returns
{
ts.FormatDiagnosticsHost
}
*/
function
getFormatDiagnosticsHost
(
cwd
)
{
return
{
getCanonicalFileName
:
fileName
=>
fileName
,
getCurrentDirectory
:
(
)
=>
cwd
,
getNewLine
:
(
)
=>
ts
.
sys
.
newLine
,
}
;
}
exports
.
getFormatDiagnosticsHost
=
getFormatDiagnosticsHost
;
/**
* Reads JSON data with optional comments using the LKG TypeScript compiler
*
@param
{
string
} jsonPath
*/
function
readJson
(
jsonPath
)
{
const
jsonText
=
fs
.
readFileSync
(
jsonPath
,
"utf8"
)
;
const
result
=
ts
.
parseConfigFileTextToJson
(
jsonPath
,
jsonText
)
;
if
(
result
.
error
)
{
reportDiagnostics
(
[
result
.
error
]
)
;
throw
new
Error
(
"An error occurred during parse."
)
;
}
return
result
.
config
;
}
exports
.
readJson
=
readJson
;
/**
*
@param
{
File
} file
*/
function
streamFromFile
(
file
)
{
return
file
.
isBuffer
(
)
?
streamFromBuffer
(
file
.
contents
)
:
file
.
isStream
(
)
?
file
.
contents
:
fs
.
createReadStream
(
file
.
path
,
{
autoClose
:
true
}
)
;
}
exports
.
streamFromFile
=
streamFromFile
;
/**
*
@param
{
Buffer
} buffer
*/
function
streamFromBuffer
(
buffer
)
{
return
new
Readable
(
{
read
(
)
{
this
.
push
(
buffer
)
;
this
.
push
(
null
)
;
}
}
)
;
}
exports
.
streamFromBuffer
=
streamFromBuffer
;
/**
*
@param
{
string | string[]
} source
*
@param
{
string | string[]
} dest
*
@returns
{
boolean
}
*/
function
needsUpdate
(
source
,
dest
)
{
if
(
typeof
source
===
"string"
&&
typeof
dest
===
"string"
)
{
if
(
fs
.
existsSync
(
dest
)
)
{
const
{
mtime
:
outTime
}
=
fs
.
statSync
(
dest
)
;
const
{
mtime
:
inTime
}
=
fs
.
statSync
(
source
)
;
if
(
+
inTime
<=
+
outTime
)
{
return
false
;
}
}
}
else
if
(
typeof
source
===
"string"
&&
typeof
dest
!==
"string"
)
{
const
{
mtime
:
inTime
}
=
fs
.
statSync
(
source
)
;
for
(
const
filepath
of
dest
)
{
if
(
fs
.
existsSync
(
filepath
)
)
{
const
{
mtime
:
outTime
}
=
fs
.
statSync
(
filepath
)
;
if
(
+
inTime
>
+
outTime
)
{
return
true
;
}
}
else
{
return
true
;
}
}
return
false
;
}
else
if
(
typeof
source
!==
"string"
&&
typeof
dest
===
"string"
)
{
if
(
fs
.
existsSync
(
dest
)
)
{
const
{
mtime
:
outTime
}
=
fs
.
statSync
(
dest
)
;
for
(
const
filepath
of
source
)
{
if
(
fs
.
existsSync
(
filepath
)
)
{
const
{
mtime
:
inTime
}
=
fs
.
statSync
(
filepath
)
;
if
(
+
inTime
>
+
outTime
)
{
return
true
;
}
}
else
{
return
true
;
}
}
return
false
;
}
}
else
if
(
typeof
source
!==
"string"
&&
typeof
dest
!==
"string"
)
{
for
(
let
i
=
0
;
i
<
source
.
length
;
i
++
)
{
if
(
!
dest
[
i
]
)
{
continue
;
}
if
(
fs
.
existsSync
(
dest
[
i
]
)
)
{
const
{
mtime
:
outTime
}
=
fs
.
statSync
(
dest
[
i
]
)
;
const
{
mtime
:
inTime
}
=
fs
.
statSync
(
source
[
i
]
)
;
if
(
+
inTime
>
+
outTime
)
{
return
true
;
}
}
else
{
return
true
;
}
}
return
false
;
}
return
true
;
}
exports
.
needsUpdate
=
needsUpdate
;
function
getDiffTool
(
)
{
const
program
=
process
.
env
.
DIFF
;
if
(
!
program
)
{
log
.
warn
(
"Add the 'DIFF' environment variable to the path of the program you want to use."
)
;
process
.
exit
(
1
)
;
}
return
program
;
}
exports
.
getDiffTool
=
getDiffTool
;
/**
* Find the size of a directory recursively.
* Symbolic links can cause a loop.
*
@param
{
string
} root
*
@returns
{
number
} bytes
*/
function
getDirSize
(
root
)
{
const
stats
=
fs
.
lstatSync
(
root
)
;
if
(
!
stats
.
isDirectory
(
)
)
{
return
stats
.
size
;
}
return
fs
.
readdirSync
(
root
)
.
map
(
file
=>
getDirSize
(
path
.
join
(
root
,
file
)
)
)
.
reduce
(
(
acc
,
num
)
=>
acc
+
num
,
0
)
;
}
exports
.
getDirSize
=
getDirSize
;
/**
* Flattens a project with project references into a single project.
*
@param
{
string
} projectSpec The path to a tsconfig.json file or its containing directory.
*
@param
{
string
} flattenedProjectSpec The output path for the flattened tsconfig.json file.
*
@param
{
FlattenOptions
} [options] Options used to flatten a project hierarchy.
*
*
@typedef
FlattenOptions
*
@property
{
string
} [cwd] The path to use for the current working directory. Defaults to `process.cwd()`.
*
@property
{
import("../../lib/typescript").CompilerOptions
} [compilerOptions] Compiler option overrides.
*
@property
{
boolean
} [force] Forces creation of the output project.
*
@property
{
string[]
} [exclude] Files to exclude (relative to `cwd`)
*/
function
flatten
(
projectSpec
,
flattenedProjectSpec
,
options
=
{
}
)
{
const
cwd
=
normalizeSlashes
(
options
.
cwd
?
path
.
resolve
(
options
.
cwd
)
:
process
.
cwd
(
)
)
;
const
files
=
[
]
;
const
resolvedOutputSpec
=
path
.
resolve
(
cwd
,
flattenedProjectSpec
)
;
const
resolvedOutputDirectory
=
path
.
dirname
(
resolvedOutputSpec
)
;
const
resolvedProjectSpec
=
resolveProjectSpec
(
projectSpec
,
cwd
,
undefined
)
;
const
project
=
readJson
(
resolvedProjectSpec
)
;
const
skipProjects
=
/**
@type
{
Set<string>
}*/
(
new
Set
(
)
)
;
const
skipFiles
=
new
Set
(
options
&&
options
.
exclude
&&
options
.
exclude
.
map
(
file
=>
normalizeSlashes
(
path
.
resolve
(
cwd
,
file
)
)
)
)
;
recur
(
resolvedProjectSpec
,
project
)
;
if
(
options
.
force
||
needsUpdate
(
files
,
resolvedOutputSpec
)
)
{
const
config
=
{
extends
:
normalizeSlashes
(
path
.
relative
(
resolvedOutputDirectory
,
resolvedProjectSpec
)
)
,
compilerOptions
:
options
.
compilerOptions
||
{
}
,
files
:
files
.
map
(
file
=>
normalizeSlashes
(
path
.
relative
(
resolvedOutputDirectory
,
file
)
)
)
}
;
mkdirp
.
sync
(
resolvedOutputDirectory
)
;
fs
.
writeFileSync
(
resolvedOutputSpec
,
JSON
.
stringify
(
config
,
undefined
,
2
)
,
"utf8"
)
;
}
/**
*
@param
{
string
} projectSpec
*
@param
{
object
} project
*/
function
recur
(
projectSpec
,
project
)
{
if
(
skipProjects
.
has
(
projectSpec
)
)
return
;
skipProjects
.
add
(
project
)
;
if
(
project
.
references
)
{
for
(
const
ref
of
project
.
references
)
{
const
referencedSpec
=
resolveProjectSpec
(
ref
.
path
,
cwd
,
projectSpec
)
;
const
referencedProject
=
readJson
(
referencedSpec
)
;
recur
(
referencedSpec
,
referencedProject
)
;
}
}
if
(
project
.
include
)
{
throw
new
Error
(
"Flattened project may not have an 'include' list."
)
;
}
if
(
!
project
.
files
)
{
throw
new
Error
(
"Flattened project must have an explicit 'files' list."
)
;
}
const
projectDirectory
=
path
.
dirname
(
projectSpec
)
;
for
(
let
file
of
project
.
files
)
{
file
=
normalizeSlashes
(
path
.
resolve
(
projectDirectory
,
file
)
)
;
if
(
skipFiles
.
has
(
file
)
)
continue
;
skipFiles
.
add
(
file
)
;
files
.
push
(
file
)
;
}
}
}
exports
.
flatten
=
flatten
;
/**
*
@param
{
string
} file
*/
function
normalizeSlashes
(
file
)
{
return
file
.
replace
(
/
\\
/
g
,
"/"
)
;
}
/**
*
@param
{
string
} projectSpec
*
@param
{
string
} cwd
*
@param
{
string | undefined
} referrer
*
@returns
{
string
}
*/
function
resolveProjectSpec
(
projectSpec
,
cwd
,
referrer
)
{
let
projectPath
=
normalizeSlashes
(
path
.
resolve
(
cwd
,
referrer
?
path
.
dirname
(
referrer
)
:
""
,
projectSpec
)
)
;
const
stats
=
fs
.
statSync
(
projectPath
)
;
if
(
stats
.
isFile
(
)
)
return
normalizeSlashes
(
projectPath
)
;
return
normalizeSlashes
(
path
.
resolve
(
cwd
,
projectPath
,
"tsconfig.json"
)
)
;
}
/**
*
@param
{
string | ((file: File) => string) | { cwd?: string }
} [dest]
*
@param
{
{ cwd?: string }
} [opts]
*/
function
rm
(
dest
,
opts
)
{
if
(
dest
&&
typeof
dest
===
"object"
)
opts
=
dest
,
dest
=
undefined
;
let
failed
=
false
;
const
cwd
=
path
.
resolve
(
opts
&&
opts
.
cwd
||
process
.
cwd
(
)
)
;
/**
@type
{
{ file: File, deleted: boolean, promise: Promise<any>, cb: Function }[]
} */
const
pending
=
[
]
;
const
processDeleted
=
(
)
=>
{
if
(
failed
)
return
;
while
(
pending
.
length
&&
pending
[
0
]
.
deleted
)
{
const
{
file
,
cb
}
=
pending
.
shift
(
)
;
duplex
.
push
(
file
)
;
cb
(
)
;
}
}
;
const
duplex
=
new
Duplex
(
{
objectMode
:
true
,
/**
*
@param
{
string|Buffer|File
} file
*/
write
(
file
,
_
,
cb
)
{
if
(
failed
)
return
;
if
(
typeof
file
===
"string"
||
Buffer
.
isBuffer
(
file
)
)
return
cb
(
new
Error
(
"Only Vinyl files are supported."
)
)
;
const
basePath
=
typeof
dest
===
"string"
?
path
.
resolve
(
cwd
,
dest
)
:
typeof
dest
===
"function"
?
path
.
resolve
(
cwd
,
dest
(
file
)
)
:
file
.
base
;
const
filePath
=
path
.
resolve
(
basePath
,
file
.
relative
)
;
file
.
cwd
=
cwd
;
file
.
base
=
basePath
;
file
.
path
=
filePath
;
const
entry
=
{
file
,
deleted
:
false
,
cb
,
promise
:
del
(
file
.
path
)
.
then
(
(
)
=>
{
entry
.
deleted
=
true
;
processDeleted
(
)
;
}
,
err
=>
{
failed
=
true
;
pending
.
length
=
0
;
cb
(
err
)
;
}
)
}
;
pending
.
push
(
entry
)
;
}
,
final
(
cb
)
{
const
endThenCb
=
(
)
=>
(
duplex
.
push
(
null
)
,
cb
(
)
)
;
// signal end of read queue
processDeleted
(
)
;
if
(
pending
.
length
)
{
Promise
.
all
(
pending
.
map
(
entry
=>
entry
.
promise
)
)
.
then
(
(
)
=>
processDeleted
(
)
)
.
then
(
(
)
=>
endThenCb
(
)
,
endThenCb
)
;
return
;
}
endThenCb
(
)
;
}
,
read
(
)
{
}
}
)
;
return
duplex
;
}
exports
.
rm
=
rm
;
class
Debouncer
{
/**
*
@param
{
number
} timeout
*
@param
{
() => Promise<any>
} action
*/
constructor
(
timeout
,
action
)
{
this
.
_timeout
=
timeout
;
this
.
_action
=
action
;
}
enqueue
(
)
{
if
(
this
.
_timer
)
{
clearTimeout
(
this
.
_timer
)
;
this
.
_timer
=
undefined
;
}
if
(
!
this
.
_deferred
)
{
this
.
_deferred
=
new
Deferred
(
)
;
}
this
.
_timer
=
setTimeout
(
(
)
=>
this
.
run
(
)
,
100
)
;
return
this
.
_deferred
.
promise
;
}
run
(
)
{
if
(
this
.
_timer
)
{
clearTimeout
(
this
.
_timer
)
;
this
.
_timer
=
undefined
;
}
const
deferred
=
this
.
_deferred
;
this
.
_deferred
=
undefined
;
this
.
_projects
=
undefined
;
try
{
deferred
.
resolve
(
this
.
_action
(
)
)
;
}
catch
(
e
)
{
deferred
.
reject
(
e
)
;
}
}
}
exports
.
Debouncer
=
Debouncer
;
Back
|
FazBrowse Home
|
New Git URL