Skip to content

Commit

Permalink
feat: the default banner is the LICENSE file
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 17, 2016
1 parent 937188f commit 635e2c0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 49 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Mickael Jeanroy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
107 changes: 58 additions & 49 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,62 +31,71 @@ const MagicString = require('magic-string');

const EOL = '\n';

module.exports = (options) => {
module.exports = (options = {}) => {
return {
transformBundle(code) {
const file = path.resolve(options.file);
const file = options.file || 'LICENSE';
const filePath = path.resolve(file);

const deferred = Q.defer();

fs.readFile(file, 'utf-8', (error, content) => {
if (error) {
return deferred.reject(error);
fs.exists(filePath, (exists) => {
if (!exists) {
deferred.reject(new Error(`File ${filePath} does not exist`));
return;
}

// Import the `package.json` of the project.
// The `resolve` function
const pkgPath = path.resolve(process.cwd(), 'package.json');
const pkg = require(pkgPath);

// Create the template function with lodash.
const tmpl = _.template(content);

// Generate the banner.
let banner = tmpl({_, moment, pkg});

// Make a block comment if needed
const trimmedBanner = banner.trim();
const start = trimmedBanner.slice(0, 3);
if (start !== '/**') {
const bannerContent = trimmedBanner
.split(`${EOL}`)
.map((line) => _.trimEnd(` * ${line}`))
.join(`${EOL}`);

banner = `/**${EOL}${bannerContent}${EOL} */${EOL}`;
}

// Create a magicString: do not manipulate the string directly since it
// will be used to generate the sourcemap.
const magicString = new MagicString(code);

// Prepend the banner.
magicString.prepend(`${banner}${EOL}`);

// Create the result object.
const result = {
code: magicString.toString(),
};

// Add sourceMap information if it is enabled.
if (options.sourceMap !== false) {
result.map = magicString.generateMap({
hires: true,
});
}

// Everything is ok.
return deferred.resolve(result);
fs.readFile(filePath, 'utf-8', (error, content) => {
if (error) {
deferred.reject(error);
return;
}

// Import the `package.json` of the project.
// Use `process.cdwd()`
const pkgPath = path.resolve(process.cwd(), 'package.json');
const pkg = require(pkgPath);

// Create the template function with lodash.
const tmpl = _.template(content);

// Generate the banner.
let banner = tmpl({_, moment, pkg});

// Make a block comment if needed
const trimmedBanner = banner.trim();
const start = trimmedBanner.slice(0, 3);
if (start !== '/**') {
const bannerContent = trimmedBanner
.split(`${EOL}`)
.map((line) => _.trimEnd(` * ${line}`))
.join(`${EOL}`);

banner = `/**${EOL}${bannerContent}${EOL} */${EOL}`;
}

// Create a magicString: do not manipulate the string directly since it
// will be used to generate the sourcemap.
const magicString = new MagicString(code);

// Prepend the banner.
magicString.prepend(`${banner}${EOL}`);

// Create the result object.
const result = {
code: magicString.toString(),
};

// Add sourceMap information if it is enabled.
if (options.sourceMap !== false) {
result.map = magicString.generateMap({
hires: true,
});
}

// Everything is ok.
deferred.resolve(result);
});
});

return deferred.promise;
Expand Down
15 changes: 15 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ describe('rollup-plugin-license', () => {
});
});

it('should prepend default LICENSE file banner to bundle', (done) => {
const instance = plugin();

const code = 'var foo = 0;';

const promise = instance.transformBundle(code);

promise.then((result) => {
expect(result).toBeDefined();
expect(result.code).toBeDefined();
expect(result.code).toContain('The MIT License (MIT)');
done();
});
});

it('should prepend banner and create block comment', (done) => {
const instance = plugin({
file: path.join(__dirname, 'fixtures', 'banner.txt'),
Expand Down

0 comments on commit 635e2c0

Please sign in to comment.