Skip to content

Commit c99ab14

Browse files
authoredMar 9, 2024··
fix: ensure proper support for corporate proxies (#143)
Proxies can be passed as env variables or as npm config options. The highest precedence have the GLOBAL_AGENT_* env variables before the regular env variables followed by the npm configuration options. 1.) env: GLOBAL_AGENT_HTTP_PROXY 2.) env: HTTP_PROXY 3.) npm: http-proxy 4.) npm: proxy Fixes #142
1 parent c68817b commit c99ab14

File tree

5 files changed

+187
-35
lines changed

5 files changed

+187
-35
lines changed
 

‎.npmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
engine-strict=true
1+
engine-strict=true

‎README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,20 @@ yo easy-ui5 [project|library] <sub-generator-id>
104104
If you are running Easy UI5 behind a coporate proxy, just use the default proxy environment variables for Node.js to configure your corporate proxy:
105105

106106
- `HTTP_PROXY`: Specify the value to use as the HTTP proxy for all connections, e.g., `HTTP_PROXY="http://proxy.mycompany.com:8080/"`.
107-
- `HTTPS_PROXY`: Specify the value to use as the HTTPS proxy for all connections, e.g., `HTTPS_PROXY="https://proxy.mycompany.com:8080/"`.
107+
- `HTTPS_PROXY`: Specify the value to use as the HTTPS proxy for all connections, e.g., `HTTPS_PROXY="http://proxy.mycompany.com:8080/"`.
108108
- `NO_PROXY`: Define the hosts that should bypass the proxy, e.g., `NO_PROXY="localhost,.mycompany.com,192.168.6.254:80"`.
109109

110+
In addition, Easy UI5 also supports proxy configuration from the `.npmrc` configuration:
111+
112+
```text
113+
http-proxy=http://proxy.mycompany.com:8080/
114+
https-proxy=http://proxy.mycompany.com:8080/
115+
proxy=http://proxy.mycompany.com:8080/
116+
no-proxy=localhost,.mycompany.com,192.168.6.254:80
117+
```
118+
119+
This configuration is shared with npm itself since this proxy configuration is used to download the packages from npm.
120+
110121
## How to obtain support
111122

112123
Please use the GitHub bug tracking system to post questions, bug reports or to create pull requests.

‎generators/app/index.js

+30-17
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,33 @@ import { Octokit } from "@octokit/rest";
1515
import { throttling } from "@octokit/plugin-throttling";
1616
const MyOctokit = Octokit.plugin(throttling);
1717
import spawn from "cross-spawn";
18+
import nodeFetch from "node-fetch";
1819

1920
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
2021

