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

Revert "Run each console command in its own child process" #3314

Merged
merged 1 commit into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ listeners.forEach(listener => process.removeListener("warning", listener));
let options = { logger: console };

const inputArguments = process.argv.slice(2);
const userWantsGeneralHelp =
inputArguments.length === 1 && ["help", "--help"].includes(inputArguments[0]);
const userWantsGeneralHelp =
inputArguments.length === 1 && ['help', '--help'].includes(inputArguments[0]);

if (userWantsGeneralHelp) {
command.displayGeneralHelp();
Expand Down
1 change: 1 addition & 0 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var pkg = require("./package.json");
module.exports = {
build: require("./lib/build"),
create: require("./lib/commands/create/helpers"),
console: require("./lib/repl"),
contracts: require("@truffle/workflow-compile"),
package: require("./lib/package"),
test: require("./lib/test"),
Expand Down
7 changes: 3 additions & 4 deletions packages/core/lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Command {

let args = yargs();

Object.keys(this.commands).forEach(function (command) {
Object.keys(this.commands).forEach(function(command) {
args = args.command(commands[command]);
});

Expand Down Expand Up @@ -68,7 +68,7 @@ class Command {
return {
name: chosenCommand,
argv,
command,
command
};
}

Expand Down Expand Up @@ -134,11 +134,10 @@ class Command {
const newOptions = Object.assign({}, clone, argv);

result.command.run(newOptions, callback);

analytics.send({
command: result.name ? result.name : "other",
args: result.argv._,
version: bundled || "(unbundled) " + core,
version: bundled || "(unbundled) " + core
});
} catch (err) {
callback(err);
Expand Down
46 changes: 0 additions & 46 deletions packages/core/lib/console-child.js

This file was deleted.

72 changes: 26 additions & 46 deletions packages/core/lib/console.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const repl = require("repl");
const ReplManager = require("./repl");
const Command = require("./command");
const provision = require("@truffle/provisioner");
const {
Expand All @@ -12,7 +12,6 @@ const TruffleError = require("@truffle/error");
const fse = require("fs-extra");
const path = require("path");
const EventEmitter = require("events");
const spawnSync = require("child_process").spawnSync;

const processInput = input => {
const inputComponents = input.trim().split(" ");
Expand Down Expand Up @@ -44,10 +43,9 @@ class Console extends EventEmitter {

this.options = options;

this.repl = options.repl || new ReplManager(options);
this.command = new Command(tasks);

this.repl = null;

this.interfaceAdapter = createInterfaceAdapter({
provider: options.provider,
networkType: options.networks[options.network].type
Expand All @@ -56,22 +54,37 @@ class Console extends EventEmitter {
provider: options.provider,
networkType: options.networks[options.network].type
});

// Bubble the ReplManager's exit event
this.repl.on("exit", () => this.emit("exit"));

// Bubble the ReplManager's reset event
this.repl.on("reset", () => this.emit("reset"));
}

start() {
start(callback) {
if (!this.repl) this.repl = new Repl(this.options);

// TODO: This should probalby be elsewhere.
// It's here to ensure the repl manager instance gets
// passed down to commands.
this.options.repl = this.repl;

try {
this.interfaceAdapter.getAccounts().then(fetchedAccounts => {
const abstractions = this.provision();

this.repl = repl.start({
this.repl.start({
prompt: "truffle(" + this.options.network + ")> ",
eval: this.interpret.bind(this)
context: {
web3: this.web3,
interfaceAdapter: this.interfaceAdapter,
accounts: fetchedAccounts
},
interpreter: this.interpret.bind(this),
done: callback
});

this.repl.context.web3 = this.web3;
this.repl.context.interfaceAdapter = this.interfaceAdapter;
this.repl.context.accounts = fetchedAccounts;

this.resetContractsInConsoleContext(abstractions);
});
} catch (error) {
Expand Down Expand Up @@ -130,49 +143,16 @@ class Console extends EventEmitter {
abstractions.forEach(abstraction => {
contextVars[abstraction.contract_name] = abstraction;
});
}

runSpawn(inputStrings, options, callback) {
let childPath;
if (typeof BUNDLE_CONSOLE_CHILD_FILENAME !== "undefined") {
childPath = path.join(__dirname, BUNDLE_CONSOLE_CHILD_FILENAME);
} else {
childPath = path.join(__dirname, "../lib/console-child.js");
}

const spawnOptions = { stdio: ["inherit", "inherit", "inherit"] };

const spawnInput = "--network " + options.network + " -- " + inputStrings;

try {
spawnSync(
"node",
["--no-deprecation", childPath, spawnInput],
spawnOptions
);

try {
this.provision();
} catch (e) {
console.log(e);
}
} catch (err) {
callback(err);
}
//want repl to exit when it receives an exit command
this.repl.on("exit", () => {
process.exit();
});
//display prompt when child repl process is finished
this.repl.displayPrompt();
this.repl.setContextVars(contextVars);
}

interpret(input, context, filename, callback) {
const processedInput = processInput(input);
if (
this.command.getCommand(processedInput, this.options.noAliases) != null
) {
return this.runSpawn(processedInput, this.options, error => {
return this.command.run(processedInput, this.options, error => {
if (error) {
// Perform error handling ourselves.
if (error instanceof TruffleError) {
Expand Down
32 changes: 23 additions & 9 deletions packages/core/lib/debug/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const selectors = require("@truffle/debugger").selectors;
const { session, solidity, trace, evm, controller } = selectors;

const analytics = require("../services/analytics");
const repl = require("repl");
const ReplManager = require("../repl");

const { DebugPrinter } = require("./printer");

Expand Down Expand Up @@ -40,8 +40,9 @@ class DebugInterpreter {
this.printer = new DebugPrinter(config, session);
this.txHash = txHash;
this.lastCommand = "n";

this.repl = config.repl || new ReplManager(config);
this.enabledExpressions = new Set();
this.repl = null;
}

async setOrClearBreakpoint(args, setOrClear) {
Expand Down Expand Up @@ -275,14 +276,24 @@ class DebugInterpreter {
? DebugUtils.formatPrompt(this.network, this.txHash)
: DebugUtils.formatPrompt(this.network);

this.repl = repl.start({
prompt: prompt,
eval: util.callbackify(this.interpreter.bind(this)),
this.repl.start({
prompt,
interpreter: util.callbackify(this.interpreter.bind(this)),
ignoreUndefined: true,
done: terminate
});
}

setPrompt(prompt) {
this.repl.activate.bind(this.repl)({
prompt,
context: {},
//this argument only *adds* things, so it's safe to set it to {}
ignoreUndefined: true
//set to true because it's set to true below :P
});
}

async interpreter(cmd) {
cmd = cmd.trim();
let cmdArgs, splitArgs;
Expand All @@ -293,7 +304,10 @@ class DebugInterpreter {
}

//split arguments for commands that want that; split on runs of spaces
splitArgs = cmd.trim().split(/ +/).slice(1);
splitArgs = cmd
.trim()
.split(/ +/)
.slice(1);
debug("splitArgs %O", splitArgs);

//warning: this bit *alters* cmd!
Expand All @@ -310,7 +324,7 @@ class DebugInterpreter {

//quit if that's what we were given
if (cmd === "q") {
process.exit();
return await util.promisify(this.repl.stop.bind(this.repl))();
}

let alreadyFinished = this.session.view(trace.finishedOrUnloaded);
Expand Down Expand Up @@ -393,7 +407,7 @@ class DebugInterpreter {
if (this.session.view(selectors.session.status.success)) {
txSpinner.succeed();
//if successful, change prompt
this.repl.setPrompt(DebugUtils.formatPrompt(this.network, cmdArgs));
this.setPrompt(DebugUtils.formatPrompt(this.network, cmdArgs));
} else {
txSpinner.fail();
loadFailed = true;
Expand All @@ -416,7 +430,7 @@ class DebugInterpreter {
if (this.session.view(selectors.session.status.loaded)) {
await this.session.unload();
this.printer.print("Transaction unloaded.");
this.repl.setPrompt(DebugUtils.formatPrompt(this.network));
this.setPrompt(DebugUtils.formatPrompt(this.network));
} else {
this.printer.print("No transaction to unload.");
this.printer.print("");
Expand Down
Loading