@kingironman2011/license-checker 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # License Checker
2
+
3
+ Scan a Node.js project’s production and development dependencies, print their license information, and copy discovered license files into an output directory.
4
+
5
+ ## Requirements
6
+
7
+ - Node.js 24 or newer
8
+ - A project with dependencies installed
9
+
10
+ ## Install
11
+
12
+ Run the CLI directly with pnpm:
13
+
14
+ ```sh
15
+ pnpm dlx @kingironman2011/license-checker
16
+ ```
17
+
18
+ Or install it in a project:
19
+
20
+ ```sh
21
+ pnpm add -D @kingironman2011/license-checker
22
+ ```
23
+
24
+ Then run:
25
+
26
+ ```sh
27
+ pnpm exec license-checker
28
+ ```
29
+
30
+ The command scans the current directory by default. Pass a project directory to scan another project:
31
+
32
+ ```sh
33
+ pnpm exec license-checker ./path/to/project
34
+ ```
35
+
36
+ ## CLI options
37
+
38
+ ```text
39
+ Usage: license-checker [project-directory] [options]
40
+
41
+ Options:
42
+ --check List dependencies with no license metadata or license file
43
+ --silent, -s Do not print the license report
44
+ --extract, --files <dir> Copy license files into <dir>
45
+ --production, --prod Scan production dependencies only
46
+ --development, --dev Scan development dependencies only
47
+ --all Scan both dependency scopes (default)
48
+ --help, -h Show this help
49
+ ```
50
+
51
+ Examples:
52
+
53
+ ```sh
54
+ # Check production dependencies in the current directory
55
+ pnpm exec license-checker --production
56
+
57
+ # Find dependencies without license metadata or a license file (exits nonzero if any are found)
58
+ pnpm exec license-checker --check
59
+
60
+ # Copy discovered license files to ./licenses
61
+ pnpm exec license-checker --extract ./licenses
62
+ ```
63
+
64
+ When `--check` finds dependencies whose license is unknown and which have no license files, it lists them and exits with status 1. Otherwise it reports that all dependencies have license metadata or license files. `--silent` suppresses report output; it does not change the exit status.
65
+
66
+ ## Library API
67
+
68
+ The package also exports `runLicenseCheck` for use from JavaScript or TypeScript:
69
+
70
+ ```ts
71
+ import { runLicenseCheck } from "@kingironman2011/license-checker";
72
+
73
+ const dependencies = await runLicenseCheck({
74
+ start: process.cwd(),
75
+ production: true,
76
+ });
77
+
78
+ console.log(dependencies);
79
+ ```
80
+
81
+ Options include `start` (project directory), `production`, `development`, and `files` (directory for extracted license files). With neither scope selected, both production and development dependencies are scanned. Each result includes the package name, version, license, license file paths, and dependency scope.
82
+
83
+ ## Development
84
+
85
+ ```sh
86
+ pnpm install
87
+ pnpm build
88
+ pnpm lint
89
+ pnpm format:check
90
+ ```
91
+
@@ -8,6 +8,8 @@ export function getNormalizedArguments(args) {
8
8
  const arg = args[index];
9
9
  if (arg === "--silent" || arg === "-s")
10
10
  options.silent = true;
11
+ else if (arg === "--check")
12
+ options.check = true;
11
13
  else if (arg === "--production" || arg === "--prod") {
12
14
  options.production = true;
13
15
  options.development = false;
@@ -44,6 +46,7 @@ function printHelp() {
44
46
  console.log(`Usage: license-checker [project-directory] [options]
45
47
 
46
48
  Options:
49
+ --check List dependencies with no license metadata or license file
47
50
  --silent, -s Do not print the license report
48
51
  --extract, --files <dir> Copy license files into <dir>
49
52
  --production, --prod Scan production dependencies only
package/dist/cli.js CHANGED
@@ -6,8 +6,24 @@ import { runLicenseCheck } from "./index.js";
6
6
  async function main() {
7
7
  const options = getNormalizedArguments(process.argv.slice(2));
8
8
  const items = await runLicenseCheck(options);
9
+ const missing = options.check
10
+ ? items.filter((item) => item.license.toLowerCase() === "unknown" && item.licenseFiles.length === 0)
11
+ : [];
12
+ if (options.check && missing.length > 0)
13
+ process.exitCode = 1;
9
14
  if (options.silent)
10
15
  return;
16
+ if (options.check) {
17
+ if (missing.length > 0) {
18
+ console.log(`Found ${missing.length} dependencies with missing licenses:\n`);
19
+ for (const item of missing)
20
+ console.log(`${item.name}@${item.version}`);
21
+ }
22
+ else {
23
+ console.log(`All ${items.length} dependencies have license metadata or license files.`);
24
+ }
25
+ return;
26
+ }
11
27
  console.log(`Found ${items.length} dependencies:\n`);
12
28
  for (const item of items)
13
29
  console.log(`${item.name}@${item.version} ${item.license}`);
package/dist/types.d.ts CHANGED
@@ -12,6 +12,7 @@ export interface LicenseCheckOptions {
12
12
  production?: boolean;
13
13
  development?: boolean;
14
14
  silent?: boolean;
15
+ check?: boolean;
15
16
  files?: string;
16
17
  }
17
18
  export interface PackageJson {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kingironman2011/license-checker",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A NPM Package to check other packages licenses and extract them.",
5
5
  "keywords": [
6
6
  "checker",
@@ -1,4 +0,0 @@
1
- import type { LicenseCheckOptions } from "../types.js";
2
- export interface CliOptions extends LicenseCheckOptions {
3
- }
4
- export declare function getNormalizedArguments(args: string[]): CliOptions;
@@ -1,53 +0,0 @@
1
- import path from "node:path";
2
- export function getNormalizedArguments(args) {
3
- const options = {
4
- start: process.cwd(),
5
- silent: false,
6
- };
7
- for (let index = 0; index < args.length; index += 1) {
8
- const arg = args[index];
9
- if (arg === "--silent" || arg === "-s")
10
- options.silent = true;
11
- else if (arg === "--production" || arg === "--prod") {
12
- options.production = true;
13
- options.development = false;
14
- }
15
- else if (arg === "--development" || arg === "--dev") {
16
- options.production = false;
17
- options.development = true;
18
- }
19
- else if (arg === "--all") {
20
- options.production = false;
21
- options.development = false;
22
- }
23
- else if (arg === "--extract" || arg === "-o" || arg === "--files") {
24
- const value = args[index + 1];
25
- if (!value || value.startsWith("-"))
26
- throw new Error(`${arg} requires a directory`);
27
- options.files = path.resolve(value);
28
- index += 1;
29
- }
30
- else if (arg === "--help" || arg === "-h") {
31
- printHelp();
32
- process.exit(0);
33
- }
34
- else if (arg.startsWith("-")) {
35
- throw new Error(`Unknown option: ${arg}`);
36
- }
37
- else {
38
- options.start = path.resolve(arg);
39
- }
40
- }
41
- return options;
42
- }
43
- function printHelp() {
44
- console.log(`Usage: license-checker [project-directory] [options]
45
-
46
- Options:
47
- --silent, -s Do not print the license report
48
- --extract, --files <dir> Copy license files into <dir>
49
- --production, --prod Scan production dependencies only
50
- --development, --dev Scan development dependencies only
51
- --all Scan both dependency scopes (default)
52
- --help, -h Show this help`);
53
- }
package/dist/lib/cli.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
package/dist/lib/cli.js DELETED
@@ -1,23 +0,0 @@
1
- #!/usr/bin/env node
2
- import path from "node:path";
3
- import { fileURLToPath } from "node:url";
4
- import { getNormalizedArguments } from "./cli/options.js";
5
- import { runLicenseCheck } from "./index.js";
6
- async function main() {
7
- const options = getNormalizedArguments(process.argv.slice(2));
8
- const items = await runLicenseCheck(options);
9
- if (options.silent)
10
- return;
11
- console.log(`Found ${items.length} dependencies:\n`);
12
- for (const item of items)
13
- console.log(`${item.name}@${item.version} ${item.license}`);
14
- if (options.files)
15
- console.log(`\nLicense files copied to ${options.files}`);
16
- }
17
- const invokedPath = process.argv[1] ? path.resolve(process.argv[1]) : "";
18
- if (invokedPath === fileURLToPath(import.meta.url)) {
19
- main().catch((error) => {
20
- console.error(error instanceof Error ? error.message : error);
21
- process.exitCode = 1;
22
- });
23
- }
@@ -1,2 +0,0 @@
1
- import type { DependencyLicense, DependencyScope } from "../types.js";
2
- export declare function readInstalledPackages(projectDir: string, scope: Exclude<DependencyScope, "both">): Promise<DependencyLicense[]>;
@@ -1,66 +0,0 @@
1
- import { readFile, realpath } from "node:fs/promises";
2
- import path from "node:path";
3
- async function resolveDependency(parentDir, name) {
4
- let currentDir = parentDir;
5
- while (true) {
6
- const candidate = path.join(currentDir, "node_modules", name, "package.json");
7
- try {
8
- await readFile(candidate, "utf8");
9
- return path.dirname(candidate);
10
- }
11
- catch {
12
- const parent = path.dirname(currentDir);
13
- if (parent === currentDir)
14
- return undefined;
15
- currentDir = parent;
16
- }
17
- }
18
- }
19
- export async function readInstalledPackages(projectDir, scope) {
20
- const root = JSON.parse(await readFile(path.join(projectDir, "package.json"), "utf8"));
21
- const roots = scope === "production"
22
- ? { ...root.dependencies, ...root.optionalDependencies }
23
- : (root.devDependencies ?? {});
24
- const found = new Map();
25
- const visited = new Set();
26
- async function visit(packagePath, dependencyName) {
27
- let realPackagePath;
28
- try {
29
- realPackagePath = await realpath(packagePath);
30
- }
31
- catch {
32
- return;
33
- }
34
- if (visited.has(realPackagePath))
35
- return;
36
- visited.add(realPackagePath);
37
- const pkg = JSON.parse(await readFile(path.join(packagePath, "package.json"), "utf8"));
38
- const name = pkg.name ?? dependencyName;
39
- const version = pkg.version ?? "unknown";
40
- const key = `${name}@${version}`;
41
- found.set(key, {
42
- name,
43
- version,
44
- license: pkg.license && typeof pkg.license === "string"
45
- ? pkg.license
46
- : ((typeof pkg.license === "object" ? pkg.license.type : undefined) ??
47
- pkg.licenses?.map(({ type }) => type ?? "Unknown").join(" OR ") ??
48
- "Unknown"),
49
- scope,
50
- packagePath,
51
- licenseFiles: [],
52
- });
53
- const dependencies = { ...pkg.dependencies, ...pkg.optionalDependencies };
54
- for (const childName of Object.keys(dependencies).toSorted()) {
55
- const childPath = await resolveDependency(packagePath, childName);
56
- if (childPath)
57
- await visit(childPath, childName);
58
- }
59
- }
60
- for (const name of Object.keys(roots).toSorted()) {
61
- const packagePath = await resolveDependency(projectDir, name);
62
- if (packagePath)
63
- await visit(packagePath, name);
64
- }
65
- return [...found.values()];
66
- }
@@ -1,3 +0,0 @@
1
- import type { DependencyLicense, LicenseCheckOptions } from "./types.js";
2
- export type { DependencyLicense, DependencyScope, LicenseCheckOptions, } from "./types.js";
3
- export declare function runLicenseCheck(options?: LicenseCheckOptions): Promise<DependencyLicense[]>;
package/dist/lib/index.js DELETED
@@ -1,28 +0,0 @@
1
- import path from "node:path";
2
- import { readInstalledPackages } from "./dependencies/read-installed-packages.js";
3
- import { collectLicenseResults } from "./licenses/collect-license-results.js";
4
- import { writeIndividualLicenseFilesToDir } from "./output/write-output.js";
5
- export async function runLicenseCheck(options = {}) {
6
- const projectDir = path.resolve(options.start ?? process.cwd());
7
- const scopes = options.production
8
- ? ["production"]
9
- : options.development
10
- ? ["development"]
11
- : ["production", "development"];
12
- const results = new Map();
13
- for (const scope of scopes) {
14
- const packages = await collectLicenseResults(await readInstalledPackages(projectDir, scope));
15
- for (const item of packages) {
16
- const key = `${item.name}@${item.version}`;
17
- const existing = results.get(key);
18
- if (existing)
19
- existing.scope = "both";
20
- else
21
- results.set(key, item);
22
- }
23
- }
24
- const items = [...results.values()].toSorted((a, b) => a.name.localeCompare(b.name) || a.version.localeCompare(b.version));
25
- if (options.files)
26
- await writeIndividualLicenseFilesToDir(path.resolve(options.files), items);
27
- return items;
28
- }
@@ -1,2 +0,0 @@
1
- import type { DependencyLicense } from "../types.js";
2
- export declare function collectLicenseResults(packages: DependencyLicense[]): Promise<DependencyLicense[]>;
@@ -1,12 +0,0 @@
1
- import { readdir } from "node:fs/promises";
2
- export async function collectLicenseResults(packages) {
3
- for (const item of packages) {
4
- const entries = await readdir(item.packagePath, { withFileTypes: true });
5
- item.licenseFiles = entries
6
- .filter((entry) => entry.isFile() &&
7
- /^(license|licence|copying|notice)(\.|$)/i.test(entry.name))
8
- .map((entry) => entry.name)
9
- .toSorted((a, b) => a.localeCompare(b));
10
- }
11
- return packages.toSorted((a, b) => a.name.localeCompare(b.name) || a.version.localeCompare(b.version));
12
- }
@@ -1,2 +0,0 @@
1
- import type { DependencyLicense } from "../types.js";
2
- export declare function writeIndividualLicenseFilesToDir(outputDir: string, items: DependencyLicense[]): Promise<void>;
@@ -1,15 +0,0 @@
1
- import { cp, mkdir, writeFile } from "node:fs/promises";
2
- import path from "node:path";
3
- export async function writeIndividualLicenseFilesToDir(outputDir, items) {
4
- for (const item of items) {
5
- const safeName = item.name.replaceAll("/", "+");
6
- const destination = path.join(outputDir, item.scope, `${safeName}@${item.version}`);
7
- await mkdir(destination, { recursive: true });
8
- for (const file of item.licenseFiles) {
9
- await cp(path.join(item.packagePath, file), path.join(destination, file));
10
- }
11
- if (item.licenseFiles.length === 0) {
12
- await writeFile(path.join(destination, "LICENSE-INFO.txt"), `License metadata: ${item.license}\n`);
13
- }
14
- }
15
- }
@@ -1,29 +0,0 @@
1
- export type DependencyScope = "production" | "development" | "both";
2
- export interface DependencyLicense {
3
- name: string;
4
- version: string;
5
- license: string;
6
- scope: DependencyScope;
7
- packagePath: string;
8
- licenseFiles: string[];
9
- }
10
- export interface LicenseCheckOptions {
11
- start?: string;
12
- production?: boolean;
13
- development?: boolean;
14
- silent?: boolean;
15
- files?: string;
16
- }
17
- export interface PackageJson {
18
- name?: string;
19
- version?: string;
20
- license?: string | {
21
- type?: string;
22
- };
23
- licenses?: Array<{
24
- type?: string;
25
- }>;
26
- dependencies?: Record<string, string>;
27
- optionalDependencies?: Record<string, string>;
28
- devDependencies?: Record<string, string>;
29
- }
package/dist/lib/types.js DELETED
@@ -1 +0,0 @@
1
- export {};