diff --git a/grunt/sync.js b/grunt/sync.js index 9308e5892c9eae776365ec8d0903635cfd7813b6..3117f04ffdd2d26e15637e7687415925091741a2 100644 --- a/grunt/sync.js +++ b/grunt/sync.js @@ -11,7 +11,12 @@ module.exports = function (grunt) { var simpleJsonRequest = require('simple-json-request'); var semver = require('semver'); var getJson = function (uri) { - return simpleJsonRequest.get({url: uri}); + grunt.log.debug('Requesting JSON from: ' + uri); + return simpleJsonRequest.get({url: uri}).catch(function (e) { + grunt.log.fail('Unable to request JSON from:' + uri); + grunt.log.fail('(' + e.statusCode + ') ' + e); + return []; + }); }; // Internal variables. @@ -33,10 +38,32 @@ module.exports = function (grunt) { grunt.verbose.writeln((expired ? 'EXPIRED' : 'VALID')[expired ? 'red' : 'green']); } + // While the Bootswatch project attempts to maintain version parity with + // Bootstrap, it doesn't always happen. This causes issues when the system + // expects a 1:1 version match between Bootstrap and Bootswatch. + // @see https://github.com/thomaspark/bootswatch/issues/892#ref-issue-410070082 + var mapVersion = function ($version, $package) { + if ($package === 'bootswatch') { + switch ($version) { + // This version is "broken" because of jsDelivr's API limit. + case '3.4.1': + $version = '3.4.0'; + break; + + // This version doesn't exist. + case '3.1.1': + $version = '3.2.0'; + break; + } + } + return $version; + }; + var getApiV1Json = function ($package) { var $json = {name: $package, assets: []}; var $latest = '0.0.0'; var $versions = []; + grunt.log.writeln('Retrieving data for: ' + $package); return getJson('https://data.jsdelivr.com/v1/package/npm/' + $package) .catch(function (error) { if (!(error instanceof Error)) { @@ -56,7 +83,7 @@ module.exports = function (grunt) { if (!$version.match(/^3\.\d+\.\d+$/)) { return Promise.resolve(); } - return getJson('https://data.jsdelivr.com/v1/package/npm/' + $package + '@' + $version + '/flat') + return getJson('https://data.jsdelivr.com/v1/package/npm/' + $package + '@' + mapVersion($version, $package) + '/flat') .then(function ($versionJson) { // Skip empty files. if (!$versionJson.files || !$versionJson.files.length) { @@ -176,7 +203,7 @@ module.exports = function (grunt) { for (var version in libraries[library]) { if (!libraries[library].hasOwnProperty(version)) continue; - var endpoint = library + '#' + version; + var endpoint = library + '#' + mapVersion(version, library); // Check if library is already installed. If so, skip. var versionPath = path.join(librariesPath, version); diff --git a/src/Plugin/Provider/JsDelivr.php b/src/Plugin/Provider/JsDelivr.php index c538c6f8fbd3fb80e211d828ecbfa8403b366c6e..d85003b7d1c06762c14250c1cda56a1d99e566d5 100644 --- a/src/Plugin/Provider/JsDelivr.php +++ b/src/Plugin/Provider/JsDelivr.php @@ -82,8 +82,9 @@ class JsDelivr extends ProviderBase { if (!isset($this->themes[$version])) { $this->themes[$version] = $this->cacheGet('themes.' . Unicode::escapeDelimiter($version), [], function ($themes) use ($version) { foreach (['bootstrap', 'bootswatch'] as $package) { - $files = $this->requestApiV1($package, $version); - $themes = $this->parseThemes($files, $package, $version, $themes); + $mappedVersion = $this->mapVersion($version, $package); + $files = $this->requestApiV1($package, $mappedVersion); + $themes = $this->parseThemes($files, $package, $mappedVersion, $themes); } return $themes; }); @@ -111,6 +112,30 @@ class JsDelivr extends ProviderBase { return $this->versions[$package]; } + /** + * {@inheritdoc} + */ + protected function mapVersion($version, $package = NULL) { + // While the Bootswatch project attempts to maintain version parity with + // Bootstrap, it doesn't always happen. This causes issues when the system + // expects a 1:1 version match between Bootstrap and Bootswatch. + // @see https://github.com/thomaspark/bootswatch/issues/892#ref-issue-410070082 + if ($package === 'bootswatch') { + switch ($version) { + // This version is "broken" because of jsDelivr's API limit. + case '3.4.1': + $version = '3.4.0'; + break; + + // This version doesn't exist. + case '3.1.1': + $version = '3.2.0'; + break; + } + } + return $version; + } + /** * Parses JSON from the API and retrieves valid files. * diff --git a/src/Plugin/Provider/ProviderBase.php b/src/Plugin/Provider/ProviderBase.php index a1ca93a0fab4054381505af54142f50cf458e613..b02614a92cf34bec149816e8fe3d9b0c003bd7f3 100644 --- a/src/Plugin/Provider/ProviderBase.php +++ b/src/Plugin/Provider/ProviderBase.php @@ -317,6 +317,19 @@ class ProviderBase extends PluginBase implements ProviderInterface { return $this->pluginDefinition['versions']; } + /** + * Allows providers a way to map a version to a different version. + * + * @param string $version + * The version to map. + * + * @return string + * The mapped version. + */ + protected function mapVersion($version) { + return $version; + } + /** * {@inheritdoc} */