From 1cbd75f422b385b96f178cc2a3393098ee557b2f Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Wed, 3 Feb 2021 13:10:28 -0600 Subject: [PATCH] Change emojiRegex to load lazily --- ui/app/controllers/exec.js | 2 +- ui/app/routes/exec.js | 6 ++++-- ui/app/utils/escape-task-name.js | 4 +--- ui/tests/unit/utils/escape-task-name-test.js | 9 +++++---- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ui/app/controllers/exec.js b/ui/app/controllers/exec.js index 9e269152731..7b13f7d7c99 100644 --- a/ui/app/controllers/exec.js +++ b/ui/app/controllers/exec.js @@ -100,7 +100,7 @@ export default class ExecController extends Controller { this.terminal.writeln('Customize your command, then hit ‘return’ to run.'); this.terminal.writeln(''); this.terminal.write( - `$ nomad alloc exec -i -t -task ${escapeTaskName(taskName)} ${ + `$ nomad alloc exec -i -t -task ${escapeTaskName(this.emojiRegex, taskName)} ${ this.taskState.allocation.shortId } ` ); diff --git a/ui/app/routes/exec.js b/ui/app/routes/exec.js index 4ad7fbea92c..b7f7e4a68c8 100644 --- a/ui/app/routes/exec.js +++ b/ui/app/routes/exec.js @@ -28,12 +28,14 @@ export default class ExecRoute extends Route.extend(WithWatchers) { .catch(notifyError(this)); const xtermImport = import('xterm').then(module => module.Terminal); + const emojiRegexImport = import('emoji-regex').then(module => module.default); - return Promise.all([jobPromise, xtermImport]); + return Promise.all([jobPromise, emojiRegexImport, xtermImport]); } - setupController(controller, [job, Terminal]) { + setupController(controller, [job, emojiRegex, Terminal]) { super.setupController(controller, job); + controller.emojiRegex = emojiRegex; controller.setUpTerminal(Terminal); } diff --git a/ui/app/utils/escape-task-name.js b/ui/app/utils/escape-task-name.js index 68d0276f207..139866e096d 100644 --- a/ui/app/utils/escape-task-name.js +++ b/ui/app/utils/escape-task-name.js @@ -1,6 +1,4 @@ -import emojiRegex from 'emoji-regex'; - -export default function escapeTaskName(taskName) { +export default function escapeTaskName(emojiRegex, taskName) { // Store emoji character components differently to bypass escaping: // "string🥳" becomes "stringUNICODE55358.UNICODE56691." const taskNameWithTransformedEmoji = taskName.replace(emojiRegex(), emoji => { diff --git a/ui/tests/unit/utils/escape-task-name-test.js b/ui/tests/unit/utils/escape-task-name-test.js index 0d57cfe943a..3cc97f852a9 100644 --- a/ui/tests/unit/utils/escape-task-name-test.js +++ b/ui/tests/unit/utils/escape-task-name-test.js @@ -1,11 +1,12 @@ +import emojiRegex from 'emoji-regex'; import escapeTaskName from 'nomad-ui/utils/escape-task-name'; import { module, test } from 'qunit'; module('Unit | Utility | escape-task-name', function() { test('it escapes task names for the faux exec CLI', function(assert) { - assert.equal(escapeTaskName('plain'), 'plain'); - assert.equal(escapeTaskName('a space'), 'a\\ space'); - assert.equal(escapeTaskName('dollar $ign'), 'dollar\\ \\$ign'); - assert.equal(escapeTaskName('emoji🥳'), 'emoji🥳'); + assert.equal(escapeTaskName(emojiRegex, 'plain'), 'plain'); + assert.equal(escapeTaskName(emojiRegex, 'a space'), 'a\\ space'); + assert.equal(escapeTaskName(emojiRegex, 'dollar $ign'), 'dollar\\ \\$ign'); + assert.equal(escapeTaskName(emojiRegex, 'emoji🥳'), 'emoji🥳'); }); });