Skip to content

Commit

Permalink
feat(listable): Output newline-delimited JSON with --ndjson
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Feb 14, 2019
1 parent ad49245 commit 742781b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions commands/changed/README.md
Expand Up @@ -20,6 +20,7 @@ package-2
`lerna changed` supports all of the flags supported by [`lerna ls`](https://github.com/lerna/lerna/tree/master/commands/list#options):

- [`--json`](https://github.com/lerna/lerna/tree/master/commands/list#--json)
- [`--ndjson`](https://github.com/lerna/lerna/tree/master/commands/list#--ndjson)
- [`-a`, `--all`](https://github.com/lerna/lerna/tree/master/commands/list#--all)
- [`-l`, `--long`](https://github.com/lerna/lerna/tree/master/commands/list#--long)
- [`-p`, `--parseable`](https://github.com/lerna/lerna/tree/master/commands/list#--parseable)
Expand Down
11 changes: 11 additions & 0 deletions commands/list/README.md
Expand Up @@ -25,6 +25,7 @@ In any case, you can always pass `--loglevel silent` to create pristine chains o
## Options

- [`--json`](#--json)
- [`--ndjson`](#--ndjson)
- [`-a`, `--all`](#--all)
- [`-l`, `--long`](#--long)
- [`-p`, `--parseable`](#--parseable)
Expand Down Expand Up @@ -61,6 +62,16 @@ $ lerna ls --json --all | json -a -c 'this.private === true' name
package-3
```

### `--ndjson`

Show information as [newline-delimited JSON](http://ndjson.org).

```sh
$ lerna ls --ndjson
{"name":"package-1","version":"1.0.0","private":false,"location":"/path/to/packages/pkg-1"}
{"name":"package-2","version":"1.0.0","private":false,"location":"/path/to/packages/pkg-2"}
```

### `--all`

Alias: `-a`
Expand Down
10 changes: 10 additions & 0 deletions utils/listable/__tests__/listable-format.test.js
Expand Up @@ -128,6 +128,16 @@ pkg-3 v3.0.0 pkgs/pkg-3 (PRIVATE)
`);
});

test("NDJSON output", () => {
const { text } = formatWithOptions({ ndjson: true, all: true });

expect(text).toMatchInlineSnapshot(`
{"name":"pkg-1","version":"1.0.0","private":false,"location":"__TEST_ROOTDIR__/pkgs/pkg-1"}
{"name":"pkg-2","private":false,"location":"__TEST_ROOTDIR__/pkgs/pkg-2"}
{"name":"pkg-3","version":"3.0.0","private":true,"location":"__TEST_ROOTDIR__/pkgs/pkg-3"}
`);
});

test("parseable output", () => {
const { text } = formatWithOptions({ parseable: true });

Expand Down
4 changes: 4 additions & 0 deletions utils/listable/__tests__/listable-options.test.js
Expand Up @@ -10,6 +10,10 @@ describe("listable.options()", () => {
expect(parsed("--json")).toHaveProperty("json", true);
});

it("provides --ndjson", () => {
expect(parsed("--ndjson")).toHaveProperty("ndjson", true);
});

it("provides --all", () => {
expect(parsed("--all")).toHaveProperty("all", true);
});
Expand Down
17 changes: 14 additions & 3 deletions utils/listable/lib/listable-format.js
Expand Up @@ -16,6 +16,8 @@ function listableFormat(pkgList, options) {

if (viewOptions.showJSON) {
text = formatJSON(resultList);
} else if (viewOptions.showNDJSON) {
text = formatNDJSON(resultList);
} else if (viewOptions.showParseable) {
text = formatParseable(resultList, viewOptions);
} else {
Expand All @@ -32,6 +34,7 @@ function parseViewOptions(options) {
showAll: alias === "la" || options.all,
showLong: alias === "la" || alias === "ll" || options.long,
showJSON: options.json,
showNDJSON: options.ndjson,
showParseable: options.parseable,
isTopological: options.toposort,
};
Expand All @@ -54,16 +57,24 @@ function filterResultList(pkgList, viewOptions) {
return result;
}

function formatJSON(resultList) {
function toJSONList(resultList) {
// explicit re-mapping exposes non-enumerable properties
const data = resultList.map(pkg => ({
return resultList.map(pkg => ({
name: pkg.name,
version: pkg.version,
private: pkg.private,
location: pkg.location,
}));
}

function formatJSON(resultList) {
return JSON.stringify(toJSONList(resultList), null, 2);
}

return JSON.stringify(data, null, 2);
function formatNDJSON(resultList) {
return toJSONList(resultList)
.map(data => JSON.stringify(data))
.join("\n");
}

function makeParseable(pkg) {
Expand Down
5 changes: 5 additions & 0 deletions utils/listable/lib/listable-options.js
Expand Up @@ -9,6 +9,11 @@ function listableOptions(yargs) {
describe: "Show information as a JSON array",
type: "boolean",
},
ndjson: {
group: "Command Options:",
describe: "Show information as newline-delimited JSON",
type: "boolean",
},
a: {
group: "Command Options:",
describe: "Show private packages that are normally hidden",
Expand Down

0 comments on commit 742781b

Please sign in to comment.