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

Setting to include LMD license into output #167

Closed
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion bin/lmd_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ LmdBuilder.prototype.closeStreams = function () {
* @param {Object} data
*/
LmdBuilder.prototype.templatePackage = function (data) {
return (data.build_info ? data.build_info + '\n' : '') +
return (data.license_header ? data.license_header + '\n' : '') +
(data.build_info ? data.build_info + '\n' : '') +
data.lmd_js +
'\n(' +
data.global + ',' +
Expand Down Expand Up @@ -865,6 +866,7 @@ LmdBuilder.prototype.renderLmdPackage = function (config, modulesBundle, isOptim
options = JSON.stringify(options);

result = this.templatePackage({
license_header: this._licenseHeader(config),
build_info: this._buildInfo(config),
lmd_js: lmd_js,
global: config.global || 'this',
Expand Down Expand Up @@ -923,6 +925,17 @@ LmdBuilder.prototype.renderLmdBundle = function (config, modulesBundle) {
});
};

LmdBuilder.prototype._licenseHeader = function (config) {
if (config.lmd_license_header === true) {
var licensePath = path.resolve(__dirname, '../LICENCE'),
text = fs.readFileSync(licensePath, 'utf8'),
license = text.replace(/^LMD includes as dependencies[\s\S]+/m, '');
return '/*! LMD loader and stock plugins: ' + license.trim() + '\n*/';
} else {
return false;
}
};

LmdBuilder.prototype._buildInfo = function (config) {
var configFile = path.basename(this.configFile),
mixinFiles = (config.mixins || []).map(function (mixin) {
Expand Down
2 changes: 1 addition & 1 deletion lib/lmd_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var fs = require('fs'),
require('colors');

var DEFAULT_DEPENDS_MASK = "*.lmd.json";
var SOURCE_TWEAK_FLAGS = ["warn", "log", "pack", "lazy", "optimize"];
var SOURCE_TWEAK_FLAGS = ["warn", "log", "pack", "lazy", "optimize", "lmd_license_header"];
exports.SOURCE_TWEAK_FLAGS = SOURCE_TWEAK_FLAGS;

var INHERITABLE_FIELDS = SOURCE_TWEAK_FLAGS.concat(['version', 'main', 'global', 'pack_options', 'mixins', 'bundles_callback']);
Expand Down
6 changes: 6 additions & 0 deletions test/build/fixtures/license_header/.lmd/default.lmd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"root": "..",
"modules": {
"empty": "empty.js"
}
}
7 changes: 7 additions & 0 deletions test/build/fixtures/license_header/.lmd/disabled.lmd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"root": "..",
"lmd_license_header": false,
"modules": {
"empty": "empty.js"
}
}
7 changes: 7 additions & 0 deletions test/build/fixtures/license_header/.lmd/enabled.lmd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"root": "..",
"lmd_license_header": true,
"modules": {
"empty": "empty.js"
}
}
Empty file.
73 changes: 73 additions & 0 deletions test/build/test.licenseheader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*global describe, it, beforeEach, afterEach*/
/*jshint expr:true*/

var path = require('path'),
Stream = require('stream'),
vow = require('vow'),
expect = require('chai').expect;

var Builder = require('../..');

var fixtures = path.join(__dirname, 'fixtures', 'license_header');

function cfgPath(name) {
return path.join(fixtures, '.lmd', name + '.lmd.json');
}

function readStream(stream) {
var promise = vow.promise(),
body = '';

if (!stream.readable) {
promise.reject(new Error('stream is not readable'));
return promise;
}

stream.on('data', function (chunk) {
body += chunk;
});

stream.on('end', function () {
promise.fulfill(body);
});

stream.on('error', function (error) {
promise.reject(error);
});

return promise;
}

describe('lmd', function() {

describe('license header', function() {
it('is disabled by default', function (done) {
var build = new Builder(cfgPath('default'));

readStream(build)
.then(function (body) {
expect(body).to.match(/^\/\/ This file was/);
})
.then(done, done);
});
it('could be enabled', function (done) {
var build = new Builder(cfgPath('enabled'));

readStream(build)
.then(function (body) {
expect(body).to.match(/^\/\*! .+? MIT License/);
})
.then(done, done);
});
it('could be disabled', function (done) {
var build = new Builder(cfgPath('disabled'));

readStream(build)
.then(function (body) {
expect(body).to.match(/^\/\/ This file was/);
})
.then(done, done);
});

});
});