@newos/cli
Version:
Command-line interface for the NewOS
140 lines • 6.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_1 = require("lodash");
const upgrades_1 = require("@newos/upgrades");
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(`OpenZeppelin file ${projectFile.filePath} not found. Run 'openzeppelin 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;
if (publish)
this.projectFile.publish = publish;
upgrades_1.Loggy.noSpin(__filename, 'initProjectFile', 'init-project-file', `Project initialized. Write a new contract in the contracts folder and run 'newos deploy' to deploy it.`);
}
bumpVersion(version) {
this.projectFile.version = version;
}
add(contractName) {
upgrades_1.Loggy.spin(__filename, 'add', `add-${contractName}`, `Adding ${contractName}`);
this.projectFile.addContract(contractName);
upgrades_1.Loggy.succeed(`add-${contractName}`, `Added contract ${contractName}`);
}
addAll() {
const manager = new ContractManager_1.default(this.projectFile);
manager.getContractNames().forEach(name => this.add(name));
}
remove(contractName) {
if (!this.projectFile.hasContract(contractName)) {
upgrades_1.Loggy.noSpin.error(__filename, 'remove', `remove-${contractName}`, `Contract ${contractName} to be removed was not found`);
}
else {
upgrades_1.Loggy.spin(__filename, 'remove', `remove-${contractName}`, `Removing ${contractName}`);
this.projectFile.removeContract(contractName);
upgrades_1.Loggy.succeed(`remove-${contractName}`, `Removed contract ${contractName}`);
}
}
checkCanAdd(contractName) {
const path = upgrades_1.Contracts.getLocalPath(contractName);
if (!fs_extra_1.default.existsSync(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 = upgrades_1.getBuildArtifacts();
return lodash_1.every(lodash_1.map(this.projectFile.contracts, contractName => this.validate(contractName, buildArtifacts)));
}
// Contract model
validate(contractName, buildArtifacts) {
const contract = upgrades_1.Contracts.getFromLocal(contractName).upgradeable;
const warnings = upgrades_1.validate(contract, {}, buildArtifacts);
new ValidationLogger_1.default(contract).log(warnings, buildArtifacts);
return upgrades_1.validationPasses(warnings);
}
// Contract model
hasBytecode(contractDataPath) {
if (!fs_extra_1.default.existsSync(contractDataPath))
return false;
const bytecode = fs_extra_1.default.readJsonSync(contractDataPath).bytecode;
return bytecode && bytecode !== '0x';
}
// Contract model
getContractSourcePath(contractName) {
try {
const contractDataPath = upgrades_1.Contracts.getLocalPath(contractName);
const { compiler, sourcePath } = fs_extra_1.default.readJsonSync(contractDataPath);
return { sourcePath, compilerVersion: compiler.version };
}
catch (error) {
error.message = `$Could not find ${contractName} in contracts directory. Error: ${error.message}.`;
throw error;
}
}
writePackage() {
this.projectFile.write();
}
// DependencyController
async linkDependencies(dependencies, installDependencies = false) {
const linkedDependencies = await Promise.all(dependencies.map(async (depNameVersion) => {
const dependency = installDependencies
? await 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';
upgrades_1.Loggy.noSpin(__filename, 'linkDependencies', 'link-dependencies', `${label} linked to the project. Run 'openzeppelin deploy' 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';
upgrades_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