Skip to content

Commit 393ff79

Browse files
committed
feat: add checkLicense
1 parent 28f24c3 commit 393ff79

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lib/utils/check-license.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { goTry } from "go-go-try";
2+
import { expect, test } from "vitest";
3+
import { checkLicense } from "./check-license";
4+
5+
test("undefined", () => {
6+
const [err] = goTry(() => checkLicense());
7+
expect(err).toBeDefined();
8+
});
9+
10+
test("empty", () => {
11+
const [err] = goTry(() => checkLicense(""));
12+
expect(err).toBeDefined();
13+
});
14+
15+
test("unlicensed", () => {
16+
const [err1] = goTry(() => checkLicense("UNLICENSED"));
17+
const [err2] = goTry(() => checkLicense("unlicensed"));
18+
expect(err1).toBeDefined();
19+
expect(err2).toBeDefined();
20+
});
21+
22+
test("see license in ...", () => {
23+
const [err1] = goTry(() => checkLicense("SEE LICENSE IN ..."));
24+
const [err2] = goTry(() => checkLicense("see license in ..."));
25+
const [err3] = goTry(() => checkLicense("See license in ..."));
26+
expect(err1).toBeDefined();
27+
expect(err2).toBeDefined();
28+
expect(err3).toBeDefined();
29+
});
30+
31+
test("valid licenses", () => {
32+
const [err1] = goTry(() => checkLicense("MIT"));
33+
const [err2] = goTry(() => checkLicense("Apache-2.0"));
34+
const [err3] = goTry(() => checkLicense("MIT AND Apache-2.0"));
35+
const [err4] = goTry(() => checkLicense("(ISC OR GPL-4.0)"));
36+
expect(err1).toBeUndefined();
37+
expect(err2).toBeUndefined();
38+
expect(err3).toBeUndefined();
39+
expect(err4).toBeUndefined();
40+
});

lib/utils/check-license.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function checkLicense(license = "") {
2+
// See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#license
3+
license = license.trim().toLowerCase();
4+
if (!license) throw new Error("no license");
5+
if (license.startsWith("unlicense")) throw new Error("unlicensed");
6+
if (license.startsWith("see ")) throw new Error("external license");
7+
}

0 commit comments

Comments
 (0)