From 2564da313981097956c958f8bec5ad7be5e21f13 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 31 Oct 2018 21:48:06 +0100 Subject: [PATCH] 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 --- lib/utils/template_generator.js | 30 ++++++++++++++++----- test/template.js | 47 ++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/lib/utils/template_generator.js b/lib/utils/template_generator.js index 7cef7ca43..2e238dd46 100644 --- a/lib/utils/template_generator.js +++ b/lib/utils/template_generator.js @@ -43,7 +43,7 @@ class TemplateGenerator { const self = this; let ext; try { - ext = this.getExternalProject(uri); + ext = await this.getExternalProject(uri); } catch (e) { console.error(utils.errorMessage(e).red); process.exit(1); @@ -56,7 +56,7 @@ class TemplateGenerator { } catch (err) { let {url_fallback, filePath_fallback, browse_fallback, embarkVersion} = ext; 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); tmpFilePath = fs.tmpDir(filePath_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'); } url = hgi.tarball(); - folder = `${hgi.user}/${hgi.project}/${hgi.committish || 'master'}`; if (fallback) { url_fallback = hgi_fallback.tarball(); folder_fallback = `${hgi_fallback.user}/${hgi_fallback.project}/master`; } - - return { + const returnObject = { url, - filePath: utils.joinPath(".embark/templates/", folder, "archive.zip"), browse: decodeURIComponent(hgi.browse()), url_fallback, filePath_fallback: fallback && utils.joinPath(".embark/templates/", folder_fallback, "archive.zip"), browse_fallback: fallback && decodeURIComponent(hgi_fallback.browse()), 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; diff --git a/test/template.js b/test/template.js index 9f0cdaac3..85a513678 100644 --- a/test/template.js +++ b/test/template.js @@ -1,7 +1,9 @@ -/*globals describe, it*/ +/*globals describe, it, before*/ const assert = require('assert'); const TemplateGenerator = require('../lib/utils/template_generator'); const semver = require('semver'); +const sinon = require('sinon'); +const request = require('request'); describe('TemplateGenerator', function () { describe('getExternalProject', function () { @@ -9,6 +11,9 @@ describe('TemplateGenerator', function () { before(() => { templateGenerator = new TemplateGenerator(); + sinon.stub(request, 'get').callsFake((options, callback) => { + callback(null, {}, {default_branch: 'master'}); + }); }); describe('with named template', function () { @@ -31,40 +36,40 @@ describe('TemplateGenerator', function () { describe('with git host URL', function () { - it('returns correct info for GitHub URL', function () { - let result = templateGenerator.getExternalProject("http://github.com/embark-framework/embark"); + it('returns correct info for GitHub URL', async function () { + 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"); - 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.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); 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.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"); }); - it('returns correct info for Bitbucket URL', function () { - let result = templateGenerator.getExternalProject("https://bitbucket.org/embark-framework/embark"); + it('returns correct info for Bitbucket URL', async function () { + 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.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); 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.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"); }); - it('returns correct info for GitLab URL', function () { - let result = templateGenerator.getExternalProject("https://gitlab.com/embark-framework/embark"); + it('returns correct info for GitLab URL', async function () { + 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.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); 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.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"); @@ -74,40 +79,40 @@ describe('TemplateGenerator', function () { describe('with git host shortcut', function () { - it('returns correct info for GitHub shortcut', function () { - let result = templateGenerator.getExternalProject("github:embark-framework/embark"); + it('returns correct info for GitHub shortcut', async function () { + let result = await templateGenerator.getExternalProject("github:embark-framework/embark"); 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.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); 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.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"); }); - it('returns correct info for Bitbucket shortcut', function () { - let result = templateGenerator.getExternalProject("bitbucket:embark-framework/embark"); + it('returns correct info for Bitbucket shortcut', async function () { + 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.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); 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.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"); }); - it('returns correct info for GitLab shortcut', function () { - let result = templateGenerator.getExternalProject("gitlab:embark-framework/embark"); + it('returns correct info for GitLab shortcut', async function () { + 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.filePath.replace(/\\/g,'/'), ".embark/templates/embark-framework/embark/master/archive.zip"); 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.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");