auto-retry download with master branch

a fallback download of the master branch is attempted only for unqualified
template names
This commit is contained in:
Michael Bradley, Jr 2018-10-11 12:09:21 -05:00 committed by Pascal Precht
parent b995aac813
commit 1f47b163e4
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
1 changed files with 60 additions and 22 deletions

View File

@ -15,7 +15,29 @@ class TemplateGenerator {
} }
} }
downloadAndGenerate(uri, destinationFolder, name) { download(url, tmpFilePath, browse) {
console.log(__('Installing template from ' + browse).green);
console.log(__('Downloading template...').green);
fs.mkdirpSync(utils.dirname(tmpFilePath));
return new Promise((resolve, reject) => {
utils.downloadFile(url, tmpFilePath, (err) => {
if (err) {
console.error(utils.errorMessage(err).red);
reject(err);
} else {
resolve();
}
});
});
}
downloadFailed() {
console.error('Does the template really exist?'.red);
console.error(`Embark's supported templates: https://embark.status.im/templates/`.green);
process.exit(1);
}
async downloadAndGenerate(uri, destinationFolder, name) {
const fspath = utils.joinPath(destinationFolder, name); const fspath = utils.joinPath(destinationFolder, name);
this.checkPathExists(fspath); this.checkPathExists(fspath);
const self = this; const self = this;
@ -28,27 +50,31 @@ class TemplateGenerator {
} }
let {url, filePath, browse} = ext; let {url, filePath, browse} = ext;
let tmpFilePath = fs.tmpDir(filePath); let tmpFilePath = fs.tmpDir(filePath);
console.log(__('Installing template from ' + browse).green); try {
console.log(__('Downloading template...').green); try {
await this.download(url, tmpFilePath, browse);
fs.mkdirpSync(utils.dirname(tmpFilePath)); } catch (err) {
utils.downloadFile(url, tmpFilePath, (err) => { let {url_fallback, filePath_fallback, browse_fallback} = ext;
if (err) { if (url_fallback) {
console.error(utils.errorMessage(err).red); console.log(__('Retrying with the master branch...').green);
console.error('Does the template really exist?'.red); tmpFilePath = fs.tmpDir(filePath_fallback);
console.error(`Embark's supported templates: https://embark.status.im/templates/`.green); await this.download(url_fallback, tmpFilePath, browse_fallback);
process.exit(1); } else {
} throw new Error();
utils.extractZip(tmpFilePath, fspath, {
map: file => {
let fixed_path = file.path.split('/');
fixed_path.shift(); // remove first directory
file.path = utils.joinPath(...fixed_path);
return file;
} }
}, () => { }
self.installTemplate(fspath, name, true); } catch (e) {
}); this.downloadFailed();
}
utils.extractZip(tmpFilePath, fspath, {
map: file => {
let fixed_path = file.path.split('/');
fixed_path.shift(); // remove first directory
file.path = utils.joinPath(...fixed_path);
return file;
}
}, () => {
self.installTemplate(fspath, name, true);
}); });
} }
@ -102,24 +128,36 @@ class TemplateGenerator {
getExternalProject(uri) { getExternalProject(uri) {
let url, folder, hgi; let url, folder, hgi;
let fallback, url_fallback, folder_fallback, hgi_fallback;
hgi = hostedGitInfo.fromUrl(uri); hgi = hostedGitInfo.fromUrl(uri);
if (!hgi || hgi.user.includes('#')) { if (!hgi || hgi.user.includes('#')) {
let templateAndBranch = uri.split('#'); let templateAndBranch = uri.split('#');
if (templateAndBranch.length === 1) { if (templateAndBranch.length === 1) {
fallback = true;
let embarkVersion = semver(require('../../package.json').version); let embarkVersion = semver(require('../../package.json').version);
templateAndBranch.push(`${embarkVersion.major}.${embarkVersion.minor}`); templateAndBranch.push(`${embarkVersion.major}.${embarkVersion.minor}`);
} }
templateAndBranch[0] = `embark-framework/embark-${templateAndBranch[0]}-template`; templateAndBranch[0] = `embark-framework/embark-${templateAndBranch[0]}-template`;
hgi = hostedGitInfo.fromUrl(templateAndBranch.join('#')); hgi = hostedGitInfo.fromUrl(templateAndBranch.join('#'));
if (fallback) {
hgi_fallback = hostedGitInfo.fromUrl(templateAndBranch[0]);
}
} }
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'}`; 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 { return {
url, url,
filePath: utils.joinPath(".embark/templates/", folder, "archive.zip"), filePath: utils.joinPath(".embark/templates/", folder, "archive.zip"),
browse: decodeURIComponent(hgi.browse()) browse: decodeURIComponent(hgi.browse()),
url_fallback,
filePath_fallback: fallback && utils.joinPath(".embark/templates/", folder_fallback, "archive.zip"),
browse_fallback: fallback && decodeURIComponent(hgi_fallback.browse())
}; };
} }
} }