Skip to content

Commit

Permalink
Merge pull request #22 from UUX-Brasil/dev
Browse files Browse the repository at this point in the history
Dev > master release v3.0.0
  • Loading branch information
caiofreitaso authored Apr 13, 2017
2 parents 6a94edf + 5a4f73a commit f141033
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 22 deletions.
11 changes: 11 additions & 0 deletions .license-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"header": "THIRD-PARTY SOFTWARE NOTICES AND INFORMATION\n\nNote: OTTN is not the author of the files below.\n\nThe notices below are provided for informational purposes only and are not\nthe license terms under which OTTN distributes these files.\n\n",
"file": "LICENSE-3RD-PARTY.txt",
"skipPrefix": "spotlight@",
"allowedLicenseList":
[
"MIT", "MIT*", "Apache-2.0", "Apache-2.0*", "BSD-3-Clause", "BSD-3-Clause*",
"BSD", "BSD*", "ISC", "ISC*", "AFLv2.1", "Unlicense", "WTFPL", "BSD-2-Clause",
"(MIT AND CC-BY-3.0)", "Public Domain", "Apache License, Version 2.0"
]
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

## v3.0.0
- [2017-04-10] *major* [TM-4192](https://jiralabone.atlassian.net/browse/TM-4192) **Change license builder to only support JSON config files**

## v2.2.0
- [2016-01-24] *minor* [TM-4078](https://jiralabone.atlassian.net/browse/TM-4078) **Add license builder to nodejs tools**

Expand Down
34 changes: 15 additions & 19 deletions lib/cmd/license-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@

const program = require('commander');
const ThirdPartyLicenseGenerator = require('./third-party-license-generator');
const LicenseFileHandler = require('./license-file-handler');

program
.option('--header <value>', 'Add a header to license file.')
.option('--allow <value>', 'Comma separated list of allowed license names to be verified.')
.option('--file <value>', 'File to write the license list.')
.option('--skipPrefix <value>', 'Skip licenses of dependencies that start with this')
.option('--config <value>', 'Use the config file.')
.parse(process.argv);

let header = program.header;
let allowedLicenseList = program.allow.split(',');
let file = program.file;
let skipPrefix = program.skipPrefix;
let licensePromise = LicenseFileHandler
.getConfig(program.config)
.then((content) => {
return LicenseFileHandler.parseFile(content);
});

let dependPromise = ThirdPartyLicenseGenerator
.getDependenciesDetails();

ThirdPartyLicenseGenerator
.getDependenciesDetails()
.then((dependenciesDetails) => {
return ThirdPartyLicenseGenerator.createThirdPartyLicense(dependenciesDetails, {
header: header,
allowedLicenseList: allowedLicenseList,
file: file,
skipPrefix: skipPrefix
});
Promise
.all([licensePromise,dependPromise])
.then((arr) => {
return ThirdPartyLicenseGenerator.createThirdPartyLicense(arr[1], arr[0]);
})
.then((resultMessage) => {
console.log(resultMessage);
Expand All @@ -36,10 +33,9 @@ ThirdPartyLicenseGenerator
}

if (error.error) { console.error(error.error); }

if (error.stderr) { console.error(error.stderr); }

if (error.stdout) { console.log(error.stdout); }
if (error.errorMessage) { console.error(error.errorMessage); }

console.error('Could not generate 3rd party licenses');
process.exit(1);
Expand Down
69 changes: 69 additions & 0 deletions lib/cmd/license-file-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const fs = require('fs');

class LicenseFileHandler
{
static getMissingFields(obj)
{
let missingFields = [];

if(!obj.header) missingFields.push('header');
if(!obj.file) missingFields.push('file');
if(!obj.skipPrefix) missingFields.push('skipPrefix');
if(!obj.allowedLicenseList) missingFields.push('allowedLicenseList');

return missingFields;
}

static getFile(filename)
{
return new Promise((resolve, reject) => {
return fs.readFile(filename, 'utf8', (err, data) => {
if (err)
return reject(
{
'errorCode': 'LICENSE_CONFIG_NOT_FOUND',
'errorMessage': err.toString()
});
return resolve(data);
});
});
}

static parseFile(arg)
{
return new Promise((resolve, reject) => {
let object = null;

try {
object = JSON.parse(arg);
} catch (e) {
return reject(
{
'errorCode': 'LICENSE_CONFIG_CORRUPTED',
'errorMessage': 'License config file is not a valid JSON.'
});
}

let missingFields = LicenseFileHandler.getMissingFields(object);

if (missingFields.length === 0)
return resolve(object);
return reject(
{
'errorCode': 'LICENSE_CONFIG_CORRUPTED',
'errorMessage': 'License config file does not have the parameters: '
+ missingFields.toString()
+ '.'
});
});
}

static getConfig(filename)
{
return LicenseFileHandler.getFile(filename ? filename : '.license-config');
}
}

module.exports = LicenseFileHandler;
11 changes: 11 additions & 0 deletions mylicense.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"header": "MY SUPER DUPER LICENSE 3000",
"file": "ULTRA-LICENSE-DO-MILHAO.txt",
"skipPrefix": "prefix@",
"allowedLicenseList":
[
"MIT", "MIT*", "Apache-2.0", "Apache-2.0*", "BSD-3-Clause", "BSD-3-Clause*",
"BSD", "BSD*", "ISC", "ISC*", "AFLv2.1", "Unlicense", "WTFPL", "BSD-2-Clause",
"(MIT AND CC-BY-3.0)", "Public Domain"
]
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itaas-nodejs-tools",
"version": "2.2.0",
"version": "3.0.0",
"private": true,
"description": "Node.js tools",
"main": "./lib/index.js",
Expand All @@ -13,7 +13,8 @@
"lint": "eslint .",
"coverage": "istanbul cover --include-all-sources true node_modules/mocha/bin/_mocha -- --recursive -R dot -c",
"nodemon": "nodemon --exec \"npm run lint && npm run coverage\"",
"unit-test": "mocha --recursive -c test/unit"
"unit-test": "mocha --recursive -c test/unit",
"license": "node lib/cmd/license-cmd.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -49,7 +50,7 @@
"request": "^2.72.0",
"should": "^9.0.1"
},
"author": "Itaas",
"author": "iTaaS",
"license": "ISC",
"homepage": "https://github.com/UUX-Brasil/itaas-nodejs-tools#readme"
}
98 changes: 98 additions & 0 deletions test/unit/cmd/license-file-handler-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict';
/* global describe, it */

const should = require('should'); // eslint-disable-line no-unused-vars
const LicenseFileHandler = require('../../../lib/cmd/license-file-handler');

const default_config = '{\n\
\"header\": \"THIRD-PARTY SOFTWARE NOTICES AND INFORMATION\\n\\n\
Note: OTTN is not the author of the files below.\\n\\n\
The notices below are provided for informational purposes only and are not\\n\
the license terms under which OTTN distributes these files.\\n\\n\",\n\
\"file\": \"LICENSE-3RD-PARTY.txt\",\n\
\"skipPrefix\": \"spotlight@\",\n\
\"allowedLicenseList\":\n\
[\n\
\"MIT\", \"MIT*\", \"Apache-2.0\", \"Apache-2.0*\", \"BSD-3-Clause\", \"BSD-3-Clause*\",\n\
\"BSD\", \"BSD*\", \"ISC\", \"ISC*\", \"AFLv2.1\", \"Unlicense\", \"WTFPL\", \"BSD-2-Clause\",\n\
\"(MIT AND CC-BY-3.0)\", \"Public Domain\", "Apache License, Version 2.0"\n\
]\n\
}\n';

const custom_config = '{\n\
\"header\": \"MY SUPER DUPER LICENSE 3000\",\n\
\"file\": \"ULTRA-LICENSE-DO-MILHAO.txt\",\n\
\"skipPrefix\": \"prefix@\",\n\
\"allowedLicenseList\":\n\
[\n\
\"MIT\", \"MIT*\", \"Apache-2.0\", \"Apache-2.0*\", \"BSD-3-Clause\", \"BSD-3-Clause*\",\n\
\"BSD\", \"BSD*\", \"ISC\", \"ISC*\", \"AFLv2.1\", \"Unlicense\", \"WTFPL\", \"BSD-2-Clause\",\n\
\"(MIT AND CC-BY-3.0)\", \"Public Domain\"\n\
]\n\
}\n';


describe('LicenseFileHandler', function() {
describe('.getFile', function() {
it('get default license file (/.license-config)', function() {
return LicenseFileHandler
.getConfig()
.then((content) => {
content.should.be.eql(default_config);
});
});

it('get custom license file (/mylicense.json)', function() {
return LicenseFileHandler
.getFile('mylicense.json')
.then((content) => {
content.should.be.eql(custom_config);
});
});

it('get and parse default license file (/.license-config)', function() {
let test = null;
return LicenseFileHandler
.getConfig()
.then((content) => {
return LicenseFileHandler.parseFile(content);
})
.then((obj) => {
test = obj;
return LicenseFileHandler.parseFile(default_config);
})
.then((comparison) => {
test.should.be.eql(comparison);
});
});

it('get and parse custom license file (/mylicense.json)', function() {
return LicenseFileHandler
.getFile('mylicense.json')
.then((content) => {
let test = LicenseFileHandler.parseFile(content);
let comparison = LicenseFileHandler.parseFile(custom_config);

test.should.be.eql(comparison);
});
});

it('fails when given an invalid file path', function() {
return LicenseFileHandler
.getFile('aaaaaaaa')
.should.be.rejectedWith({'errorCode': 'LICENSE_CONFIG_NOT_FOUND'});
});

it('fails when given an invalid JSON object', function() {
return LicenseFileHandler
.parseFile('aaaaaaaa')
.should.be.rejectedWith({'errorCode': 'LICENSE_CONFIG_CORRUPTED'});
});

it('fails when given a valid JSON object without the needed parameters', function() {
return LicenseFileHandler
.parseFile('{\"header\":\"asas\"}')
.should.be.rejectedWith({'errorCode': 'LICENSE_CONFIG_CORRUPTED'});
});
});
});

0 comments on commit f141033

Please sign in to comment.