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

Add interactive near repl with auto-injected nearlib, near and account #128

Merged
merged 13 commits into from
Nov 4, 2019
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's --experimental-repl-await?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(would be helpful to leave a comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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);
}
}
};
1 change: 1 addition & 0 deletions get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function getConfig() {
if (e.code == 'MODULE_NOT_FOUND') {
console.warn(`[WARNING] Didn't find config at ${configPath}, using default shell config`);
const defaultConfig = require('./blank_project/src/config')(process.env.NODE_ENV || 'development');
console.log(defaultConfig);
return defaultConfig;
}
throw e;
Expand Down
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);
}