Skip to content

Commit

Permalink
Include bundled dependencies licenses in LICENSE file
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 5, 2021
1 parent 1ed8af4 commit 1aab70b
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -123,6 +123,7 @@
"pretty-bytes": "5.5.0",
"rimraf": "3.0.2",
"rollup": "2.35.1",
"rollup-plugin-license": "2.2.0",
"rollup-plugin-node-globals": "1.4.0",
"rollup-plugin-terser": "7.0.2",
"shelljs": "0.8.4",
Expand Down
29 changes: 29 additions & 0 deletions scripts/build/build.js
Expand Up @@ -11,6 +11,7 @@ const bundler = require("./bundler");
const bundleConfigs = require("./config");
const util = require("./util");
const Cache = require("./cache");
const saveLicense = require("./save-license");

// Errors in promises should be fatal.
const loggedErrors = new Set();
Expand Down Expand Up @@ -160,6 +161,25 @@ async function run(params) {
await execa("rm", ["-rf", ".cache"]);
}

const withoutCache = !fs.existsSync(path.join(__dirname, "../../.cache"));

const allDependencies = [];
if (withoutCache) {
params.onLicenseFound = (dependencies) => {
for (const dependency of dependencies) {
if (
!allDependencies.some(
(item) =>
item.name === dependency.name &&
item.version === dependency.version
)
) {
allDependencies.push(dependency);
}
}
};
}

const bundleCache = new Cache(".cache/", CACHE_VERSION);
await bundleCache.load();

