From 1b7935678c1edce0fabff7d26c9a0e6ab487907a Mon Sep 17 00:00:00 2001 From: "Michael Bradley, Jr" Date: Mon, 24 Sep 2018 17:35:56 -0500 Subject: [PATCH] use hosted-git-info pkg to process --template name/url --- lib/utils/template_generator.js | 73 +++++++-------------------------- 1 file changed, 15 insertions(+), 58 deletions(-) diff --git a/lib/utils/template_generator.js b/lib/utils/template_generator.js index 59d042db..1501ec59 100644 --- a/lib/utils/template_generator.js +++ b/lib/utils/template_generator.js @@ -1,4 +1,5 @@ let fs = require('../core/fs.js'); +let hostedGitInfo = require('hosted-git-info'); let utils = require('./utils.js'); class TemplateGenerator { @@ -76,72 +77,28 @@ class TemplateGenerator { console.log(__('Installing packages...').green); utils.runCmd('npm install'); } - console.log(__('Init complete').green); console.log('\n' + __('App ready at ').green + templatePath); } - extractGithubUrlAndFolder(uri){ - - /* first matching group is the url, second the repoPart and third the branch with a hash in the beginning (if existing) - e.g. (["git@github.com/status-im/dappcon-workshop-dapp#master", "status-im/dappcon-workshop-dapp", "#master" ]) - should work with all formats of the following: - * git@github.com/status-im/dappcon-workshop-dapp#start-here - * git@github.com/status-im/dappcon-workshop-dapp - * http://www.github.com/status-im/dappcon-workshop-dapp - * https://www.github.com/status-im/dappcon-workshop-dapp - * github.com/status-im/dappcon-workshop-dapp#start-here - - sadly it doesn't extract from http(s)://github.com/status-im/dappcon-workshop-dapp/tree/start-here - thats why we have a special case later - */ - const match = uri.match(/github\.com+\/(.+?)(#.*)?$/); - const githubPart = "https://github.com/"; - let repoPart = match !== null? match[1] : null; - let branchName = match !== null? match[2] : null; - - if (branchName && branchName !== '#'){ - branchName = branchName.substring(1); - } else { - branchName = "master"; } - - let url, folder; - if (uri.includes("/tree")){ - //e.g http(s)://github.com/status-im/dappcon-workshop-dapp/tree/start-here - let repoPartAndBranch = repoPart.split("/tree/"); - repoPart = repoPartAndBranch[0]; - branchName = repoPartAndBranch[1]; - url = "https://github.com/" + repoPart + "/archive/"+ branchName +".zip"; - folder = repoPart + "/" + branchName; - } else if (repoPart !== undefined) { - url = githubPart + repoPart + "/archive/" + branchName + ".zip"; - folder = repoPart + "/" + branchName; - } - - return { - 'url': url, - 'folder': folder - }; } getExternalProject(uri) { - let url, folder; - const regex = /^((git@)?(www\.)?github\.com\/)|(https?:\/\/)/; - if (!uri.match(regex) && uri.split('/').length >= 2) { - //e.g embark-framework/embark, embark-framework/embark#branch, embark-framework/embark#features/branch - let repoPartAndBranch = uri.split('#'); - let repoPart = repoPartAndBranch[0]; - let branchName = (repoPartAndBranch.length === 2)? repoPartAndBranch[1] : "master"; - url = "https://github.com/" + repoPart + "/archive/"+ branchName + ".zip"; - folder = repoPart + "/" + branchName; - } else if (uri.indexOf('/') === -1) { - url = "https://github.com/embark-framework/embark-" + uri + "-template/archive/master.zip"; - folder = "embark-framework/embark-" + uri + "-template"; - } else { - let urlAndFolder = this.extractGithubUrlAndFolder(uri); - url = urlAndFolder.url; - folder = urlAndFolder.folder; + let url, folder, hgi; + try { + hgi = hostedGitInfo.fromUrl(uri); + if (!hgi) { + let x = uri.split('#'); + x[0] = `embark-framework/embark-${x[0]}-template`; + hgi = hostedGitInfo.fromUrl(x.join('#')); + } + if(!hgi) { throw new Error(); } + url = hgi.tarball(); + folder = `${hgi.user}/${hgi.project}/${hgi.committish || 'master'}`; + } catch (e) { + console.error('Unsupported template name or git host URL'.red); + process.exit(1); } return {