tokyo-solidity-template
Version:
Make solidity contract based on user input
122 lines (89 loc) • 4.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _fs = require("fs");
var _fs2 = _interopRequireDefault(_fs);
var _path = require("path");
var _mkdirp = require("mkdirp");
var _mkdirp2 = _interopRequireDefault(_mkdirp);
var _ncp = require("ncp");
var _findUp = require("find-up");
var _findUp2 = _interopRequireDefault(_findUp);
var _Logger = require("./Logger");
var _Logger2 = _interopRequireDefault(_Logger);
var _Builder = require("./Builder");
var _Builder2 = _interopRequireDefault(_Builder);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const logger = new _Logger2.default(true);
const defaultTargetPath = (0, _path.resolve)(__dirname, "../../out");
const defaultTemplPath = (0, _path.resolve)(__dirname, "../../templates");
const defaultStaticPath = (0, _path.resolve)(__dirname, "../../static");
const defaultBaseContractPath = _findUp2.default.sync("tokyo-reusable-crowdsale/contracts", { cwd: __dirname });
const defaultBaseTestHelperPath = _findUp2.default.sync("tokyo-reusable-crowdsale/test/helpers", { cwd: __dirname });
/**
* @title Generator
* @notice Generator make directories for output, build template.
*/
class Generator extends _Builder2.default {
constructor(input, targetPath = defaultTargetPath, tmplPath = defaultTemplPath, staticPath = defaultStaticPath, baseContractPath = defaultBaseContractPath, baseTestHelperPath = defaultBaseTestHelperPath) {
super(input); // validate in Builder's constructor
this.path = {
tmpl: tmplPath, // `/templates`
static: staticPath, // `/static`
target: {
root: targetPath, // `/out`
contracts: (0, _path.resolve)(targetPath, "./contracts"), // `/out/contracts`
migrations: (0, _path.resolve)(targetPath, "./migrations"), // `/out/migrations`
test: (0, _path.resolve)(targetPath, "./test") // `/out/test`
},
base: {
contracts: baseContractPath, // `/tokyo-reusable-crowdsale/contracts`
test: baseTestHelperPath // `/tokyo-reusable-crowdsale/test`
}
};
}
write() {
var _this = this;
return _asyncToGenerator(function* () {
logger.log("generator writting...");
_this._makeDirectories();
yield _this._copyStatic();
yield _this._copyBaseContracts();
// DEBUG: super.build() isn't supported by babel
yield _this.build(_this.path); // Copy templates with user input
})();
}
_makeDirectories() {
logger.log("making directories...");
(0, _mkdirp2.default)(this.path.target.contracts);
(0, _mkdirp2.default)(this.path.target.migrations);
(0, _mkdirp2.default)(this.path.target.test);
}
_copyStatic() {
logger.log("copying truffle static files...");
const staticFiles = _fs2.default.readdirSync(this.staticPath());
staticFiles.forEach(file => {
this.fs.copy(this.staticPath(file), this.targetPath(file));
});
}
_copyBaseContracts() {
const sourcePath = this.path.base.contracts;
const targetPath = (0, _path.resolve)(this.path.target.contracts, "./base");
logger.log("copying base contracts...");
logger.log("from", sourcePath);
logger.log("to", targetPath);
return (0, _ncp.ncp)(sourcePath, targetPath);
}
targetPath(...args) {
return (0, _path.join)(this.path.target.root, ...args);
}
tmplPath(...args) {
return (0, _path.join)(this.path.tmpl, ...args);
}
staticPath(...args) {
return (0, _path.join)(this.path.static, ...args);
}
}
exports.default = Generator;