Skip to content

Commit dbfac8c

Browse files
authored
feat: add list cmd (#414)
This commit adds the list command. It behaves like calling npm ls without an argument and lists all installed dependencies. See #351
1 parent 2403878 commit dbfac8c

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ openupm deps com.my.package
118118

119119
Checkout [the commands doc page](./docs/cmd-deps.md) for more information.
120120

121+
### List installed packages
122+
123+
Use `openupm ls` to print the names and versions of installed packages.
124+
125+
```sh
126+
openupm ls
127+
```
128+
129+
Checkout [the commands doc page](./docs/cmd-ls.md) for more information.
130+
121131
### Global command options
122132

123133
There are also some global options that work for every command. You can read about them [here](./docs/global-opts.md).

docs/cmd-ls.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# `openupm ls`
3+
4+
The `ls` command prints the name and version of each installed package for a project.
5+
6+
This command has the `list` alias. On this doc page we will always use the primary command name `ls`.
7+
8+
## Options
9+
10+
### Project directory
11+
12+
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.
13+
14+
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.
15+
16+
```sh
17+
openupm add com.my.package -c /home/user/dev/MyProject
18+
```

src/cli/cmd-ls.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Command } from "@commander-js/extra-typings";
2+
import { Logger } from "npmlog";
3+
import { loadProjectManifestUsing } from "../app/get-dependencies";
4+
import { partialApply } from "../domain/fp-utils";
5+
import type { DebugLog } from "../domain/logging";
6+
import { makePackageSpec } from "../domain/package-spec";
7+
import { recordEntries } from "../domain/record-utils";
8+
import type { ReadTextFile } from "../io/fs";
9+
import { withErrorLogger } from "./error-logging";
10+
import { workDirOpt } from "./opt-wd";
11+
12+
/**
13+
* Makes the `openupm ls` cli command with the given dependencies.
14+
* @param readTextFile IO function for reading a text file.
15+
* @param debugLog IO function for debug-logs.
16+
* @param log Logger for cli output.
17+
* @returns The command.
18+
*/
19+
export function makeLsCmd(
20+
readTextFile: ReadTextFile,
21+
debugLog: DebugLog,
22+
log: Logger
23+
) {
24+
const getDependencies = partialApply(
25+
loadProjectManifestUsing,
26+
readTextFile,
27+
debugLog
28+
);
29+
30+
return new Command("ls")
31+
.aliases(["list"])
32+
.summary("list all currently installed packages")
33+
.description(
34+
`Print the names and versions of all installed packages.
35+
openupm ls`
36+
)
37+
.addOption(workDirOpt)
38+
.action(
39+
withErrorLogger(log, async function (options) {
40+
const projectDirectory = options.chdir;
41+
const manifest = await getDependencies(projectDirectory);
42+
43+
const dependencies = recordEntries(manifest.dependencies ?? {});
44+
45+
dependencies.forEach(([name, version]) => {
46+
log.notice("", makePackageSpec(name, version));
47+
});
48+
})
49+
);
50+
}

src/cli/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { fetchCheckUrlExists } from "../io/www";
1515
import { makeAddCmd } from "./cmd-add";
1616
import { makeDepsCmd } from "./cmd-deps";
1717
import { makeLoginCmd } from "./cmd-login";
18+
import { makeLsCmd } from "./cmd-ls";
1819
import { makeRemoveCmd } from "./cmd-remove";
1920
import { makeSearchCmd } from "./cmd-search";
2021
import { makeViewCmd } from "./cmd-view";
@@ -114,6 +115,8 @@ export function makeOpenupmCli(
114115
)
115116
);
116117

118+
program.addCommand(makeLsCmd(readTextFile, debugLog, log));
119+
117120
// prompt for invalid command
118121
program.on("command:*", function () {
119122
log.warn("", `unknown command: ${program.args.join(" ")}`);

test/e2e/ls.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ResultCodes } from "../../src/cli/result-codes";
2+
import { buildProjectManifest } from "../common/data-project-manifest";
3+
import { runOpenupm } from "./run";
4+
import { prepareHomeDirectory } from "./setup/directories";
5+
import { prepareUnityProject } from "./setup/project";
6+
7+
describe("list installed packages", () => {
8+
test("should list installed packages", async () => {
9+
const homeDirectory = await prepareHomeDirectory();
10+
const projectDirectory = await prepareUnityProject(homeDirectory, {
11+
manifest: buildProjectManifest((manifest) =>
12+
manifest
13+
.addDependency("dev.comradevanti.opt-unity", "2.0.0", true, true)
14+
.addDependency("com.unity.ugui", "1.0.0", true, false)
15+
),
16+
});
17+
18+
const result = await runOpenupm(projectDirectory, ["ls"]);
19+
20+
expect(result.code).toEqual(ResultCodes.Ok);
21+
expect(result.stdErr).toEqual(
22+
expect.arrayContaining([
23+
expect.stringContaining("[email protected]"),
24+
expect.stringContaining("[email protected]"),
25+
])
26+
);
27+
});
28+
});

0 commit comments

Comments
 (0)