From 66fbade3668540cb96baec3cbc56a9e81319c3c2 Mon Sep 17 00:00:00 2001 From: Leonid Borisenko Date: Wed, 1 Jan 2014 12:49:14 +0300 Subject: [PATCH] Setting to include LMD license into output Configuration setting is named 'lmd_license_header' and could be set to true. License is prepended to output as multiline comment with '/*!' starting token (so that it could be bypassed by minifiers). --- bin/lmd_builder.js | 15 +++- lib/lmd_common.js | 2 +- .../license_header/.lmd/default.lmd.json | 6 ++ .../license_header/.lmd/disabled.lmd.json | 7 ++ .../license_header/.lmd/enabled.lmd.json | 7 ++ test/build/fixtures/license_header/empty.js | 0 test/build/test.licenseheader.js | 73 +++++++++++++++++++ 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 test/build/fixtures/license_header/.lmd/default.lmd.json create mode 100644 test/build/fixtures/license_header/.lmd/disabled.lmd.json create mode 100644 test/build/fixtures/license_header/.lmd/enabled.lmd.json create mode 100644 test/build/fixtures/license_header/empty.js create mode 100644 test/build/test.licenseheader.js diff --git a/bin/lmd_builder.js b/bin/lmd_builder.js index 0edba99..74ef699 100644 --- a/bin/lmd_builder.js +++ b/bin/lmd_builder.js @@ -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 + ',' + @@ -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', @@ -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) { diff --git a/lib/lmd_common.js b/lib/lmd_common.js index bea2941..25b6f2c 100644 --- a/lib/lmd_common.js +++ b/lib/lmd_common.js @@ -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']); diff --git a/test/build/fixtures/license_header/.lmd/default.lmd.json b/test/build/fixtures/license_header/.lmd/default.lmd.json new file mode 100644 index 0000000..1808b0a --- /dev/null +++ b/test/build/fixtures/license_header/.lmd/default.lmd.json @@ -0,0 +1,6 @@ +{ + "root": "..", + "modules": { + "empty": "empty.js" + } +} diff --git a/test/build/fixtures/license_header/.lmd/disabled.lmd.json b/test/build/fixtures/license_header/.lmd/disabled.lmd.json new file mode 100644 index 0000000..b2dec66 --- /dev/null +++ b/test/build/fixtures/license_header/.lmd/disabled.lmd.json @@ -0,0 +1,7 @@ +{ + "root": "..", + "lmd_license_header": false, + "modules": { + "empty": "empty.js" + } +} diff --git a/test/build/fixtures/license_header/.lmd/enabled.lmd.json b/test/build/fixtures/license_header/.lmd/enabled.lmd.json new file mode 100644 index 0000000..dc77383 --- /dev/null +++ b/test/build/fixtures/license_header/.lmd/enabled.lmd.json @@ -0,0 +1,7 @@ +{ + "root": "..", + "lmd_license_header": true, + "modules": { + "empty": "empty.js" + } +} diff --git a/test/build/fixtures/license_header/empty.js b/test/build/fixtures/license_header/empty.js new file mode 100644 index 0000000..e69de29 diff --git a/test/build/test.licenseheader.js b/test/build/test.licenseheader.js new file mode 100644 index 0000000..9e3b03c --- /dev/null +++ b/test/build/test.licenseheader.js @@ -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); + }); + + }); +});