Skip to content

Commit

Permalink
Merge 9bb185d into 6e8c98c
Browse files Browse the repository at this point in the history
  • Loading branch information
cheahkhing committed Nov 5, 2019
2 parents 6e8c98c + 9bb185d commit 4c5242c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/tasks/yarn-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ const {prerelease, satisfies} = require('semver');

const errors = require('../errors');
const yarn = require('../utils/yarn');
const getProxyAgent = require('../utils/get-proxy-agent');

const subTasks = {
dist: ctx => packageInfo('ghost', {version: ctx.version}).then(({dist, engines = {}}) => {
dist: ctx => packageInfo('ghost', {
version: ctx.version,
agent: getProxyAgent()
}).then(({dist, engines = {}}) => {
const skipNodeVersionCheck = (process.env.GHOST_NODE_VERSION_CHECK === 'false');
const isPrerelease = Boolean(prerelease(cliPackage.version));

Expand Down
17 changes: 17 additions & 0 deletions lib/utils/get-proxy-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const {getProxyForUrl} = require('proxy-from-env');
const HttpsProxyAgent = require('https-proxy-agent');
const NPM_REGISTRY = 'https://registry.npmjs.org/';

let proxyAgent = false;

module.exports = function getProxyAgent() {
// Initialize Proxy Agent for proxy support if needed
const proxyAddress = getProxyForUrl(NPM_REGISTRY);
if (proxyAddress) {
proxyAgent = new HttpsProxyAgent(proxyAddress);
}

return proxyAgent;
};
5 changes: 4 additions & 1 deletion lib/utils/pre-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const semver = require('semver');
const latestVersion = require('latest-version');
const pkg = require('../../package.json');
const getProxyAgent = require('./get-proxy-agent');

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

const tasks = [{
title: 'Checking for Ghost-CLI updates',
task: () => latestVersion(pkg.name).then((latest) => {
task: () => latestVersion(pkg.name, {
agent: getProxyAgent()
}).then((latest) => {
if (semver.lt(pkg.version, latest)) {
const chalk = require('chalk');

Expand Down
6 changes: 5 additions & 1 deletion lib/utils/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ const packageJson = require('package-json');

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

const MIN_RELEASE = '>= 1.0.0';

const utils = {
async loadVersions() {
const result = await packageJson('ghost', {allVersions: true});
const result = await packageJson('ghost', {
allVersions: true,
agent: getProxyAgent()
});
const versions = Object.keys(result.versions)
.filter(v => semver.satisfies(v, MIN_RELEASE))
.sort(semver.rcompare);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"generate-password": "1.4.2",
"global-modules": "2.0.0",
"got": "9.6.0",
"https-proxy-agent": "^3.0.1",
"inquirer": "7.0.0",
"is-running": "2.1.0",
"latest-version": "5.1.0",
Expand All @@ -74,6 +75,7 @@
"path-is-root": "0.1.0",
"portfinder": "1.0.25",
"prettyjson": "1.2.1",
"proxy-from-env": "^1.0.0",
"read-last-lines": "1.7.1",
"replace-in-file": "4.2.0",
"rxjs": "6.5.3",
Expand Down
23 changes: 13 additions & 10 deletions test/unit/tasks/yarn-install-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ describe('Unit: Tasks > yarn-install', function () {
const dist = proxyquire(modulePath, {
'package-json': infoStub
}).subTasks.dist;
const ctx = {version: '1.5.0'};
const ctx = {version: '1.5.0', agent: false};

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

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

return dist(ctx).then(() => {
delete process.env.GHOST_NODE_VERSION_CHECK;
expect(infoStub.calledOnce).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
expect(ctx).to.deep.equal({
agent: false,
version: '1.5.0',
shasum: 'asdf1234',
tarball: 'something.tgz'
Expand All @@ -169,15 +170,15 @@ describe('Unit: Tasks > yarn-install', function () {
'package-json': infoStub,
'../../package.json': {version: '1.0.0'}
}).subTasks.dist;
const ctx = {version: '1.5.0'};
const ctx = {version: '1.5.0', agent: false};

return dist(ctx).then(() => {
expect(false, 'error should have been thrown').to.be.true;
}).catch((error) => {
expect(error).to.be.an.instanceof(errors.SystemError);
expect(error.message).to.equal('Ghost v1.5.0 is not compatible with this version of the CLI.');
expect(infoStub.calledOnce).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
});
});

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

return dist(ctx).then(() => {
expect(infoStub.calledOnce).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
expect(ctx).to.deep.equal({
agent: false,
version: '1.5.0',
shasum: 'asdf1234',
tarball: 'something.tgz'
Expand All @@ -210,12 +212,13 @@ describe('Unit: Tasks > yarn-install', function () {
const dist = proxyquire(modulePath, {
'package-json': infoStub
}).subTasks.dist;
const ctx = {version: '1.5.0'};
const ctx = {version: '1.5.0', agent: false};

return dist(ctx).then(() => {
expect(infoStub.calledOnce).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0'})).to.be.true;
expect(infoStub.calledWithExactly('ghost', {version: '1.5.0', agent: false})).to.be.true;
expect(ctx).to.deep.equal({
agent: false,
version: '1.5.0',
shasum: 'asdf1234',
tarball: 'something.tgz'
Expand Down
6 changes: 3 additions & 3 deletions test/unit/utils/pre-checks-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Unit: Utils > pre-checks', function () {
}).catch((err) => {
expect(err.message).to.equal(testError.message);
expect(latestVersion.calledOnce).to.be.true;
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
expect(latestVersion.calledWithExactly('ghost', {agent: false})).to.be.true;
done();
});
});
Expand All @@ -57,7 +57,7 @@ describe('Unit: Utils > pre-checks', function () {
}, {log}).then(([task]) => task.task()).then(() => {
expect(log.called).to.be.false;
expect(latestVersion.calledOnce).to.be.true;
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
expect(latestVersion.calledWithExactly('ghost', {agent: false})).to.be.true;
});
});

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

expect(latestVersion.calledOnce).to.be.true;
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
expect(latestVersion.calledWithExactly('ghost', {agent: false})).to.be.true;
});
});

Expand Down

0 comments on commit 4c5242c

Please sign in to comment.