embark-area-51/lib/utils/template_generator.js

116 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let fs = require('../core/fs.js');
let hostedGitInfo = require('hosted-git-info');
2018-08-07 23:02:36 +00:00
let utils = require('./utils.js');
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;
}
checkPathExists(fspath) {
if (fs.existsSync(fspath)) {
console.error(`${fspath} already exists, will not overwrite`.red);
process.exit(1);
}
}
2018-07-06 08:38:09 +00:00
downloadAndGenerate(uri, destinationFolder, name) {
const fspath = utils.joinPath(destinationFolder, name);
this.checkPathExists(fspath);
2018-07-06 08:52:47 +00:00
const self = this;
let {url, filePath, browse} = this.getExternalProject(uri);
2018-07-06 08:38:09 +00:00
let tmpFilePath = fs.tmpDir(filePath);
console.log(__('Installing template from ' + browse).green);
2018-09-24 22:38:52 +00:00
console.log(__('Downloading template...').green);
2018-07-06 08:38:09 +00:00
fs.mkdirpSync(utils.dirname(tmpFilePath));
utils.downloadFile(url, tmpFilePath, (err) => {
if (err) {
console.error(err.red);
console.error('Does the template really exist?'.red);
console.error(`Embark's supported templates: https://embark.status.im/templates/`.green);
process.exit(1);
}
utils.extractZip(tmpFilePath, fspath, {
2018-07-06 08:38:09 +00:00
map: file => {
let fixed_path = file.path.split('/');
fixed_path.shift(); // remove first directory
file.path = utils.joinPath(...fixed_path);
return file;
}
2018-07-09 13:30:27 +00:00
}, () => {
2018-07-06 08:52:47 +00:00
self.installTemplate(fspath, name, true);
2018-07-06 08:38:09 +00:00
});
});
}
2017-03-30 11:12:39 +00:00
generate(destinationFolder, name) {
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
this.installTemplate(
fspath,
name,
(this.templateName === 'boilerplate' || this.templateName === 'demo')
);
2018-07-06 08:52:47 +00:00
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);
}
}
installTemplate(templatePath, name, installPackages) {
utils.cd(templatePath);
2017-03-30 11:12:39 +00:00
utils.sed('package.json', '%APP_NAME%', name);
if (fs.existsSync('dot.gitignore')) {
fs.moveSync('dot.gitignore', '.gitignore');
}
2018-07-06 08:52:47 +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-01-03 17:42:38 +00:00
utils.runCmd('npm install');
}
2018-05-08 21:49:46 +00:00
console.log(__('Init complete').green);
2018-07-06 08:52:47 +00:00
console.log('\n' + __('App ready at ').green + templatePath);
2017-01-14 00:17:29 +00:00
}
2018-07-06 08:38:09 +00:00
}
}
2018-07-06 08:38:09 +00:00
getExternalProject(uri) {
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);
2018-07-06 08:38:09 +00:00
}
return {
url,
filePath: utils.joinPath(".embark/templates/", folder, "archive.zip"),
browse: decodeURIComponent(hgi.browse())
2018-07-06 08:38:09 +00:00
};
}
2017-03-30 11:12:39 +00:00
}
module.exports = TemplateGenerator;