Skip to content

Commit

Permalink
chore: update husky files
Browse files Browse the repository at this point in the history
  • Loading branch information
TuDo1403 committed Mar 14, 2024
1 parent 02804a1 commit e5a96dd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
8 changes: 4 additions & 4 deletions .husky/generate-layout.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
rm -rf logs/storage/*
dirOutputs=$(ls out | grep '^[^.]*\.sol$') # assuming the out dir is at 'out'
while IFS= read -r contractDir; do
innerdirOutputs=$(ls out/$contractDir)
innerdirOutputs=$(ls out/"$contractDir")

while IFS= read -r jsonFile; do
fileIn=out/$contractDir/$jsonFile
fileOut=logs/storage/$contractDir:${jsonFile%.json}.log
node .husky/storage-logger.js $fileIn $fileOut &
fileIn=out/"$contractDir"/$jsonFile
fileOut=logs/storage/"$contractDir":${jsonFile%.json}.log
node .husky/storage-logger.js "$fileIn" "$fileOut" &
done <<< "$innerdirOutputs"
done <<< "$dirOutputs"

Expand Down
4 changes: 2 additions & 2 deletions .husky/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ forge build --sizes 2>&1 | sed -n '/Contract/,$p' > logs/contract-code-sizes.log
git add logs

output=$(git status -s)
line_count=$(echo "$output" | wc -l)
if [ "$line_count" -gt 1 ]; then
word_count=$(echo "$output" | wc -w)
if [ "$word_count" -gt 0 ]; then
git commit -m "chore: storage layout"
fi

Expand Down
67 changes: 36 additions & 31 deletions .husky/storage-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,49 @@ const fileIn = process.argv[2];
const fileOut = process.argv[3];

if (!fileIn) {
console.error('Invalid input');
console.error('Invalid input');
}

fs.readFile(fileIn, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
if (err) {
console.error('Error reading file:', err);
return;
}

try {
const jsonData = JSON.parse(data);
if (typeof jsonData.storageLayout == 'undefined' || jsonData.ast.absolutePath == 'undefined') {
return;
}

try {
const jsonData = JSON.parse(data);
if (typeof jsonData.storageLayout == 'undefined') {
return;
}
if (jsonData.storageLayout.storage.length == 0) {
return;
}

if (jsonData.storageLayout.storage.length == 0) {
return;
}
// Skip generating storage layout for files locate in the directory "src"
if (!jsonData.ast.absolutePath.startsWith('src')) {
return;
}

const outputData = jsonData.storageLayout.storage
.map(({ contract, label, offset, slot, type: typeId }) => {
const typeObj = jsonData.storageLayout.types[typeId];
const typeLabel = typeObj.label;
const numberOfBytes = typeObj.numberOfBytes;
return `${contract}:${label} (storage_slot: ${slot}) (offset: ${offset}) (type: ${typeLabel}) (numberOfBytes: ${numberOfBytes})`;
})
.join('\n');
if (!fileOut) {
console.log(outputData);
} else {
fs.writeFile(fileOut, outputData, 'utf-8', err => {
if (err) {
console.error('Error writing file:', err);
return;
}
});
const outputData = jsonData.storageLayout.storage
.map(({ contract, label, offset, slot, type: typeId }) => {
const typeObj = jsonData.storageLayout.types[typeId];
const typeLabel = typeObj.label;
const numberOfBytes = typeObj.numberOfBytes;
return `${contract}:${label} (storage_slot: ${slot}) (offset: ${offset}) (type: ${typeLabel}) (numberOfBytes: ${numberOfBytes})`;
})
.join('\n');
if (!fileOut) {
console.log(outputData);
} else {
fs.writeFile(fileOut, outputData, 'utf-8', err => {
if (err) {
console.error('Error writing file:', err);
return;
}
} catch (err) {
console.error('Error parsing JSON:', err);
});
}
} catch (err) {
console.error('Error parsing JSON:', err);
}
});

0 comments on commit e5a96dd

Please sign in to comment.