-
Notifications
You must be signed in to change notification settings - Fork 4
/
prettier-ignore-log-lines.js
52 lines (41 loc) · 1.31 KB
/
prettier-ignore-log-lines.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
/**
* Simple script for adding / prettier-ignore / to instrumentation lines
*/
const replace = require('replace-in-file')
const prettierConfig = require('./prettier.config')
const PRINT_WIDTH = prettierConfig.printWidth
const prettierIgnore = '/* prettier-ignore */ '
// Including leading whitespace
const instrumentationLines = [
/\s+nestedCountersInstance.countEvent.+/g,
/\s+nestedCountersInstance.countRareEvent.+/g,
/\s+stateManager.statemanager_fatal.+/g,
/\s+if\s*\(\s*logFlags.+/g,
]
const options = {
files: 'src/**/*.ts',
from: instrumentationLines,
to: (match) => {
const numColumns = match.length
const shortLine = numColumns < PRINT_WIDTH
const alreadyHasPrettierIgnore = match.includes('prettier-ignore')
if (shortLine || alreadyHasPrettierIgnore) {
return match
}
const firstNonWhitespaceIndex = match.search(/\S/)
// Insert / prettier-ignore / after the whitespace but before the line content
const updatedLine = [
match.slice(0, firstNonWhitespaceIndex),
prettierIgnore,
match.slice(firstNonWhitespaceIndex),
].join('')
return updatedLine
},
}
const main = async () => {
const results = await replace(options)
console.log('Replacement results:', results)
}
main().catch((err) => {
console.error('An error occured: ', err)
})