Enable downloading templates that don't have a master branch (#1019)

* fix no master branch repos for templates

* oopsie doopsie

* stub request and use async to get externalUrl
This commit is contained in:
Jonathan Rainville 2018-10-31 21:48:06 +01:00 committed by André Medeiros
parent 13f0b766a7
commit 2564da3139
2 changed files with 50 additions and 27 deletions

View File

@ -43,7 +43,7 @@ class TemplateGenerator {
const self = this; const self = this;
let ext; let ext;
try { try {
ext = this.getExternalProject(uri); ext = await this.getExternalProject(uri);
} catch (e) { } catch (e) {
console.error(utils.errorMessage(e).red); console.error(utils.errorMessage(e).red);
process.exit(1); process.exit(1);
@ -56,7 +56,7 @@ class TemplateGenerator {
} catch (err) { } catch (err) {
let {url_fallback, filePath_fallback, browse_fallback, embarkVersion} = ext; let {url_fallback, filePath_fallback, browse_fallback, embarkVersion} = ext;
if (url_fallback) { if (url_fallback) {
console.log(__('Retrying with the master branch...').yellow); console.log(__('Retrying with the default branch...').yellow);
console.log((__(`It may not be compatible with your Embark version`) + ` ${embarkVersion}`).yellow); console.log((__(`It may not be compatible with your Embark version`) + ` ${embarkVersion}`).yellow);
tmpFilePath = fs.tmpDir(filePath_fallback); tmpFilePath = fs.tmpDir(filePath_fallback);
await this.download(url_fallback, tmpFilePath, browse_fallback); await this.download(url_fallback, tmpFilePath, browse_fallback);
@ -144,21 +144,39 @@ class TemplateGenerator {
} }
if(!hgi) { throw new Error('Unsupported template name or git host URL'); } if(!hgi) { throw new Error('Unsupported template name or git host URL'); }
url = hgi.tarball(); url = hgi.tarball();
folder = `${hgi.user}/${hgi.project}/${hgi.committish || 'master'}`;
if (fallback) { if (fallback) {
url_fallback = hgi_fallback.tarball(); url_fallback = hgi_fallback.tarball();
folder_fallback = `${hgi_fallback.user}/${hgi_fallback.project}/master`; folder_fallback = `${hgi_fallback.user}/${hgi_fallback.project}/master`;
} }
const returnObject = {
return {
url, url,
filePath: utils.joinPath(".embark/templates/", folder, "archive.zip"),
browse: decodeURIComponent(hgi.browse()), browse: decodeURIComponent(hgi.browse()),
url_fallback, url_fallback,
filePath_fallback: fallback && utils.joinPath(".embark/templates/", folder_fallback, "archive.zip"), filePath_fallback: fallback && utils.joinPath(".embark/templates/", folder_fallback, "archive.zip"),
browse_fallback: fallback && decodeURIComponent(hgi_fallback.browse()), browse_fallback: fallback && decodeURIComponent(hgi_fallback.browse()),
embarkVersion embarkVersion
}; };
if (hgi.committish) {
folder = `${hgi.user}/${hgi.project}/${hgi.committish}`;
returnObject.filePath = utils.joinPath(".embark/templates/", folder, "archive.zip");
return returnObject;
}
return new Promise((resolve, reject) => {
const request = require('request');
request.get({
url: `https://api.github.com/repos/${hgi.user}/${hgi.project}`, json: true, headers: {
'User-Agent': 'embark'
}
}, (err, resp, body) => {
if (err) {
return reject(err);
}
folder = `${hgi.user}/${hgi.project}/${body.default_branch}`;
returnObject.url = returnObject.url.replace('/master', '/' + body.default_branch);
returnObject.filePath = utils.joinPath(".embark/templates/", folder, "archive.zip");
resolve(returnObject);
});
});
} }
} }
module.exports = TemplateGenerator; module.exports = TemplateGenerator;

View File

@ -1,7 +1,9 @@
/*globals describe, it*/ /*globals describe, it, before*/
const assert = require('assert'); const assert = require('assert');
const TemplateGenerator = require('../lib/utils/template_generator'); const TemplateGenerator = require('../lib/utils/template_generator');
const semver = require('semver'); const semver = require('semver');
const sinon = require('sinon');
const request = require('request');
describe('TemplateGenerator', function () { describe('TemplateGenerator', function () {
describe('getExternalProject', function () { describe('getExternalProject', function () {
@ -9,6 +11,9 @@ describe('TemplateGenerator', function () {
before(() => { before(() => {
templateGenerator = new TemplateGenerator(); templateGenerator = new TemplateGenerator();
sinon.stub(request, 'get').callsFake((options, callback) => {
callback(null, {}, {default_branch: 'master'});
});
}); });
describe('with named template', function () { describe('with named template', function () {
@ -31,40 +36,40 @@ describe('TemplateGenerator', function () {
describe('with git host URL', function () { describe('with git host URL', function () {
it('returns correct info for GitHub URL', function () { it('returns correct info for GitHub URL', async function () {
let result = templateGenerator.getExternalProject("http://github.com/embark-framework/embark"); let result = await templateGenerator.getExternalProject("https://github.com/embark-framework/embark");
assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master"); assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master");
result = templateGenerator.getExternalProject("https://github.com/embark-framework/embark"); result = await templateGenerator.getExternalProject("https://github.com/embark-framework/embark");
assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master"); assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip");
assert.strictEqual(result.browse, "https://github.com/embark-framework/embark"); assert.strictEqual(result.browse, "https://github.com/embark-framework/embark");
result = templateGenerator.getExternalProject("https://github.com/embark-framework/embark#features/branch"); result = await templateGenerator.getExternalProject("https://github.com/embark-framework/embark#features/branch");
assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/features%2Fbranch"); assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/features%2Fbranch");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip");
assert.strictEqual(result.browse, "https://github.com/embark-framework/embark/tree/features/branch"); assert.strictEqual(result.browse, "https://github.com/embark-framework/embark/tree/features/branch");
}); });
it('returns correct info for Bitbucket URL', function () { it('returns correct info for Bitbucket URL', async function () {
let result = templateGenerator.getExternalProject("https://bitbucket.org/embark-framework/embark"); let result = await templateGenerator.getExternalProject("https://bitbucket.org/embark-framework/embark");
assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/master.tar.gz"); assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/master.tar.gz");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip");
assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark"); assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark");
result = templateGenerator.getExternalProject("https://bitbucket.org/embark-framework/embark#features/branch"); result = await templateGenerator.getExternalProject("https://bitbucket.org/embark-framework/embark#features/branch");
assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/features%2Fbranch.tar.gz"); assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/features%2Fbranch.tar.gz");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip");
assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark/src/features/branch"); assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark/src/features/branch");
}); });
it('returns correct info for GitLab URL', function () { it('returns correct info for GitLab URL', async function () {
let result = templateGenerator.getExternalProject("https://gitlab.com/embark-framework/embark"); let result = await templateGenerator.getExternalProject("https://gitlab.com/embark-framework/embark");
assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=master"); assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=master");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip");
assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark"); assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark");
result = templateGenerator.getExternalProject("https://gitlab.com/embark-framework/embark#features/branch"); result = await templateGenerator.getExternalProject("https://gitlab.com/embark-framework/embark#features/branch");
assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=features%2Fbranch"); assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=features%2Fbranch");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip");
assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark/tree/features/branch"); assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark/tree/features/branch");
@ -74,40 +79,40 @@ describe('TemplateGenerator', function () {
describe('with git host shortcut', function () { describe('with git host shortcut', function () {
it('returns correct info for GitHub shortcut', function () { it('returns correct info for GitHub shortcut', async function () {
let result = templateGenerator.getExternalProject("github:embark-framework/embark"); let result = await templateGenerator.getExternalProject("github:embark-framework/embark");
assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master"); assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master");
result = templateGenerator.getExternalProject("embark-framework/embark"); result = await templateGenerator.getExternalProject("embark-framework/embark");
assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master"); assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/master");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip");
assert.strictEqual(result.browse, "https://github.com/embark-framework/embark"); assert.strictEqual(result.browse, "https://github.com/embark-framework/embark");
result = templateGenerator.getExternalProject("embark-framework/embark#features/branch"); result = await templateGenerator.getExternalProject("embark-framework/embark#features/branch");
assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/features%2Fbranch"); assert.strictEqual(result.url, "https://codeload.github.com/embark-framework/embark/tar.gz/features%2Fbranch");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip");
assert.strictEqual(result.browse, "https://github.com/embark-framework/embark/tree/features/branch"); assert.strictEqual(result.browse, "https://github.com/embark-framework/embark/tree/features/branch");
}); });
it('returns correct info for Bitbucket shortcut', function () { it('returns correct info for Bitbucket shortcut', async function () {
let result = templateGenerator.getExternalProject("bitbucket:embark-framework/embark"); let result = await templateGenerator.getExternalProject("bitbucket:embark-framework/embark");
assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/master.tar.gz"); assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/master.tar.gz");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip");
assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark"); assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark");
result = templateGenerator.getExternalProject("bitbucket:embark-framework/embark#features/branch"); result = await templateGenerator.getExternalProject("bitbucket:embark-framework/embark#features/branch");
assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/features%2Fbranch.tar.gz"); assert.strictEqual(result.url, "https://bitbucket.org/embark-framework/embark/get/features%2Fbranch.tar.gz");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip");
assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark/src/features/branch"); assert.strictEqual(result.browse, "https://bitbucket.org/embark-framework/embark/src/features/branch");
}); });
it('returns correct info for GitLab shortcut', function () { it('returns correct info for GitLab shortcut', async function () {
let result = templateGenerator.getExternalProject("gitlab:embark-framework/embark"); let result = await templateGenerator.getExternalProject("gitlab:embark-framework/embark");
assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=master"); assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=master");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip");
assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark"); assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark");
result = templateGenerator.getExternalProject("gitlab:embark-framework/embark#features/branch"); result = await templateGenerator.getExternalProject("gitlab:embark-framework/embark#features/branch");
assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=features%2Fbranch"); assert.strictEqual(result.url, "https://gitlab.com/embark-framework/embark/repository/archive.tar.gz?ref=features%2Fbranch");
assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip"); assert.strictEqual(result.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/features/branch/archive.zip");
assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark/tree/features/branch"); assert.strictEqual(result.browse, "https://gitlab.com/embark-framework/embark/tree/features/branch");