Skip to content

Commit

Permalink
use nocase: true for licenseFile (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke-zhang-04 authored and mjeanroy committed Jun 6, 2021
1 parent 42908bc commit 6f58420
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ class LicensePlugin {
pkg = pkgJson;

// Read license file, if it exists.
const licenseFile = glob.sync(path.join(dir, 'LICENSE*'))[0];
const cwd = this._cwd || process.cwd();
const absolutePath = path.join(dir, 'LICENSE*');
const relativeToCwd = path.relative(cwd, absolutePath);
const licenseFile = this._findGlob(relativeToCwd, cwd)[0];
if (licenseFile) {
pkg.licenseText = fs.readFileSync(licenseFile, 'utf-8');
}
Expand Down Expand Up @@ -330,6 +333,21 @@ class LicensePlugin {
console.warn(`[${this.name}] -- ${msg}`);
}

/**
* Find given file, matching given pattern.
*
* @param {string} pattern Pattern to look for.
* @param {string} cwd Working directory.
* @returns {*} All match.
* @private
*/
_findGlob(pattern, cwd) {
return glob.sync(pattern, {
cwd,
nocase: true,
});
}

/**
* Read banner from given options and returns it.
*
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/fake-package-8/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
license.md file
15 changes: 15 additions & 0 deletions test/fixtures/fake-package-8/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "fake-package",
"version": "1.0.0",
"description": "Fake package used in unit tests",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Mickael Jeanroy <[email protected]>",
"license": "MIT",
"private": true,
"dependencies": {
"lodash": "*"
}
}
25 changes: 25 additions & 0 deletions test/fixtures/fake-package-8/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016-2020 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.
*/

console.log('fake-package');
13 changes: 13 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ describe('LicensePlugin', () => {
});
});

it('should load pkg including license text from license.md file ignoring case of license file', () => {
const id = path.join(__dirname, 'fixtures', 'fake-package-8', 'src', 'index.js');
const result = plugin.scanDependency(id);

expect(result).not.toBeDefined();
expect(addDependency).toHaveBeenCalled();
expect(plugin._dependencies).toEqual({
'fake-package': Object.assign(fakePackage, {
licenseText: 'license.md file',
}),
});
});

it('should load pkg including license text from LICENSE.txt file', () => {
const id = path.join(__dirname, 'fixtures', 'fake-package-3', 'src', 'index.js');
const result = plugin.scanDependency(id);
Expand Down

0 comments on commit 6f58420

Please sign in to comment.