Skip to content

Instantly share code, notes, and snippets.

@CodeWitchBella
Last active February 9, 2022 17:14
Show Gist options
  • Save CodeWitchBella/da8cac411fb2009aaafe4de88396b685 to your computer and use it in GitHub Desktop.
Save CodeWitchBella/da8cac411fb2009aaafe4de88396b685 to your computer and use it in GitHub Desktop.
simple regex-based script to find factory usages
const fs = require('fs')
const path = require('path')
let files = 0
let lines = 0
function rd(dir) {
for (const dirent of fs.readdirSync(dir, { withFileTypes: true })) {
const fname = path.join(dir, dirent.name)
if (dirent.isDirectory()) {
rd(fname)
} else if (dirent.isFile()) {
if (fname.endsWith('.sketch') || fname.endsWith('.png') || fname.endsWith('.jpg')) continue
const matches = fs
.readFileSync(fname, 'utf-8')
.matchAll(/(?<![a-zA-Z0-9.])(?<!new )(?<!function )[A-Z][a-zA-Z]+\(/g)
let first = true
for (const match of matches) {
if (
[
'List(',
'Boolean(',
'Map(',
'Number(',
'Set(',
'String(',
'Record(',
'OrderedSet(',
'OrderedMap(',
'Array(',
'LinePart(',
].includes(match[0])
) {
continue
}
if (first) {
first = false
console.log(fname + ':' + getLineNumber(match))
files++
}
console.log(' ' + getLine(match))
lines++
}
}
}
}
rd('modules')
console.log({ files, lines })
function getLineNumber(match) {
let index = match.index
while (index > 0) {
if (match.input[index] === '\n') {
const tail = match.input.slice(index + 1)
const head = match.input.slice(0, index)
return head.split('\n').length + 1
}
index--
}
}
function getLine(match) {
let index = match.index
while (index > 0) {
if (match.input[index] === '\n') {
const tail = match.input.slice(index + 1)
const head = match.input.slice(0, index)
return head.split('\n').length + 1 + ': ' + tail.slice(0, tail.indexOf('\n')).trim()
}
index--
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment