Skip to content

Commit

Permalink
Merge pull request #102 from xswordsx/multiple-licenses
Browse files Browse the repository at this point in the history
Check for SPDX-compatible licenses
  • Loading branch information
davglass authored May 30, 2017
2 parents b52a16d + c5c8b0f commit 2b49e29
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ var flatten = function(options) {

data[key] = moduleInfo;

// Include property in output unless custom format has set property to false.
// Include property in output unless custom format has set property to false.
var include = function(property) {
return (options.customFormat === undefined || options.customFormat[property] !== false);
};

if (include("repository") && json.repository) {
/*istanbul ignore else*/
if (typeof json.repository === 'object' && typeof json.repository.url === 'string') {
Expand Down Expand Up @@ -125,9 +125,9 @@ var flatten = function(options) {
}
});
} else if (typeof licenseData === 'object' && (licenseData.type || licenseData.name)) {
moduleInfo.licenses = licenseData.type || licenseData.name;
moduleInfo.licenses = license(licenseData.type || licenseData.name);
} else if (typeof licenseData === 'string') {
moduleInfo.licenses = licenseData;
moduleInfo.licenses = license(licenseData);
}
} else if (license(json.readme)) {
moduleInfo.licenses = license(json.readme);
Expand Down Expand Up @@ -166,16 +166,16 @@ var flatten = function(options) {
content = fs.readFileSync(licenseFile, { encoding: 'utf8' });
moduleInfo.licenses = license(content);
}

var lf = options.basePath ? path.relative(options.basePath, licenseFile) : licenseFile;
if (!content) {
content = fs.readFileSync(lf, { encoding: 'utf8' });
}

if (include("licenseFile")) {
moduleInfo.licenseFile = options.basePath ? path.relative(options.basePath, licenseFile) : licenseFile;
}

if (include("licenseText") && options.customFormat) {
if (options._args && !options._args.csv) {
moduleInfo.licenseText = content.trim();
Expand Down Expand Up @@ -341,7 +341,7 @@ exports.asCSV = function(sorted, customFormat, csvComponentPrefix) {
['"module name"','"license"','"repository"'].forEach(function(item) {
textArr.push(item);
});
text.push(textArr.join(','));
text.push(textArr.join(','));
}

Object.keys(sorted).forEach(function(key) {
Expand All @@ -353,7 +353,7 @@ exports.asCSV = function(sorted, customFormat, csvComponentPrefix) {
if (customFormat && Object.keys(customFormat).length > 0) {
if (csvComponentPrefix) {
lineArr.push('"'+prefix+'"');
}
}
lineArr.push('"' + key + '"');
Object.keys(customFormat).forEach(function forEachCallback(item) {
lineArr.push('"' + module[item] + '"');
Expand Down
5 changes: 5 additions & 0 deletions lib/license.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var spdx = require('spdx');

var MIT_LICENSE = /ermission is hereby granted, free of charge, to any/;
var BSD_LICENSE = /edistribution and use in source and binary forms, with or withou/;
var BSD_SOURCE_CODE_LICENSE = /edistribution and use of this software in source and binary forms, with or withou/;
Expand All @@ -13,6 +15,9 @@ var CC0_1_0 = /The\s+person\s+who\s+associated\s+a\s+work\s+with\s+this\s+deed\s


module.exports = function(str) {
if (spdx.valid(str || '')) {
return str;
}
if (str) {
str = str.replace('\n', '');
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"nopt": "^2.2.0",
"read-installed": "~4.0.3",
"semver": "^5.3.0",
"spdx": "^0.5.1",
"treeify": "^1.0.1"
},
"devDependencies": {
Expand Down
28 changes: 28 additions & 0 deletions tests/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,32 @@ describe('license parser', function() {
var data = license('this is empty, hi');
assert.equal(data, null);
});

describe('SPDX licenses', function() {

it('should parse a basic SPDX license', function() {
var data = [
'MIT',
'LGPL-2.0',
'Apache-2.0',
'BSD-2-Clause'
];
data.forEach(function (licenseType) {
assert.equal(license(licenseType), licenseType);
});
});

it('should parse more complicated license expressions', function() {
var data = [
'(GPL-2.0+ WITH Bison-exception-2.2)',
'LGPL-2.0 OR (ISC AND BSD-3-Clause+)',
'Apache-2.0 OR ISC OR MIT',
];
data.forEach(function (licenseType) {
assert.equal(license(licenseType), licenseType);
});

});

});
});

0 comments on commit 2b49e29

Please sign in to comment.