Skip to content

Commit

Permalink
Add json output to ls and updated commands (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricky authored and evocateur committed May 21, 2017
1 parent a5ef53d commit 1a8571c
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 9 deletions.
36 changes: 36 additions & 0 deletions README.md
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
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
@@ -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
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
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
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
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",
},
]
`;

0 comments on commit 1a8571c

Please sign in to comment.