Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit aa38ded

Browse files
chore(version-info): disable remote requests when offline
When the internet is not accessible build scripts are unnecessarily slow because of failed attempts to download git tag and CDN version information from remote servers. This commit does a synchronous check of internet connectivity before attempting any remote requests. Closes #14759
1 parent b03957f commit aa38ded

File tree

5 files changed

+2661
-2529
lines changed

5 files changed

+2661
-2529
lines changed

lib/versions/is-online.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
3+
var isOnlineAsync = require('is-online');
4+
5+
isOnlineAsync(function(err, isOnline) {
6+
console.log(isOnline ? 'online' : 'offline');
7+
});

lib/versions/version-info.js

+59-47
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
'use strict';
22

3+
var childProcess = require('child_process');
34
var fs = require('fs');
45
var path = require('path');
56
var shell = require('shelljs');
67
var semver = require('semver');
78
var _ = require('lodash');
89

910
var currentPackage, previousVersions, cdnVersion, gitRepoInfo;
10-
11+
var connectivityStatus = childProcess.execSync('node is-online.js', { cwd: __dirname, encoding: 'utf8' }).trim();
12+
var isOnline = connectivityStatus == 'online';
13+
console.log('Running ' + connectivityStatus);
1114

1215
/**
1316
* Load information about this project from the package.json
@@ -100,58 +103,68 @@ var getPreviousVersions = function() {
100103
// not contain all commits when cloned with git clone --depth=...
101104
// Needed e.g. for Travis
102105
var repo_url = currentPackage.repository.url;
103-
var tagResults = shell.exec('git ls-remote --tags ' + repo_url,
104-
{silent: true});
105-
if (tagResults.code === 0) {
106-
return _(tagResults.output.match(/v[0-9].*[0-9]$/mg))
107-
.map(function(tag) {
108-
var version = semver.parse(tag);
109-
return version;
110-
})
111-
.filter()
112-
.map(function(version) {
113-
// angular.js didn't follow semantic version until 1.20rc1
114-
if ((version.major === 1 && version.minor === 0 && version.prerelease.length > 0) || (version.major === 1 && version.minor === 2 && version.prerelease[0] === 'rc1')) {
115-
version.version = [version.major, version.minor, version.patch].join('.') + version.prerelease.join('');
116-
version.raw = 'v' + version.version;
117-
}
118-
version.docsUrl = 'http://code.angularjs.org/' + version.version + '/docs';
119-
// Versions before 1.0.2 had a different docs folder name
120-
if (version.major < 1 || (version.major === 1 && version.minor === 0 && version.patch < 2)) {
121-
version.docsUrl += '-' + version.version;
122-
version.isOldDocsUrl = true;
123-
}
124-
return version;
125-
})
126-
.sort(semver.compare)
127-
.value();
106+
if (isOnline) {
107+
console.log('looking up remote git tags...');
108+
var tagResults = shell.exec('git ls-remote --tags ' + repo_url,
109+
{silent: true});
110+
if (tagResults.code === 0) {
111+
return _(tagResults.output.match(/v[0-9].*[0-9]$/mg))
112+
.map(function(tag) {
113+
var version = semver.parse(tag);
114+
return version;
115+
})
116+
.filter()
117+
.map(function(version) {
118+
// angular.js didn't follow semantic version until 1.20rc1
119+
if ((version.major === 1 && version.minor === 0 && version.prerelease.length > 0) || (version.major === 1 && version.minor === 2 && version.prerelease[0] === 'rc1')) {
120+
version.version = [version.major, version.minor, version.patch].join('.') + version.prerelease.join('');
121+
version.raw = 'v' + version.version;
122+
}
123+
version.docsUrl = 'http://code.angularjs.org/' + version.version + '/docs';
124+
// Versions before 1.0.2 had a different docs folder name
125+
if (version.major < 1 || (version.major === 1 && version.minor === 0 && version.patch < 2)) {
126+
version.docsUrl += '-' + version.version;
127+
version.isOldDocsUrl = true;
128+
}
129+
return version;
130+
})
131+
.sort(semver.compare)
132+
.value();
133+
}
128134
} else {
129-
return [];
135+
console.log('-- skipping remote git tag lookup');
130136
}
137+
return [];
131138
};
132139

133140
var getCdnVersion = function() {
134-
return _(previousVersions)
135-
.filter(function(tag) {
136-
return semver.satisfies(tag, currentPackage.branchVersion);
137-
})
138-
.reverse()
139-
.reduce(function(cdnVersion, version) {
140-
if (!cdnVersion) {
141-
// Note: need to use shell.exec and curl here
142-
// as version-infos returns its result synchronously...
143-
var cdnResult = shell.exec('curl http://ajax.googleapis.com/ajax/libs/angularjs/' + version + '/angular.min.js ' +
144-
'--head --write-out "%{http_code}" -o /dev/null -silent',
145-
{silent: true});
146-
if (cdnResult.code === 0) {
147-
var statusCode = cdnResult.output.trim();
148-
if (statusCode === '200') {
149-
cdnVersion = version;
141+
if (isOnline) {
142+
console.log('searching for CDN versions...');
143+
return _(previousVersions)
144+
.filter(function(tag) {
145+
return semver.satisfies(tag, currentPackage.branchVersion);
146+
})
147+
.reverse()
148+
.reduce(function(cdnVersion, version) {
149+
if (!cdnVersion) {
150+
// Note: need to use shell.exec and curl here
151+
// as version-infos returns its result synchronously...
152+
var cdnResult = shell.exec('curl http://ajax.googleapis.com/ajax/libs/angularjs/' + version + '/angular.min.js ' +
153+
'--head --write-out "%{http_code}" -o /dev/null -silent',
154+
{silent: true});
155+
if (cdnResult.code === 0) {
156+
var statusCode = cdnResult.output.trim();
157+
if (statusCode === '200') {
158+
console.log('-- found CDN version ' + version);
159+
cdnVersion = version;
160+
}
150161
}
151162
}
152-
}
153-
return cdnVersion;
154-
}, null);
163+
return cdnVersion;
164+
}, null);
165+
} else {
166+
console.log('-- skipping CDN version lookup');
167+
}
155168
};
156169

157170
/**
@@ -198,7 +211,6 @@ var getSnapshotVersion = function() {
198211
return version;
199212
};
200213

201-
202214
exports.currentPackage = currentPackage = getPackage();
203215
exports.gitRepoInfo = gitRepoInfo = getGitRepoInfo();
204216
exports.previousVersions = previousVersions = getPreviousVersions();

0 commit comments

Comments
 (0)