Expand All @@ -173,6 +193,15 @@ async function run(params) {

if (!params.playground) {
await preparePackage();
if (withoutCache) {
saveLicense(allDependencies);
} else {
console.warn(
chalk.red(
"Bundled dependencies licenses not included in `dist/LICENSE`."
)
);
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions scripts/build/bundler.js
Expand Up @@ -13,6 +13,7 @@ const json = require("@rollup/plugin-json");
const replace = require("@rollup/plugin-replace");
const { terser } = require("rollup-plugin-terser");
const { babel } = require("@rollup/plugin-babel");
const license = require("rollup-plugin-license");
const nativeShims = require("./rollup-plugins/native-shims");
const executable = require("./rollup-plugins/executable");
const evaluate = require("./rollup-plugins/evaluate");
Expand Down Expand Up @@ -128,7 +129,7 @@ function getBabelConfig(bundle) {
return config;
}

function getRollupConfig(bundle) {
function getRollupConfig(bundle, options) {
const config = {
input: bundle.input,

Expand Down Expand Up @@ -218,6 +219,14 @@ function getRollupConfig(bundle) {
ascii_only: true,
},
}),
options.onLicenseFound &&
license({
cwd: PROJECT_ROOT,
thirdParty: {
includePrivate: true,
output: options.onLicenseFound,
},
}),
].filter(Boolean);

if (bundle.target === "node") {
Expand Down Expand Up @@ -360,7 +369,7 @@ async function checkCache(cache, inputOptions, outputOption) {
}

module.exports = async function createBundle(bundle, cache, options) {
const inputOptions = getRollupConfig(bundle);
const inputOptions = getRollupConfig(bundle, options);
const outputOptions = getRollupOutputOptions(bundle, options);

if (!Array.isArray(outputOptions) && outputOptions.skipped) {
Expand Down
77 changes: 77 additions & 0 deletions scripts/build/save-license.js
@@ -0,0 +1,77 @@
"use strict";

const fs = require("fs");
const path = require("path");
const { outdent } = require("outdent");
const file = path.join(__dirname, "../../dist/LICENSE");
const separator = `\n${"-".repeat(40)}\n`;

function saveLicense(dependencies) {
dependencies.sort(
(dependencyA, dependencyB) =>
dependencyA.name.localeCompare(dependencyB.name) ||
dependencyA.version.localeCompare(dependencyB.version)
);

const prettierLicense = fs.readFileSync(file, "utf8").trim();

const licenses = [
...new Set(
dependencies
.filter(({ license }) => license)
.map(({ license }) => license)
),
];
const text = outdent`
# Prettier license
Prettier is released under the MIT license:
${prettierLicense}
# Licenses of bundled dependencies
The published Prettier artifact additionally contains code with the following licenses:
${licenses.join(", ")}
# Bundled dependencies
`;

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

const meta = [];

if (dependency.license) {
meta.push(`License: ${dependency.license}`);
}
if (dependency.author && dependency.author.name) {
meta.push(`By: ${dependency.author.name}`);
}
if (dependency.repository && dependency.repository.url) {
meta.push(`Repository: ${dependency.repository.url}`);
}

if (meta.length > 0) {
text += "\n" + meta.join("\n") + "\n";
}

if (dependency.licenseText) {
text +=
"\n" +
dependency.licenseText
.trim()
.split("\n")
.map((line) => (line ? `> ${line}` : ">"))
.join("\n") +
"\n";
}
return text;
})
.join(separator);

fs.writeFileSync(file, text + "\n\n" + content + "\n");
}

module.exports = saveLicense;
92 changes: 84 additions & 8 deletions yarn.lock
Expand Up @@ -1983,6 +1983,11 @@ arr-union@^3.1.0:
resolved "https://registry.yarnpkg.com/arr-union/-/arr-union-3.1.0.tgz#e39b09aea9def866a8f206e288af63919bae39c4"
integrity sha1-45sJrqne+Gao8gbiiK9jkZuuOcQ=

array-find-index@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/array-find-index/-/array-find-index-1.0.2.tgz#df010aa1287e164bbda6f9723b0a96a1ec4187a1"
integrity sha1-3wEKoSh+Fku9pvlyOwqWoexBh6E=

array-includes@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.1.1.tgz#cdd67e6852bdf9c1215460786732255ed2459348"
Expand Down Expand Up @@ -2497,6 +2502,11 @@ comment-json@^4.0.6, comment-json@^4.1.0:
has-own-prop "^2.0.0"
repeat-string "^1.6.1"

commenting@1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/commenting/-/commenting-1.1.0.tgz#fae14345c6437b8554f30bc6aa6c1e1633033590"
integrity sha512-YeNK4tavZwtH7jEgK1ZINXzLKm6DZdEMfsaaieOsCAN0S8vsY7UeuO3Q7d/M018EFgE+IeUAuBOKkFccBZsUZA==

commondir@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
Expand Down Expand Up @@ -3094,6 +3104,7 @@ eslint-plugin-jest@24.1.3:

"eslint-plugin-prettier-internal-rules@link:scripts/tools/eslint-plugin-prettier-internal-rules":
version "0.0.0"
uid ""

eslint-plugin-react@7.22.0:
version "7.22.0"
Expand Down Expand Up @@ -3665,7 +3676,7 @@ glob-to-regexp@^0.4.1:
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==

glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
glob@7.1.6, glob@^7.0.0, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
Expand Down Expand Up @@ -5133,6 +5144,11 @@ lodash.zip@^4.2.0:
resolved "https://registry.yarnpkg.com/lodash.zip/-/lodash.zip-4.2.0.tgz#ec6662e4896408ed4ab6c542a3990b72cc080020"
integrity sha1-7GZi5IlkCO1KtsVCo5kLcswIACA=

lodash@4.17.19:
version "4.17.19"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==

lodash@4.17.20, lodash@^4.17.13, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.4:
version "4.17.20"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
Expand Down Expand Up @@ -5160,20 +5176,20 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

magic-string@0.25.7, magic-string@^0.25.7:
version "0.25.7"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
dependencies:
sourcemap-codec "^1.4.4"

magic-string@^0.22.5:
version "0.22.5"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.22.5.tgz#8e9cf5afddf44385c1da5bc2a6a0dbd10b03657e"
integrity sha512-oreip9rJZkzvA8Qzk9HFs8fZGF/u7H/gtrE8EN6RjKJ9kh2HlC+yQ2QezifqTZfGyiuAV0dRv5a+y/8gBb1m9w==
dependencies:
vlq "^0.2.2"

magic-string@^0.25.7:
version "0.25.7"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
dependencies:
sourcemap-codec "^1.4.4"

make-dir@^3.0.0, make-dir@^3.0.2, make-dir@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
Expand Down Expand Up @@ -5309,6 +5325,16 @@ mixin-deep@^1.2.0:
for-in "^1.0.2"
is-extendable "^1.0.1"

mkdirp@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

moment@2.27.0:
version "2.27.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
integrity sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down Expand Up @@ -5653,6 +5679,11 @@ p-try@^2.0.0:
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==

package-name-regex@1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/package-name-regex/-/package-name-regex-1.0.8.tgz#4b8388cace6cc415f462ab4a269583d9a2d9f131"
integrity sha512-g3vB2J62dLqf4m50VM4tJUC4sixw3JB+Igd0cF3P/gJhAvmvsmFEV2eWZTeLbwfkKEWTf3+gwQ2C6JFFRxWHEQ==

parent-module@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2"
Expand Down Expand Up @@ -6294,6 +6325,21 @@ rimraf@3.0.2, rimraf@^3.0.0, rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

rollup-plugin-license@2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-license/-/rollup-plugin-license-2.2.0.tgz#0d19139bbe44dda500fbf15530af07c91949e348"
integrity sha512-xXb1vviEwlJMX+VGUSsglcMA/Rh9d2QzEm94awy4FlnsPqGrXoTYYGOR3UXR6gYIxiJFkr7qmkKF/NXfre/y8g==
dependencies:
commenting "1.1.0"
glob "7.1.6"
lodash "4.17.19"
magic-string "0.25.7"
mkdirp "1.0.4"
moment "2.27.0"
package-name-regex "1.0.8"
spdx-expression-validate "2.0.0"
spdx-satisfies "5.0.0"

rollup-plugin-node-globals@1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-node-globals/-/rollup-plugin-node-globals-1.4.0.tgz#5e1f24a9bb97c0ef51249f625e16c7e61b7c020b"
Expand Down Expand Up @@ -6638,6 +6684,15 @@ sourcemap-codec@^1.4.4:
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==

spdx-compare@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/spdx-compare/-/spdx-compare-1.0.0.tgz#2c55f117362078d7409e6d7b08ce70a857cd3ed7"
integrity sha512-C1mDZOX0hnu0ep9dfmuoi03+eOdDoz2yvK79RxbcrVEG1NO1Ph35yW102DHWKN4pk80nwCgeMmSY5L25VE4D9A==
dependencies:
array-find-index "^1.0.2"
spdx-expression-parse "^3.0.0"
spdx-ranges "^2.0.0"

spdx-correct@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.1.tgz#dece81ac9c1e6713e5f7d1b6f17d468fa53d89a9"
Expand All @@ -6659,11 +6714,32 @@ spdx-expression-parse@^3.0.0:
spdx-exceptions "^2.1.0"
spdx-license-ids "^3.0.0"

spdx-expression-validate@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/spdx-expression-validate/-/spdx-expression-validate-2.0.0.tgz#25c9408e1c63fad94fff5517bb7101ffcd23350b"
integrity sha512-b3wydZLM+Tc6CFvaRDBOF9d76oGIHNCLYFeHbftFXUWjnfZWganmDmvtM5sm1cRwJc/VDBMLyGGrsLFd1vOxbg==
dependencies:
spdx-expression-parse "^3.0.0"

spdx-license-ids@^3.0.0:
version "3.0.5"
resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.5.tgz#3694b5804567a458d3c8045842a6358632f62654"
integrity sha512-J+FWzZoynJEXGphVIS+XEh3kFSjZX/1i9gFBaWQcB+/tmpe2qUsSBABpcxqxnAxFdiUFEgAX1bjYGQvIZmoz9Q==

spdx-ranges@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/spdx-ranges/-/spdx-ranges-2.1.1.tgz#87573927ba51e92b3f4550ab60bfc83dd07bac20"
integrity sha512-mcdpQFV7UDAgLpXEE/jOMqvK4LBoO0uTQg0uvXUewmEFhpiZx5yJSZITHB8w1ZahKdhfZqP5GPEOKLyEq5p8XA==

spdx-satisfies@5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/spdx-satisfies/-/spdx-satisfies-5.0.0.tgz#d740b8f14caeada36fb307629dee87146970a256"
integrity sha512-/hGhwh20BeGmkA+P/lm06RvXD94JduwNxtx/oX3B5ClPt1/u/m5MCaDNo1tV3Y9laLkQr/NRde63b9lLMhlNfw==
dependencies:
spdx-compare "^1.0.0"
spdx-expression-parse "^3.0.0"
spdx-ranges "^2.0.0"

split-string@^3.0.1, split-string@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2"
Expand Down

0 comments on commit 1aab70b

Please sign in to comment.