Skip to content

Commit

Permalink
add list logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Nov 1, 2024
1 parent ca714b5 commit e2d0cab
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/cli/cmd-ls.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Command } from "@commander-js/extra-typings";
import { Logger } from "npmlog";
import { loadProjectManifestUsing } from "../app/get-dependencies";
import { partialApply } from "../domain/fp-utils";
import type { DebugLog } from "../domain/logging";
import { makePackageSpec } from "../domain/package-spec";
import { recordEntries } from "../domain/record-utils";
import type { ReadTextFile } from "../io/fs";
import { withErrorLogger } from "./error-logging";
import { workDirOpt } from "./opt-wd";

/**
* Makes the `openupm ls` cli command with the given dependencies.
Expand All @@ -16,12 +21,30 @@ export function makeLsCmd(
debugLog: DebugLog,
log: Logger
) {
const getDependencies = partialApply(
loadProjectManifestUsing,
readTextFile,
debugLog
);

return new Command("ls")
.aliases(["list"])
.summary("list all currently installed packages")
.description(
`Print the names and versions of all installed packages.
openupm ls`
)
.action(withErrorLogger(log, async function (options) {}));
.addOption(workDirOpt)
.action(
withErrorLogger(log, async function (options) {
const projectDirectory = options.chdir;
const manifest = await getDependencies(projectDirectory);

const dependencies = recordEntries(manifest.dependencies ?? {});

dependencies.forEach(([name, version]) => {
log.notice("", makePackageSpec(name, version));
});
})
);
}
28 changes: 28 additions & 0 deletions test/e2e/ls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ResultCodes } from "../../src/cli/result-codes";
import { buildProjectManifest } from "../common/data-project-manifest";
import { runOpenupm } from "./run";
import { prepareHomeDirectory } from "./setup/directories";
import { prepareUnityProject } from "./setup/project";

describe("list installed packages", () => {
test("should list installed packages", async () => {
const homeDirectory = await prepareHomeDirectory();
const projectDirectory = await prepareUnityProject(homeDirectory, {
manifest: buildProjectManifest((manifest) =>
manifest
.addDependency("dev.comradevanti.opt-unity", "2.0.0", true, true)
.addDependency("com.unity.ugui", "1.0.0", true, false)
),
});

const result = await runOpenupm(projectDirectory, ["ls"]);

expect(result.code).toEqual(ResultCodes.Ok);
expect(result.stdErr).toEqual(
expect.arrayContaining([
"dev.comradevanti.opt-unity@2.0.0",
"com.unity.ugui@1.0.0",
])
);
});
});

0 comments on commit e2d0cab

Please sign in to comment.