Skip to content

Commit ee2c3d0

Browse files
cheahkhingacburdine
authored andcommitted
feat(proxy): add support for cli behind proxy (#1042)
1 parent eb64b92 commit ee2c3d0

File tree

7 files changed

+47
-16
lines changed

7 files changed

+47
-16
lines changed

lib/tasks/yarn-install.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ const {prerelease, satisfies} = require('semver');
99

1010
const errors = require('../errors');
1111
const yarn = require('../utils/yarn');
12+
const getProxyAgent = require('../utils/get-proxy-agent');
1213

1314
const subTasks = {
14-
dist: ctx => packageInfo('ghost', {version: ctx.version}).then(({dist, engines = {}}) => {
15+
dist: ctx => packageInfo('ghost', {
16+
version: ctx.version,
17+
agent: getProxyAgent()
18+
}).then(({dist, engines = {}}) => {
1519
const skipNodeVersionCheck = (process.env.GHOST_NODE_VERSION_CHECK === 'false');
1620
const isPrerelease = Boolean(prerelease(cliPackage.version));
1721

lib/utils/get-proxy-agent.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const {getProxyForUrl} = require('proxy-from-env');
2+
const NPM_REGISTRY = 'https://registry.npmjs.org/';
3+
4+
let proxyAgent = false;
5+
6+
module.exports = function getProxyAgent() {
7+
// Initialize Proxy Agent for proxy support if needed
8+
const proxyAddress = getProxyForUrl(NPM_REGISTRY);
9+
if (proxyAddress) {
10+
const HttpsProxyAgent = require('https-proxy-agent');
11+
proxyAgent = new HttpsProxyAgent(proxyAddress);
12+
}
13+
14+
return proxyAgent;
15+
};

lib/utils/pre-checks.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const semver = require('semver');
66
const latestVersion = require('latest-version');
77
const pkg = require('../../package.json');
8+
const getProxyAgent = require('./get-proxy-agent');
89

910
/**
1011
* Checks if a version update is available
@@ -16,7 +17,9 @@ module.exports = function preChecks(ui, system) {
1617

1718
const tasks = [{
1819
title: 'Checking for Ghost-CLI updates',
19-
task: () => latestVersion(pkg.name).then((latest) => {
20+
task: () => latestVersion(pkg.name, {
21+
agent: getProxyAgent()
22+
}).then((latest) => {
2023
if (semver.lt(pkg.version, latest)) {
2124
const chalk = require('chalk');
2225

lib/utils/version.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ const packageJson = require('package-json');
66

77
const cliPackage = require('../../package.json');
88
const {CliError, SystemError} = require('../errors');
9+
const getProxyAgent = require('./get-proxy-agent');
910

1011
const MIN_RELEASE = '>= 1.0.0';
1112

1213
const utils = {
1314
async loadVersions() {
14-
const result = await packageJson('ghost', {allVersions: true});
15+
const result = await packageJson('ghost', {
16+
allVersions: true,
17+
agent: getProxyAgent()
18+
});
1519
const versions = Object.keys(result.versions)
1620
.filter(v => semver.satisfies(v, MIN_RELEASE))
1721
.sort(semver.rcompare);

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"generate-password": "1.4.2",
6363
"global-modules": "2.0.0",
6464
"got": "9.6.0",
65+
"https-proxy-agent": "^3.0.1",
6566
"inquirer": "7.0.0",
6667
"is-running": "2.1.0",
6768
"latest-version": "5.1.0",
@@ -75,6 +76,7 @@
7576
"path-is-root": "0.1.0",
7677
"portfinder": "1.0.25",
7778
"prettyjson": "1.2.1",
79+
"proxy-from-env": "^1.0.0",
7880
"read-last-lines": "1.7.1",
7981
"replace-in-file": "4.2.0",
8082
"rxjs": "6.5.3",

test/unit/tasks/yarn-install-spec.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ describe('Unit: Tasks > yarn-install', function () {
120120
const dist = proxyquire(modulePath, {
121121
'package-json': infoStub
122122
}).subTasks.dist;
123-
const ctx = {version: '1.5.0'};
123+
const ctx = {version: '1.5.0', agent: false};
124124

125125
return dist(ctx).then(() => {
126126
expect(false, 'error should have been thrown').to.be.true;
127127
}).catch((error) => {
128128
expect(error).to.be.an.instanceof(errors.SystemError);
129129
expect(error.message).to.equal('Ghost v1.5.0 is not compatible with the current Node version.');
130130
expect(infoStub.calledOnce).to.be.true;
131-
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
131+
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
132132
});
133133
});
134134

@@ -141,14 +141,15 @@ describe('Unit: Tasks > yarn-install', function () {
141141
const dist = proxyquire(modulePath, {
142142
'package-json': infoStub
143143
}).subTasks.dist;
144-
const ctx = {version: '1.5.0'};
144+
const ctx = {version: '1.5.0', agent: false};
145145
process.env.GHOST_NODE_VERSION_CHECK = 'false';
146146

147147
return dist(ctx).then(() => {
148148
delete process.env.GHOST_NODE_VERSION_CHECK;
149149
expect(infoStub.calledOnce).to.be.true;
150-
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
150+
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
151151
expect(ctx).to.deep.equal({
152+
agent: false,
152153
version: '1.5.0',
153154
shasum: 'asdf1234',
154155
tarball: 'something.tgz'
@@ -169,15 +170,15 @@ describe('Unit: Tasks > yarn-install', function () {
169170
'package-json': infoStub,
170171
'../../package.json': {version: '1.0.0'}
171172
}).subTasks.dist;
172-
const ctx = {version: '1.5.0'};
173+
const ctx = {version: '1.5.0', agent: false};
173174

174175
return dist(ctx).then(() => {
175176
expect(false, 'error should have been thrown').to.be.true;
176177
}).catch((error) => {
177178
expect(error).to.be.an.instanceof(errors.SystemError);
178179
expect(error.message).to.equal('Ghost v1.5.0 is not compatible with this version of the CLI.');
179180
expect(infoStub.calledOnce).to.be.true;
180-
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
181+
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
181182
});
182183
});
183184

@@ -191,12 +192,13 @@ describe('Unit: Tasks > yarn-install', function () {
191192
'package-json': infoStub,
192193
'../../package.json': {version: '1.10.0-beta.0'}
193194
}).subTasks.dist;
194-
const ctx = {version: '1.5.0'};
195+
const ctx = {version: '1.5.0', agent: false};
195196

196197
return dist(ctx).then(() => {
197198
expect(infoStub.calledOnce).to.be.true;
198-
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
199+
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
199200
expect(ctx).to.deep.equal({
201+
agent: false,
200202
version: '1.5.0',
201203
shasum: 'asdf1234',
202204
tarball: 'something.tgz'
@@ -210,12 +212,13 @@ describe('Unit: Tasks > yarn-install', function () {
210212
const dist = proxyquire(modulePath, {
211213
'package-json': infoStub
212214
}).subTasks.dist;
213-
const ctx = {version: '1.5.0'};
215+
const ctx = {version: '1.5.0', agent: false};
214216

215217
return dist(ctx).then(() => {
216218
expect(infoStub.calledOnce).to.be.true;
217-
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
219+
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
218220
expect(ctx).to.deep.equal({
221+
agent: false,
219222
version: '1.5.0',
220223
shasum: 'asdf1234',
221224
tarball: 'something.tgz'

test/unit/utils/pre-checks-spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Unit: Utils > pre-checks', function () {
4141
}).catch((err) => {
4242
expect(err.message).to.equal(testError.message);
4343
expect(latestVersion.calledOnce).to.be.true;
44-
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
44+
expect(latestVersion.calledWithExactly('ghost', {agent: false})).to.be.true;
4545
done();
4646
});
4747
});
@@ -57,7 +57,7 @@ describe('Unit: Utils > pre-checks', function () {
5757
}, {log}).then(([task]) => task.task()).then(() => {
5858
expect(log.called).to.be.false;
5959
expect(latestVersion.calledOnce).to.be.true;
60-
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
60+
expect(latestVersion.calledWithExactly('ghost', {agent: false})).to.be.true;
6161
});
6262
});
6363

@@ -76,7 +76,7 @@ describe('Unit: Utils > pre-checks', function () {
7676
expect(stripAnsi(msg)).to.match(/You are running an outdated version of Ghost-CLI/);
7777

7878
expect(latestVersion.calledOnce).to.be.true;
79-
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
79+
expect(latestVersion.calledWithExactly('ghost', {agent: false})).to.be.true;
8080
});
8181
});
8282

0 commit comments

Comments
 (0)