Skip to content

Commit

Permalink
Separate license and third-party licenses (#17073)
Browse files Browse the repository at this point in the history
* Separate license and third-party licenses

* Minor tweak

* Line break

* Add a dot

* DEBUG pkg-pr-new

* Revert "DEBUG pkg-pr-new"

This reverts commit ade850d.
  • Loading branch information
fisker authored Feb 9, 2025
1 parent 0c2b4bb commit 0fcd5db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { outdent } from "outdent";
import rollupPluginLicense from "rollup-plugin-license";
import { DIST_DIR, PROJECT_ROOT } from "../utils/index.js";

const PROJECT_LICENSE_FILE = path.join(PROJECT_ROOT, "LICENSE");
const LICENSE_FILE = path.join(DIST_DIR, "LICENSE");
const separator = `\n${"-".repeat(40)}\n\n`;

function toBlockQuote(text) {
Expand Down Expand Up @@ -44,7 +42,7 @@ function getDependencies(results) {
return dependencies;
}

async function getLicenseText(dependencies) {
function getLicenseText(dependencies) {
dependencies = dependencies.filter(
(dependency, index) =>
// Exclude ourself
Expand All @@ -63,8 +61,6 @@ async function getLicenseText(dependencies) {
dependencyA.version.localeCompare(dependencyB.version),
);

const prettierLicense = await fs.readFile(PROJECT_LICENSE_FILE, "utf8");

const licenses = [
...new Set(
dependencies
Expand All @@ -73,31 +69,16 @@ async function getLicenseText(dependencies) {
),
];

const text = outdent`
# Prettier license
Prettier is released under the MIT license:
const head = outdent`
# Licenses of bundled dependencies
${prettierLicense.trim()}
The published Prettier artifact additionally contains code with the following licenses:
${new Intl.ListFormat("en-US", { type: "conjunction" }).format(licenses)}.
`;

if (licenses.length === 0) {
return text;
}

const parts = [
text,
outdent`
## Licenses of bundled dependencies
The published Prettier artifact additionally contains code with the following licenses:
${licenses.join(", ")}
`,
];

const content = dependencies
.map((dependency) => {
let text = `### ${dependency.name}@v${dependency.version}\n`;
let text = `## ${dependency.name}@v${dependency.version}\n`;

const meta = [];

Expand All @@ -106,16 +87,16 @@ async function getLicenseText(dependencies) {
}

if (dependency.license) {
meta.push(`License: ${dependency.license}`);
meta.push(`License: ${dependency.license} `);
}
if (dependency.homepage) {
meta.push(`Homepage: <${dependency.homepage}>`);
meta.push(`Homepage: <${dependency.homepage}> `);
}
if (dependency.repository?.url) {
meta.push(`Repository: <${dependency.repository.url}>`);
meta.push(`Repository: <${dependency.repository.url}> `);
}
if (dependency.author) {
meta.push(`Author: ${dependency.author.text()}`);
meta.push(`Author: ${dependency.author.text()} `);
}
if (dependency.contributors?.length > 0) {
const contributors = dependency.contributors
Expand All @@ -136,19 +117,14 @@ async function getLicenseText(dependencies) {
})
.join(separator);

return [
...parts,
outdent`
## Bundled dependencies
${content}
`,
].join("\n\n");
return [head, content].join("\n\n");
}

async function buildLicense({ file, files, results, cliOptions }) {
async function buildDependenciesLicense({ file, files, results, cliOptions }) {
const fileName = file.output.file;

if (files.at(-1) !== file) {
throw new Error("license should be last file to build.");
throw new Error(`${fileName} should be last file to build.`);
}

const shouldBuildLicense =
Expand All @@ -157,7 +133,7 @@ async function buildLicense({ file, files, results, cliOptions }) {
typeof cliOptions.minify !== "boolean";

if (!shouldBuildLicense) {
return;
return { skipped: true };
}

const dependencies = getDependencies(results);
Expand All @@ -166,9 +142,9 @@ async function buildLicense({ file, files, results, cliOptions }) {
throw new Error("Fail to collect dependencies.");
}

const text = await getLicenseText(dependencies);
const text = getLicenseText(dependencies);

await fs.writeFile(LICENSE_FILE, text);
await fs.writeFile(path.join(DIST_DIR, fileName), text);
}

export default buildLicense;
export default buildDependenciesLicense;
10 changes: 8 additions & 2 deletions scripts/build/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import url from "node:url";
import createEsmUtils from "esm-utils";
import { outdent } from "outdent";
import { copyFile, DIST_DIR, PROJECT_ROOT } from "../utils/index.js";
import buildDependenciesLicense from "./build-dependencies-license.js";
import buildJavascriptModule from "./build-javascript-module.js";
import buildLicense from "./build-license.js";
import buildPackageJson from "./build-package-json.js";
import buildTypes from "./build-types.js";
import esmifyTypescriptEslint from "./esmify-typescript-eslint.js";
Expand Down Expand Up @@ -859,7 +859,13 @@ const metaFiles = [
},
{
input: "LICENSE",
build: buildLicense,
build: copyFileBuilder,
},
{
output: {
file: "THIRD-PARTY-NOTICES.md",
},
build: buildDependenciesLicense,
},
].map((file) => ({
...file,
Expand Down

0 comments on commit 0fcd5db

Please sign in to comment.