Skip to content

Commit

Permalink
fix: fails to check monorepo with non-published packages (#89)
Browse files Browse the repository at this point in the history
* fix: fails to check monorepo with non-published packages

* chore: add test
  • Loading branch information
splix authored and ofrobots committed May 29, 2019
1 parent b41bb7e commit 96a7279
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export class LicenseChecker extends EventEmitter {
private readonly processedPackages: Set<string> = new Set();
// Cache for packageName@version's that failed for fetching.
private readonly failedPackages: Set<string> = new Set();
// Local packages, for monorepo
private readonly localPackages: Set<string> = new Set<string>();
private readonly opts: LicenseCheckerOptions;
private config: config.Config = {};
// Licenses in this expression must be valid license IDs defined in
Expand Down Expand Up @@ -235,6 +237,11 @@ export class LicenseChecker extends EventEmitter {
const spec = `${packageName}@${versionSpec}`;
if (this.failedPackages.has(spec)) return;

// remove tilde/caret to check for an exact version, ^0.5.0-rc.0 becomes 0.5.0-rc.0
const version = versionSpec.replace(/^[^~]/, '');
// if the dependency is a local package then skip verification at this step. will be checked independently
if (this.localPackages.has(`${packageName}@${version}`)) return;

try {
const json = await packageJson(packageName, {
version: versionSpec,
Expand Down Expand Up @@ -366,6 +373,13 @@ export class LicenseChecker extends EventEmitter {
if (packageJsons.length === 0) {
console.log('No package.json files have been found.');
}
for (const pj of packageJsons) {
const content = await fsReadFile(pj, 'utf8');
const json = JSON.parse(content);
if (json && json.name && json.version) {
this.localPackages.add(`${json.name}@${json.version}`);
}
}
for (const pj of packageJsons) {
this.emit('package.json', pj);
const content = await fsReadFile(pj, 'utf8');
Expand Down
53 changes: 53 additions & 0 deletions test/checker-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,59 @@ test.serial('local monorepo directory is checked correctly', t => {
);
});

test.serial('local monorepo with local dependency is checked correctly', t => {
const topLevelPackageJson = JSON.stringify({
name: 'hello',
version: '1.0.0',
license: 'Apache-2.0',
dependencies: {
'hello-sub': '^1.0.0',
},
});
const subPackageJson = JSON.stringify({
name: 'hello-sub',
version: '1.0.0',
license: 'Apache-2.0',
dependencies: {
baz: '^7.0.0',
},
});
return withFixtures(
{
'path/to/dir': {
'package.json': topLevelPackageJson,
'another-file': 'hello, world',
packages: {
'sub-package': {
'package.json': subPackageJson,
},
},
},
},
async () => {
requestedPackages = [];
const nonGreenPackages: string[] = [];
const packageJsonPaths: string[] = [];
const checker = new LicenseChecker();
checker
.on('non-green-license', arg => {
nonGreenPackages.push(`${arg.packageName}@${arg.version}`);
})
.on('package.json', filePath => {
packageJsonPaths.push(filePath);
});
await checker.checkLocalDirectory('path/to/dir');
console.log(JSON.stringify(requestedPackages, null, 2));
t.deepEqual(requestedPackages, ['baz@^7.0.0']);
t.deepEqual(nonGreenPackages, ['baz@7.8.9']);
t.deepEqual(packageJsonPaths, [
'path/to/dir/package.json',
'path/to/dir/packages/sub-package/package.json',
]);
}
);
});

test.serial('package whitelist should be respected (local repo)', t => {
const packageJson = JSON.stringify({
name: 'hello',
Expand Down

0 comments on commit 96a7279

Please sign in to comment.