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

Commit 7eb423d

Browse files
committed
core(console): factor Console class to use validTruffleConsoleCommands
- simplifies the console class. - changes error output in console/repl
1 parent 54222cb commit 7eb423d

File tree

5 files changed

+21
-40
lines changed

5 files changed

+21
-40
lines changed

packages/core/lib/commands/develop/run.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@ const {
66
} = require("../../configAdapter");
77

88
const runConsole = async (config, ganacheOptions) => {
9-
const { Console, validTruffleConsoleCommands } = require("../../console");
9+
const { Console } = require("../../console");
1010
const { Environment } = require("@truffle/environment");
1111

1212
await Environment.develop(config, ganacheOptions);
13-
const c = new Console(
14-
validTruffleConsoleCommands,
15-
config.with({ noAliases: true })
16-
);
13+
const c = new Console(config.with({ noAliases: true }));
1714
c.on("exit", () => process.exit());
1815
return await c.start();
1916
};

packages/core/lib/console-child.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {
88
displayGeneralHelp
99
} = require("./command-utils");
1010
const { handleHelpInput } = require("./cliHelp");
11-
const { validTruffleConsoleCommands } = require("./console");
11+
const { validTruffleConsoleCommands } = require("./commands/commands");
1212

1313
// we split off the part Truffle cares about and need to convert to an array
1414
const input = process.argv[2].split(" -- ");

packages/core/lib/console.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@ const { spawn } = require("child_process");
1616
const Require = require("@truffle/require");
1717
const debug = require("debug")("console");
1818
const { getCommand, parseQuotesAndEscapes } = require("./command-utils");
19-
const {
20-
validTruffleCommands,
21-
validTruffleConsoleCommands
22-
} = require("./commands/commands");
19+
const { validTruffleConsoleCommands } = require("./commands/commands");
2320

2421
// Create an expression that returns a string when evaluated
2522
// by the REPL
2623
const makeIIFE = str => `(() => "${str}")()`;
2724

28-
const processInput = (input, allowedCommands) => {
25+
const processInput = input => {
2926
const words = input.trim().split(/\s+/);
3027

3128
// empty input
@@ -42,20 +39,19 @@ const processInput = (input, allowedCommands) => {
4239
}
4340

4441
const normalizedCommand = cmd.toLowerCase();
45-
if (validTruffleCommands.includes(normalizedCommand)) {
46-
return allowedCommands.includes(normalizedCommand)
47-
? words.slice(1).join(" ")
48-
: makeIIFE(`ℹ️ : '${cmd}' is not allowed within Truffle REPL`);
49-
}
50-
return makeIIFE(`ℹ️ : '${cmd}' is not a valid Truffle command`);
42+
return validTruffleConsoleCommands.includes(normalizedCommand)
43+
? words.slice(1).join(" ")
44+
: makeIIFE(
45+
`ℹ️ : '${words[0]} ${cmd}' is not valid in Console environment.`
46+
);
5147
}
5248

5349
// an expression
5450
return input.trim();
5551
};
5652

5753
class Console extends EventEmitter {
58-
constructor(allowedCommands, options) {
54+
constructor(options) {
5955
super();
6056
EventEmitter.call(this);
6157

@@ -72,7 +68,6 @@ class Console extends EventEmitter {
7268
"build_directory"
7369
]);
7470

75-
this.allowedCommands = allowedCommands;
7671
this.options = options;
7772

7873
this.repl = null;
@@ -401,9 +396,9 @@ class Console extends EventEmitter {
401396
}
402397

403398
async interpret(input, context, filename, callback) {
404-
const processedInput = processInput(input, this.allowedCommands);
399+
const processedInput = processInput(input);
405400
if (
406-
this.allowedCommands.includes(processedInput.split(/\s+/)[0]) &&
401+
validTruffleConsoleCommands.includes(processedInput.split(/\s+/)[0]) &&
407402
getCommand({
408403
inputStrings: processedInput.split(/\s+/),
409404
options: {},
@@ -530,6 +525,5 @@ class Console extends EventEmitter {
530525
}
531526

532527
module.exports = {
533-
validTruffleConsoleCommands,
534528
Console
535529
};

packages/core/test/lib/console.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const assert = require("chai").assert;
22
const path = require("path");
33
const { Console } = require("../../lib/console");
4-
const { validTruffleConsoleCommands } = require("../../lib/commands/commands");
54
const sinon = require("sinon");
65
const Config = require("@truffle/config");
76
const Web3 = require("web3");
@@ -37,7 +36,7 @@ describe("Console", function () {
3736
{ path: pathToMoreUserJs },
3837
{ path: pathToUserJs, as: "namespace" }
3938
];
40-
truffleConsole = new Console(validTruffleConsoleCommands, consoleOptions);
39+
truffleConsole = new Console(consoleOptions);
4140
sinon
4241
.stub(truffleConsole.interfaceAdapter, "getAccounts")
4342
.returns(["0x0"]);
@@ -83,10 +82,7 @@ describe("Console", function () {
8382
"test/sources/moreUserVariables.js"
8483
);
8584
otherConsoleOptions["require-none"] = true;
86-
otherTruffleConsole = new Console(
87-
validTruffleConsoleCommands,
88-
otherConsoleOptions
89-
);
85+
otherTruffleConsole = new Console(otherConsoleOptions);
9086
});
9187

9288
it("won't load any user-defined JS", async function () {
@@ -115,10 +111,7 @@ describe("Console", function () {
115111
config.working_directory,
116112
"test/sources/nameConflicts.js"
117113
);
118-
otherTruffleConsole = new Console(
119-
validTruffleConsoleCommands,
120-
otherConsoleOptions
121-
);
114+
otherTruffleConsole = new Console(otherConsoleOptions);
122115
});
123116

124117
it("won't let users clobber Truffle variables", async function () {
@@ -144,10 +137,7 @@ describe("Console", function () {
144137
provider: new Web3.providers.WebsocketProvider("ws://localhost:666"),
145138
resolver: new Resolver(config)
146139
});
147-
otherTruffleConsole = new Console(
148-
validTruffleConsoleCommands,
149-
otherConsoleOptions
150-
);
140+
otherTruffleConsole = new Console(otherConsoleOptions);
151141
});
152142

153143
it("accepts options.r", async function () {

packages/truffle/test/scenarios/commands/develop.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ describe("truffle develop", function () {
7878
[
7979
{
8080
cmd: "console",
81-
expectedError: `ℹ️ : 'console' is not allowed within Truffle REPL`
81+
expectedError: `ℹ️ : 'truffle console' is not valid in Console environment.`
8282
},
8383
{
8484
cmd: "CONSOLE",
85-
expectedError: `ℹ️ : 'CONSOLE' is not allowed within Truffle REPL`
85+
expectedError: `ℹ️ : 'truffle CONSOLE' is not valid in Console environment.`
8686
},
8787
{
8888
cmd: "develop",
89-
expectedError: `ℹ️ : 'develop' is not allowed within Truffle REPL`
89+
expectedError: `ℹ️ : 'truffle develop' is not valid in Console environment.`
9090
},
9191
{
9292
cmd: "alakazam",
93-
expectedError: `ℹ️ : 'alakazam' is not a valid Truffle command`
93+
expectedError: `ℹ️ : 'truffle alakazam' is not valid in Console environment`
9494
},
9595
{
9696
cmd: "",

0 commit comments

Comments
 (0)