Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Add interactive near repl with auto-injected nearlib, near and …
Browse files Browse the repository at this point in the history
…`account`
  • Loading branch information
vgrichina committed Sep 1, 2019
1 parent e547fb7 commit 8dfaae8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
3 changes: 2 additions & 1 deletion bin/near
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
#!/usr/bin/env node --experimental-repl-await

const yargs = require('yargs');
const main = require('../');
Expand Down Expand Up @@ -217,6 +217,7 @@ yargs // eslint-disable-line
.command(newProject)
.command(stake)
.command(login)
.command(require('../commands/repl'))
.config(config)
.alias({
'accountId': ['account_id'],
Expand Down
14 changes: 14 additions & 0 deletions commands/repl.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
22 changes: 6 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions utils/connect.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8dfaae8

Please sign in to comment.