Skip to content

Commit

Permalink
Merge branch 'master' into optimize-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti authored Jan 14, 2024
2 parents 0acc2af + 4b91c63 commit eb26e69
Show file tree
Hide file tree
Showing 29 changed files with 565 additions and 331 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@

### Features

* command install as an alias of command add ([b0a30f3](https://github.com/openupm/openupm-cli/commit/b0a30f3cdff6249712f532f376d5980354b9e94a))
* command "install" as an alias of command add ([b0a30f3](https://github.com/openupm/openupm-cli/commit/b0a30f3cdff6249712f532f376d5980354b9e94a))

# [1.2.0](https://github.com/openupm/openupm-cli/compare/1.1.1...1.2.0) (2020-01-05)

Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![npm](https://img.shields.io/npm/v/openupm-cli) ![NPM](https://img.shields.io/npm/l/openupm-cli) ![npm](https://img.shields.io/npm/dm/openupm-cli)

The command-line tool to maintain the Unity manifest file for 3rd-party upm registries, offering a similar but lighter experience like *npm* or *yarn* for NodeJS.
The command-line tool to maintain the Unity manifest file for 3rd-party upm registries, offering a similar but lighter experience like *npm* or *yarn* for Node.js.

The tool is designed to work with [the OpenUPM registry](https://openupm.com), but can also work with any upm registries, including the official Unity registry.

Expand Down Expand Up @@ -75,7 +75,7 @@ internal/modules/cjs/loader.js:818
Error: Cannot find module 'node:net'
```

Please install [nodejs 16 or above](https://nodejs.org/en/download/).
Please install [Node.js 16 or above](https://nodejs.org/en/download/).

## China region

Expand Down Expand Up @@ -152,9 +152,9 @@ open deps <pkg> --deep

### Authenticate with a scoped registry

Starting from Unity 2019.3.4f1, you can configure the`.upmconfig.toml` file to authenticate with a scoped registry. The `openupm login` command helps you authenticate with an npm server and store the info to the UPM config file.
Starting from Unity 2019.3.4f1, you can configure the`.upmconfig.toml` file to authenticate with a scoped registry. The `openupm login` command helps you authenticate with a npm server and store the info to the UPM config file.

There are two ways to authenticate with an npm server:
There are two ways to authenticate with a npm server:
- using token (recommended): a server-generated string for the grant of access and publishing rights.
- using basic authentication: the `username:password` pair (base64 encoded) is stored to authenticate with the server on each request.

Expand Down Expand Up @@ -266,7 +266,7 @@ openupm --verbose ...

## Work with Unity official (upstream) registry

Most commands can fallback to Unity upstream registry if necessary, to make it easier to mix the official registry with a 3rd-party registry. i.e.
Most commands can fall back to Unity upstream registry if necessary, to make it easier to mix the official registry with a 3rd-party registry. i.e.

```
$ openupm add com.unity.addressables com.littlebigfun.addressable-importer
Expand Down Expand Up @@ -299,7 +299,9 @@ You may need to set both http_proxy and https_proxy environment variables at the
<table>
<tbody>
<tr>
<!--suppress HtmlDeprecatedAttribute -->
<td align="center" valign="top" width="14.28%"><a href="http://littlebigfun.com"><img src="https://avatars.githubusercontent.com/u/125390?v=4?s=100" width="100px;" alt="Favo Yang"/><br /><sub><b>Favo Yang</b></sub></a><br /><a href="https://github.com/openupm/openupm-cli/commits?author=favoyang" title="Code">💻</a> <a href="#maintenance-favoyang" title="Maintenance">🚧</a></td>
<!--suppress HtmlDeprecatedAttribute -->
<td align="center" valign="top" width="14.28%"><a href="https://comradevanti.itch.io"><img src="https://avatars.githubusercontent.com/u/31240807?v=4?s=100" width="100px;" alt="Ramon Brullo"/><br /><sub><b>Ramon Brullo</b></sub></a><br /><a href="https://github.com/openupm/openupm-cli/commits?author=ComradeVanti" title="Code">💻</a></td>
</tr>
</tbody>
Expand Down
11 changes: 7 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"semver": "^7.5.4",
"ts-brand": "^0.0.2",
"update-notifier": "^5.1.0",
"yaml": "^2.0.1"
"yaml": "^2.2.2"
},
"volta": {
"node": "16.20.2"
Expand Down
15 changes: 11 additions & 4 deletions src/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
compareEditorVersion,
tryParseEditorVersion,
} from "./types/editor-version";
import { fetchPackageDependencies, fetchPackument } from "./registry-client";
import {
fetchPackageDependencies,
fetchPackument,
getNpmClient,
} from "./registry-client";
import { DomainName } from "./types/domain-name";
import { SemanticVersion } from "./types/semantic-version";
import {
Expand Down Expand Up @@ -52,6 +56,8 @@ export const add = async function (
const env = await parseEnv(options, true);
if (env === null) return 1;

const client = getNpmClient();

const addSingle = async function (pkg: PackageReference): Promise<AddResult> {
// dirty flag
let dirty = false;
Expand All @@ -69,9 +75,9 @@ export const add = async function (
const pkgsInScope: DomainName[] = [];
if (version === undefined || !isPackageUrl(version)) {
// verify name
let packument = await fetchPackument(env.registry, name);
let packument = await fetchPackument(env.registry, name, client);
if (!packument && env.upstream) {
packument = await fetchPackument(env.upstreamRegistry, name);
packument = await fetchPackument(env.upstreamRegistry, name, client);
if (packument) isUpstreamPackage = true;
}
if (!packument) {
Expand Down Expand Up @@ -150,7 +156,8 @@ export const add = async function (
env.upstreamRegistry,
name,
version,
true
true,
client
);
// add depsValid to pkgsInScope.
depsValid
Expand Down
19 changes: 13 additions & 6 deletions src/cmd-deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import log from "./logger";
import { parseEnv } from "./utils/env";
import { fetchPackageDependencies } from "./registry-client";
import { Dependency, fetchPackageDependencies, getNpmClient } from "./registry-client";
import { isPackageUrl } from "./types/package-url";
import {
packageReference,
Expand All @@ -15,6 +15,12 @@ export type DepsOptions = CmdOptions<{
deep?: boolean;
}>;

function errorPrefixForError(errorReason: Dependency["reason"]): string {
if (errorReason === "package404") return "missing dependency";
else if (errorReason === "version404") return "missing dependency version";
return "unknown";
}

/**
* @throws Error An unhandled error occurred
*/
Expand All @@ -26,6 +32,8 @@ export const deps = async function (
const env = await parseEnv(options, false);
if (env === null) return 1;

const client = getNpmClient();

const [name, version] = splitPackageReference(pkg);

if (version !== undefined && isPackageUrl(version))
Expand All @@ -36,7 +44,8 @@ export const deps = async function (
env.upstreamRegistry,
name,
version,
options.deep
options.deep || false,
client
);
depsValid
.filter((x) => !x.self)
Expand All @@ -46,10 +55,8 @@ export const deps = async function (
depsInvalid
.filter((x) => !x.self)
.forEach((x) => {
let reason = "unknown";
if (x.reason == "package404") reason = "missing dependency";
else if (x.reason == "version404") reason = "missing dependency version";
log.warn(reason, packageReference(x.name, x.version));
const prefix = errorPrefixForError(x.reason);
log.warn(prefix, packageReference(x.name, x.version));
});

return 0;
Expand Down
7 changes: 6 additions & 1 deletion src/cmd-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export const login = async function (
return 0;
};

/**
* The result of a login attempt. Either success with the token, or failure.
*/
type LoginResult = { code: 0; token: string } | { code: 1 };

/**
* Return npm login token
*/
Expand All @@ -91,7 +96,7 @@ const npmLogin = async function (
password: string,
email: string,
registry: RegistryUrl
) {
): Promise<LoginResult> {
const client = getNpmClient();
try {
const data = await client.adduser(registry, {
Expand Down
63 changes: 16 additions & 47 deletions src/cmd-search.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import npmSearch, { Options } from "libnpmsearch";
import npmFetch from "npm-registry-fetch";
import Table from "cli-table";
import log from "./logger";
import { is404Error, isHttpError } from "./utils/error-type-guards";
import * as os from "os";
import assert from "assert";
import { tryGetLatestVersion, UnityPackument } from "./types/packument";
import { UnityPackument } from "./types/packument";
import { parseEnv } from "./utils/env";
import { DomainName } from "./types/domain-name";
import { SemanticVersion } from "./types/semantic-version";
import { CmdOptions } from "./types/options";
import { Registry } from "./registry-client";

type DateString = string;

type TableRow = [DomainName, SemanticVersion, DateString, ""];
import { formatAsTable } from "./output-formatting";

type SearchResultCode = 0 | 1;

export type SearchOptions = CmdOptions;

export type SearchedPackument = Omit<UnityPackument, "versions"> & {
type SearchedPackument = Omit<UnityPackument, "versions"> & {
versions: Record<SemanticVersion, "latest">;
};

export type OldSearchResult =
type OldSearchResult =
| SearchedPackument[]
| Record<DomainName, SearchedPackument>;

Expand All @@ -45,14 +40,14 @@ const getNpmFetchOptions = function (registry: Registry): Options {
const searchEndpoint = async function (
registry: Registry,
keyword: string
): Promise<TableRow[] | undefined> {
): Promise<SearchedPackument[] | undefined> {
try {
// NOTE: The results of the search will be Packument objects so we can change the type
const results = <SearchedPackument[]>(
await npmSearch(keyword, getNpmFetchOptions(registry))
);
log.verbose("npmsearch", results.join(os.EOL));
return results.map(getTableRow);
return results;
} catch (err) {
if (isHttpError(err) && !is404Error(err)) {
log.error("", err.message);
Expand All @@ -64,32 +59,28 @@ const searchEndpoint = async function (
const searchOld = async function (
registry: Registry,
keyword: string
): Promise<TableRow[] | undefined> {
): Promise<SearchedPackument[] | undefined> {
// all endpoint
try {
const results = <OldSearchResult | undefined>(
await npmFetch.json("/-/all", getNpmFetchOptions(registry))
);
let objects: SearchedPackument[] = [];
let packuments: SearchedPackument[] = [];
if (results) {
if (Array.isArray(results)) {
// results is an array of objects
objects = results;
packuments = results;
} else {
// results is an object
if ("_updated" in results) delete results["_updated"];
objects = Object.values(results);
packuments = Object.values(results);
}
}
log.verbose("endpoint.all", objects.join(os.EOL));
// prepare rows
const rows = objects.map((packument) => {
return getTableRow(packument);
});
log.verbose("endpoint.all", packuments.join(os.EOL));
// filter keyword
const klc = keyword.toLowerCase();
return rows.filter(
(row) => row.filter((x) => x.toLowerCase().includes(klc)).length > 0
return packuments.filter((packument) =>
packument.name.toLowerCase().includes(klc)
);
} catch (err) {
if (isHttpError(err) && !is404Error(err)) {
Expand All @@ -99,26 +90,6 @@ const searchOld = async function (
}
};

const getTable = function () {
return new Table({
head: ["Name", "Version", "Date"],
colWidths: [42, 20, 12],
});
};

const getTableRow = function (packument: SearchedPackument): TableRow {
const name = packument.name;
const version = tryGetLatestVersion(packument);
let date = "";
if (packument.time && packument.time.modified)
date = packument.time.modified.split("T")[0]!;
if (packument.date) {
date = packument.date.toISOString().slice(0, 10);
}
assert(version !== undefined);
return [name, version, date, ""];
};

export async function search(
keyword: string,
options: SearchOptions
Expand All @@ -127,17 +98,15 @@ export async function search(
const env = await parseEnv(options, false);
if (env === null) return 1;

const table = getTable();
// search endpoint
let results = await searchEndpoint(env.registry, keyword);
// search old search
if (results === undefined) {
results = (await searchOld(env.registry, keyword)) || [];
results = await searchOld(env.registry, keyword);
}
// search upstream
if (results && results.length) {
results.forEach((x) => table.push(x.slice(0, -1)));
console.log(table.toString());
if (results !== undefined && results.length > 0) {
console.log(formatAsTable(results));
} else log.notice("", `No matches found for "${keyword}"`);
return 0;
}
8 changes: 5 additions & 3 deletions src/cmd-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import log from "./logger";
import assert from "assert";
import { tryGetLatestVersion, UnityPackument } from "./types/packument";
import { parseEnv } from "./utils/env";
import { fetchPackument } from "./registry-client";
import { fetchPackument, getNpmClient } from "./registry-client";
import { DomainName } from "./types/domain-name";
import {
packageReference,
Expand All @@ -23,6 +23,8 @@ export const view = async function (
// parse env
const env = await parseEnv(options, false);
if (env === null) return 1;
const client = getNpmClient();

// parse name
const [name, version] = splitPackageReference(pkg);
if (version) {
Expand All @@ -33,9 +35,9 @@ export const view = async function (
return 1;
}
// verify name
let packument = await fetchPackument(env.registry, name);
let packument = await fetchPackument(env.registry, name, client);
if (!packument && env.upstream)
packument = await fetchPackument(env.upstreamRegistry, name);
packument = await fetchPackument(env.upstreamRegistry, name, client);
if (!packument) {
log.error("404", `package not found: ${name}`);
return 1;
Expand Down
Loading

0 comments on commit eb26e69

Please sign in to comment.