Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(publish): add --summary-file option #2653

Merged
merged 15 commits into from Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions commands/publish/README.md
Expand Up @@ -67,6 +67,7 @@ This is useful when a previous `lerna publish` failed to publish all packages to
- [`--tag-version-prefix`](#--tag-version-prefix)
- [`--temp-tag`](#--temp-tag)
- [`--yes`](#--yes)
- [`--summary-file <dir/filename>`](#--summary-file)
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved

### `--canary`

Expand Down Expand Up @@ -295,6 +296,27 @@ lerna publish --canary --yes
When run with this flag, `lerna publish` will skip all confirmation prompts.
Useful in [Continuous integration (CI)](https://en.wikipedia.org/wiki/Continuous_integration) to automatically answer the publish confirmation prompt.

### `--summary-file`

```sh
lerna publish --canary --yes --summary-file ./output.json
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved
```

When run with this flag, once a successfully publish it will create a json summary report(see below for an example).
HamishBuckmaster marked this conversation as resolved.
Show resolved Hide resolved

```json
[
{
"packageName": "package1",
"version": "v1.0.1-alpha"
},
{
"packageName": "package2",
"version": "v2.0.1-alpha"
}
]
```

## Deprecated Options

### `--skip-npm`
Expand Down
27 changes: 27 additions & 0 deletions commands/publish/__tests__/publish-command.test.js
Expand Up @@ -34,6 +34,8 @@ const initFixture = require("@lerna-test/init-fixture")(__dirname);
const path = require("path");
const fs = require("fs-extra");

const fsmain = require("fs");

// file under test
const lernaPublish = require("@lerna-test/command-runner")(require("../command"));

Expand Down Expand Up @@ -306,6 +308,31 @@ Map {
});
});

describe("--summary-file", () => {
it("skips creating the summary file", async () => {
const cwd = await initFixture("normal");
const fsSpy = jest.spyOn(fs, "writeFileSync");
await lernaPublish(cwd);

expect(fsSpy).not.toHaveBeenCalled();
});

it("creates the summary file", async () => {
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved
const cwd = await initFixture("normal");
const fsSpy = jest.spyOn(fsmain, "writeFileSync");
await lernaPublish(cwd)("--summary-file", "./output.json");

const expectedJsonResponse = [
{ packageName: "package-1", version: "1.0.1" },
{ packageName: "package-2", version: "1.0.1" },
{ packageName: "package-3", version: "1.0.1" },
{ packageName: "package-4", version: "1.0.1" },
];
expect(fsSpy).toHaveBeenCalled();
expect(fsSpy).toHaveBeenCalledWith("./output.json", JSON.stringify(expectedJsonResponse));
});
});

describe("--no-verify-access", () => {
it("skips package access verification", async () => {
const cwd = await initFixture("normal");
Expand Down
5 changes: 5 additions & 0 deletions commands/publish/command.js
Expand Up @@ -110,6 +110,11 @@ exports.builder = (yargs) => {
hidden: true,
type: "boolean",
},
"summary-file": {
// Json output.
hidden: true,
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved
type: "string",
HamishBuckmaster marked this conversation as resolved.
Show resolved Hide resolved
},
// y: {
HamishBuckmaster marked this conversation as resolved.
Show resolved Hide resolved
// describe: "Skip all confirmation prompts.",
// alias: "yes",
Expand Down
24 changes: 22 additions & 2 deletions commands/publish/index.js
@@ -1,6 +1,7 @@
"use strict";

const os = require("os");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const pMap = require("p-map");
Expand Down Expand Up @@ -237,10 +238,29 @@ class PublishCommand extends Command {

return chain.then(() => {
const count = this.packagesToPublish.length;
const message = this.packagesToPublish.map((pkg) => ` - ${pkg.name}@${pkg.version}`);

output("Successfully published:");
output(message.join(os.EOL));

if (this.options.summaryFile) {
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved
// create a json object and output it to a file location.
const filePath = this.options.summaryFile || "./output.json";
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved
const jsonObject = this.packagesToPublish.map((pkg) => {
return {
packageName: pkg.name,
version: pkg.version,
};
});
output(jsonObject);
try {
fs.writeFileSync(filePath, JSON.stringify(jsonObject));
output("Locate Summary Report Here: ", filePath);
HamishBuckmaster marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
output("Failed to create the summary report", error);
}
} else {
const message = this.packagesToPublish.map((pkg) => ` - ${pkg.name}@${pkg.version}`);
output(message.join(os.EOL));
}

this.logger.success("published", "%d %s", count, count === 1 ? "package" : "packages");
});
Expand Down