UNPKG

cc-zos

Version:

Command-line interface for the ZeppelinOS smart contract platform

62 lines (46 loc) 2.38 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _ccZosLib = require('cc-zos-lib'); var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const log = new _ccZosLib.Logger('Session'); const TIMEOUT_15_MIN_IN_SECONDS = 15 * 60; const ZOS_SESSION_PATH = '.zos.session'; const Session = { getSession() { const session = _ccZosLib.FileSystem.parseJsonIfExists(ZOS_SESSION_PATH); if (_lodash2.default.isEmpty(session)) return undefined; const expires = new Date(session.expires); if (expires <= new Date()) return undefined; return _lodash2.default.pick(session, 'network', 'timeout', 'from'); }, getOptions(overrides = {}) { const session = this.getSession(); if (!session) return overrides; log.info(`Using session with ${describe(_lodash2.default.omitBy(session, (v, key) => overrides[key]))}`); return _extends({}, this.getSession(), overrides); }, open({ network, from, timeout }, expires = TIMEOUT_15_MIN_IN_SECONDS) { const expirationTimestamp = new Date(new Date().getTime() + expires * 1000); _ccZosLib.FileSystem.writeJson(ZOS_SESSION_PATH, { network, from, timeout, expires: expirationTimestamp }); log.info(`Using ${describe({ network, from, timeout })} by default.`); }, close() { if (_ccZosLib.FileSystem.exists(ZOS_SESSION_PATH)) _ccZosLib.FileSystem.remove(ZOS_SESSION_PATH); log.info(`Closed zos session.`); }, ignoreFile() { const GIT_IGNORE = '.gitignore'; if (_ccZosLib.FileSystem.exists(GIT_IGNORE) && _ccZosLib.FileSystem.read(GIT_IGNORE).toString().indexOf(ZOS_SESSION_PATH) < 0) { _ccZosLib.FileSystem.append(GIT_IGNORE, `\n${ZOS_SESSION_PATH}\n`); } } }; function describe(session) { return _lodash2.default.compact([session.network && `network ${session.network}`, session.from && `sender address ${session.from}`, session.timeout && `timeout ${session.timeout} seconds`]).join(', ') || 'no options'; } exports.default = Session;