Skip to content

Commit

Permalink
fix: fix searching for license/notice files with symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Jun 23, 2024
1 parent 7dea8ee commit 7181af4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
7 changes: 2 additions & 5 deletions src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,12 @@ class LicensePlugin {
// We found it!
pkg = pkgJson;

// Read license & notice files, if it exists.
const cwd = this._cwd || process.cwd();

const licenseText = readFile(dir, cwd, ['license', 'licence']);
const licenseText = readFile(dir, ['license', 'licence']);
if (licenseText) {
pkg.licenseText = licenseText;
}

const noticeText = readFile(dir, cwd, 'notice');
const noticeText = readFile(dir, 'notice');
if (noticeText) {
pkg.noticeText = noticeText;
}
Expand Down
14 changes: 7 additions & 7 deletions src/read-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ const pathsMatch = (target) => {
* Find file and returns its content if file exists.
*
* @param {string} dir File directory.
* @param {string} cwd Working directory.
* @param {string|Array<string>} names Potential filenames.
* @returns {string|null} File content, or `null` if file does not exist.
*/
export function readFile(dir, cwd, names) {
export function readFile(dir, names) {
const inputs = _.castArray(names);
// eslint-disable-next-line new-cap
const finder = new fdir();

for (let i = 0; i < inputs.length; ++i) {
const input = inputs[i];
const absolutePath = path.join(dir, input);
const relativeToCwd = path.relative(cwd, absolutePath);
const relativeToDir = path.relative(dir, absolutePath);

const findings = finder
.withRelativePaths()
.filter(pathsMatch(relativeToCwd))
.crawl(cwd)
.withSymlinks()
.withMaxDepth(input.split(path.sep).length)
.filter(pathsMatch(relativeToDir))
.crawl(dir)
.sync();

const firstPath = findings[0];

if (firstPath) {
const file = path.join(cwd, firstPath);
const file = path.join(dir, firstPath);
return fs.readFileSync(file, 'utf-8');
}
}
Expand Down
35 changes: 29 additions & 6 deletions test/read-file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,53 @@
*/

import path from 'path';
import fs from 'fs';
import tmp from 'tmp';
import { readFile } from '../src/read-file';

describe('readFile', () => {
let tmpDir;

beforeEach(() => {
tmpDir = tmp.dirSync({
unsafeCleanup: true,
});
});

afterEach(() => {
tmpDir.removeCallback();
});

it('should read file using exact name', () => {
const dir = path.join(__dirname, 'fixtures', 'fake-package-2');
const cwd = __dirname;
const name = 'LICENSE.md';
const content = readFile(dir, cwd, name);
const content = readFile(dir, name);
expect(content).toEqual('LICENSE.md file');
});

it('should read file using exact name following symlink', () => {
const dirName = 'fake-package-2';
const dir = path.join(__dirname, 'fixtures', dirName);
const dirLink = path.join(tmpDir.name, `link-to-${dirName}`);

fs.symlinkSync(dir, dirLink, 'dir');

const name = 'LICENSE.md';
const content = readFile(dirLink, name);
expect(content).toEqual('LICENSE.md file');
});

it('should read file using non matching case name', () => {
const dir = path.join(__dirname, 'fixtures', 'fake-package-2');
const cwd = __dirname;
const name = 'license.md';
const content = readFile(dir, cwd, name);
const content = readFile(dir, name);
expect(content).toEqual('LICENSE.md file');
});

it('should read file using name without extension', () => {
const dir = path.join(__dirname, 'fixtures', 'fake-package-2');
const cwd = __dirname;
const name = 'license';
const content = readFile(dir, cwd, name);
const content = readFile(dir, name);
expect(content).toEqual('LICENSE.md file');
});
});

0 comments on commit 7181af4

Please sign in to comment.