UNPKG

@authereum/zos

Version:

Command-line interface for the ZeppelinOS smart contract platform

154 lines 7.99 kB
'use strict'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_every_1 = __importDefault(require("lodash.every")); const lodash_map_1 = __importDefault(require("lodash.map")); const zos_lib_1 = require("zos-lib"); const Session_1 = __importDefault(require("../network/Session")); const Dependency_1 = __importDefault(require("../dependency/Dependency")); const NetworkController_1 = __importDefault(require("../network/NetworkController")); const ValidationLogger_1 = __importDefault(require("../../interface/ValidationLogger")); const ConfigManager_1 = __importDefault(require("../config/ConfigManager")); const ProjectFile_1 = __importDefault(require("../files/ProjectFile")); const ContractManager_1 = __importDefault(require("./ContractManager")); const DEFAULT_VERSION = '0.1.0'; class LocalController { constructor(projectFile = new ProjectFile_1.default(), init = false) { if (!init && !projectFile.exists()) { throw Error(`ZeppelinOS file ${projectFile.filePath} not found. Run 'zos init' first to initialize the project.`); } this.projectFile = projectFile; } init(name, version, force = false, publish = false) { if (!name) throw Error('A project name must be provided to initialize the project.'); this.initProjectFile(name, version, force, publish); Session_1.default.ignoreFile(); ConfigManager_1.default.initialize(); } initProjectFile(name, version, force = false, publish) { if (this.projectFile.exists() && !force) { throw Error(`Cannot overwrite existing file ${this.projectFile.filePath}`); } if (this.projectFile.name && !force) { throw Error(`Cannot initialize already initialized package ${this.projectFile.name}`); } this.projectFile.name = name; this.projectFile.version = version || DEFAULT_VERSION; this.projectFile.contracts = {}; if (publish) this.projectFile.publish = publish; zos_lib_1.Loggy.noSpin(__filename, 'initProjectFile', 'init-project-file', `Project initialized. Write a new contract in the contracts folder and run 'zos create' to deploy it.`); } bumpVersion(version) { this.projectFile.version = version; } add(contractAlias, contractName) { zos_lib_1.Loggy.spin(__filename, 'add', `add-${contractAlias}`, `Adding ${contractAlias === contractName ? contractAlias : `${contractAlias}:${contractName}`}`); this.projectFile.addContract(contractAlias, contractName); zos_lib_1.Loggy.succeed(`add-${contractAlias}`, `Added contract ${contractAlias}`); } addAll() { const manager = new ContractManager_1.default(this.projectFile); manager.getContractNames().forEach(name => this.add(name, name)); } remove(contractAlias) { if (!this.projectFile.hasContract(contractAlias)) { zos_lib_1.Loggy.noSpin.error(__filename, 'remove', `remove-${contractAlias}`, `Contract ${contractAlias} to be removed was not found`); } else { zos_lib_1.Loggy.spin(__filename, 'remove', `remove-${contractAlias}`, `Removing ${contractAlias}`); this.projectFile.unsetContract(contractAlias); zos_lib_1.Loggy.succeed(`remove-${contractAlias}`, `Removed contract ${contractAlias}`); } } checkCanAdd(contractName) { const path = zos_lib_1.Contracts.getLocalPath(contractName); if (!zos_lib_1.FileSystem.exists(path)) { throw Error(`Contract ${contractName} not found in path ${path}`); } if (!this.hasBytecode(path)) { throw Error(`Contract ${contractName} is abstract and cannot be deployed.`); } } // Contract model validateAll() { const buildArtifacts = zos_lib_1.getBuildArtifacts(); return lodash_every_1.default(lodash_map_1.default(this.projectFile.contractAliases, contractAlias => this.validate(contractAlias, buildArtifacts))); } // Contract model validate(contractAlias, buildArtifacts) { const contractName = this.projectFile.contract(contractAlias); const contract = zos_lib_1.Contracts.getFromLocal(contractName || contractAlias); const warnings = zos_lib_1.validate(contract, {}, buildArtifacts); new ValidationLogger_1.default(contract).log(warnings, buildArtifacts); return zos_lib_1.validationPasses(warnings); } // Contract model hasBytecode(contractDataPath) { if (!zos_lib_1.FileSystem.exists(contractDataPath)) return false; const bytecode = zos_lib_1.FileSystem.parseJson(contractDataPath).bytecode; return bytecode && bytecode !== '0x'; } // Contract model getContractSourcePath(contractAlias) { const contractName = this.projectFile.contract(contractAlias); if (contractName) { const contractDataPath = zos_lib_1.Contracts.getLocalPath(contractName); const { compiler, sourcePath } = zos_lib_1.FileSystem.parseJson(contractDataPath); return { sourcePath, compilerVersion: compiler.version }; } else { throw Error(`Could not find ${contractAlias} in contracts directory.`); } } writePackage() { this.projectFile.write(); } // DependencyController linkDependencies(dependencies, installDependencies = false) { return __awaiter(this, void 0, void 0, function* () { const linkedDependencies = yield Promise.all(dependencies.map((depNameVersion) => __awaiter(this, void 0, void 0, function* () { const dependency = installDependencies ? yield Dependency_1.default.install(depNameVersion) : Dependency_1.default.fromNameWithVersion(depNameVersion); this.projectFile.setDependency(dependency.name, dependency.requirement); return dependency.name; }))); if (linkedDependencies.length > 0) { const label = linkedDependencies.length === 1 ? 'Dependency' : 'Dependencies'; zos_lib_1.Loggy.noSpin(__filename, 'linkDependencies', 'link-dependencies', `${label} linked to the project. Run 'zos create' to deploy one of its contracts.`); } }); } // DependencyController unlinkDependencies(dependenciesNames) { const unlinkedDependencies = dependenciesNames.map(dep => { const dependency = Dependency_1.default.fromNameWithVersion(dep); this.projectFile.unsetDependency(dependency.name); return dependency.name; }); if (unlinkedDependencies.length > 0) { const label = unlinkedDependencies.length === 1 ? 'Dependency' : 'Dependencies'; zos_lib_1.Loggy.noSpin(__filename, 'linkDependencies', 'link-dependencies', `${label} ${unlinkedDependencies.join(', ')} unlinked.`); } } onNetwork(network, txParams, networkFile) { return new NetworkController_1.default(network, txParams, networkFile); } } exports.default = LocalController; //# sourceMappingURL=LocalController.js.map