Skip to content

Commit

Permalink
fix(publish): Make the missing license warning clearer and more actio…
Browse files Browse the repository at this point in the history
…nable (#1921)
  • Loading branch information
ken-kenware authored and evocateur committed Feb 13, 2019
1 parent 5c1b84c commit 8a71ac4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
31 changes: 30 additions & 1 deletion commands/publish/__tests__/publish-licenses.test.js
Expand Up @@ -11,6 +11,7 @@ jest.mock("../../version/lib/is-anything-committed");
jest.mock("../../version/lib/is-behind-upstream");
jest.mock("../../version/lib/remote-branch-exists");

const fs = require("fs-extra");
const path = require("path");

// mocked modules
Expand Down Expand Up @@ -70,9 +71,37 @@ describe("licenses", () => {
await lernaPublish(cwd)();

const [warning] = loggingOutput("warn");
expect(warning).toMatch("Packages package-1, package-3 are missing a license");
expect(warning).toMatchInlineSnapshot(`
"Packages package-1 and package-3 are missing a license.
One way to fix this is to add a LICENSE.md file to the root of this repository.
See https://choosealicense.com for additional guidance."
`);

expect(createTempLicenses).toHaveBeenLastCalledWith(undefined, []);
expect(removeTempLicenses).toHaveBeenLastCalledWith([]);
});

it("warns when one package needs a license", async () => {
const cwd = await initFixture("licenses");

// remove root license so warning is triggered
await fs.remove(path.join(cwd, "LICENSE"));

await lernaPublish(cwd)();

const [warning] = loggingOutput("warn");
expect(warning).toMatch("Package package-1 is missing a license.");
});

it("warns when multiple packages need a license", async () => {
const cwd = await initFixture("licenses-missing");

// simulate _all_ packages missing a license
await fs.remove(path.join(cwd, "packages/package-2/LICENSE"));

await lernaPublish(cwd)();

const [warning] = loggingOutput("warn");
expect(warning).toMatch("Packages package-1, package-2, and package-3 are missing a license.");
});
});
18 changes: 17 additions & 1 deletion commands/publish/index.js
Expand Up @@ -378,9 +378,25 @@ class PublishCommand extends Command {
.then(packagesWithoutLicense => {
if (packagesWithoutLicense.length && !this.project.licensePath) {
this.packagesToBeLicensed = [];

const names = packagesWithoutLicense.map(pkg => pkg.name);
const noun = names.length > 1 ? "Packages" : "Package";
const verb = names.length > 1 ? "are" : "is";
const list =
names.length > 1
? `${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${
names[names.length - 1] /* oxford commas _are_ that important */
}`
: names[0];

this.logger.warn(
"ENOLICENSE",
`Packages ${packagesWithoutLicense.map(pkg => pkg.name).join(", ")} are missing a license`
"%s %s %s missing a license.\n%s\n%s",
noun,
list,
verb,
"One way to fix this is to add a LICENSE.md file to the root of this repository.",
"See https://choosealicense.com for additional guidance."
);
} else {
this.packagesToBeLicensed = packagesWithoutLicense;
Expand Down

0 comments on commit 8a71ac4

Please sign in to comment.