Skip to content

Commit

Permalink
fix:Serverless-Devs#595 Serverless Devs 代理模式的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
xsahxl committed Oct 12, 2022
1 parent f91ad2f commit 996ad96
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 5 deletions.
2 changes: 2 additions & 0 deletions bin/s-set-proxy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../lib/set/proxy');
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.2",
"global-agent": "^3.0.0",
"husky": "^4.2.3",
"inquirer": "8.2.0",
"inquirer-autocomplete-prompt": "^1.3.0",
Expand Down
1 change: 1 addition & 0 deletions shell/postbuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ esbuild lib/config/get/index.js --bundle --log-level=error --minify --external:@
esbuild lib/config/rename/index.js --bundle --log-level=error --minify --external:@serverless-devs/core --external:@serverless-devs/ui --platform=node --format=cjs --target=node10.4 --allow-overwrite --outfile=lib/config/rename/index.js
esbuild lib/set/index.js --bundle --log-level=error --minify --external:@serverless-devs/core --external:@serverless-devs/ui --platform=node --format=cjs --target=node10.4 --allow-overwrite --outfile=lib/set/index.js
esbuild lib/set/registry/index.js --bundle --log-level=error --minify --external:@serverless-devs/core --external:@serverless-devs/ui --platform=node --format=cjs --target=node10.4 --allow-overwrite --outfile=lib/set/registry/index.js
esbuild lib/set/proxy/index.js --bundle --log-level=error --minify --external:@serverless-devs/core --external:@serverless-devs/ui --platform=node --format=cjs --target=node10.4 --allow-overwrite --outfile=lib/set/proxy/index.js
esbuild lib/set/locale/index.js --bundle --log-level=error --minify --external:@serverless-devs/core --external:@serverless-devs/ui --platform=node --format=cjs --target=node10.4 --allow-overwrite --outfile=lib/set/locale/index.js
esbuild lib/set/analysis/index.js --bundle --log-level=error --minify --external:@serverless-devs/core --external:@serverless-devs/ui --platform=node --format=cjs --target=node10.4 --allow-overwrite --outfile=lib/set/analysis/index.js
esbuild lib/set/workspace/index.js --bundle --log-level=error --minify --external:@serverless-devs/core --external:@serverless-devs/ui --platform=node --format=cjs --target=node10.4 --allow-overwrite --outfile=lib/set/workspace/index.js
Expand Down
19 changes: 19 additions & 0 deletions src/global-agent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const globalAgent = require('global-agent');
import { getConfig } from '../utils/handler-set-config';

export const setProxy = () => {
const proxy_enable = getConfig('proxy_enable');
if (!proxy_enable) return;

globalAgent.bootstrap();

const http_proxy = getConfig('http_proxy');
const https_proxy = getConfig('https_proxy');
if (http_proxy) {
(global as any).GLOBAL_AGENT.HTTP_PROXY = http_proxy;
}
if (https_proxy) {
(global as any).GLOBAL_AGENT.HTTPS_PROXY = https_proxy;
}
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
};
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import SpecialCommad from './special-commad';
import help from './help';
import { COMMAND_LIST } from './constant';
import checkNodeVersion from './check-node-version';
import { setProxy } from './global-agent';
import core from './utils/core';
const pkg = require('../package.json');
const { lodash } = core;
const { join, includes } = lodash;

(async () => {
(process as any).noDeprecation = true;
// 检查node版本是否过低
checkNodeVersion();
// ignore warning
setProxy();
process.env['CLI_VERSION'] = pkg.version;
registerCommandChecker(program);
const system_command = program
Expand All @@ -39,9 +43,6 @@ const { join, includes } = lodash;
// TODO: 目前core和s并不依赖temp_params环境变量,只是提供给组件用,后续组件移除temp_params后,此行代码可以删掉
process.env['temp_params'] = join(process.argv.slice(2), ' ');

// ignore warning
(process as any).noDeprecation = true;

new UpdateNotifier().init().notify();

if (process.argv.length === 2) {
Expand Down
4 changes: 2 additions & 2 deletions src/set/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ ${emoji('📖')} Document: ${colors.underline(
program
.name('s set')
.usage('[commands] [options]')
.command('registry', `${emoji('👀')} Set up a custom registry`)
// .command('locale', `${emoji('🔧')} Set up current language`)
.command('registry', `${emoji('👀')} Set registry information`)
.command('proxy', `${emoji('🔧')} Set proxy information`)
.command('analysis', `${emoji('👉')} Set to enable or disable analysis`)
.command('workspace', `${emoji('🙊')} Set workspace path`)
.helpOption('-h, --help', 'Display help for command')
Expand Down
86 changes: 86 additions & 0 deletions src/set/proxy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import program from '@serverless-devs/commander';
import { logger, getProcessArgv } from '../../utils';
import { CommandError } from '../../error';
import core from '../../utils/core';
import { emoji } from '../../utils/common';
const { inquirer } = core;
import { setConfig } from '../../utils/handler-set-config';
const { colors, lodash } = core;
const { has } = lodash;

const choices = [
{
name: 'http_proxy',
value: 'http_proxy',
},
{
name: 'https_proxy',
value: 'https_proxy',
},
];

export const registryInquire = [
{
type: 'list',
name: 'proxy',
message: 'Choose a proxy?',
choices,
},
];
program
.name('s set proxy')
.usage('[options]')
.option('--enable', 'whether to enable proxy')
.option('--http_proxy <http_proxy_value>', 'Specify the http_proxy.')
.option('--https_proxy <https_proxy_value>', 'Specify the https_proxy.')
.helpOption('-h, --help', 'Display help for command')
.addHelpCommand(false)
.description(
`Set proxy information.
Example:
$ s set proxy
$ s set proxy --http_proxy xxxx:xxx --https_proxy xxxx:xxx
$ s set proxy --enable false
${emoji('📖')} Document: ${colors.underline(
'https://github.com/Serverless-Devs/Serverless-Devs/tree/master/docs/zh/command/set.md',
)}`,
)
.parse(process.argv);
(async () => {
const argv = getProcessArgv();
const { http_proxy, https_proxy } = argv;
if (http_proxy || https_proxy || has(argv, 'enable')) {
http_proxy && setConfig('http_proxy', http_proxy);
https_proxy && setConfig('https_proxy', https_proxy);
has(argv, 'enable') && setConfig('proxy_enable', argv.enable === 'false' ? false : true);
return logger.success('Setup succeeded');
}

if (program.args.length === 0) {
const answer = await inquirer.prompt([
{
type: 'input',
message: 'Please enter http_proxy: ',
name: 'http_proxy',
},
{
type: 'input',
message: 'Please enter https_proxy: ',
name: 'https_proxy',
},
{
type: 'confirm',
message: 'Do you want to enable proxy',
default: true,
name: 'http_proxy_enable',
},
]);
setConfig('http_proxy', answer.http_proxy);
setConfig('https_proxy', answer.https_proxy);
setConfig('proxy_enable', answer.http_proxy_enable);
}
})().catch(err => {
throw new CommandError(err.message);
});

0 comments on commit 996ad96

Please sign in to comment.