Skip to content

Commit

Permalink
feat(publish/index.js): Added OutputJson Command
Browse files Browse the repository at this point in the history
Outputs the packages and versions from publishing to a JSON file.
  • Loading branch information
HamishBuckmaster committed Oct 27, 2020
1 parent f6e7a13 commit f0cc5ac
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
22 changes: 22 additions & 0 deletions commands/publish/README.md
Expand Up @@ -65,6 +65,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)

### `--canary`

Expand Down Expand Up @@ -291,6 +292,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
```

When run with this flag, once a successfully publish it will create a json summary report(see below for an example).

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

## Deprecated Options

### `--skip-npm`
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,
type: "string",
},
// y: {
// 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 pFinally = require("p-finally");
Expand Down Expand Up @@ -238,10 +239,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) {
// create a json object and output it to a file location.
const filePath = this.options.summaryFile || "./output.json";
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);
} 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

0 comments on commit f0cc5ac

Please sign in to comment.