From e5a96dd8205d1e072fd5e6783a22957111779490 Mon Sep 17 00:00:00 2001 From: TuDo1403 Date: Thu, 14 Mar 2024 11:37:55 +0700 Subject: [PATCH] chore: update husky files --- .husky/generate-layout.sh | 8 ++--- .husky/pre-push | 4 +-- .husky/storage-logger.js | 67 +++++++++++++++++++++------------------ 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/.husky/generate-layout.sh b/.husky/generate-layout.sh index 8fc74d4..0e66916 100755 --- a/.husky/generate-layout.sh +++ b/.husky/generate-layout.sh @@ -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" diff --git a/.husky/pre-push b/.husky/pre-push index 6397927..4a821d8 100755 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -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 diff --git a/.husky/storage-logger.js b/.husky/storage-logger.js index ec74913..d5a13df 100644 --- a/.husky/storage-logger.js +++ b/.husky/storage-logger.js @@ -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); + } }); \ No newline at end of file