Skip to content

Commit

Permalink
feat: support http_proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Apr 19, 2020
1 parent 1f14666 commit 4afde73
Show file tree
Hide file tree
Showing 5 changed files with 511 additions and 48 deletions.
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

![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 NodeJS.

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.
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.

- [openupm-cli](#openupm-cli)
- [How it works](#how-it-works)
Expand All @@ -17,13 +17,14 @@ The tool is designed to work with [the OpenUPM registry](https://openupm.com), b
- [View package information](#view-package-information)
- [View package dependencies](#view-package-dependencies)
- [Command options](#command-options)
- [Work with unity official (upstream) registry](#work-with-unity-official-upstream-registry)
- [Work with Unity official (upstream) registry](#work-with-unity-official-upstream-registry)
- [Work with proxy](#work-with-proxy)

## How it works

The command line tool installs the 3rd-party registry as a scoped registry, and maintains the `Packages/manifest.json` file when adding/removing packages. If the manifest file is modified, the *Unity Package Manager* will detect the changes and try to resolve the package dependencies.
The command-line tool installs the 3rd-party registry as a scoped registry and maintains the `Packages/manifest.json` file when adding/removing packages. If the manifest file is modified, the *Unity Package Manager* will detect the changes and try to resolve the package dependencies.

> Notice: the command line tool does not directly install/uninstall package tarballs, at least for now.
> Notice: the command-line tool does not directly install/uninstall package tarballs, at least for now.
## Installation

Expand All @@ -40,7 +41,7 @@ yarn global add openupm-cli

### Windows platform troubleshooting

If npm is not available in your cmd/powershell/git-bash, please configure your [environment variables](https://stackoverflow.com/questions/27864040/fixing-npm-path-in-windows-8-and-10).
If npm is not available in your CMD/PowerShell/Git-Bash, please configure your [environment variables](https://stackoverflow.com/questions/27864040/fixing-npm-path-in-windows-8-and-10).

```
# for npm
Expand Down Expand Up @@ -74,9 +75,9 @@ openupm remove <pkg> [otherPkgs...]
openupm search <keyword>
```

If the registry doesn't support the new search endpoint, it will fall back to old `/-/all` endpoint. If no package was found, it will search unity official registry instead.
If the registry doesn't support the new search endpoint, it will fall back to old `/-/all` endpoint. If no package was found, it will search the Unity official registry instead.

Due to the fact that upstream unity registry doesn't support search endpoint as of December 2019, the search command will only query the current registry.
Because the upstream Unity registry doesn't support the search endpoint as of December 2019, the search command will only query the current registry.

### View package information
```
Expand All @@ -96,13 +97,13 @@ open deps <pkg> --deep

### Command options

The cli assumes current working directory (cwd) is the root of an Unity project (the parent of `Assets` folder). However you can specify the cwd.
The cli assumes the current working directory (CWD) is the root of a Unity project (the parent of the `Assets` folder). However, you can specify the CWD.

```
openupm --chdir <unity-project-path>
```

Specify other 3rd-party registry (defaults to openupm registry)
Specify another 3rd-party registry (defaults to openupm registry)

```
openupm --registry <registry-url>
Expand All @@ -111,7 +112,7 @@ i.e.
openupm --registry http://127.0.0.1:4873
```

Turn off unity official (upstream) registry
Turn off Unity official (upstream) registry

```
openupm --no-upstream ...
Expand All @@ -123,13 +124,23 @@ Turn on debug logging
openupm --verbose ...
```

### Work with unity official (upstream) registry
## Work with Unity official (upstream) registry

Most commands can fallback to unity upstream registry if necessary, to make it easier to mix official registry with 3rd-party registry. i.e.
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.

```
$ openupm add com.unity.addressables com.littlebigfun.addressable-importer
added: com.unity.addressables@1.4.0 # from unity registry
added: com.littlebigfun.addressable-importer@0.4.1 # from openupm registry
...
```

## Work with proxy

OpenUPM-CLI supports HTTP, HTTPS, or SOCKS proxy specified by the http_proxy environment variable.

```
http_proxy="http://127.0.0.1:8080" openupm ...
```

You may need set both http_proxy and https_proxy environment variables in system-level to [enable Unity work with a proxy](https://forum.unity.com/threads/using-unity-when-behind-a-proxy.69896/).
13 changes: 9 additions & 4 deletions lib/cmd-search.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const log = require("./logger");
const superagent = require("superagent");
const Table = require("cli-table");
const { env, getCache, parseEnv, getLatestVersion } = require("./core");
const {
env,
getCache,
parseEnv,
getLatestVersion,
httpGet
} = require("./core");
const { isConnectionError, is404Error } = require("./error-handler");

const searchEndpoint = async function(keyword, registry) {
if (!registry) registry = env.registry;
let serverUrl = `${registry}/-/v1/search?text=${keyword}&size=20&from=0&quality=0.65&popularity=0.98&maintenance=0.5`;
try {
log.debug(`http get ${serverUrl}`);
const response = await superagent.get(serverUrl).accept("json");
const response = await httpGet(serverUrl).accept("json");
response.body = JSON.parse(response.text);
log.debug(`status ${response.status}`);
log.debug(response.body);
Expand Down Expand Up @@ -48,7 +53,7 @@ const searchOld = async function(keyword) {
: `${env.registry}/-/all`;
try {
log.debug(`http get ${serverUrl}`);
const response = await superagent.get(serverUrl).accept("json");
const response = await httpGet(serverUrl).accept("json");
response.body = JSON.parse(response.text);
log.debug(`status ${response.status}`);
log.debug(response.body);
Expand Down
17 changes: 15 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const log = require("./logger");
const path = require("path");
const os = require("os");
const superagent = require("superagent");
// extend with Request#proxy()
require("superagent-proxy")(superagent);
const url = require("url");
const { isConnectionError, is404Error } = require("./error-handler");

Expand Down Expand Up @@ -61,7 +63,7 @@ const parseEnv = function(options, { checkPath }) {
return true;
};

// parse name to {name, version}
// Parse name to {name, version}
const parseName = function(pkg) {
const segs = pkg.split("@");
const name = segs[0];
Expand All @@ -70,14 +72,24 @@ const parseName = function(pkg) {
return { name, version };
};

// Superagent.get with proxy
const httpGet = function(url) {
const req = superagent.get(url);
var proxy = process.env.http_proxy;
if (proxy) {
req.proxy(proxy);
}
return req;
};

// Fetch package info json from registry
const fetchPackageInfo = async function(name, registry) {
if (!registry) registry = env.registry;
log.debug(`fetchPackageInfo: ${name} registry=${registry}`);
try {
let pkgUrl = `${registry}/${name}`;
log.debug(`http get ${pkgUrl}`);
const response = await superagent.get(pkgUrl).accept("json");
const response = await httpGet(pkgUrl).accept("json");
response.body = JSON.parse(response.text);
log.debug(`status ${response.status}`);
log.debug(response.body);
Expand Down Expand Up @@ -259,6 +271,7 @@ module.exports = {
fetchPackageInfo,
fetchPackageDependencies,
getLatestVersion,
httpGet,
loadManifest,
parseEnv,
parseName,
Expand Down
Loading

0 comments on commit 4afde73

Please sign in to comment.