Skip to content

Commit

Permalink
fix: handle internal package.json file, with a non valid package name
Browse files Browse the repository at this point in the history
Close #553
  • Loading branch information
mjeanroy committed May 22, 2020
1 parent 16ca8d6 commit 4c283d7
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"magic-string": "0.25.7",
"mkdirp": "1.0.4",
"moment": "2.25.1",
"package-name-regex": "1.0.8",
"spdx-expression-validate": "2.0.0",
"spdx-satisfies": "5.0.0"
},
Expand Down
7 changes: 6 additions & 1 deletion src/license-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import _ from 'lodash';
import moment from 'moment';
import MagicString from 'magic-string';
import glob from 'glob';
import packageNameRegex from 'package-name-regex';

import {Dependency} from './dependency.js';
import {generateBlockComment} from './generate-block-comment.js';
Expand Down Expand Up @@ -176,7 +177,11 @@ class LicensePlugin {

// We are probably in a package.json specifying the type of package (module, cjs).
// Nevertheless, if the package name is not defined, we must not use this `package.json` descriptor.
if (pkgJson.name) {
const license = pkgJson.license || pkgJson.licenses;
const hasLicense = license && license.length > 0;
const name = pkgJson.name;
const isValidPackageName = name && packageNameRegex.test(name);
if (isValidPackageName || hasLicense) {
// We found it!
pkg = pkgJson;

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/fake-package-6/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {test} from './internal/index';
3 changes: 3 additions & 0 deletions test/fixtures/fake-package-6/internal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function test() {
return true;
}
4 changes: 4 additions & 0 deletions test/fixtures/fake-package-6/internal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@fake-package/core/internal",
"private": true
}
15 changes: 15 additions & 0 deletions test/fixtures/fake-package-6/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 <mickael.jeanroy@gmail.com>",
"license": "MIT",
"private": true,
"dependencies": {
"lodash": "*"
}
}
1 change: 1 addition & 0 deletions test/fixtures/fake-package-7/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {test} from './internal/index';
3 changes: 3 additions & 0 deletions test/fixtures/fake-package-7/internal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function test() {
return true;
}
5 changes: 5 additions & 0 deletions test/fixtures/fake-package-7/internal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@fake-package/core/internal",
"private": true,
"license": "MIT"
}
15 changes: 15 additions & 0 deletions test/fixtures/fake-package-7/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 <mickael.jeanroy@gmail.com>",
"license": "MIT",
"private": true,
"dependencies": {
"lodash": "*"
}
}
34 changes: 34 additions & 0 deletions test/license-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,40 @@ describe('LicensePlugin', () => {
});
});

it('should load pkg and going up directories until a valid package name is found', () => {
const id = path.join(__dirname, 'fixtures', 'fake-package-6', 'internal', 'index.js');
const result = plugin.scanDependency(id);

expect(result).not.toBeDefined();
expect(addDependency).toHaveBeenCalled();
expect(plugin._dependencies).toEqual({
'fake-package': fakePackage,
});
});

it('should load pkg and going up directories until a package name with a license is found even with a non valid package name', () => {
const id = path.join(__dirname, 'fixtures', 'fake-package-7', 'internal', 'index.js');
const result = plugin.scanDependency(id);

expect(result).not.toBeDefined();
expect(addDependency).toHaveBeenCalled();
expect(plugin._dependencies).toEqual({
'@fake-package/core/internal': {
name: '@fake-package/core/internal',
maintainers: [],
version: null,
description: null,
repository: null,
homepage: null,
private: true,
license: 'MIT',
licenseText: null,
author: null,
contributors: [],
},
});
});

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

0 comments on commit 4c283d7

Please sign in to comment.