2017-03-29 17:50:05 +00:00
|
|
|
let fs = require('../core/fs.js');
|
2018-09-24 22:35:56 +00:00
|
|
|
let hostedGitInfo = require('hosted-git-info');
|
2018-08-07 23:02:36 +00:00
|
|
|
let utils = require('./utils.js');
|
2018-10-10 16:18:36 +00:00
|
|
|
let semver = require('semver');
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class TemplateGenerator {
|
2017-06-26 20:48:05 +00:00
|
|
|
constructor(templateName) {
|
2017-03-30 11:12:39 +00:00
|
|
|
this.templateName = templateName;
|
|
|
|
}
|
2016-08-21 14:42:42 +00:00
|
|
|
|
2018-08-23 20:42:47 +00:00
|
|
|
checkPathExists(fspath) {
|
|
|
|
if (fs.existsSync(fspath)) {
|
|
|
|
console.error(`${fspath} already exists, will not overwrite`.red);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 17:09:21 +00:00
|
|
|
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) {
|
2018-08-23 20:42:47 +00:00
|
|
|
const fspath = utils.joinPath(destinationFolder, name);
|
|
|
|
this.checkPathExists(fspath);
|
2018-07-06 08:52:47 +00:00
|
|
|
const self = this;
|
2018-09-25 14:20:59 +00:00
|
|
|
let ext;
|
|
|
|
try {
|
|
|
|
ext = this.getExternalProject(uri);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(utils.errorMessage(e).red);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
let {url, filePath, browse} = ext;
|
2018-07-06 08:38:09 +00:00
|
|
|
let tmpFilePath = fs.tmpDir(filePath);
|
2018-10-11 17:09:21 +00:00
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await this.download(url, tmpFilePath, browse);
|
|
|
|
} catch (err) {
|
|
|
|
let {url_fallback, filePath_fallback, browse_fallback} = ext;
|
|
|
|
if (url_fallback) {
|
2018-10-11 17:17:27 +00:00
|
|
|
console.log(__('Retrying with the master branch...').yellow);
|
2018-10-11 17:09:21 +00:00
|
|
|
tmpFilePath = fs.tmpDir(filePath_fallback);
|
|
|
|
await this.download(url_fallback, tmpFilePath, browse_fallback);
|
|
|
|
} else {
|
|
|
|
throw new Error();
|
2018-07-06 08:38:09 +00:00
|
|
|
}
|
2018-10-11 17:09:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2018-10-11 19:46:45 +00:00
|
|
|
return this.downloadFailed();
|
2018-10-11 17:09:21 +00:00
|
|
|
}
|
|
|
|
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);
|
2018-07-06 08:38:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
generate(destinationFolder, name) {
|
2018-08-23 20:42:47 +00:00
|
|
|
const fspath = utils.joinPath(destinationFolder, name);
|
|
|
|
this.checkPathExists(fspath);
|
2018-09-24 22:38:52 +00:00
|
|
|
console.log(__('Initializing Embark template...').green);
|
2018-07-06 08:52:47 +00:00
|
|
|
let templatePath = fs.embarkPath(utils.joinPath('templates', this.templateName));
|
2017-03-30 11:26:03 +00:00
|
|
|
fs.copySync(templatePath, fspath);
|
2018-07-06 08:52:47 +00:00
|
|
|
|
2018-09-24 22:39:09 +00:00
|
|
|
this.installTemplate(
|
|
|
|
fspath,
|
|
|
|
name,
|
2018-09-27 19:56:33 +00:00
|
|
|
(this.templateName === 'boilerplate' || this.templateName === 'demo'),
|
|
|
|
() => {
|
|
|
|
if (name === 'embark_demo') {
|
|
|
|
console.log('-------------------'.yellow);
|
|
|
|
console.log(__('Next steps:').green);
|
|
|
|
console.log(('-> ' + ('cd ' + fspath).bold.cyan).green);
|
|
|
|
console.log('-> '.green + 'embark run'.bold.cyan);
|
|
|
|
console.log(__('For more info go to http://embark.status.im').green);
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 22:39:09 +00:00
|
|
|
);
|
2018-07-06 08:52:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 19:56:33 +00:00
|
|
|
installTemplate(templatePath, name, installPackages, cb) {
|
2018-07-06 08:52:47 +00:00
|
|
|
utils.cd(templatePath);
|
2017-03-30 11:12:39 +00:00
|
|
|
utils.sed('package.json', '%APP_NAME%', name);
|
2018-08-12 19:09:04 +00:00
|
|
|
if (fs.existsSync('dot.gitignore')) {
|
|
|
|
fs.moveSync('dot.gitignore', '.gitignore');
|
|
|
|
}
|
2018-07-06 08:52:47 +00:00
|
|
|
|
2018-08-12 20:02:03 +00:00
|
|
|
if (fs.existsSync('dot.gitignore')) {
|
|
|
|
fs.moveSync('dot.gitignore', '.gitignore');
|
|
|
|
}
|
2017-01-14 00:17:29 +00:00
|
|
|
|
2018-07-06 08:52:47 +00:00
|
|
|
if (installPackages) {
|
2018-05-08 21:49:46 +00:00
|
|
|
console.log(__('Installing packages...').green);
|
2018-09-24 22:40:15 +00:00
|
|
|
utils.runCmd('npm install', null, (err) => {
|
|
|
|
if (err) {
|
2018-09-27 19:55:44 +00:00
|
|
|
console.error(utils.errorMessage(err).red);
|
2018-09-27 19:56:02 +00:00
|
|
|
process.exit(1);
|
2018-09-24 22:40:15 +00:00
|
|
|
}
|
|
|
|
console.log(__('Init complete').green);
|
|
|
|
console.log('\n' + __('App ready at ').green + templatePath);
|
2018-09-27 19:56:33 +00:00
|
|
|
if (cb) cb();
|
2018-09-24 22:40:15 +00:00
|
|
|
});
|
2018-09-05 21:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-06 08:38:09 +00:00
|
|
|
|
2018-09-05 21:59:42 +00:00
|
|
|
getExternalProject(uri) {
|
2018-09-24 22:35:56 +00:00
|
|
|
let url, folder, hgi;
|
2018-10-11 17:09:21 +00:00
|
|
|
let fallback, url_fallback, folder_fallback, hgi_fallback;
|
2018-09-25 14:20:59 +00:00
|
|
|
hgi = hostedGitInfo.fromUrl(uri);
|
|
|
|
if (!hgi || hgi.user.includes('#')) {
|
|
|
|
let templateAndBranch = uri.split('#');
|
2018-10-10 16:18:36 +00:00
|
|
|
if (templateAndBranch.length === 1) {
|
2018-10-11 17:09:21 +00:00
|
|
|
fallback = true;
|
2018-10-10 16:18:36 +00:00
|
|
|
let embarkVersion = semver(require('../../package.json').version);
|
|
|
|
templateAndBranch.push(`${embarkVersion.major}.${embarkVersion.minor}`);
|
|
|
|
}
|
2018-09-25 14:20:59 +00:00
|
|
|
templateAndBranch[0] = `embark-framework/embark-${templateAndBranch[0]}-template`;
|
|
|
|
hgi = hostedGitInfo.fromUrl(templateAndBranch.join('#'));
|
2018-10-11 17:09:21 +00:00
|
|
|
if (fallback) {
|
|
|
|
hgi_fallback = hostedGitInfo.fromUrl(templateAndBranch[0]);
|
|
|
|
}
|
2018-07-06 08:38:09 +00:00
|
|
|
}
|
2018-09-25 14:20:59 +00:00
|
|
|
if(!hgi) { throw new Error('Unsupported template name or git host URL'); }
|
|
|
|
url = hgi.tarball();
|
|
|
|
folder = `${hgi.user}/${hgi.project}/${hgi.committish || 'master'}`;
|
2018-10-11 17:09:21 +00:00
|
|
|
if (fallback) {
|
|
|
|
url_fallback = hgi_fallback.tarball();
|
|
|
|
folder_fallback = `${hgi_fallback.user}/${hgi_fallback.project}/master`;
|
|
|
|
}
|
2018-07-06 08:38:09 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
url,
|
2018-09-24 22:37:39 +00:00
|
|
|
filePath: utils.joinPath(".embark/templates/", folder, "archive.zip"),
|
2018-10-11 17:09:21 +00:00
|
|
|
browse: decodeURIComponent(hgi.browse()),
|
|
|
|
url_fallback,
|
|
|
|
filePath_fallback: fallback && utils.joinPath(".embark/templates/", folder_fallback, "archive.zip"),
|
|
|
|
browse_fallback: fallback && decodeURIComponent(hgi_fallback.browse())
|
2018-07-06 08:38:09 +00:00
|
|
|
};
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-21 14:42:42 +00:00
|
|
|
module.exports = TemplateGenerator;
|