-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileProcessor.js
63 lines (56 loc) · 2.68 KB
/
fileProcessor.js
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
import { relative } from 'node:path'
import { promises as fsAsync } from 'node:fs'
import { clock } from '@dr-js/core/module/common/time.js'
import { binary, time, padTable } from '@dr-js/core/module/common/format.js'
const processFileList = async ({
kit, kitLogger = kit,
fileList, processor,
outputPath = (kit && kit.fromOutput()) || ''
}) => {
kitLogger.padLog(`process ${fileList.length} file`)
const table = []
const totalTimeStart = clock()
let totalSizeSource = 0
let totalSizeDelta = 0
for (const filePath of fileList) {
const inputString = String(await fsAsync.readFile(filePath))
const outputString = await processor(inputString, filePath)
const sizeSource = (await fsAsync.stat(filePath)).size
let sizeOutput
if (inputString === outputString) {
kitLogger.devLog(`process skipped ${filePath}`)
sizeOutput = sizeSource
} else if (outputString) {
await fsAsync.writeFile(filePath, outputString)
sizeOutput = (await fsAsync.stat(filePath)).size
} else { // TODO: maybe not necessary to delete an empty file?
await fsAsync.unlink(filePath)
sizeOutput = 0
}
const sizeDelta = sizeOutput - sizeSource
totalSizeSource += sizeSource
totalSizeDelta += sizeDelta
kitLogger.isVerbose && table.push([
`∆ ${(100 * sizeDelta / sizeSource).toFixed(2)}% (${binary(sizeDelta)}B)`,
`${relative(outputPath, filePath)}`
])
}
kitLogger.isVerbose && table.push([ '--', '--' ])
table.push([
`∆ ${(100 * totalSizeDelta / totalSizeSource).toFixed(2)}% (${binary(totalSizeDelta)}B)`,
`TOTAL of ${fileList.length} file (${binary(totalSizeSource)}B|${time(clock() - totalTimeStart)})`
])
kitLogger.log(`result:\n ${padTable({ table, padFuncList: [ 'L', 'L' ], cellPad: ' | ', rowPad: '\n ' })}`)
return totalSizeDelta
}
const fileProcessorBabel = (inputString) => inputString
.replace(/['"]use strict['"];?\s*/g, '')
.replace(/Object\.defineProperty\(exports,\s*['"]__esModule['"],\s*{\s*value:\s*(true|!0)\s*}\)[;,]?\s*/g, '') // drop babel marker, drop this will make the `_interopRequireDefault` return `module.exports` instead of `module.exports.default`, but there's no `export default`: https://github.com/airbnb/javascript/issues/1365
.replace(/(exports\.\w+\s*=\s*)+(undefined|void 0)[;,]?\s*/g, '') // drop the duplicate pre-define of exports generated by babel
.replace(/\/\*\* @deprecated \*\/;?\s+const/g, '/** @deprecated */ const') // compact `/** @deprecated */ const ...` combo
.replace(/[\n\r]{2,}/g, '\n') // remove multi-blank lines // TODO: may also change lines in `` strings
.replace(/^[\n\r]+/, '') // remove leading blank line
export {
processFileList,
fileProcessorBabel
}