tokyo-solidity-template
Version:
Make solidity contract based on user input
108 lines (78 loc) • 3.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _tokyoSchema = require("tokyo-schema");
var _tokyoSchema2 = _interopRequireDefault(_tokyoSchema);
var _memFs = require("mem-fs");
var _memFs2 = _interopRequireDefault(_memFs);
var _memFsEditor = require("mem-fs-editor");
var _memFsEditor2 = _interopRequireDefault(_memFsEditor);
var _templateHelper = require("./templateHelper");
var templateHelper = _interopRequireWildcard(_templateHelper);
var _Parser = require("./Parser");
var _Parser2 = _interopRequireDefault(_Parser);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @title Builder
* @notice Builder read and write template with the input.
*/
class Builder {
constructor(inputObj) {
const { value, error } = (0, _tokyoSchema2.default)(inputObj);
if (error) throw error;
this.rawInput = inputObj;
this.input = value;
this.parser = new _Parser2.default(value);
this.store = _memFs2.default.create();
this.fs = _memFsEditor2.default.create(this.store);
}
/**
* @notice builds templates and generates truffle project.
*/
build() {
const parseResult = this.parser.parse();
return new Promise(done => {
this.writeInput();
this.writeStatic(parseResult);
this.writeContracts(parseResult);
this.writeMigrations(parseResult);
// TODO: dynamic test is not supported yet
// this.writeTest(parseResult);
this.fs.commit([], done);
});
}
getDataObj(parseResult) {
return {
input: this.input, rawInput: this.rawInput, helper: templateHelper, parseResult
};
}
writeStatic(parseResult) {
// package.json
this.fs.copyTpl(this.tmplPath("package.json.ejs"), this.targetPath("package.json"), this.getDataObj(parseResult));
}
writeContracts(parseResult) {
const dirName = "contracts";
// crowdsale
this.fs.copyTpl(this.tmplPath(dirName, "Crowdsale.sol.ejs"), this.targetPath(dirName, `${templateHelper.getCrowdsaleName(parseResult)}.sol`), this.getDataObj(parseResult));
// token
this.fs.copyTpl(this.tmplPath(dirName, "Token.sol.ejs"), this.targetPath(dirName, `${templateHelper.getTokenName(parseResult)}.sol`), this.getDataObj(parseResult));
}
writeMigrations(parseResult) {
const dirName = "migrations";
this.fs.copyTpl(this.tmplPath(dirName, "2_deploy_contracts.js.ejs"), this.targetPath(dirName, "2_deploy_contracts.js"), this.getDataObj(parseResult));
}
writeTest(parseResult) {
const dirName = "test";
// crowdsale
this.fs.copyTpl(this.tmplPath(dirName, "Crowdsale.js.ejs"), this.targetPath(dirName, "Crowdsale.js"), this.getDataObj(parseResult));
// token
this.fs.copyTpl(this.tmplPath(dirName, "Token.js.ejs"), this.targetPath(dirName, "Token.js"), this.getDataObj(parseResult));
}
writeInput() {
this.fs.write(this.targetPath("input.json"), // root directory
JSON.stringify(this.rawInput, null, 2));
}
}
exports.default = Builder;