How to automate check variable existence in react?

121 Views Asked by At

I have react components which render variables through props.

main.jsx

<div>
   {props.name && <h1>{props.name}</h1>}
   {props.id && <h1>{props.id}</h1>}
</div>

personal.jsx

<div>
   {props.city && <h1>{props.city}</h1>}
   {props.country && <h1>{props.country}</h1>}
</div>

the same files repeated in in multiple looks whose styles are different.

The structure look likes.

--Card-1
  --main.jsx
  --personal.jsx
--Card-2
  --main.jsx
  --personal.jsx
--Card-3
  --main.jsx
  --personal.jsx
--Card-4
  --main.jsx
  --personal.jsx

This may increase in future. I want to check/automate script to check name and id exits in evary main.js and city and country exist in every personal.jsx file

1

There are 1 best solutions below

8
On

You could use bash script like this:

#!/bin/bash
PERSONAL_COUNT=$(find . -name "personal.jsx" | wc -l)
CITY_COUNT=$(grep "props.city" **/personal.jsx | wc -l)
COUNTRY_COUNT=$(grep "props.country" **/personal.jsx | wc -l)
MAIN_COUNT=$(find . -name "main.jsx" | wc -l)
ID_COUNT=$(grep "props.id" **/main.jsx | wc -l)
NAME_COUNT=$(grep "props.name" **/main.jsx | wc -l)

[[ $MAIN_COUNT == $NAME_COUNT ]] && echo 'name: OK' || echo 'name: Too less name prop usage in main.jsx files' && exit 1
[[ $MAIN_COUNT == $ID_COUNT ]] && echo 'id: OK' || echo 'id: Too less name prop usage in main.jsx files' && exit 1
[[ $PERSONAL_COUNT == $CITY_COUNT ]] && echo 'city: OK' || echo 'city: Too less name prop usage in main.jsx files' && exit 1
[[ $PERSONAL_COUNT == $COUNTRY_COUNT ]] && echo 'country: OK' || echo 'country: Too less name prop usage in main.jsx files' && exit 1