mirror of
https://github.com/status-im/embark-area-51.git
synced 2025-01-20 12:18:49 +00:00
Added --test option to scaffold and reorganized handlebar helpers
This commit is contained in:
parent
948be74f31
commit
da88874f0c
@ -199,6 +199,7 @@ class Cmd {
|
||||
program
|
||||
.command('scaffold [contract] [environment]')
|
||||
.option('--framework <framework>', 'UI framework to use. (default: react)')
|
||||
.option('--test', 'Generate test unit for contract')
|
||||
.action(function(contract, env, options){
|
||||
|
||||
let environment = env || 'development';
|
||||
@ -208,12 +209,18 @@ class Cmd {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if(options.test && options.framework){
|
||||
console.log("both options cannot be used at the same time");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
embark.initConfig(environment, {
|
||||
embarkConfig: 'embark.json', interceptLogs: false
|
||||
});
|
||||
|
||||
options.contract = contract;
|
||||
options.framework = options.framework || 'react';
|
||||
options.test = options.test || false;
|
||||
options.env = environment;
|
||||
embark.scaffold(options);
|
||||
});
|
||||
|
@ -1,7 +1,48 @@
|
||||
const Handlebars = require('handlebars');
|
||||
|
||||
Handlebars.registerHelper('capitalize', function(word) {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifview', function(stateMutability, options) {
|
||||
let result = stateMutability == 'view' || stateMutability == 'pure' || stateMutability == 'constant';
|
||||
if (result) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifeq', function(elem, value, options){
|
||||
if (elem == value) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('iflengthgt', function(arr, val, options) {
|
||||
if (arr.length > val) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('emptyname', function(name, index) {
|
||||
return name ? name : 'output' + index;
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){
|
||||
let funCount = abiDefinition.filter(x => x.name == functionName).length;
|
||||
if(funCount == 1){
|
||||
return '.' + functionName;
|
||||
}
|
||||
return new Handlebars.SafeString(`['${functionName}(${inputs !== null ? inputs.map(input => input.type).join(',') : ''})']`);
|
||||
});
|
||||
|
||||
class Scaffolding {
|
||||
constructor(embark, options){
|
||||
this.embark = embark;
|
||||
this.options = options;
|
||||
this.test = options.test;
|
||||
this.framework = options.framework;
|
||||
this.frameworkPlugin = null;
|
||||
}
|
||||
@ -11,9 +52,15 @@ class Scaffolding {
|
||||
}
|
||||
|
||||
generate(contractName, contractConfiguration){
|
||||
if(this.framework == 'react'){
|
||||
this.embark.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||
if(this.test){
|
||||
this.framework = 'embark-test';
|
||||
this.embark.plugins.loadInternalPlugin('scaffolding-test', this.options);
|
||||
} else {
|
||||
if(this.framework == 'react'){
|
||||
this.embark.plugins.loadInternalPlugin('scaffolding-react', this.options);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let dappGenerators = this.embark.plugins.getPluginsFor('dappGenerator');
|
||||
let build = null;
|
||||
@ -26,6 +73,9 @@ class Scaffolding {
|
||||
});
|
||||
|
||||
if(build === null){
|
||||
if(this.test) {
|
||||
throw new Error("Could not find plugin for generating test cases");
|
||||
}
|
||||
throw new Error("Could not find plugin for framework '" + this.framework + "'");
|
||||
}
|
||||
|
||||
@ -34,6 +84,7 @@ class Scaffolding {
|
||||
}
|
||||
|
||||
const contract = contractConfiguration.contracts[contractName];
|
||||
|
||||
build(contract);
|
||||
}
|
||||
}
|
||||
|
@ -281,7 +281,7 @@ class Embark {
|
||||
engine.logger.error(err.message);
|
||||
engine.logger.debug(err.stack);
|
||||
} else {
|
||||
engine.logger.info("finished generating ui");
|
||||
engine.logger.info("finished file generation");
|
||||
}
|
||||
process.exit();
|
||||
});
|
||||
|
@ -2,45 +2,6 @@
|
||||
const Handlebars = require('handlebars');
|
||||
const fs = require('../../core/fs');
|
||||
|
||||
Handlebars.registerHelper('capitalize', function(word) {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifview', function(stateMutability, options) {
|
||||
let result = stateMutability == 'view' || stateMutability == 'pure' || stateMutability == 'constant';
|
||||
if (result) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifeq', function(elem, value, options){
|
||||
if (elem == value) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('iflengthgt', function(arr, val, options) {
|
||||
if (arr.length > val) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('emptyname', function(name, index) {
|
||||
return name ? name : 'output' + index;
|
||||
});
|
||||
|
||||
|
||||
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){
|
||||
let funCount = abiDefinition.filter(x => x.name == functionName).length;
|
||||
if(funCount == 1){
|
||||
return '.' + functionName;
|
||||
}
|
||||
return new Handlebars.SafeString(`['${functionName}(${inputs !== null ? inputs.map(input => input.type).join(',') : ''})']`);
|
||||
});
|
||||
|
||||
class ScaffoldingReact {
|
||||
constructor(embark, options){
|
||||
this.embark = embark;
|
||||
@ -75,26 +36,30 @@ class ScaffoldingReact {
|
||||
}
|
||||
|
||||
async build(contract){
|
||||
this._buildHTML(contract);
|
||||
try {
|
||||
this._buildHTML(contract);
|
||||
|
||||
const filename = contract.className.toLowerCase();
|
||||
const filename = contract.className.toLowerCase();
|
||||
|
||||
this._generateFile(contract, 'dapp.js.tpl', 'js',
|
||||
{
|
||||
'title': contract.className,
|
||||
'contractName': contract.className,
|
||||
'functions': contract.abiDefinition.filter(x => x.type == 'function')
|
||||
});
|
||||
this._generateFile(contract, 'dapp.js.tpl', 'js',
|
||||
{
|
||||
'title': contract.className,
|
||||
'contractName': contract.className,
|
||||
'functions': contract.abiDefinition.filter(x => x.type == 'function')
|
||||
});
|
||||
|
||||
// Update config
|
||||
const contents = fs.readFileSync("./embark.json");
|
||||
let embarkJson = JSON.parse(contents);
|
||||
embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js';
|
||||
embarkJson.app[filename + ".html"] = "app/" + filename + '.html';
|
||||
// Update config
|
||||
const contents = fs.readFileSync("./embark.json");
|
||||
let embarkJson = JSON.parse(contents);
|
||||
embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js';
|
||||
embarkJson.app[filename + ".html"] = "app/" + filename + '.html';
|
||||
|
||||
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
|
||||
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
|
||||
|
||||
this.embark.logger.info(filename + ".html generated");
|
||||
this.embark.logger.info('./app/' + filename + ".html generated");
|
||||
} catch(err){
|
||||
this.embark.logger.error(err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
49
lib/modules/scaffolding-test/index.js
Normal file
49
lib/modules/scaffolding-test/index.js
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
const Handlebars = require('handlebars');
|
||||
const fs = require('../../core/fs');
|
||||
|
||||
class ScaffoldingTest {
|
||||
constructor(embark, options){
|
||||
this.embark = embark;
|
||||
this.options = options;
|
||||
|
||||
this.embark.registerDappGenerator('embark-test', this.build.bind(this));
|
||||
}
|
||||
|
||||
_generateFile(contract, templateFilename, extension, data){
|
||||
const filename = contract.className.toLowerCase() + '.' + extension;
|
||||
const filePath = './test/' + filename;
|
||||
|
||||
if (fs.existsSync(filePath)){
|
||||
throw new Error("file '" + filePath + "' already exists");
|
||||
}
|
||||
|
||||
const templatePath = fs.embarkPath('lib/modules/scaffolding-test/templates/' + templateFilename);
|
||||
const source = fs.readFileSync(templatePath).toString();
|
||||
const template = Handlebars.compile(source);
|
||||
|
||||
// Write template
|
||||
const result = template(data);
|
||||
fs.writeFileSync(filePath, result);
|
||||
}
|
||||
|
||||
|
||||
build(contract){
|
||||
const filename = contract.className.toLowerCase();
|
||||
|
||||
try {
|
||||
this._generateFile(contract, 'test.js.tpl', 'js',
|
||||
{
|
||||
'title': contract.className,
|
||||
'contractName': contract.className,
|
||||
'functions': contract.abiDefinition.filter(x => x.type == 'function')
|
||||
});
|
||||
|
||||
this.embark.logger.info('test/' + filename + ".js generated");
|
||||
} catch(err){
|
||||
this.embark.logger.error(err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ScaffoldingTest;
|
32
lib/modules/scaffolding-test/templates/test.js.tpl
Normal file
32
lib/modules/scaffolding-test/templates/test.js.tpl
Normal file
@ -0,0 +1,32 @@
|
||||
describe("{{title}}", function() {
|
||||
this.timeout(0);
|
||||
|
||||
before(function(done) {
|
||||
this.timeout(0);
|
||||
var contractsConfig = {
|
||||
"{{title}}": {
|
||||
args: []
|
||||
}
|
||||
};
|
||||
EmbarkSpec.deployAll(contractsConfig, () => { done() });
|
||||
});
|
||||
|
||||
{{#each functions}}
|
||||
xit("{{name}} should do something", async () => {
|
||||
{{#each inputs}}
|
||||
let {{name}} = null;
|
||||
{{/each}}
|
||||
{{#ifview stateMutability}}
|
||||
let result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}).call()
|
||||
{{else}}
|
||||
let receipt = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}).send({
|
||||
{{#if payable}}
|
||||
value: 0,
|
||||
{{/if}}
|
||||
from: web3.eth.defaultAccount
|
||||
});
|
||||
{{/ifview}}
|
||||
});
|
||||
|
||||
{{/each}}
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user