mirror of https://github.com/embarklabs/embark.git
Merge pull request #603 from embark-framework/template_downloader
Template downloader
This commit is contained in:
commit
82df17b376
|
@ -45,6 +45,7 @@ class Cmd {
|
||||||
.description(__('New Application'))
|
.description(__('New Application'))
|
||||||
.option('--simple', __('create a barebones project meant only for contract development'))
|
.option('--simple', __('create a barebones project meant only for contract development'))
|
||||||
.option('--locale [locale]', __('language to use (default: en)'))
|
.option('--locale [locale]', __('language to use (default: en)'))
|
||||||
|
.option('--template [url]', __('download template'))
|
||||||
.action(function (name, options) {
|
.action(function (name, options) {
|
||||||
i18n.setOrDetectLocale(options.locale);
|
i18n.setOrDetectLocale(options.locale);
|
||||||
if (name === undefined) {
|
if (name === undefined) {
|
||||||
|
@ -63,7 +64,7 @@ class Cmd {
|
||||||
if (options.simple) {
|
if (options.simple) {
|
||||||
embark.generateTemplate('simple', './', inputvalue);
|
embark.generateTemplate('simple', './', inputvalue);
|
||||||
} else {
|
} else {
|
||||||
embark.generateTemplate('boilerplate', './', inputvalue);
|
embark.generateTemplate('boilerplate', './', inputvalue, options.template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -71,7 +72,7 @@ class Cmd {
|
||||||
if (options.simple) {
|
if (options.simple) {
|
||||||
embark.generateTemplate('simple', './', name);
|
embark.generateTemplate('simple', './', name);
|
||||||
} else {
|
} else {
|
||||||
embark.generateTemplate('boilerplate', './', name);
|
embark.generateTemplate('boilerplate', './', name, options.template);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,22 +6,37 @@ class TemplateGenerator {
|
||||||
this.templateName = templateName;
|
this.templateName = templateName;
|
||||||
}
|
}
|
||||||
|
|
||||||
generate(destinationFolder, name) {
|
downloadAndGenerate(uri, destinationFolder, name) {
|
||||||
let templatePath = fs.embarkPath(utils.joinPath('templates', this.templateName));
|
const self = this;
|
||||||
console.log(__('Initializing Embark Template....').green);
|
let {url, filePath} = this.getExternalProject(uri);
|
||||||
|
let tmpFilePath = fs.tmpDir(filePath);
|
||||||
|
console.log(__('Installing Template from ' + uri + '....').green);
|
||||||
|
|
||||||
|
fs.mkdirpSync(utils.dirname(tmpFilePath));
|
||||||
|
utils.downloadFile(url, tmpFilePath, () => {
|
||||||
let fspath = utils.joinPath(destinationFolder, name);
|
let fspath = utils.joinPath(destinationFolder, name);
|
||||||
|
|
||||||
fs.copySync(templatePath, fspath);
|
utils.extractZip(tmpFilePath, fspath, {
|
||||||
utils.cd(fspath);
|
map: file => {
|
||||||
utils.sed('package.json', '%APP_NAME%', name);
|
let fixed_path = file.path.split('/');
|
||||||
|
fixed_path.shift(); // remove first directory
|
||||||
if (name === 'embark_demo') {
|
file.path = utils.joinPath(...fixed_path);
|
||||||
console.log(__('Installing packages...').green);
|
return file;
|
||||||
utils.runCmd('npm install');
|
}
|
||||||
|
}).then(() => {
|
||||||
|
self.installTemplate(fspath, name, true);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(__('Init complete').green);
|
generate(destinationFolder, name) {
|
||||||
console.log('\n' + __('App ready at ').green + fspath);
|
console.log(__('Initializing Embark Template....').green);
|
||||||
|
|
||||||
|
let templatePath = fs.embarkPath(utils.joinPath('templates', this.templateName));
|
||||||
|
let fspath = utils.joinPath(destinationFolder, name);
|
||||||
|
fs.copySync(templatePath, fspath);
|
||||||
|
|
||||||
|
this.installTemplate(fspath, name, (name === 'embark_demo'));
|
||||||
|
|
||||||
if (name === 'embark_demo') {
|
if (name === 'embark_demo') {
|
||||||
console.log('-------------------'.yellow);
|
console.log('-------------------'.yellow);
|
||||||
|
@ -31,6 +46,47 @@ class TemplateGenerator {
|
||||||
console.log(__('For more info go to http://embark.status.im').green);
|
console.log(__('For more info go to http://embark.status.im').green);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
getExternalProject(uri) {
|
||||||
|
let match = uri.match(
|
||||||
|
/\.[a-z]+\/([-a-zA-Z0-9@:%_+.~#?&\/=]+)/
|
||||||
|
);
|
||||||
|
|
||||||
|
let url, folder;
|
||||||
|
|
||||||
|
if (uri.startsWith('http')) {
|
||||||
|
url = uri + "/archive/master.zip";
|
||||||
|
folder = match[1];
|
||||||
|
} else if (uri.startsWith('github')) {
|
||||||
|
url = "https://" + uri + "/archive/master.zip";
|
||||||
|
folder = match[1];
|
||||||
|
} else if (uri.split('/').length === 2) {
|
||||||
|
url = "https://github.com/" + uri + "/archive/master.zip";
|
||||||
|
folder = uri;
|
||||||
|
} else if (uri.indexOf('/') === -1) {
|
||||||
|
url = "https://github.com/embark-framework/embark-" + uri + "-template/archive/master.zip";
|
||||||
|
folder = "embark-framework/embark-" + uri + "-template";
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
url,
|
||||||
|
filePath: utils.joinPath(".embark/templates/", folder, "archive.zip")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = TemplateGenerator;
|
module.exports = TemplateGenerator;
|
||||||
|
|
|
@ -86,6 +86,11 @@ function createWriteStream() {
|
||||||
return fs.createWriteStream.apply(fs.createWriteStream, arguments);
|
return fs.createWriteStream.apply(fs.createWriteStream, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tmpDir() {
|
||||||
|
let os = require('os');
|
||||||
|
return utils.joinPath(os.tmpdir(), ...arguments);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
mkdirpSync,
|
mkdirpSync,
|
||||||
mkdirp,
|
mkdirp,
|
||||||
|
@ -105,5 +110,6 @@ module.exports = {
|
||||||
removeSync,
|
removeSync,
|
||||||
embarkPath,
|
embarkPath,
|
||||||
dappPath,
|
dappPath,
|
||||||
createWriteStream
|
createWriteStream,
|
||||||
|
tmpDir
|
||||||
};
|
};
|
||||||
|
|
|
@ -53,10 +53,14 @@ class Embark {
|
||||||
simulator.run(options);
|
simulator.run(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
generateTemplate(templateName, destinationFolder, name) {
|
generateTemplate(templateName, destinationFolder, name, url) {
|
||||||
this.context = [constants.contexts.templateGeneration];
|
this.context = [constants.contexts.templateGeneration];
|
||||||
let TemplateGenerator = require('./cmds/template_generator.js');
|
let TemplateGenerator = require('./cmds/template_generator.js');
|
||||||
let templateGenerator = new TemplateGenerator(templateName);
|
let templateGenerator = new TemplateGenerator(templateName);
|
||||||
|
|
||||||
|
if (url) {
|
||||||
|
return templateGenerator.downloadAndGenerate(url, destinationFolder, name);
|
||||||
|
}
|
||||||
templateGenerator.generate(destinationFolder, name);
|
templateGenerator.generate(destinationFolder, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,11 @@ function joinPath() {
|
||||||
return path.join.apply(path.join, arguments);
|
return path.join.apply(path.join, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dirname() {
|
||||||
|
const path = require('path');
|
||||||
|
return path.dirname.apply(path.dirname, arguments);
|
||||||
|
}
|
||||||
|
|
||||||
function filesMatchingPattern(files) {
|
function filesMatchingPattern(files) {
|
||||||
const globule = require('globule');
|
const globule = require('globule');
|
||||||
return globule.find(files, {nonull: true});
|
return globule.find(files, {nonull: true});
|
||||||
|
@ -168,6 +173,14 @@ function extractTar(filename, packageDirectory, cb) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractZip(filename, packageDirectory, cb) {
|
||||||
|
const decompress = require('decompress');
|
||||||
|
|
||||||
|
decompress(filename, packageDirectory).then((_files) => {
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function proposeAlternative(word, _dictionary, _exceptions) {
|
function proposeAlternative(word, _dictionary, _exceptions) {
|
||||||
const propose = require('propose');
|
const propose = require('propose');
|
||||||
let exceptions = _exceptions || [];
|
let exceptions = _exceptions || [];
|
||||||
|
@ -298,29 +311,31 @@ function buildUrlFromConfig (configObj){
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
joinPath: joinPath,
|
joinPath,
|
||||||
filesMatchingPattern: filesMatchingPattern,
|
dirname,
|
||||||
fileMatchesPattern: fileMatchesPattern,
|
filesMatchingPattern,
|
||||||
recursiveMerge: recursiveMerge,
|
fileMatchesPattern,
|
||||||
checkIsAvailable: checkIsAvailable,
|
recursiveMerge,
|
||||||
httpGet: httpGet,
|
checkIsAvailable,
|
||||||
httpsGet: httpsGet,
|
httpGet,
|
||||||
httpGetJson: httpGetJson,
|
httpsGet,
|
||||||
httpsGetJson: httpsGetJson,
|
httpGetJson,
|
||||||
hexToNumber: hexToNumber,
|
httpsGetJson,
|
||||||
|
hexToNumber,
|
||||||
pingEndpoint,
|
pingEndpoint,
|
||||||
decodeParams: decodeParams,
|
decodeParams,
|
||||||
runCmd: runCmd,
|
runCmd,
|
||||||
cd: cd,
|
cd: cd,
|
||||||
sed: sed,
|
sed: sed,
|
||||||
exit: exit,
|
exit: exit,
|
||||||
downloadFile: downloadFile,
|
downloadFile,
|
||||||
extractTar: extractTar,
|
extractTar,
|
||||||
proposeAlternative: proposeAlternative,
|
extractZip,
|
||||||
|
proposeAlternative,
|
||||||
pwd: pwd,
|
pwd: pwd,
|
||||||
getExternalContractUrl,
|
getExternalContractUrl,
|
||||||
toChecksumAddress: toChecksumAddress,
|
toChecksumAddress,
|
||||||
sha3: sha3,
|
sha3,
|
||||||
normalizeInput,
|
normalizeInput,
|
||||||
buildUrl,
|
buildUrl,
|
||||||
buildUrlFromConfig
|
buildUrlFromConfig
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
"colors": "^1.1.2",
|
"colors": "^1.1.2",
|
||||||
"commander": "^2.15.1",
|
"commander": "^2.15.1",
|
||||||
"css-loader": "^0.28.11",
|
"css-loader": "^0.28.11",
|
||||||
|
"decompress": "^4.2.0",
|
||||||
"deep-equal": "^1.0.1",
|
"deep-equal": "^1.0.1",
|
||||||
"ejs": "^2.5.8",
|
"ejs": "^2.5.8",
|
||||||
"embark-web3-provider-engine": "14.0.7",
|
"embark-web3-provider-engine": "14.0.7",
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
/*globals describe, it*/
|
||||||
|
const assert = require('assert');
|
||||||
|
const TemplateGenerator = require('../lib/cmds/template_generator');
|
||||||
|
|
||||||
|
describe('TemplateGenerator', function () {
|
||||||
|
describe('getExternalProject', function () {
|
||||||
|
let templateGenerator;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
templateGenerator = new TemplateGenerator();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with github link', function () {
|
||||||
|
|
||||||
|
it('return correct zip filename for https link', function () {
|
||||||
|
let result = templateGenerator.getExternalProject("https://github.com/embark-framework/embark");
|
||||||
|
assert.strictEqual(result.url, "https://github.com/embark-framework/embark/archive/master.zip");
|
||||||
|
assert.strictEqual(result.filePath, ".embark/templates/embark-framework/embark/archive.zip");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return correct zip filename for http link', function () {
|
||||||
|
let result = templateGenerator.getExternalProject("http://github.com/embark-framework/embark");
|
||||||
|
assert.strictEqual(result.url, "http://github.com/embark-framework/embark/archive/master.zip");
|
||||||
|
assert.strictEqual(result.filePath, ".embark/templates/embark-framework/embark/archive.zip");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return correct zip filename without protocol specified ', function () {
|
||||||
|
let result = templateGenerator.getExternalProject("github.com/embark-framework/embark");
|
||||||
|
assert.strictEqual(result.url, "https://github.com/embark-framework/embark/archive/master.zip");
|
||||||
|
assert.strictEqual(result.filePath, ".embark/templates/embark-framework/embark/archive.zip");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return correct zip filename without protocol specified ', function () {
|
||||||
|
let result = templateGenerator.getExternalProject("github.com/embark-framework/embark");
|
||||||
|
assert.strictEqual(result.url, "https://github.com/embark-framework/embark/archive/master.zip");
|
||||||
|
assert.strictEqual(result.filePath, ".embark/templates/embark-framework/embark/archive.zip");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return correct zip filename with just username/repo specified', function () {
|
||||||
|
let result = templateGenerator.getExternalProject("embark-framework/embark");
|
||||||
|
assert.strictEqual(result.url, "https://github.com/embark-framework/embark/archive/master.zip");
|
||||||
|
assert.strictEqual(result.filePath, ".embark/templates/embark-framework/embark/archive.zip");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return correct zip filename with just embark template specified', function () {
|
||||||
|
let result = templateGenerator.getExternalProject("react");
|
||||||
|
assert.strictEqual(result.url, "https://github.com/embark-framework/embark-react-template/archive/master.zip");
|
||||||
|
assert.strictEqual(result.filePath, ".embark/templates/embark-framework/embark-react-template/archive.zip");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue