diff --git a/bin/near b/bin/near index 0334fe34..3c8e073d 100755 --- a/bin/near +++ b/bin/near @@ -1,4 +1,4 @@ -#!/usr/bin/env node +#!/usr/bin/env node --experimental-repl-await const yargs = require('yargs'); const main = require('../'); @@ -217,6 +217,7 @@ yargs // eslint-disable-line .command(newProject) .command(stake) .command(login) + .command(require('../commands/repl')) .config(config) .alias({ 'accountId': ['account_id'], diff --git a/commands/repl.js b/commands/repl.js new file mode 100644 index 00000000..b7eb502b --- /dev/null +++ b/commands/repl.js @@ -0,0 +1,14 @@ +module.exports = { + command: 'repl', + desc: 'launch interactive Node.js shell with NEAR connection available to use', + builder: (yargs) => yargs, + handler: async (argv) => { + const repl = require('repl'); + const context = repl.start('> ').context; + context.nearlib = require('nearlib'); + context.near = await require('../utils/connect')(argv); + if (argv.accountId) { + context.account = await context.near.account(argv.accountId); + } + } +}; \ No newline at end of file diff --git a/index.js b/index.js index 463c6411..543807ea 100755 --- a/index.js +++ b/index.js @@ -1,6 +1,3 @@ -const nearjs = require('nearlib'); -const { KeyPair, keyStores } = require('nearlib'); -const UnencryptedFileSystemKeyStore = keyStores.UnencryptedFileSystemKeyStore; const fs = require('fs'); const yargs = require('yargs'); const bs58 = require('bs58'); @@ -9,6 +6,12 @@ const rimraf = require('rimraf'); const readline = require('readline'); const URL = require('url').URL; +const nearjs = require('nearlib'); +const { KeyPair, keyStores } = require('nearlib'); +const UnencryptedFileSystemKeyStore = keyStores.UnencryptedFileSystemKeyStore; + +const connect = require('./utils/connect'); + ncp.limit = 16; // TODO: Fix promisified wrappers to handle error properly @@ -35,19 +38,6 @@ exports.clean = async function() { console.log("Clean complete."); }; -async function connect(options) { - if (options.keyPath === undefined && options.helperUrl === undefined) { - const homeDir = options.homeDir || `${process.env.HOME}/.near`; - options.keyPath = `${homeDir}/validator_key.json`; - } - // TODO: search for key store. - const keyStore = new UnencryptedFileSystemKeyStore('./neardev'); - options.deps = { - keyStore, - }; - return await nearjs.connect(options); -} - exports.createAccount = async function(options) { let near = await connect(options); let keyPair; diff --git a/utils/connect.js b/utils/connect.js new file mode 100644 index 00000000..40ae2099 --- /dev/null +++ b/utils/connect.js @@ -0,0 +1,15 @@ +const nearlib = require('nearlib'); +const UnencryptedFileSystemKeyStore = nearlib.keyStores.UnencryptedFileSystemKeyStore; + +module.exports = async function connect(options) { + if (options.keyPath === undefined && options.helperUrl === undefined) { + const homeDir = options.homeDir || `${process.env.HOME}/.near`; + options.keyPath = `${homeDir}/validator_key.json`; + } + // TODO: search for key store. + const keyStore = new UnencryptedFileSystemKeyStore('./neardev'); + options.deps = { + keyStore, + }; + return await nearlib.connect(options); +} \ No newline at end of file