Skip to content

Commit bf56018

Browse files
committed
feat(list): Extract @lerna/listable utility
1 parent 41f231f commit bf56018

12 files changed

Lines changed: 453 additions & 149 deletions

File tree

‎commands/list/command.js‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
const filterable = require("@lerna/filter-options");
4+
const listable = require("@lerna/listable");
45

56
/**
67
* @see https://github.com/yargs/yargs/blob/master/docs/advanced.md#providing-a-command-module
@@ -12,31 +13,7 @@ exports.aliases = ["ls", "la", "ll"];
1213
exports.describe = "List local packages";
1314

1415
exports.builder = yargs => {
15-
yargs.options({
16-
json: {
17-
group: "Command Options:",
18-
describe: "Show information as a JSON array",
19-
type: "boolean",
20-
},
21-
a: {
22-
group: "Command Options:",
23-
describe: "Show private packages that are normally hidden",
24-
type: "boolean",
25-
alias: "all",
26-
},
27-
l: {
28-
group: "Command Options:",
29-
describe: "Show extended information",
30-
type: "boolean",
31-
alias: "long",
32-
},
33-
p: {
34-
group: "Command Options:",
35-
describe: "Show parseable output instead of columnified view",
36-
type: "boolean",
37-
alias: "parseable",
38-
},
39-
});
16+
listable.options(yargs);
4017

4118
return filterable(yargs);
4219
};

‎commands/list/index.js‎

Lines changed: 10 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"use strict";
22

3-
const chalk = require("chalk");
4-
const columnify = require("columnify");
5-
const path = require("path");
63
const Command = require("@lerna/command");
4+
const listable = require("@lerna/listable");
75
const output = require("@lerna/output");
86

97
module.exports = factory;
@@ -18,114 +16,18 @@ class ListCommand extends Command {
1816
}
1917

2018
initialize() {
21-
const alias = this.options._[0];
22-
23-
this.showAll = alias === "la" || this.options.all;
24-
this.showLong = alias === "la" || alias === "ll" || this.options.long;
25-
26-
this.resultList = this.showAll
27-
? this.filteredPackages
28-
: this.filteredPackages.filter(pkg => !pkg.private);
29-
30-
// logged after output
31-
this.count = this.resultList.length;
19+
this.result = listable.format(this.filteredPackages, this.options);
3220
}
3321

3422
execute() {
35-
let result;
36-
37-
if (this.options.json) {
38-
result = this.formatJSON();
39-
} else if (this.options.parseable) {
40-
result = this.formatParseable();
41-
} else {
42-
result = this.formatColumns();
43-
}
44-
45-
output(result);
46-
47-
this.logger.success("found", "%d %s", this.count, this.count === 1 ? "package" : "packages");
48-
}
49-
50-
formatJSON() {
51-
// explicit re-mapping exposes non-enumerable properties
52-
const data = this.resultList.map(pkg => ({
53-
name: pkg.name,
54-
version: pkg.version,
55-
private: pkg.private,
56-
location: pkg.location,
57-
}));
58-
59-
return JSON.stringify(data, null, 2);
60-
}
61-
62-
formatParseable() {
63-
const mapper = this.showLong
64-
? pkg => {
65-
const result = [pkg.location, pkg.name];
66-
67-
// sometimes the version is inexplicably missing?
68-
if (pkg.version) {
69-
result.push(pkg.version);
70-
} else {
71-
result.push("MISSING");
72-
}
73-
74-
if (pkg.private) {
75-
result.push("PRIVATE");
76-
}
77-
78-
return result.join(":");
79-
}
80-
: pkg => pkg.location;
81-
82-
return this.resultList.map(mapper).join("\n");
83-
}
84-
85-
formatColumns() {
86-
const formattedResults = this.resultList.map(result => {
87-
const formatted = {
88-
name: result.name,
89-
};
90-
91-
if (result.version) {
92-
formatted.version = chalk.green(`v${result.version}`);
93-
} else {
94-
formatted.version = chalk.yellow("MISSING");
95-
}
96-
97-
if (result.private) {
98-
formatted.private = `(${chalk.red("PRIVATE")})`;
99-
}
100-
101-
formatted.location = chalk.grey(path.relative(".", result.location));
102-
103-
return formatted;
104-
});
105-
106-
return columnify(formattedResults, {
107-
showHeaders: false,
108-
columns: this.getColumnOrder(),
109-
config: {
110-
version: {
111-
align: "right",
112-
},
113-
},
114-
});
115-
}
116-
117-
getColumnOrder() {
118-
const columns = ["name"];
119-
120-
if (this.showLong) {
121-
columns.push("version", "location");
122-
}
123-
124-
if (this.showAll) {
125-
columns.push("private");
126-
}
127-
128-
return columns;
23+
output(this.result.text);
24+
25+
this.logger.success(
26+
"found",
27+
"%d %s",
28+
this.result.count,
29+
this.result.count === 1 ? "package" : "packages"
30+
);
12931
}
13032
}
13133

‎commands/list/package.json‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
"dependencies": {
3434
"@lerna/command": "file:../../core/command",
3535
"@lerna/filter-options": "file:../../core/filter-options",
36-
"@lerna/output": "file:../../utils/output",
37-
"chalk": "^2.3.1",
38-
"columnify": "^1.5.4"
36+
"@lerna/listable": "file:../../utils/listable",
37+
"@lerna/output": "file:../../utils/output"
3938
}
4039
}

‎integration/lerna-ls.test.js‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const cliRunner = require("@lerna-test/cli-runner");
44
const initFixture = require("@lerna-test/init-fixture")(__dirname);
5-
const multiLineTrimRight = require("@lerna-test/multi-line-trim-right");
65

76
// normalize temp directory paths in snapshots
87
expect.addSnapshotSerializer(require("@lerna-test/serialize-tempdir"));
@@ -12,14 +11,12 @@ let lerna;
1211

1312
beforeAll(async () => {
1413
const cwd = await initFixture("lerna-ls");
15-
const run = cliRunner(cwd);
1614

17-
// wrap runner to remove trailing whitespace added by columnify
18-
lerna = (...args) => run(...args).then(result => multiLineTrimRight(result.stdout));
15+
lerna = cliRunner(cwd);
1916
});
2017

2118
test("lerna list", async () => {
22-
const stdout = await lerna("list");
19+
const { stdout } = await lerna("list");
2320
expect(stdout).toMatchInlineSnapshot(`
2421
package-1
2522
@test/package-2
@@ -28,7 +25,7 @@ package-3
2825
});
2926

3027
test("lerna ls", async () => {
31-
const stdout = await lerna("ls");
28+
const { stdout } = await lerna("ls");
3229
expect(stdout).toMatchInlineSnapshot(`
3330
package-1
3431
@test/package-2
@@ -37,7 +34,7 @@ package-3
3734
});
3835

3936
test("lerna ls --all", async () => {
40-
const stdout = await lerna("ls", "--all");
37+
const { stdout } = await lerna("ls", "--all");
4138
expect(stdout).toMatchInlineSnapshot(`
4239
package-1
4340
@test/package-2
@@ -47,7 +44,7 @@ package-4 (PRIVATE)
4744
});
4845

4946
test("lerna ls --long", async () => {
50-
const stdout = await lerna("ls", "--long");
47+
const { stdout } = await lerna("ls", "--long");
5148
expect(stdout).toMatchInlineSnapshot(`
5249
package-1 v1.0.0 packages/pkg-1
5350
@test/package-2 v2.0.0 packages/pkg-2
@@ -56,7 +53,7 @@ package-3 MISSING packages/pkg-3
5653
});
5754

5855
test("lerna ls --parseable", async () => {
59-
const stdout = await lerna("ls", "--parseable");
56+
const { stdout } = await lerna("ls", "--parseable");
6057
expect(stdout).toMatchInlineSnapshot(`
6158
<PROJECT_ROOT>/packages/pkg-1
6259
<PROJECT_ROOT>/packages/pkg-2
@@ -65,7 +62,7 @@ test("lerna ls --parseable", async () => {
6562
});
6663

6764
test("lerna ls --all --long --parseable", async () => {
68-
const stdout = await lerna("ls", "-alp");
65+
const { stdout } = await lerna("ls", "-alp");
6966
expect(stdout).toMatchInlineSnapshot(`
7067
<PROJECT_ROOT>/packages/pkg-1:package-1:1.0.0
7168
<PROJECT_ROOT>/packages/pkg-2:@test/package-2:2.0.0
@@ -75,7 +72,7 @@ test("lerna ls --all --long --parseable", async () => {
7572
});
7673

7774
test("lerna la", async () => {
78-
const stdout = await lerna("la");
75+
const { stdout } = await lerna("la");
7976
expect(stdout).toMatchInlineSnapshot(`
8077
package-1 v1.0.0 packages/pkg-1
8178
@test/package-2 v2.0.0 packages/pkg-2
@@ -85,7 +82,7 @@ package-4 v4.0.0 packages/pkg-4 (PRIVATE)
8582
});
8683

8784
test("lerna ll", async () => {
88-
const stdout = await lerna("ll");
85+
const { stdout } = await lerna("ll");
8986
expect(stdout).toMatchInlineSnapshot(`
9087
package-1 v1.0.0 packages/pkg-1
9188
@test/package-2 v2.0.0 packages/pkg-2

‎package-lock.json‎

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎utils/listable/README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# `@lerna/listable`
2+
3+
> Shared logic for listing package information
4+
5+
This is an internal package for [Lerna](https://github.com/lerna/lerna/#readme), YMMV.
6+
7+
## Usage
8+
9+
### `listable.format()`
10+
11+
```js
12+
const listable = require('@lerna/listable');
13+
14+
const { text, count } = listable.format(packages, options);
15+
```
16+
17+
### `listable.options()`
18+
19+
```js
20+
const listable = require('@lerna/listable');
21+
22+
exports.builder = yargs => {
23+
listable.options(yargs);
24+
};
25+
```

0 commit comments

Comments
 (0)