Skip to content

Commit

Permalink
Merge pull request #24 from Adamant-im/dev
Browse files Browse the repository at this point in the history
Merge dev branch into master
  • Loading branch information
martiliones authored Aug 16, 2022
2 parents 26eef52 + 6f0f330 commit 525de6a
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 33 deletions.
5 changes: 4 additions & 1 deletion bin/adamant.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { program, CommanderError } = require('commander');
const chalk = require('chalk');
const parseShell = require('shell-quote').parse;

const semver = require('semver');
const leven = require('leven');
Expand Down Expand Up @@ -108,6 +109,8 @@ enhanceErrorMessages('missingArgument', (argName) => (
`Missing required argument ${chalk.yellow(`<${argName}>`)}.`
));

enhanceErrorMessages('unknownCommand', () => `Unknown command.`);

enhanceErrorMessages('unknownOption', (optionName) => (
`Unknown option ${chalk.yellow(optionName)}.`
));
Expand All @@ -125,7 +128,7 @@ if (INTERACTIVE_MODE) {

prompt(async (command) => {
try {
await program.parseAsync(command.split(' '), { from: 'user' });
await program.parseAsync(parseShell(command), { from: 'user' });
} catch (err) {
if (!(err instanceof CommanderError)) {
console.log(err);
Expand Down
14 changes: 12 additions & 2 deletions config.default.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{
/**
The Console will use this ADM passPhrase by default.
See 'Installing and configuring' in Readme for details.
**/
"passPhrase": "distance expect praise frequent..",

/** Choose 'mainnet' or 'testnet' **/
"network": "testnet",

/** Port used to run RPC server **/
"rpc": {
"port": 5080
},

/** Additionally, you can specify what ADM nodes to use **/
"networks": {
"testnet": {
"nodes": [
Expand Down Expand Up @@ -32,6 +43,5 @@
}
]
}
},
"passPhrase": ""
}
}
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adamant-console",
"version": "2.0.0",
"version": "2.1.0",
"description": "Console API and JSON-RPC for interacting with ADAMANT Blockchain",
"main": "lib/api/index.js",
"bin": {
Expand All @@ -9,6 +9,8 @@
"engines": {
"node": ">=14"
},
"author": "ADAMANT Foundation <[email protected]> (https://adamant.im)",
"license": "GPL-3.0",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "npx eslint -f visualstudio .",
Expand All @@ -18,11 +20,6 @@
"type": "git",
"url": "git+https://github.com/Adamant-im/adamant-console.git"
},
"keywords": [
"ADAMANT"
],
"author": "",
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/Adamant-im/adamant-console/issues"
},
Expand All @@ -41,9 +38,24 @@
"ed2curve": "^0.3.0",
"jayson": "^3.6.6",
"joi": "^17.6.0",
"jsonminify": "^0.4.2",
"leven": "^3.1.0",
"semver": "^7.3.7",
"shell-quote": "^1.7.3",
"sodium-browserify-tweetnacl": "^0.2.6",
"tweetnacl": "^1.0.3"
}
},
"keywords": [
"adm",
"adamant",
"blockchain",
"messenger",
"wallet",
"secure",
"encryption",
"crypto",
"cryptocurrency",
"CLI",
"console"
]
}
20 changes: 14 additions & 6 deletions prompt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ module.exports = (callback) => {
console.log(`Welcome to ADM CLI v${packageInfo.version}.`);
console.log('Type "help" for more information.');

process.stdin.on('keypress', (str, key) => {
process.stdin.on('keypress', (eventName, key) => {
if (key.name === 'up') {
rl.write(
history.back(rl.line),
{ ctrl: true, name: 'u' },
);
const line = history.back(rl.line);

if (line) {
rl.write(
null,
{ ctrl: true, name: 'u' },
);

rl.write(line);
}
}

if (key.name === 'down') {
rl.write(
history.next(rl.line),
null,
{ ctrl: true, name: 'u' },
);

rl.write(history.next(rl.line));
}
});

Expand Down
70 changes: 53 additions & 17 deletions utils/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */

const Joi = require('joi');
const jsonminify = require('jsonminify');
const chalk = require('chalk');
const os = require('os');
const fs = require('fs');
const path = require('path');
Expand All @@ -14,27 +15,62 @@ const configFileName = process.env.ADM_CONFIG_FILENAME || 'config.json';
const homeDir = os.homedir();
const configDirPath = process.env.ADM_CONFIG_PATH || `${homeDir}/${configPathName}`;

const configFilePath = path.resolve(`${configDirPath}/${configFileName}`);
const configFilePath = path.normalize(`${configDirPath}/${configFileName}`);
const localConfigFilePath = path.resolve(configFileName);

const defaultConfig = require('../config.default.json');
const defaultConfigFilePath = path.join(__dirname, '../config.default.json');

let config = defaultConfig;
function loadConfig(configPath) {
let parsedConfig = {};

if (fs.existsSync(configFilePath)) {
const loadedConfig = require(configFilePath);
try {
const data = fs.readFileSync(configPath, 'utf8');

config = {
...defaultConfig,
...loadedConfig,
};
} else if (fs.existsSync(configFileName)) {
const localConfigFilePath = path.resolve(configFileName);
const loadedConfig = require(localConfigFilePath);
parsedConfig = JSON.parse(jsonminify(data));
} catch (err) {
console.log(chalk.red(
'Failed to parse the configuration from:\n'
+ `└── ${chalk.yellow(configPath)}.`,
));
console.log();
console.log(err);
process.exit(1);
}

config = {
...defaultConfig,
...loadedConfig,
};
return parsedConfig;
}

let config = loadConfig(defaultConfigFilePath);

const configPaths = [
configFilePath,
localConfigFilePath,
];

for (const configPath of configPaths) {
let existingConfigPath;

const configWithComments = `${configPath}c`;

if (fs.existsSync(configPath)) {
existingConfigPath = configPath;
} else if (
!process.env.ADM_CONFIG_FILENAME
&& fs.existsSync(configWithComments)
) {
existingConfigPath = configWithComments;
}

if (existingConfigPath) {
const loadedConfig = loadConfig(existingConfigPath);

config = {
...config,
...loadedConfig,
};

break;
}
}

const netSchema = Joi.object({
Expand Down

0 comments on commit 525de6a

Please sign in to comment.