embark/lib/cmds/template_generator.js

93 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let fs = require('../core/fs.js');
let utils = require('../utils/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;
}
2018-07-06 08:38:09 +00:00
downloadAndGenerate(uri, destinationFolder, name) {
2018-07-06 08:52:47 +00:00
const self = this;
2018-07-06 08:38:09 +00:00
let {url, filePath} = this.getExternalProject(uri);
let tmpFilePath = fs.tmpDir(filePath);
2018-07-06 08:52:47 +00:00
console.log(__('Installing Template from ' + uri + '....').green);
2018-07-06 08:38:09 +00:00
fs.mkdirpSync(utils.dirname(tmpFilePath));
utils.downloadFile(url, tmpFilePath, () => {
let fspath = utils.joinPath(destinationFolder, name);
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-06 08:52:47 +00:00
}).then(() => {
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-05-08 21:49:46 +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));
let fspath = utils.joinPath(destinationFolder, name);
2017-03-30 11:26:03 +00:00
fs.copySync(templatePath, fspath);
2018-01-03 17:42:38 +00:00
2018-07-06 08:52:47 +00:00
this.installTemplate(fspath, name, (name === 'embark_demo'));
2017-01-14 00:17:29 +00:00
2017-03-30 11:12:39 +00:00
if (name === 'embark_demo') {
console.log('-------------------'.yellow);
2018-05-08 21:49:46 +00:00
console.log(__('Next steps:').green);
2017-03-30 11:26:03 +00:00
console.log(('-> ' + ('cd ' + fspath).bold.cyan).green);
2017-03-30 11:12:39 +00:00
console.log('-> '.green + 'embark run'.bold.cyan);
2018-05-08 21:49:46 +00:00
console.log(__('For more info go to http://embark.status.im').green);
2017-03-30 11:12:39 +00:00
}
2017-01-14 00:17:29 +00:00
}
2018-07-06 08:38:09 +00:00
2018-07-06 08:52:47 +00:00
installTemplate(templatePath, name, installPackages) {
utils.cd(templatePath);
utils.sed('package.json', '%APP_NAME%', name);
if (installPackages) {
console.log(__('Installing packages...').green);
utils.runCmd('npm install');
}
console.log(__('Init complete').green);
console.log('\n' + __('App ready at ').green + templatePath);
}
2018-07-06 08:38:09 +00:00
getExternalProject(uri) {
let match = uri.match(
/\.[a-z]+\/([-a-zA-Z0-9@:%_+.~#?&\/=]+)/
);
let url, folder;
if (uri.startsWith('http')) {
2018-07-06 08:41:37 +00:00
url = uri + "/archive/master.zip";
folder = match[1];
2018-07-06 08:38:09 +00:00
} else if (uri.startsWith('github')) {
2018-07-06 08:41:37 +00:00
url = "https://" + uri + "/archive/master.zip";
folder = match[1];
2018-07-06 08:38:09 +00:00
} else if (uri.split('/').length === 2) {
2018-07-06 08:41:37 +00:00
url = "https://github.com/" + uri + "/archive/master.zip";
folder = uri;
2018-07-06 08:38:09 +00:00
} else if (uri.indexOf('/') === -1) {
2018-07-06 08:41:37 +00:00
url = "https://github.com/embark-framework/embark-" + uri + "-template/archive/master.zip";
folder = "embark-framework/embark-" + uri + "-template";
2018-07-06 08:38:09 +00:00
}
return {
url,
filePath: utils.joinPath(".embark/templates/", folder, "archive.zip")
};
}
2017-03-30 11:12:39 +00:00
}
module.exports = TemplateGenerator;