Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v6.x backport] repl: force editorMode in .load #15775

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 8 additions & 7 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1301,15 +1301,16 @@ function defineDefaultCommands(repl) {
try {
var stats = fs.statSync(file);
if (stats && stats.isFile()) {
var self = this;
this.editorMode = true;
REPLServer.super_.prototype.setPrompt.call(this, '');
var data = fs.readFileSync(file, 'utf8');
var lines = data.split('\n');
this.displayPrompt();
lines.forEach(function(line) {
if (line) {
self.write(line + '\n');
}
});
for (var n = 0; n < lines.length; n++) {
if (lines[n])
this.write(`${lines[n]}\n`);
}
this.turnOffEditorMode();
this.write('\n');
} else {
this.outputStream.write('Failed to load:' + file +
' is not a valid file\n');
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/repl-load-multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const getLunch = () =>
placeOrder('tacos')
.then(eat);

const placeOrder = (order) => Promise.resolve(order);
const eat = (food) => '<nom nom nom>';
39 changes: 39 additions & 0 deletions test/parallel/test-repl-load-multiline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common');
const path = require('path');
const fixtures = common.fixturesDir;
const assert = require('assert');
const repl = require('repl');

const command = `.load ${path.join(fixtures, 'repl-load-multiline.js')}`;
const terminalCode = '\u001b[1G\u001b[0J \u001b[1G';
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');

const expected = `${command}
const getLunch = () =>
placeOrder('tacos')
.then(eat);
const placeOrder = (order) => Promise.resolve(order);
const eat = (food) => '<nom nom nom>';

undefined
`;

let accum = '';

const inputStream = new common.ArrayStream();
const outputStream = new common.ArrayStream();

outputStream.write = (data) => accum += data.replace('\r', '');

const r = repl.start({
prompt: '',
input: inputStream,
output: outputStream,
terminal: true,
useColors: false
});

r.write(`${command}\n`);
assert.strictEqual(accum.replace(terminalCodeRegex, ''), expected);
r.close();