chedder
Version:
106 lines • 4.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const CLIConfig_js_1 = require("./CLIConfig.js");
const CommandsAPI_js_1 = require("./CommandsAPI.js");
const CLIOptions_js_1 = require("./CLIOptions.js");
const CommandLineArgs_js_1 = require("./util/CommandLineArgs.js");
const color = __importStar(require("./util/color.js"));
const ExtensionAPI_js_1 = require("./ExtensionAPI.js");
const saveConfig_js_1 = require("./util/saveConfig.js");
const chedder_js_1 = require("./contracts/chedder.js");
const os = __importStar(require("os"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const network_js_1 = require("./near-api/network.js");
// default accountId
CLIOptions_js_1.options.accountId.value = CLIConfig_js_1.cliConfig.userAccount;
// process command line args
const args = new CommandLineArgs_js_1.CommandLineArgs(CLIOptions_js_1.options);
// command is the 1st positional argument
const command = args.getCommand();
// Show config info if requested
// Set config if requested
if (CLIOptions_js_1.options.cliConfig.value) {
saveConfig_js_1.saveConfig(CLIOptions_js_1.options.accountId.value, CLIOptions_js_1.options.contractName.value);
process.exit(0);
}
if (CLIOptions_js_1.options.info.value) {
console.log(`config.js:`);
console.log(` Your account : ${color.yellow}${CLIConfig_js_1.cliConfig.userAccount}${color.normal}`);
console.log(` Contract account: ${color.yellow}${CLIConfig_js_1.cliConfig.contractAccount}${color.normal}`);
process.exit(0);
}
function getCredentials(accountId) {
const homedir = os.homedir();
const CREDENTIALS_FILE = path.join(homedir, ".near-credentials/default/" + accountId + ".json");
let credentialsString = fs.readFileSync(CREDENTIALS_FILE).toString();
credentials = JSON.parse(credentialsString);
if (!credentials.private_key) {
throw Error("INVALID CREDENTIALS FILE. no priv.key");
}
}
// TODO configure
// if (command=="configure") {
// args.requireOptionString(options.accountId,"default account Id")
// process.exit(0);
// }
// -------------------
// PROCESS COMMAND //
// -------------------
// get contract API + Extensions
const API = new ExtensionAPI_js_1.ExtensionAPI();
// check the command is in the API
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (command && typeof API[command] !== "function") {
color.logErr("unknown command " + color.yellow + command + color.normal);
console.log(`${CommandsAPI_js_1.nickname} --help to see a list of commands`);
process.exit(1);
}
// Show help if requested or if no command
if (CLIOptions_js_1.options.help.value || !command) {
args.ShowHelpPage(command, API);
process.exit(0);
}
// get credentials
let credentials = { account_id: "", private_key: "" };
getCredentials(CLIConfig_js_1.cliConfig.userAccount);
//initialize contract proxy
//if (env.NODE_ENV=="development")
network_js_1.setNetwork('testnet');
chedder_js_1.chedder.contract_account = CLIConfig_js_1.cliConfig.contractAccount;
chedder_js_1.chedder.signer = CLIConfig_js_1.cliConfig.userAccount;
chedder_js_1.chedder.signer_private_key = credentials.private_key;
execute(API[command], args);
async function execute(command, args) {
try {
await command(args);
}
catch (ex) {
console.log(color.red + "Error:" + color.normal);
console.error(ex);
}
}
// call the commands API function
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// (API as any)[command](args)
//# sourceMappingURL=chedder.js.map