-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from gonzalobustosglob/feat/cli
feat: cli feature initial commit. Basic CLI support is provided.
- Loading branch information
Showing
8 changed files
with
929 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
feat: cli feature initial commit. Basic CLI support is provided. | ||
|
||
# Please enter the commit message for your changes. Lines starting | ||
# with '#' will be ignored, and an empty message aborts the commit. | ||
# | ||
# Date: Tue Aug 15 15:18:29 2023 -0300 | ||
# | ||
# On branch feat/cli | ||
# Changes to be committed: | ||
# new file: cli/cli.cjs | ||
# modified: package-lock.json | ||
# modified: package.json | ||
# new file: tsconfig.lib.json | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env node --experimental-vm-modules --experimental-wasm-threads | ||
|
||
const chalk = require("chalk"); | ||
const { printBanner } = require("./gui.cjs"); | ||
const configureRepl = require("./repl.cjs"); | ||
|
||
printBanner(); | ||
|
||
/** | ||
* Generates a key pair using snarkyjs. | ||
* @param {Object} snarkyjs - The snarkyjs library. | ||
* @returns {Object} - An object containing the private and public keys. | ||
*/ | ||
function genKeyPair(snarkyjs) { | ||
return () => { | ||
const priv = snarkyjs.PrivateKey.random(); | ||
const pub = priv.toPublicKey(); | ||
|
||
return { | ||
priv, | ||
pub, | ||
}; | ||
}; | ||
} | ||
|
||
/** | ||
* Dynamically imports a module from an absolute path. | ||
* @param {string} absolutePath - The absolute path of the module to import. | ||
* @returns {Promise} - A promise that resolves to the imported module. | ||
*/ | ||
function dynamicImport(absolutePath) { | ||
return import(absolutePath); | ||
} | ||
|
||
// Start a REPL session with the given options. | ||
configureRepl(dynamicImport, genKeyPair); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @fileoverview This module exports functions to print banners and messages related to Mina Testing Utils. | ||
* @module cli/gui | ||
* @requires cfonts | ||
* @requires chalk | ||
*/ | ||
|
||
const cfonts = require("cfonts"); | ||
const chalk = require("chalk"); | ||
|
||
const loadMinaMsg = ` | ||
Please load the Mina REPL context by executing ${chalk.underline.bold( | ||
".loadMina" | ||
)} before running any commands. | ||
`; | ||
|
||
/** | ||
* Prints a banner with the name of the module. | ||
* @function | ||
* @name printBanner | ||
* @returns {void} | ||
*/ | ||
module.exports.printBanner = function printBanner() { | ||
cfonts.say("Mina Testing Utils", { | ||
font: "block", | ||
align: "left", | ||
gradient: ["red", "yellow"], | ||
}); | ||
console.log( | ||
` | ||
${chalk.bold(loadMinaMsg)} | ||
` | ||
); | ||
}; | ||
|
||
/** | ||
* Prints an error message indicating that the Mina REPL context needs to be loaded. | ||
* @function | ||
* @name printLoadMinaErrorMsg | ||
* @returns {void} | ||
*/ | ||
module.exports.printLoadMinaErrorMsg = function printLoadMinaErrorMsg() { | ||
console.log(chalk.red(loadMinaMsg)); | ||
}; | ||
|
||
/** | ||
* Returns a success message indicating that the Mina REPL context was loaded successfully. | ||
* @function | ||
* @name minaLoadedOkMsg | ||
* @returns {string} The success message. | ||
*/ | ||
module.exports.minaLoadedOkMsg = function minaLoadedOkMsg() { | ||
return `Snarky loaded successfully! You can access it through the ${chalk.green.bold("mina")} object.`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const repl = require("repl"); | ||
const ora = require("ora"); | ||
const homedir = require("os").homedir(); | ||
|
||
const { printLoadMinaErrorMsg, minaLoadedOkMsg } = require("./gui.cjs"); | ||
|
||
function configureRepl(dynamicImport, genKeyPair) { | ||
// Start a REPL session with the given options. | ||
const REPL = repl.start({ | ||
prompt: "mina-testing-utils> ", | ||
ignoreUndefined: true, | ||
replMode: repl.REPL_MODE_STRICT, | ||
require, | ||
}); | ||
|
||
// Set command history file. | ||
REPL.setupHistory(`${homedir}/.mina-testing-utils-history`, (err) => { | ||
if (err) console.warn("Repl history could not be loaded."); | ||
}); | ||
|
||
// Set the await flag to true in the REPL context. | ||
REPL.context.await = true; | ||
// Add extra context to the REPL context. | ||
const extraContext = { | ||
mina: { | ||
loadContractFromPath: dynamicImport, | ||
}, | ||
}; | ||
Object.assign(REPL.context, extraContext); | ||
|
||
// Override the eval function to print a message if the user tries to run a command before loading the Mina context. | ||
const evalFn = REPL.eval; | ||
REPL.eval = (cmd, context, filename, callback) => { | ||
printLoadMinaErrorMsg(); | ||
evalFn(cmd, context, filename, (err, result) => { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
if (result instanceof Promise) { | ||
result.then((res) => { | ||
callback(null, res); | ||
}); | ||
} else { | ||
callback(null, result); | ||
} | ||
} | ||
}); | ||
}; | ||
// Define a custom command for the REPL. | ||
REPL.defineCommand("loadMina", { | ||
help: "Loads snarkyjs", | ||
action() { | ||
const spinner = ora({ | ||
text: "Loading snarkyjs...", | ||
discardStdin: false, | ||
}).start(); | ||
this.clearBufferedCommand(); | ||
dynamicImport("snarkyjs") | ||
.then((snarkyjs) => { | ||
const local = snarkyjs.Mina.LocalBlockchain({ | ||
proofsEnabled: false, | ||
}); | ||
snarkyjs.Mina.setActiveInstance(local); | ||
|
||
const minaContext = { | ||
snarkyjs, | ||
local, | ||
testAccounts: local.testAccounts, | ||
genKeyPair: genKeyPair(snarkyjs), | ||
}; | ||
|
||
Object.assign(this.context.mina, minaContext); | ||
|
||
spinner.succeed(minaLoadedOkMsg()); | ||
console.log(); | ||
// Restore eval function. | ||
this.displayPrompt(); | ||
REPL.eval = evalFn; | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
spinner.fail("Error while loading snarkyjs"); | ||
}); | ||
}, | ||
}); | ||
} | ||
|
||
module.exports = configureRepl; |
Oops, something went wrong.