Skip to content

Commit

Permalink
Merge pull request #131 from adaptlearning/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chris-steele authored Sep 25, 2020
2 parents cf9f891 + f373d87 commit 97abffe
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 13 deletions.
6 changes: 4 additions & 2 deletions lib/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ var bower = require('bower'),
module.exports = {
ManifestFilename: 'adapt.json',
FrameworkFilename: 'package.json',
DefaultProjectManifestPath : './'+this.ManifestFilename,
DefaultProjectFrameworkPath: './'+this.FrameworkFilename,
DefaultProjectManifestPath: './adapt.json',
DefaultProjectFrameworkPath: './package.json',
DefaultCreateType : 'course',
DefaultTypeNames : {
'course':'my-adapt-course',
'component':'my-adapt-component'
},
DefaultUserAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36',
DefaultGitHubOrg: 'adaptlearning',
FrameworkRepository : process.env.ADAPT_FRAMEWORK || 'https://github.com/adaptlearning/adapt_framework',
FrameworkRepositoryName : 'adapt_framework',
ComponentRepository : process.env.ADAPT_COMPONENT || 'https://github.com/adaptlearning/adapt-component',
Expand Down
15 changes: 9 additions & 6 deletions lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var bower = require('bower'),
Constants = require('../Constants'),
Plugin = require('../Plugin'),
create = require('./create/index'),
highest = require('../promise/highest'),
_ = require('lodash');

module.exports = {
Expand All @@ -14,12 +15,14 @@ module.exports = {
localDir = arguments.length >= 4 ? arguments[2] : undefined,
branch = arguments.length >= 5 ? arguments[3] : Constants.DefaultBranch,
done = arguments[arguments.length-1];

confirm({
type: type,
localDir: localDir,
branch: branch,
renderer: renderer
highest()
.then(function (tag) {
return confirm({
type: type,
localDir: localDir,
branch: tag,
renderer: renderer
});
})
.then(function (properties) {
var action = create[properties.type];
Expand Down
109 changes: 109 additions & 0 deletions lib/promise/highest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
var async = require('async');
var Q = require('q');
var request = require('request');
var semver = require('semver');
var Constants = require('../Constants');
var _ = require('lodash');

module.exports = function () {
var deferred = Q.defer();

checkLatestAdaptRepoVersion(function (error, latestFrameworkTag) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve(latestFrameworkTag);
}
});

return deferred.promise;
}

function checkLatestAdaptRepoVersion(versionLimit, callback) {
if (typeof versionLimit === 'function') {
callback = versionLimit;
versionLimit = undefined;
}
// used in pagination
var nextPage = `https://api.github.com/repos/${Constants.DefaultGitHubOrg}/${Constants.FrameworkRepositoryName}/releases`;

var _getReleases = function (done) {
request({
headers: {
'User-Agent': Constants.DefaultUserAgent
},
uri: nextPage,
method: 'GET'
}, done);
};
var _requestHandler = function (error, response, body) {
if (response) {
// we've exceeded the API limit
if (response.statusCode === 403 && response.headers['x-ratelimit-remaining'] === '0') {
var reqsReset = new Date(response.headers['x-ratelimit-reset'] * 1000);
error = `You have exceeded GitHub's request limit of ${response.headers['x-ratelimit-limit']} requests per hour. Please wait until at least ${reqsReset.toTimeString()} before trying again.`;
}
else if (response.statusCode !== 200) {
error = 'GitubAPI did not respond with a 200 status code.';
}
}
if (error) {
return callback(new Error(`Couldn't check latest version of ${Constants.FrameworkRepositoryName}. ${error}`));
}
nextPage = parseLinkHeader(response.headers.link).next;
try {
// parse and sort releases (newest first)
var releases = JSON.parse(body).sort((a, b) => {
if (semver.lt(a.tag_name, b.tag_name)) return 1;
if (semver.gt(a.tag_name, b.tag_name)) return -1;
return 0;
});
} catch (e) {
return callback(new Error(`Failed to parse GitHub release data\n${e}`));
}
var compatibleRelease;
async.someSeries(releases, function (release, cb) {
var isFullRelease = !release.draft && !release.prerelease;
var satisfiesVersion = !versionLimit || semver.satisfies(release.tag_name, versionLimit);

if (!isFullRelease || !satisfiesVersion) {
return cb(null, false);
}

compatibleRelease = release;
return cb(null, true);
}, function (error, satisfied) {
if (!satisfied) {
if (nextPage) {
return _getReleases(_requestHandler);
}
error = new Error(`Couldn't find any releases compatible with specified framework version (${versionLimit}), please check that it is a valid version.`);
}
if (error) {
return callback(error);
}
callback(error, compatibleRelease.tag_name);
});
};
// start recursion
_getReleases(_requestHandler);
}

// taken from https://gist.github.com/niallo/3109252
function parseLinkHeader(header) {
if (!header || header.length === 0) {
return [];
}
var links = {};
// Parse each part into a named link
_.each(header.split(','), function (p) {
var section = p.split(';');
if (section.length !== 2) {
throw new Error("section could not be split on ';'");
}
var url = section[0].replace(/<(.*)>/, '$1').trim();
var name = section[1].replace(/rel="(.*)"/, '$1').trim();
links[name] = url;
});
return links;
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "adapt-cli",
"version": "2.1.10",
"version": "2.1.11",
"description": "Command line tools for Adapt",
"main": "./lib/cli.js",
"directories": {
"test": "test"
},
"dependencies": {
"async": "^3.1.0",
"bower": "^1.8.4",
"bower-endpoint-parser": "^0.2.2",
"chalk": "^2.4.1",
Expand Down

0 comments on commit 97abffe

Please sign in to comment.