Created
October 6, 2021 11:19
-
-
Save igoratf/91064b66d31c5db2340e0251544e0e7b to your computer and use it in GitHub Desktop.
Extract metrics from React repositories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
import re | |
import sys | |
def get_data(directory, filename, name): | |
f = open(directory + '/' + filename) | |
lines = 0 | |
imports = 0 | |
exports = 0 | |
functions = 0 | |
useEffect = 0 | |
useState = 0 | |
useMemo = 0 | |
useContext = 0 | |
useCallback = 0 | |
for x in f: | |
lines += 1 | |
if re.search("^import .", x): | |
imports += 1 | |
if re.search("^export .", x): | |
exports += 1 | |
if re.search("[ ]*function\s\w*[(](\s|\S)*[)]", x) or re.search("\s*const\s(\s|\S)*[=](\w)*[=>]\s", x): | |
functions += 1 | |
if re.search(".useEffect\(.", x): | |
useEffect += 1 | |
if re.search(".useState(\(|\<)", x): | |
useState += 1 | |
if re.search(".useMemo\(.", x): | |
useMemo += 1 | |
if re.search(".useContext\(.", x): | |
useContext += 1 | |
if re.search(".useCallback\(.", x): | |
useCallback += 1 | |
f.close() | |
return [filename, lines, imports, exports, functions, useEffect, useState, useMemo, useContext, useCallback] | |
def iterate_files(directory, rows): | |
file_list = os.listdir(directory) | |
for filename in file_list: | |
actual_path = os.path.join(directory, filename) | |
if os.path.isdir(actual_path): | |
iterate_files(actual_path, rows) | |
else: | |
file_split = filename.split('.') | |
if len(file_split) > 1: | |
name, extension = file_split[0], file_split[1] | |
if extension == "jsx" or extension == "tsx" or extension == "js" or extension == "ts": | |
row = get_data(directory, filename, name) | |
rows.append(row) | |
def main(): | |
directory = sys.argv[1] | |
headers = ['File', 'Lines', 'Imports', 'Exports', 'Functions', 'Num of useEffect', 'Num of useState', 'Num of useMemo', 'Num of useContext', 'Num of useCallback'] | |
rows = [] | |
iterate_files(directory, rows) | |
if len(rows) > 0: | |
with open('metrics.csv', 'w', encoding='UTF8', newline='') as f: | |
writer = csv.writer(f) | |
writer.writerow(headers) | |
writer.writerows(rows) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment