diff --git a/src/license-plugin.js b/src/license-plugin.js index aa35ed1f..86bcac3d 100644 --- a/src/license-plugin.js +++ b/src/license-plugin.js @@ -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'); } @@ -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. * diff --git a/test/fixtures/fake-package-8/license.md b/test/fixtures/fake-package-8/license.md new file mode 100644 index 00000000..65e81201 --- /dev/null +++ b/test/fixtures/fake-package-8/license.md @@ -0,0 +1 @@ +license.md file \ No newline at end of file diff --git a/test/fixtures/fake-package-8/package.json b/test/fixtures/fake-package-8/package.json new file mode 100644 index 00000000..9e08f39b --- /dev/null +++ b/test/fixtures/fake-package-8/package.json @@ -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 ", + "license": "MIT", + "private": true, + "dependencies": { + "lodash": "*" + } +} diff --git a/test/fixtures/fake-package-8/src/index.js b/test/fixtures/fake-package-8/src/index.js new file mode 100644 index 00000000..05935b38 --- /dev/null +++ b/test/fixtures/fake-package-8/src/index.js @@ -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'); diff --git a/test/license-plugin.spec.js b/test/license-plugin.spec.js index ab385ec3..7f2844fe 100644 --- a/test/license-plugin.spec.js +++ b/test/license-plugin.spec.js @@ -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);