Skip to content

Commit

Permalink
feat(listable): Use QueryGraph.toposort() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed May 14, 2019
1 parent 90759c2 commit 84ce674
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 46 additions & 18 deletions utils/listable/__tests__/listable-format.test.js
@@ -1,12 +1,15 @@
"use strict";

const chalk = require("chalk");
const path = require("path");
const tempy = require("tempy");
const Package = require("@lerna/package");
const Tacks = require("tacks");

const Project = require("@lerna/project");
const loggingOutput = require("@lerna-test/logging-output");
const listable = require("..");

const { File, Dir } = Tacks;

// keep snapshots stable cross-platform
chalk.enabled = false;

Expand All @@ -30,24 +33,49 @@ describe("listable.format()", () => {

const formatWithOptions = opts => listable.format(packages, Object.assign({ _: ["ls"] }, opts));

beforeAll(() => {
const fixture = new Tacks(
Dir({
"lerna.json": File({
version: "independent",
packages: ["pkgs/*"],
}),
"package.json": File({
name: "listable-format-test",
}),
pkgs: Dir({
"pkg-1": Dir({
"package.json": File({
name: "pkg-1",
version: "1.0.0",
dependencies: { "pkg-2": "file:../pkg-2" },
}),
}),
"pkg-2": Dir({
"package.json": File({
name: "pkg-2",
// version: "2.0.0",
devDependencies: { "pkg-3": "file:../pkg-3" },
}),
}),
"pkg-3": Dir({
"package.json": File({
name: "pkg-3",
version: "3.0.0",
dependencies: { "pkg-2": "file:../pkg-2" },
private: true,
}),
}),
}),
})
);

beforeAll(async () => {
const cwd = tempy.directory();

fixture.create(cwd);
process.chdir(cwd);

packages = [
new Package(
{ name: "pkg-1", version: "1.0.0", dependencies: { "pkg-2": "file:../pkg-2" } },
path.join(cwd, "/pkgs/pkg-1")
),
new Package(
{ name: "pkg-2", devDependencies: { "pkg-3": "file:../pkg-3" } },
path.join(cwd, "/pkgs/pkg-2")
),
new Package(
{ name: "pkg-3", version: "3.0.0", dependencies: { "pkg-2": "file:../pkg-2" }, private: true },
path.join(cwd, "/pkgs/pkg-3")
),
];
packages = await Project.getPackages(cwd);
});

describe("renders", () => {
Expand Down Expand Up @@ -214,8 +242,8 @@ pkg-1
expect(loggingOutput("warn")).toContainEqual(expect.stringContaining("pkg-2 -> pkg-3 -> pkg-2"));
expect(text).toMatchInlineSnapshot(`
pkg-2
pkg-3 (PRIVATE)
pkg-1
pkg-3 (PRIVATE)
`);
});
});
Expand Down
12 changes: 3 additions & 9 deletions utils/listable/lib/listable-format.js
Expand Up @@ -3,7 +3,7 @@
const chalk = require("chalk");
const columnify = require("columnify");
const path = require("path");
const batchPackages = require("@lerna/batch-packages");
const QueryGraph = require("@lerna/query-graph");

module.exports = listableFormat;

Expand Down Expand Up @@ -40,18 +40,12 @@ function parseViewOptions(options) {
};
}

function flatBatched(pkgList) {
// allow cycles, output needs to be usable for debugging circularity
const batches = batchPackages(pkgList, false, "allDependencies");

return batches.reduce((acc, batch) => acc.concat(batch), []);
}

function filterResultList(pkgList, viewOptions) {
let result = viewOptions.showAll ? pkgList.slice() : pkgList.filter(pkg => !pkg.private);

if (viewOptions.isTopological) {
result = flatBatched(result);
// allow cycles, output needs to be usable for debugging circularity
result = QueryGraph.toposort(result);
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion utils/listable/package.json
Expand Up @@ -29,7 +29,7 @@
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"@lerna/batch-packages": "file:../batch-packages",
"@lerna/query-graph": "file:../query-graph",
"chalk": "^2.3.1",
"columnify": "^1.5.4"
}
Expand Down

0 comments on commit 84ce674

Please sign in to comment.