Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add list cmd #414

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
18 changes: 18 additions & 0 deletions docs/cmd-ls.md
Original file line number Diff line number Diff line change
@@ -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
```
50 changes: 50 additions & 0 deletions src/cli/cmd-ls.ts
Original file line number Diff line number Diff line change
@@ -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));
});
})
);
}
3 changes: 3 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(" ")}`);
Expand Down
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([
expect.stringContaining("dev.comradevanti.opt-unity@2.0.0"),
expect.stringContaining("com.unity.ugui@1.0.0"),
])
);
});
});