diff --git a/src/cli/cmd-ls.ts b/src/cli/cmd-ls.ts index b2d75848..59d4fdc8 100644 --- a/src/cli/cmd-ls.ts +++ b/src/cli/cmd-ls.ts @@ -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. @@ -16,6 +21,12 @@ 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") @@ -23,5 +34,17 @@ export function makeLsCmd( `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)); + }); + }) + ); } diff --git a/test/e2e/ls.test.ts b/test/e2e/ls.test.ts new file mode 100644 index 00000000..bb653c4f --- /dev/null +++ b/test/e2e/ls.test.ts @@ -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", + ]) + ); + }); +});