21-
// apply proxy settings to GLOBAL_AGENT to support the proxy configuration
22-
// provided via the standard Node.js environment varibales (used for fetch API)
23-
let HTTP_PROXY, HTTPS_PROXY, NO_PROXY;
24-
if (global?.GLOBAL_AGENT) {
25-
HTTP_PROXY = global.GLOBAL_AGENT.HTTP_PROXY = process.env.HTTP_PROXY || process.env.http_proxy;
26-
HTTPS_PROXY = global.GLOBAL_AGENT.HTTPS_PROXY = process.env.HTTPS_PROXY || process.env.https_proxy;
27-
NO_PROXY = global.GLOBAL_AGENT.NO_PROXY = process.env.NO_PROXY || process.env.no_proxy;
28-
}
29-
3022
// helper to retrieve config entries from npm
3123
// --> npm config set easy-ui5_addGhOrg XYZ
32-
const NPM_CONFIG_PREFIX = "easy-ui5_";
3324
let npmConfig;
34-
const getNPMConfig = (configName) => {
25+
const getNPMConfig = (configName, prefix = "easy-ui5_") => {
3526
if (!npmConfig) {
3627
npmConfig = libnpmconfig.read();
3728
}
38-
return npmConfig && npmConfig[`${NPM_CONFIG_PREFIX}${configName}`];
29+
return npmConfig && npmConfig[`${prefix}${configName}`];
3930
};
4031

32+
// apply proxy settings to GLOBAL_AGENT to support the proxy configuration for node-fetch using the GLOBAL_AGENT
33+
// ==> the configuration is derived from the environment variables ([GLOBAL_AGENT_](HTTP|HTTPS|NO)_PROXY) and the npm config ((http|https|no)-proxy)
34+
// ==> empty values will allow to override the more general proxy settings and make the proxy value undefined
35+
let HTTP_PROXY, HTTPS_PROXY, NO_PROXY;
36+
if (global?.GLOBAL_AGENT) {
37+
HTTP_PROXY = process.env.GLOBAL_AGENT_HTTP_PROXY ?? process.env.HTTP_PROXY ?? process.env.http_proxy ?? getNPMConfig("http-proxy", "") ?? getNPMConfig("proxy", "");
38+
global.GLOBAL_AGENT.HTTP_PROXY = HTTP_PROXY = HTTP_PROXY || global.GLOBAL_AGENT.HTTP_PROXY;
39+
HTTPS_PROXY = process.env.GLOBAL_AGENT_HTTPS_PROXY ?? process.env.HTTPS_PROXY ?? process.env.https_proxy ?? getNPMConfig("https-proxy", "") ?? getNPMConfig("proxy", "");
40+
global.GLOBAL_AGENT.HTTPS_PROXY = HTTPS_PROXY = HTTPS_PROXY || global.GLOBAL_AGENT.HTTPS_PROXY;
41+
NO_PROXY = process.env.GLOBAL_AGENT_NO_PROXY ?? process.env.NO_PROXY ?? process.env.no_proxy ?? getNPMConfig("no-proxy", "");
42+
global.GLOBAL_AGENT.NO_PROXY = NO_PROXY = NO_PROXY || global.GLOBAL_AGENT.NO_PROXY;
43+
}
44+
4145
// the command line options of the generator
4246
const generatorOptions = {
4347
pluginsHome: {
@@ -265,7 +269,7 @@ export default class extends Generator {
265269
});
266270
generator.branch = repoInfo.data.default_branch;
267271
} catch (e) {
268-
console.error(`Generator "${owner}/${repo}!${dir}${branch ? "#" + branch : ""}" not found! Run with --verbose for details!`);
272+
console.error(`Generator "${owner}/${repo}!${dir}${branch ? "#" + branch : ""}" not found! Run with --verbose for details!\n(Hint: ${e.message})`);
269273
if (this.options.verbose) {
270274
console.error(e);
271275
}
@@ -283,7 +287,9 @@ export default class extends Generator {
283287
});
284288
commitSHA = reqBranch.data.commit.sha;
285289
} catch (ex) {
286-
console.error(chalk.red(`Failed to retrieve the branch "${generator.branch}" for repository "${generator.name}" for "${generator.org}" organization! Run with --verbose for details!`));
290+
console.error(
291+
chalk.red(`Failed to retrieve the branch "${generator.branch}" for repository "${generator.name}" for "${generator.org}" organization! Run with --verbose for details!\n(Hint: ${e.message})`)
292+
);
287293
if (this.options.verbose) {
288294
console.error(chalk.red(ex.message));
289295
}
@@ -405,6 +411,13 @@ export default class extends Generator {
405411
// define the options for the Octokit API
406412
const octokitOptions = {
407413
userAgent: `${this.rootGeneratorName()}:${this.rootGeneratorVersion()}`,
414+
request: {
415+
fetch: (_url, _options) => {
416+
return nodeFetch(_url, {
417+
..._options,
418+
});
419+
},
420+
},
408421
auth: this.options.ghAuthToken,
409422
baseUrl: this.options.ghBaseUrl,
410423
throttle: {
@@ -555,7 +568,7 @@ export default class extends Generator {
555568
};
556569
});
557570
} catch (e) {
558-
console.error("Failed to connect to bestofui5.org to retrieve all available generators! Run with --verbose for details!");
571+
console.error(`Failed to connect to bestofui5.org to retrieve all available generators! Run with --verbose for details!\n(Hint: ${e.message})`);
559572
if (this.options.verbose) {
560573
console.error(e);
561574
}
@@ -566,7 +579,7 @@ export default class extends Generator {
566579
try {
567580
availGenerators = await listGeneratorsForOrg(this.options.ghOrg, this.options.subGeneratorPrefix, this.options.ghThreshold);
568581
} catch (e) {
569-
console.error(`Failed to connect to GitHub to retrieve all available generators for "${this.options.ghOrg}" organization! Run with --verbose for details!`);
582+
console.error(`Failed to connect to GitHub to retrieve all available generators for "${this.options.ghOrg}" organization! Run with --verbose for details!\n(Hint: ${e.message})`);
570583
if (this.options.verbose) {
571584
console.error(e);
572585
}
@@ -585,7 +598,7 @@ export default class extends Generator {
585598
try {
586599
availGenerators = availGenerators.concat(await listGeneratorsForUser(this.options.addGhOrg, this.options.addSubGeneratorPrefix, this.options.ghThreshold));
587600
} catch (e1) {
588-
console.error(`Failed to connect to GitHub to retrieve additional generators for organization or user "${this.options.addGhOrg}"! Run with --verbose for details!`);
601+
console.error(`Failed to connect to GitHub to retrieve additional generators for organization or user "${this.options.addGhOrg}"! Run with --verbose for details!\n(Hint: ${e.message})`);
589602
if (this.options.verbose) {
590603
console.error(e1);
591604
}

‎package-lock.json

+143-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"colors": "^1.4.0",
5656
"glob": "^10.3.10",
5757
"libnpmconfig": "^1.2.1",
58+
"node-fetch": "^3.3.2",
5859
"rimraf": "^5.0.5",
5960
"yeoman-environment": "^3.19.3",
6061
"yeoman-generator": "^5.10.0",

0 commit comments

Comments
 (0)
Please sign in to comment.