Skip to content

Commit

Permalink
feat(core): find license in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Angela committed Oct 14, 2024
1 parent 555e0b6 commit 6e3aacd
Show file tree
Hide file tree
Showing 13 changed files with 1,038 additions and 180 deletions.
14 changes: 13 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,17 @@
"formatter": {
"quoteStyle": "double"
}
}
},
"overrides": [
{
"include": ["packages/core/**"],
"linter": {
"rules": {
"nursery": {
"useExplicitFunctionReturnType": "error"
}
}
}
}
]
}
164 changes: 4 additions & 160 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/core/license.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare global {
type AvailableLicense = (typeof licenses)[number];
type License = AvailableLicense | AvailableLicense[] | undefined;
}

export {};
7 changes: 3 additions & 4 deletions packages/core/src/audit-licenses.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { checkLicenseStatus, type LicenseStatus } from "./check-license-status";
import { extractPackageName, readPackageJson } from "./file-utils";
import { findLicense } from "./find-license";
import { findLicense } from "./license-finder/find-license";
import { getChildDependencies } from "./get-child-dependencies";

interface PackageInfo {
package: string;
path: string;
license: string | undefined;
license: License;
licensePath: string | undefined;
status: LicenseStatus;
}
Expand All @@ -28,7 +28,7 @@ function auditLicenses(packagePaths: string[]) {
}
const packageJson = readPackageJson(packagePath);

const { license, licensePath } = findLicense(packagePath);
const { license, licensePath } = findLicense(packageJson, packagePath);

const status = checkLicenseStatus(license);

Expand Down Expand Up @@ -72,7 +72,6 @@ const packagePaths: string[] = [
"/Users/angelikajeziorska/Documents/projects/license-auditor/node_modules/husky",
"/Users/angelikajeziorska/Documents/projects/license-auditor/node_modules/turbo",
"/Users/angelikajeziorska/Documents/projects/license-auditor/node_modules/typescript",
"/Users/angelikajeziorska/Documents/projects/license-auditor/node_modules/@license-auditor/eslint-config",
"/Users/angelikajeziorska/Documents/projects/license-auditor/node_modules/@total-typescript/ts-reset",
];

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/check-license-status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type LicenseStatus = "allowed" | "disallowed" | "unknown";

export function checkLicenseStatus(license: string | undefined): LicenseStatus {
export function checkLicenseStatus(license: License): LicenseStatus {
// todo: compare license with the whitelist/banlist provided through configuration
return "allowed";
}
12 changes: 8 additions & 4 deletions packages/core/src/file-utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import * as fs from "node:fs";
import * as path from "node:path";

export function readPackageJson(packagePath: string) {
export function readPackageJson(packagePath: string): object {
const packageJsonPath = path.join(packagePath, "package.json");

if (fs.existsSync(packageJsonPath)) {
const packageJsonContent = fs.readFileSync(packageJsonPath, "utf-8");
return JSON.parse(packageJsonContent);
const parsedPackageJson = JSON.parse(packageJsonContent);
if (!!parsedPackageJson && typeof parsedPackageJson === "object") {
return parsedPackageJson;
}
}
console.warn(`package.json not found for package at ${packageJsonPath}`);
// unsure how often such case happens and whether the license verification should be skipped
throw new Error(`package.json not found for package at ${packagePath}`);
}

// done this way to avoid reading package.json when checking for an existing value in Map
// if it proves unreliable reading package.json will be inevitable
export function extractPackageName(packagePath: string) {
export function extractPackageName(packagePath: string): string {
const baseName = path.basename(packagePath);
const parentName = path.basename(path.dirname(packagePath));

Expand Down
10 changes: 0 additions & 10 deletions packages/core/src/find-license.ts

This file was deleted.

Loading

0 comments on commit 6e3aacd

Please sign in to comment.