forked from thaibault/weboptimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·690 lines (636 loc) · 27.4 KB
/
index.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#!/usr/bin/env babel-node
// -*- coding: utf-8 -*-
/** @module weboptimizer */
'use strict'
/* !
region header
[Project page](https://torben.website/webOptimizer)
Copyright Torben Sickert (info["~at~"]torben.website) 16.12.2012
License
-------
This library written by Torben Sickert stand under a creative commons
naming 3.0 unported license.
See https://creativecommons.org/licenses/by/3.0/deed.de
endregion
*/
// region imports
import {
ChildProcess,
CommonSpawnOptions,
exec as execChildProcess,
ExecOptions,
spawn as spawnChildProcess
} from 'child_process'
import Tools, {CloseEventNames} from 'clientnode'
import {
AnyFunction,
EvaluationResult,
File,
Mapping,
PlainObject,
ProcedureFunction,
ProcessCloseCallback,
ProcessCloseReason,
ProcessError,
ProcessErrorCallback,
ProcessHandler
} from 'clientnode/type'
import {chmodSync, unlinkSync} from 'fs'
import {writeFile, unlink} from 'fs/promises'
import {sync as globSync} from 'glob-all'
import path, {join, resolve} from 'path'
import removeDirectoryRecursively, {
sync as removeDirectoryRecursivelySync
} from 'rimraf'
import {load as loadConfiguration} from './configurator'
import Helper from './helper'
import {
Command,
CommandLineArguments,
GivenInjection,
ResolvedBuildConfiguration,
ResolvedBuildConfigurationItem,
ResolvedConfiguration
} from './type'
// endregion
// NOTE: Environment variables can only be strings.
process.env.UV_THREADPOOL_SIZE = '128'
/**
* Main entry point.
* @param context - Location from where to build current application.
* @param currentWorkingDirectory - Current working directory to use as
* reference.
* @param commandLineArguments - Arguments to take into account.
* @param webOptimizerPath - Current optimizer context path.
* @param environment - Environment variables to take into account.
*
* @returns Nothing.
*/
const main = async (
context?:string,
currentWorkingDirectory:string = process.cwd(),
commandLineArguments:Array<string> = process.argv,
webOptimizerPath:string = __dirname,
/*
NOTE: We have to avoid that some pre-processor removes this
assignment.
*/
// eslint-disable-next-line no-eval
environment:NodeJS.ProcessEnv = eval('process.env') as NodeJS.ProcessEnv
):Promise<() => void> => {
const configuration:ResolvedConfiguration = loadConfiguration(
context,
currentWorkingDirectory,
commandLineArguments,
webOptimizerPath,
environment
)
let clear:() => void = Tools.noop() as () => void
try {
// region controller
const processOptions:ExecOptions = {
cwd: configuration.path.context,
env: environment
}
const childProcessOptions:CommonSpawnOptions = {
shell: true,
stdio: 'inherit',
...processOptions
}
const childProcesses:Array<ChildProcess> = []
const processPromises:Array<Promise<ProcessCloseReason>> = []
const possibleArguments:Array<string> = [
'build',
'build:types',
'clear',
'document',
'lint',
'preinstall',
'serve',
'test',
'test:browser',
'test:coverage',
'test:coverage:report',
'check:types'
]
const closeEventHandlers:Array<AnyFunction> = []
if (configuration.givenCommandLineArguments.length > 2) {
// region temporary save dynamically given configurations
// NOTE: We need a copy of given arguments array.
const dynamicConfiguration:PlainObject = {
givenCommandLineArguments:
configuration.givenCommandLineArguments.slice()
}
if (
configuration.givenCommandLineArguments.length > 3 &&
Tools.stringParseEncodedObject(
configuration.givenCommandLineArguments[
configuration.givenCommandLineArguments.length - 1
],
configuration as unknown as Mapping<unknown>,
'configuration'
)
)
configuration.givenCommandLineArguments.pop()
let count = 0
let filePath:string = resolve(
configuration.path.context,
`.dynamicConfiguration-${count}.json`
)
while (true) {
filePath = resolve(
configuration.path.context,
`.dynamicConfiguration-${count}.json`
)
if (!(await Tools.isFile(filePath)))
break
count += 1
}
await writeFile(filePath, JSON.stringify(dynamicConfiguration))
const additionalArguments:Array<string> =
commandLineArguments.splice(3)
/// region register exit handler to tidy up
clear = (error?:Error):void => {
// NOTE: Close handler have to be synchronous.
if (Tools.isFileSync(filePath))
unlinkSync(filePath)
if (error)
throw error
}
closeEventHandlers.push(clear)
/// endregion
// endregion
// region handle clear
/*
NOTE: Some tasks could depend on previously created artefacts
packages so a preceding clear should not be performed in that
cases.
NOTE: If we have a dependency cycle we need to preserve files
during pre-install phase.
*/
if (
![
'build',
'build:types',
'preinstall',
'serve',
'test',
'test:browser',
'test:coverage',
'test:coverage:report'
].includes(configuration.givenCommandLineArguments[2]) &&
possibleArguments.includes(
configuration.givenCommandLineArguments[2]
)
) {
if (
resolve(configuration.path.target.base) ===
resolve(configuration.path.context)
) {
// Removes all compiled files.
await Tools.walkDirectoryRecursively(
configuration.path.target.base,
async (file:File):Promise<false|undefined> => {
if (Helper.isFilePathInLocation(
file.path,
configuration.path.ignore
.concat(
configuration.module.directoryNames,
configuration.loader.directoryNames
)
.map((filePath:string):string =>
resolve(
configuration.path.context,
filePath
)
)
.filter((filePath:string):boolean =>
!configuration.path.context.startsWith(
filePath
)
)
))
return false
for (
const type in configuration.buildContext.types
)
if (new RegExp(
configuration.buildContext.types[
type
].filePathPattern
).test(file.path)) {
if (file.stats?.isDirectory()) {
await removeDirectoryRecursively(
file.path
)
return false
}
await unlink(file.path)
break
}
}
)
for (
const file of (
await Tools.walkDirectoryRecursively(
configuration.path.target.base,
():false => false,
configuration.encoding
)
)
)
if (file.name.startsWith('npm-debug'))
await unlink(file.path)
} else
await removeDirectoryRecursively(
configuration.path.target.base
)
if (await Tools.isDirectory(
configuration.path.apiDocumentation
))
await removeDirectoryRecursively(
configuration.path.apiDocumentation
)
for (const filePath of configuration.path.tidyUpOnClear)
if (filePath)
if (Tools.isFileSync(filePath))
// NOTE: Close handler have to be synchronous.
unlinkSync(filePath)
else if (Tools.isDirectorySync(filePath))
removeDirectoryRecursivelySync(filePath)
removeDirectoryRecursivelySync(
globSync(configuration.path.tidyUpOnClearGlobs)
)
}
// endregion
// region handle build
const buildConfigurations:ResolvedBuildConfiguration =
Helper.resolveBuildConfigurationFilePaths(
configuration.buildContext.types,
configuration.path.source.asset.base,
configuration.path.ignore.concat(
configuration.module.directoryNames,
configuration.loader.directoryNames
).map((filePath:string):string =>
resolve(configuration.path.context, filePath)
).filter((filePath:string):boolean =>
!configuration.path.context.startsWith(filePath)
),
configuration.package.main.fileNames
)
if ([
'build',
'document',
'test',
'test:coverage',
'test:coverage:report'
].includes(commandLineArguments[2])) {
let tidiedUp = false
const tidyUp = ():void => {
/*
Determines all none javaScript entities which have been
emitted as single module to remove.
*/
if (tidiedUp)
return
tidiedUp = true
for (const [chunkName, chunk] of Object.entries(
configuration.injection.entry.normalized
))
for (const moduleID of chunk) {
const filePath:null|string =
Helper.determineModuleFilePath(
moduleID,
configuration.module.aliases,
configuration.module.replacements
.normal,
{
file: configuration.extensions.file
.internal
},
configuration.path.context,
configuration.path.source.asset.base,
configuration.path.ignore,
configuration.module.directoryNames,
configuration.package.main.fileNames,
configuration.package.main
.propertyNames,
configuration.package
.aliasPropertyNames,
configuration.encoding
)
let type:null|string = null
if (filePath)
type = Helper.determineAssetType(
filePath,
configuration.buildContext.types,
configuration.path
)
if (
typeof type === 'string' &&
configuration.buildContext.types[type]
) {
const filePath:string =
Helper.renderFilePathTemplate(
Helper.stripLoader(
configuration.files.compose
.javaScript
),
{'[name]': chunkName}
)
/*
NOTE: Close handler have to be
synchronous.
*/
if (
configuration.buildContext.types[
type
].outputExtension === 'js' &&
Tools.isFileSync(filePath)
)
chmodSync(filePath, '755')
}
}
for (const filePath of configuration.path.tidyUp)
if (filePath)
if (Tools.isFileSync(filePath))
// NOTE: Close handler have to be synchronous.
unlinkSync(filePath)
else if (Tools.isDirectorySync(filePath))
removeDirectoryRecursivelySync(filePath)
removeDirectoryRecursivelySync(
globSync(configuration.path.tidyUpGlobs)
)
}
closeEventHandlers.push(tidyUp)
/*
Triggers complete asset compiling and bundles them into the
final productive output.
*/
processPromises.push(new Promise<ProcessCloseReason>((
resolve:ProcessCloseCallback, reject:ProcessErrorCallback
):Array<ChildProcess> => {
const commandLineArguments:Array<string> = (
configuration.commandLine.build.arguments || []
).concat(additionalArguments)
console.info(
'Running "' +
(
`${configuration.commandLine.build.command} ` +
commandLineArguments.join(' ')
).trim() +
'"'
)
/*
NOTE: Take current weboptimizer's dependencies into
account.
*/
childProcessOptions.env!.PATH +=
`:${webOptimizerPath}/node_modules/.bin`
const childProcess:ChildProcess = spawnChildProcess(
configuration.commandLine.build.command,
commandLineArguments,
childProcessOptions
)
const copyAdditionalFilesAndTidyUp:ProcedureFunction = (
):void => {
for (
const filePath of
configuration.files.additionalPaths
) {
const sourcePath:string =
join(configuration.path.source.base, filePath)
const targetPath:string =
join(configuration.path.target.base, filePath)
// NOTE: Close handler have to be synchronous.
if (Tools.isDirectorySync(sourcePath)) {
if (Tools.isDirectorySync(targetPath))
removeDirectoryRecursivelySync(targetPath)
Tools.copyDirectoryRecursiveSync(
sourcePath, targetPath
)
} else if (Tools.isFileSync(sourcePath))
Tools.copyFileSync(sourcePath, targetPath)
}
tidyUp()
}
const closeHandler:ProcessHandler =
Tools.getProcessCloseHandler(
resolve,
reject,
null,
commandLineArguments[2] === 'build' ?
copyAdditionalFilesAndTidyUp :
tidyUp
)
for (const closeEventName of CloseEventNames)
childProcess.on(closeEventName, closeHandler)
childProcesses.push(childProcess)
return childProcesses
}))
// endregion
// region handle pre-install
} else if (
configuration.library &&
configuration.givenCommandLineArguments[2] === 'preinstall'
) {
// Perform all file specific preprocessing stuff.
let testModuleFilePaths:Array<string> = []
if (
Tools.isPlainObject(
configuration['test:browser'].injection
) &&
configuration['test:browser'].injection.entry
)
testModuleFilePaths = Helper.determineModuleLocations(
configuration['test:browser'].injection.entry as
GivenInjection,
configuration.module.aliases,
configuration.module.replacements.normal,
{file: configuration.extensions.file.internal},
configuration.path.context,
configuration.path.source.asset.base,
configuration.path.ignore
).filePaths
for (const buildConfiguration of buildConfigurations) {
const expression:string = (buildConfiguration[
configuration.givenCommandLineArguments[2] as keyof
ResolvedBuildConfigurationItem
] as string).trim()
for (const filePath of buildConfiguration.filePaths)
if (!testModuleFilePaths.includes(filePath)) {
const evaluated:EvaluationResult =
Tools.stringEvaluate(
`\`${expression}\``,
{
global,
self: configuration,
buildConfiguration,
path,
additionalArguments,
filePath
}
)
if (evaluated.error)
throw new Error(
'Error occurred during processing given ' +
`command: ${evaluated.error}`
)
console.info(`Running "${evaluated.result}"`)
processPromises.push(
new Promise<ProcessCloseReason>((
resolve:(_value:ProcessCloseReason) =>
void,
reject:(_reason:Error) => void
):Array<ChildProcess> => [
Tools.handleChildProcess(
execChildProcess(
evaluated.result,
{
encoding:
configuration.encoding,
...processOptions
},
(error:Error|null):void =>
error ?
reject(error) :
resolve({
reason: 'Finished.',
parameters: []
})
)
)
])
)
}
}
}
// endregion
// region handle remaining tasks
const handleTask = (type:keyof CommandLineArguments):void => {
const tasks:Array<Command> =
Array.isArray(configuration.commandLine[type]) ?
(configuration.commandLine[type] as Array<Command>) :
[configuration.commandLine[type] as Command]
for (const task of tasks) {
const evaluated:EvaluationResult = Tools.stringEvaluate(
(
Object.prototype.hasOwnProperty.call(
task, 'indicator'
) ? task.indicator as string : 'true'
),
{global, self: configuration, path}
)
if (evaluated.error)
throw new Error(
'Error occurred during processing given task: ' +
evaluated.error
)
if (evaluated.result)
processPromises.push(new Promise<ProcessCloseReason>((
resolve:ProcessCloseCallback,
reject:ProcessErrorCallback
):Array<ChildProcess> => {
const commandLineArguments:Array<string> = (
task.arguments || []
).concat(additionalArguments)
console.info(
'Running "' +
(
`${task.command} ` +
commandLineArguments.join(' ')
).trim() +
'"'
)
const childProcess:ChildProcess =
spawnChildProcess(
task.command,
commandLineArguments,
childProcessOptions
)
const closeHandler:ProcessHandler =
Tools.getProcessCloseHandler(resolve, reject)
for (const closeEventName of CloseEventNames)
childProcess.on(closeEventName, closeHandler)
childProcesses.push(childProcess)
return childProcesses
}))
}
}
/// region a-/synchronous
if ([
'document',
'test',
'test:coverage',
'test:coverage:report'
].includes(
configuration.givenCommandLineArguments[2]
)) {
await Promise.all(processPromises)
handleTask(
configuration.givenCommandLineArguments[2] as keyof
CommandLineArguments
)
} else if ([
'build:types',
'check:types',
'lint',
'serve',
'test:browser'
].includes(configuration.givenCommandLineArguments[2]))
handleTask(
configuration.givenCommandLineArguments[2] as
keyof CommandLineArguments
)
/// endregion
// endregion
}
let finished = false
const closeHandler:ProcessHandler = (
...parameters:Array<unknown>
):void => {
if (!finished)
for (const closeEventHandler of closeEventHandlers)
closeEventHandler(...parameters)
finished = true
}
for (const closeEventName of CloseEventNames)
process.on(closeEventName, closeHandler)
if (
require.main === module &&
(
configuration.givenCommandLineArguments.length < 3 ||
!possibleArguments.includes(
configuration.givenCommandLineArguments[2]
)
)
)
console.info(
`Give one of "${possibleArguments.join('", "')}" as command ` +
'line argument. You can provide a json string as second ' +
'parameter to dynamically overwrite some configurations.\n'
)
// endregion
// region forward nested return codes
try {
await Promise.all(processPromises)
} catch (error) {
console.error(error)
process.exit((error as ProcessError).returnCode)
}
// endregion
} catch (error) {
if (configuration.debug)
throw error
else
console.error(error)
}
return clear
}
if (require.main === module)
void main()
export default main
// region vim modline
// vim: set tabstop=4 shiftwidth=4 expandtab:
// vim: foldmethod=marker foldmarker=region,endregion:
// endregion