rolldown-license-plugin 3.0.3 → 3.0.4
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/dist/index.d.ts +2 -1
- package/dist/index.js +30 -27
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Plugin, PluginContext } from "rolldown";
|
|
2
2
|
|
|
3
3
|
//#region index.d.ts
|
|
4
|
+
declare const defaultMatch: RegExp;
|
|
4
5
|
/** License information for a single bundled dependency */
|
|
5
6
|
type LicenseInfo = {
|
|
6
7
|
/** Package name from package.json */name: string; /** Package version from package.json */
|
|
@@ -31,4 +32,4 @@ declare const licensePlugin: ({
|
|
|
31
32
|
failOnUnlicensed
|
|
32
33
|
}: RolldownLicensePluginOpts) => Plugin;
|
|
33
34
|
//#endregion
|
|
34
|
-
export { LicenseInfo, RolldownLicensePluginOpts, findPkgRoot, licensePlugin, wrap };
|
|
35
|
+
export { LicenseInfo, RolldownLicensePluginOpts, defaultMatch, findPkgRoot, licensePlugin, wrap };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
2
2
|
import { join, sep } from "node:path";
|
|
3
3
|
|
|
4
4
|
//#region index.ts
|
|
@@ -7,7 +7,7 @@ const defaultMatch = /^((UN)?LICEN(S|C)E|COPYING).*$/i;
|
|
|
7
7
|
function wrap(text, width) {
|
|
8
8
|
const lines = [];
|
|
9
9
|
for (const rawLine of text.replace(/\r/g, "").split("\n")) {
|
|
10
|
-
const trimmed = rawLine.replace(/\t/g, (
|
|
10
|
+
const trimmed = rawLine.replace(/\t/g, (_tab, offset) => " ".repeat(8 - offset % 8)).trim();
|
|
11
11
|
if (trimmed.length <= width) {
|
|
12
12
|
lines.push(trimmed);
|
|
13
13
|
continue;
|
|
@@ -42,17 +42,17 @@ const nmSep = "/node_modules/";
|
|
|
42
42
|
const needsPathNormalize = sep !== "/";
|
|
43
43
|
/** Resolve the package root directory from a file path inside node_modules */
|
|
44
44
|
function findPkgRoot(fsPath) {
|
|
45
|
-
const
|
|
46
|
-
const nmIdx =
|
|
45
|
+
const normalized = needsPathNormalize ? fsPath.replaceAll(sep, "/") : fsPath;
|
|
46
|
+
const nmIdx = normalized.lastIndexOf(nmSep);
|
|
47
47
|
if (nmIdx === -1) return null;
|
|
48
48
|
const base = nmIdx + 14;
|
|
49
|
-
const firstSlash =
|
|
50
|
-
if (
|
|
49
|
+
const firstSlash = normalized.indexOf("/", base);
|
|
50
|
+
if (normalized.startsWith("@", base)) {
|
|
51
51
|
if (firstSlash === -1) return null;
|
|
52
|
-
const secondSlash =
|
|
53
|
-
return secondSlash === -1 ?
|
|
52
|
+
const secondSlash = normalized.indexOf("/", firstSlash + 1);
|
|
53
|
+
return secondSlash === -1 ? normalized : normalized.slice(0, secondSlash);
|
|
54
54
|
}
|
|
55
|
-
return firstSlash === -1 ?
|
|
55
|
+
return firstSlash === -1 ? normalized : normalized.slice(0, firstSlash);
|
|
56
56
|
}
|
|
57
57
|
/** Rolldown plugin that extracts license information from bundled dependencies */
|
|
58
58
|
const licensePlugin = ({ done, match = defaultMatch, wrapLicenseText, allow, failOnViolation = false, failOnUnlicensed = false }) => ({
|
|
@@ -68,29 +68,32 @@ const licensePlugin = ({ done, match = defaultMatch, wrapLicenseText, allow, fai
|
|
|
68
68
|
if (root) roots.add(root);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
const reads = await Promise.all(Array.from(roots, async (dir) => {
|
|
72
|
+
const [pkgRaw, entries] = await Promise.all([readFile(join(dir, "package.json"), "utf8").catch(() => null), readdir(dir).catch(() => null)]);
|
|
73
|
+
if (pkgRaw === null) return null;
|
|
74
74
|
let pkgJson;
|
|
75
75
|
try {
|
|
76
|
-
pkgJson = JSON.parse(
|
|
76
|
+
pkgJson = JSON.parse(pkgRaw);
|
|
77
77
|
} catch {
|
|
78
|
-
|
|
78
|
+
return null;
|
|
79
79
|
}
|
|
80
|
-
if (!pkgJson.name)
|
|
80
|
+
if (!pkgJson.name) return null;
|
|
81
|
+
const found = entries?.find((entry) => match.test(entry));
|
|
82
|
+
const licenseText = found ? await readFile(join(dir, found), "utf8").catch(() => "") : "";
|
|
83
|
+
return {
|
|
84
|
+
pkgJson,
|
|
85
|
+
licenseText
|
|
86
|
+
};
|
|
87
|
+
}));
|
|
88
|
+
const seen = /* @__PURE__ */ new Set();
|
|
89
|
+
const licenses = [];
|
|
90
|
+
for (const item of reads) {
|
|
91
|
+
if (!item) continue;
|
|
92
|
+
const { pkgJson } = item;
|
|
81
93
|
const key = `${pkgJson.name}@${pkgJson.version ?? ""}`;
|
|
82
94
|
if (seen.has(key)) continue;
|
|
83
95
|
seen.add(key);
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
const licensePath = join(dir, "LICENSE");
|
|
87
|
-
if (existsSync(licensePath)) licenseText = readFileSync(licensePath, "utf8");
|
|
88
|
-
else {
|
|
89
|
-
const found = readdirSync(dir).find((entry) => match.test(entry));
|
|
90
|
-
if (found) licenseText = readFileSync(join(dir, found), "utf8");
|
|
91
|
-
}
|
|
92
|
-
} catch {}
|
|
93
|
-
if (wrapLicenseText && licenseText) licenseText = wrap(licenseText, wrapLicenseText).trim();
|
|
96
|
+
const licenseText = wrapLicenseText && item.licenseText ? wrap(item.licenseText, wrapLicenseText).trim() : item.licenseText;
|
|
94
97
|
licenses.push({
|
|
95
98
|
name: pkgJson.name,
|
|
96
99
|
version: pkgJson.version ?? "",
|
|
@@ -98,7 +101,7 @@ const licensePlugin = ({ done, match = defaultMatch, wrapLicenseText, allow, fai
|
|
|
98
101
|
licenseText
|
|
99
102
|
});
|
|
100
103
|
}
|
|
101
|
-
licenses.sort((a, b) => a.name
|
|
104
|
+
licenses.sort((a, b) => a.name.localeCompare(b.name));
|
|
102
105
|
if (allow) {
|
|
103
106
|
const errors = [];
|
|
104
107
|
for (const entry of licenses) {
|
|
@@ -115,4 +118,4 @@ const licensePlugin = ({ done, match = defaultMatch, wrapLicenseText, allow, fai
|
|
|
115
118
|
});
|
|
116
119
|
|
|
117
120
|
//#endregion
|
|
118
|
-
export { findPkgRoot, licensePlugin, wrap };
|
|
121
|
+
export { defaultMatch, findPkgRoot, licensePlugin, wrap };
|
package/package.json
CHANGED