Skip to content

Commit

Permalink
refactor: Provide a better name for the content of the current licens…
Browse files Browse the repository at this point in the history
…e file
  • Loading branch information
RSeidelsohn committed Apr 8, 2023
1 parent 3105d5a commit a406048
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,32 +189,34 @@ const flatten = function flatten(options) {
// Checking that the file is in fact a normal file and not a directory for example.
/*istanbul ignore else*/
if (fs.lstatSync(licenseFile).isFile()) {
let content;
let currentLicenceFilesContent;

if (
!moduleInfo.licenses ||
moduleInfo.licenses.indexOf(UNKNOWN) > -1 ||
moduleInfo.licenses.indexOf('Custom:') === 0
) {
//Only re-check the license if we didn't get it from elsewhere
content = fs.readFileSync(licenseFile, { encoding: 'utf8' });
currentLicenceFilesContent = fs.readFileSync(licenseFile, { encoding: 'utf8' });

moduleInfo.licenses = getLicenseTitle(content);
moduleInfo.licenses = getLicenseTitle(currentLicenceFilesContent);
}

if (index === 0) {
// Treat the file with the highest precedence as licenseFile

if (clarification !== undefined && !passed_clarification_check) {
/*istanbul ignore else*/
if (!content) {
content = fs.readFileSync(licenseFile, { encoding: 'utf8' });
if (!currentLicenceFilesContent) {
currentLicenceFilesContent = fs.readFileSync(licenseFile, { encoding: 'utf8' });
}

let sha256 = createHash('sha256').update(content).digest('hex');
let sha256 = createHash('sha256').update(currentLicenceFilesContent).digest('hex');

if (clarification.checksum !== sha256) {
console.error(`Clarification checksum mismatch for ${currentPackageNameAndVersion} :(\nFile checked: ${licenseFile}`);
console.error(
`Clarification checksum mismatch for ${currentPackageNameAndVersion} :(\nFile checked: ${licenseFile}`,
);
process.exit(1);
} else {
passed_clarification_check = true;
Expand All @@ -233,15 +235,15 @@ const flatten = function flatten(options) {
if (clarification?.licenseText) {
moduleInfo.licenseText = clarification.licenseText;
} else {
if (!content) {
content = fs.readFileSync(licenseFile, { encoding: 'utf8' });
if (!currentLicenceFilesContent) {
currentLicenceFilesContent = fs.readFileSync(licenseFile, { encoding: 'utf8' });
}

/*istanbul ignore else*/
if (options._args && !options._args.csv) {
moduleInfo.licenseText = content.trim();
moduleInfo.licenseText = currentLicenceFilesContent.trim();
} else {
moduleInfo.licenseText = content
moduleInfo.licenseText = currentLicenceFilesContent
.replace(/"/g, "'")
.replace(/\r?\n|\r/g, ' ')
.trim();
Expand All @@ -267,11 +269,11 @@ const flatten = function flatten(options) {
if (clarification?.copyright) {
moduleInfo.copyright = clarification.copyright;
} else {
if (!content) {
content = fs.readFileSync(licenseFile, { encoding: 'utf8' });
if (!currentLicenceFilesContent) {
currentLicenceFilesContent = fs.readFileSync(licenseFile, { encoding: 'utf8' });
}

const linesWithCopyright = content
const linesWithCopyright = currentLicenceFilesContent
.replace(/\r\n/g, '\n')
.split('\n\n')
.filter(function selectCopyRightStatements(value) {
Expand Down

0 comments on commit a406048

Please sign in to comment.