diff --git a/README.md b/README.md index 89b84ed0..91089e5e 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,16 @@ openupm deps com.my.package Checkout [the commands doc page](./docs/cmd-deps.md) for more information. +### List installed packages + +Use `openupm ls` to print the names and versions of installed packages. + +```sh +openupm ls +``` + +Checkout [the commands doc page](./docs/cmd-ls.md) for more information. + ### Global command options There are also some global options that work for every command. You can read about them [here](./docs/global-opts.md). diff --git a/docs/cmd-ls.md b/docs/cmd-ls.md new file mode 100644 index 00000000..c9eb3839 --- /dev/null +++ b/docs/cmd-ls.md @@ -0,0 +1,18 @@ + +# `openupm ls` + +The `ls` command prints the name and version of each installed package for a project. + +This command has the `list` alias. On this doc page we will always use the primary command name `ls`. + +## Options + +### Project directory + +By default openupm expects that you run the add command inside your Unity projects root directory. Based on this it determines relative paths to your package manifest etc. + +If you need to run openupm from somewhere else you can change the working directory using the `--chdir`/`-c` option. This option accepts an **absolute** path to a Unity projects root directory. + +```sh +openupm add com.my.package -c /home/user/dev/MyProject +``` \ No newline at end of file diff --git a/src/cli/cmd-ls.ts b/src/cli/cmd-ls.ts new file mode 100644 index 00000000..59d4fdc8 --- /dev/null +++ b/src/cli/cmd-ls.ts @@ -0,0 +1,50 @@ +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. + * @param readTextFile IO function for reading a text file. + * @param debugLog IO function for debug-logs. + * @param log Logger for cli output. + * @returns The command. + */ +export function makeLsCmd( + readTextFile: ReadTextFile, + 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` + ) + .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/src/cli/index.ts b/src/cli/index.ts index c04fcd54..cc88b197 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -15,6 +15,7 @@ import { fetchCheckUrlExists } from "../io/www"; import { makeAddCmd } from "./cmd-add"; import { makeDepsCmd } from "./cmd-deps"; import { makeLoginCmd } from "./cmd-login"; +import { makeLsCmd } from "./cmd-ls"; import { makeRemoveCmd } from "./cmd-remove"; import { makeSearchCmd } from "./cmd-search"; import { makeViewCmd } from "./cmd-view"; @@ -114,6 +115,8 @@ export function makeOpenupmCli( ) ); + program.addCommand(makeLsCmd(readTextFile, debugLog, log)); + // prompt for invalid command program.on("command:*", function () { log.warn("", `unknown command: ${program.args.join(" ")}`); diff --git a/test/e2e/ls.test.ts b/test/e2e/ls.test.ts new file mode 100644 index 00000000..8062e9e5 --- /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([ + expect.stringContaining("dev.comradevanti.opt-unity@2.0.0"), + expect.stringContaining("com.unity.ugui@1.0.0"), + ]) + ); + }); +});