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

Add json output to ls and updated commands #824

Merged
merged 3 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,24 @@ Lerna determines the last git tag created and runs `git diff --name-only v6.8.1`
**Note that configuration for the `publish` command _also_ affects the
`updated` command. For example `config.publish.ignore`**

#### --json

```sh
$ lerna updated --json
```

When run with this flag, `updated` will return an array of objects in the following format:

```json
[
{
"name": "package",
"version": "1.0.0",
"private": false
}
]
```

### clean

```sh
Expand Down Expand Up @@ -439,6 +457,24 @@ List all of the public packages in the current Lerna repo.

`lerna ls` respects the `--ignore` and `--scope` flags (see [Flags](#flags)).

#### --json

```sh
$ lerna ls --json
```

When run with this flag, `ls` will return an array of objects in the following format:

```json
[
{
"name": "package",
"version": "1.0.0",
"private": false
}
]
```

### run

```sh
Expand Down
22 changes: 18 additions & 4 deletions src/commands/LsCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ export const command = "ls";

export const describe = "List all public packages";

export const builder = {};
export const builder = {
"json": {
describe: "Show information in JSON format",
group: "Command Options:",
type: "boolean"
}
};

export default class LsCommand extends Command {
get requiresGit() {
Expand All @@ -29,12 +35,20 @@ export default class LsCommand extends Command {
.map((pkg) => {
return {
name: pkg.name,
version: chalk.grey(`v${pkg.version}`),
private: pkg.isPrivate() ? `(${chalk.red("private")})` : ""
version: pkg.version,
private: pkg.isPrivate()
};
});

output(columnify(formattedPackages, { showHeaders: false }));
if (this.options.json) {
output(JSON.stringify(formattedPackages, null, 2));
} else {
formattedPackages.forEach((pkg) => {
pkg.version = chalk.grey(`v${pkg.version}`);
pkg.private = pkg.private ? `(${chalk.red("private")})` : "";
});
output(columnify(formattedPackages, { showHeaders: false }));
}

callback(null, true);
}
Expand Down
34 changes: 29 additions & 5 deletions src/commands/UpdatedCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import _ from "lodash";
import chalk from "chalk";

import { builder as publishOptions } from "./PublishCommand";
import Command from "../Command";
import output from "../utils/output";
import UpdatedPackagesCollector from "../UpdatedPackagesCollector";

const updatedOptions = _.assign(
{},
publishOptions,
{
"json": {
describe: "Show information in JSON format",
group: "Command Options:",
type: "boolean"
}
}
);

export function handler(argv) {
return new UpdatedCommand(argv._, argv).run();
}
Expand All @@ -13,7 +26,7 @@ export const command = "updated";

export const describe = "Check which packages have changed since the last publish.";

export const builder = (yargs) => yargs.options(publishOptions);
export const builder = (yargs) => yargs.options(updatedOptions);

export default class UpdatedCommand extends Command {
initialize(callback) {
Expand All @@ -33,12 +46,23 @@ export default class UpdatedCommand extends Command {
}

execute(callback) {
const formattedUpdates = this.updates.map((update) => update.package).map((pkg) =>
`- ${pkg.name}${pkg.isPrivate() ? ` (${chalk.red("private")})` : ""}`
).join("\n");
const updatedPackages = this.updates.map((update) => update.package).map((pkg) => {
return {
name: pkg.name,
version: pkg.version,
private: pkg.isPrivate()
};
});

this.logger.info("result");
output(formattedUpdates);
if (this.options.json) {
output(JSON.stringify(updatedPackages, null, 2));
} else {
const formattedUpdates = updatedPackages.map((pkg) =>
`- ${pkg.name}${pkg.private ? ` (${chalk.red("private")})` : ""}`
).join("\n");
output(formattedUpdates);
}

callback(null, true);
}
Expand Down
29 changes: 29 additions & 0 deletions test/LsCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,33 @@ describe("LsCommand", () => {
}));
});
});

describe("with --json", () => {
let testDir;

beforeEach(() => initFixture("LsCommand/basic").then((dir) => {
testDir = dir;
}));

it("should list packages as json objects", (done) => {
const lsCommand = new LsCommand([], {
json: true
}, testDir);

lsCommand.runValidations();
lsCommand.runPreparations();

lsCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
try {
// Output should be a parseable string
const jsonOutput = JSON.parse(consoleOutput());
expect(jsonOutput).toMatchSnapshot();
done();
} catch (ex) {
done.fail(ex);
}
}));
});
});
});
37 changes: 37 additions & 0 deletions test/UpdatedCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,43 @@ describe("UpdatedCommand", () => {
updatedCommand.runCommand(exitWithCode(1, done));
});
});

/** =========================================================================
* JSON Output
* ======================================================================= */

describe("with --json", () => {
let testDir;

beforeEach(() => initFixture("UpdatedCommand/basic").then((dir) => {
testDir = dir;
}));

it("should list changes as a json object", (done) => {
setupGitChanges(testDir, [
"packages/package-2/random-file",
]);

const updatedCommand = new UpdatedCommand([], {
json: true
}, testDir);

updatedCommand.runValidations();
updatedCommand.runPreparations();

updatedCommand.runCommand(exitWithCode(0, (err) => {
if (err) return done.fail(err);
try {
// Output should be a parseable string
const jsonOutput = JSON.parse(consoleOutput());
expect(jsonOutput).toMatchSnapshot();
done();
} catch (ex) {
done.fail(ex);
}
}));
});
});
});

// TODO: remove this when we _really_ remove support for SECRET_FLAG
Expand Down
30 changes: 30 additions & 0 deletions test/__snapshots__/LsCommand.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,33 @@ Array [
@test/package-1 v1.0.0 ",
]
`;

exports[`LsCommand with --json should list packages as json objects 1`] = `
Array [
Object {
"name": "package-1",
"private": false,
"version": "1.0.0",
},
Object {
"name": "package-2",
"private": false,
"version": "1.0.0",
},
Object {
"name": "package-3",
"private": false,
"version": "1.0.0",
},
Object {
"name": "package-4",
"private": false,
"version": "1.0.0",
},
Object {
"name": "package-5",
"private": true,
"version": "1.0.0",
},
]
`;
15 changes: 15 additions & 0 deletions test/__snapshots__/UpdatedCommand.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ Array [
- package-4",
]
`;

exports[`UpdatedCommand with --json should list changes as a json object 1`] = `
Array [
Object {
"name": "package-2",
"private": false,
"version": "1.0.0",
},
Object {
"name": "package-3",
"private": false,
"version": "1.0.0",
},
]
`;