From 50e5e5fa22b933add1d3e4790898a807f7bf80df Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Mon, 3 Oct 2022 19:00:44 +0800 Subject: [PATCH] refactor(cli): support proxy --- packages/cli/package.json | 5 +++-- packages/cli/src/utilities.ts | 7 ++++++- pnpm-lock.yaml | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index be4e299c4..57fa62b20 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,11 +1,12 @@ { "name": "@logto/cli", - "version": "1.0.0-beta.9", + "version": "1.0.0-beta.10", "description": "Logto CLI.", "author": "Silverhand Inc. ", "homepage": "https://github.com/logto-io/logto#readme", "license": "MPL-2.0", "main": "lib/index.js", + "bin": "lib/index.js", "files": [ "lib" ], @@ -13,7 +14,6 @@ "type": "git", "url": "git+https://github.com/logto-io/logto.git" }, - "bin": "lib/index.js", "scripts": { "precommit": "lint-staged", "build": "rimraf lib && tsc", @@ -32,6 +32,7 @@ "dependencies": { "chalk": "^4.1.2", "got": "^11.8.2", + "hpagent": "^1.0.0", "ora": "^5.0.0", "prompts": "^2.4.2", "semver": "^7.3.7", diff --git a/packages/cli/src/utilities.ts b/packages/cli/src/utilities.ts index eee72be13..f5e60060d 100644 --- a/packages/cli/src/utilities.ts +++ b/packages/cli/src/utilities.ts @@ -3,6 +3,7 @@ import { createWriteStream } from 'fs'; import chalk from 'chalk'; import got, { Progress } from 'got'; +import { HttpsProxyAgent } from 'hpagent'; import ora from 'ora'; export const safeExecSync = (command: string) => { @@ -32,8 +33,12 @@ export const log: Log = Object.freeze({ }); export const downloadFile = async (url: string, destination: string) => { + const { HTTPS_PROXY, HTTP_PROXY, https_proxy, http_proxy } = process.env; const file = createWriteStream(destination); - const stream = got.stream(url); + const proxy = HTTPS_PROXY ?? https_proxy ?? HTTP_PROXY ?? http_proxy; + const stream = got.stream(url, { + ...(proxy && { agent: { https: new HttpsProxyAgent({ proxy }) } }), + }); const spinner = ora({ text: 'Connecting', prefixText: chalk.blue('[info]'), diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 299ca13b7..415f161ab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,6 +30,7 @@ importers: chalk: ^4.1.2 eslint: ^8.21.0 got: ^11.8.2 + hpagent: ^1.0.0 lint-staged: ^13.0.0 ora: ^5.0.0 prettier: ^2.7.1 @@ -42,6 +43,7 @@ importers: dependencies: chalk: 4.1.2 got: 11.8.3 + hpagent: 1.0.0 ora: 5.4.1 prompts: 2.4.2 semver: 7.3.7 @@ -8110,6 +8112,11 @@ packages: lru-cache: 7.10.1 dev: true + /hpagent/1.0.0: + resolution: {integrity: sha512-SCleE2Uc1bM752ymxg8QXYGW0TWtAV4ZW3TqH1aOnyi6T6YW2xadCcclm5qeVjvMvfQ2RKNtZxO7uVb9CTPt1A==} + engines: {node: '>=14'} + dev: false + /html-encoding-sniffer/3.0.0: resolution: {integrity: sha512-oWv4T4yJ52iKrufjnyZPkrN0CH3QnrUqdB6In1g5Fe1mia8GmF36gnfNySxoZtxD5+NmYw1EElVXiBk93UeskA==} engines: {node: '>=12'}