Files
test-repo/scripts/build/build-license.js
T
SUZUKI Sosukeandfisker Cheung 813df18df6 Moves @types/prettier to this repository (#14212)
* Rename `types` to `thirs-party-types`

* Copy `index.d.ts` from `@types/prettier`

* Make public APIs async

* Add missing `formatAST`

* Fix `CustomParser`

* Fix `Parser.parse` signature

* Add comment

* Add `standalonw.d.ts`

* first implement

* Fix path

* Add missing parser `.d.ts`

* Fix

* Fix path

* Introduce valid tests

* Add unit tests for dts files

* Add unit tests

* Simplify unit testing

* Tweaks structures

* Revert needless change

* Fix types path

* Introduce `ts-expect`

* Fix doc utils api

* Add AstPath API

* Fix API

* Fix tests

* Fix changelog

* Move dts

* Update module specifiers

* Remove generate dts config file

* Update build scripts

* Fix type errors

* Refactor with namespace imports

* Update comments

* Fix build script

* Improve build script

* `typing` -> `types`

* Clean up

* Minor tweak

* Minor tweak

* Modify `exports` field

* Fix paths in builder

* Refactor

* Refactor

* Address review

* Update changelog

* Address reviews

* Remove `since` from tests

* Improve utils type def

* Mark deprecated methods

* Address review

* Move docs types

* Address review

* Update c8 config

* Minor tweak

---------

Co-authored-by: fisker Cheung <lionkay@gmail.com>
2023-02-23 22:15:18 +09:00

142 lines
3.5 KiB
JavaScript

import path from "node:path";
import fs from "node:fs/promises";
import { outdent } from "outdent";
import { DIST_DIR, PROJECT_ROOT } from "../utils/index.mjs";
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) {
return text
.trim()
.split("\n")
.map((line) => (line ? `> ${line}` : ">"))
.join("\n");
}
async function getLicenseText(files) {
let dependencies = files.flatMap((file) => file.dependencies);
dependencies = dependencies.filter(
(dependency, index) =>
// Exclude ourself
dependency.name !== "prettier" &&
// Unique by `name` and `version`
index ===
dependencies.findIndex(
({ name, version }) =>
dependency.name === name && dependency.version === version
)
);
dependencies.sort(
(dependencyA, dependencyB) =>
dependencyA.name.localeCompare(dependencyB.name) ||
dependencyA.version.localeCompare(dependencyB.version)
);
const prettierLicense = await fs.readFile(PROJECT_LICENSE_FILE, "utf8");
const licenses = [
...new Set(
dependencies
.filter(({ license }) => license)
.map(({ license }) => license)
),
];
const text = outdent`
# Prettier license
Prettier is released under the MIT license:
${prettierLicense.trim()}
`;
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`;
const meta = [];
if (dependency.description) {
meta.push(toBlockQuote(dependency.description) + "\n");
}
if (dependency.license) {
meta.push(`License: ${dependency.license}`);
}
if (dependency.homepage) {
meta.push(`Homepage: <${dependency.homepage}>`);
}
if (dependency.repository?.url) {
meta.push(`Repository: <${dependency.repository.url}>`);
}
if (dependency.author) {
meta.push(`Author: ${dependency.author.text()}`);
}
if (dependency.contributors?.length > 0) {
const contributors = dependency.contributors
.map((contributor) => ` - ${contributor.text()}`)
.join("\n");
meta.push(`Contributors:\n${contributors}`);
}
if (meta.length > 0) {
text += "\n" + meta.join("\n") + "\n";
}
if (dependency.licenseText) {
text += "\n" + toBlockQuote(dependency.licenseText) + "\n";
}
return text;
})
.join(separator);
return [
...parts,
outdent`
## Bundled dependencies
${content}
`,
].join("\n\n");
}
async function buildLicense({ file, files, shouldCollectLicenses }) {
if (files.indexOf(file) !== files.length - 1) {
throw new Error("license should be last file to build.");
}
if (!shouldCollectLicenses) {
return;
}
const javascriptFiles = files.filter((file) => file.kind === "javascript");
if (javascriptFiles.some((file) => !Array.isArray(file.dependencies))) {
return { skipped: true };
}
const text = await getLicenseText(javascriptFiles);
await fs.writeFile(LICENSE_FILE, text);
}
export default buildLicense;