Skip to content

Commit

Permalink
Squash duplicate SPDX-License-Identifier lines
Browse files Browse the repository at this point in the history
This commit combines the licenses in multiple SPDX declarations as also
reported in NomicFoundation#55.
  • Loading branch information
zemse committed Sep 22, 2020
1 parent 14616b0 commit 8971c09
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,38 @@ 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
const outputLinesArray = outputFileContent.split('\n');
const licenses = []; // string[]
const regex = /SPDX-License-Identifier/;
for (const [index, line] of outputLinesArray.entries()) {
if (regex.test(line)) {
const words = line.split(':');
const wordIndex = words.findIndex((word) => regex.test(word)); // Actual license idenfifier after this

const licenseIdentifier = words[wordIndex + 1].replace(/ /g, '');
licenses.push(licenseIdentifier);
outputLinesArray.splice(index, 1);
}
}
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

0 comments on commit 8971c09

Please sign in to comment.