Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate SPDX and ABIEncoderV2 lines #61

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,46 @@ async function main(args) {
return;
}

await flatten(filePaths, outputChunk => {
if (outputFilePath) {
fs.appendFileSync(outputFilePath, outputChunk);
} else {
process.stdout.write(outputChunk);
}
let outputFileContent = "";

await flatten(filePaths, (outputChunk) => {
outputFileContent += outputChunk;
});

// fix duplicate SPDX license identifiers and abiv2 pragmas
const outputLinesArray = outputFileContent.split("\n");
const licenses = []; // string[]
const spdxRegex = /SPDX-License-Identifier/;
const abiv2Regex = /pragma experimental ABIEncoderV2;/;
let abiV2Exists = false;
for (const [index, line] of outputLinesArray.entries()) {
if (spdxRegex.test(line)) {
const words = line.split(":");
const wordIndex = words.findIndex((word) => spdxRegex.test(word)); // Actual license idenfifier after this

const licenseIdentifier = words[wordIndex + 1].replace(/ /g, "");
licenses.push(licenseIdentifier);
outputLinesArray.splice(index, 1);
}
if (abiv2Regex.test(line)) {
if (abiV2Exists) {
outputLinesArray.splice(index, 1);
} else {
abiV2Exists = true;
}
}
}
outputLinesArray.unshift(
"// SPDX-License-Identifier: " + [...new Set(licenses)].join(" AND ")
);

outputFileContent = outputLinesArray.join("\n");

if (outputFilePath) {
fs.writeFileSync(outputFilePath, outputFileContent);
} else {
process.stdout.write(outputChunk);
}
}

if (require.main === module) {
Expand Down