Skip to content

Commit

Permalink
v3.4.0: add shortName config to control $ identifier name
Browse files Browse the repository at this point in the history
  • Loading branch information
bterlson committed Jun 8, 2017
1 parent 03d0566 commit b2b8aff
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Agent {
this.args.split(' ').filter(v => v.trim()) :
[this.args];
}

this.shortName = options.shortName || '$';
}

compile(code, options) {
Expand All @@ -23,7 +25,7 @@ class Agent {
if (options.async) {
return code;
} else {
return code + '\n;$.destroy();';
return code + '\n;' + this.shortName + '.destroy();';
}
}

Expand Down
7 changes: 6 additions & 1 deletion lib/ConsoleAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const cp = require('child_process');
const temp = require('temp');
const ErrorParser = require('./parseError.js');


class ConsoleAgent extends Agent {
constructor(options) {
super(options);
Expand Down Expand Up @@ -71,7 +72,11 @@ class ConsoleAgent extends Agent {

compile (code, options) {
code = super.compile(code, options);
const runtime = this.constructor.runtime;
let runtime = this.constructor.runtime;
if (runtime && this.options.shortName) {
runtime = runtime.replace(/\$/g, this.options.shortName);
}

if (!runtime) {
return code;
} else {
Expand Down
1 change: 0 additions & 1 deletion lib/agents/ch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const inception = require('../inception');
const runtimePath = require('../runtimePath');
const ConsoleAgent = require('../ConsoleAgent');


const runtimeStr = inception(
fs.readFileSync(runtimePath.for('chakra'), 'utf8')
.replace(/\r?\n/g, '')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eshost",
"version": "3.3.2",
"version": "3.4.0",
"description": "Invoke ECMAScript scripts in any command line JS engine.",
"main": "lib/eshost.js",
"scripts": {
Expand Down
11 changes: 10 additions & 1 deletion test/runify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const hosts = [
['../webkit/build/bin64/jsc.exe', 'jsc'],
/*[undefined, 'chrome'],*/

['C:/Program Files (x86)/Mozilla Firefox/firefox.exe', 'firefox'],
//['C:/Program Files (x86)/Mozilla Firefox/firefox.exe', 'firefox'],
/*['C:/Program Files (x86)/Nightly/firefox.exe', 'firefox'],
['C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', 'chrome'],
*/
Expand Down Expand Up @@ -39,6 +39,15 @@ hosts.forEach(function (record) {
return agent.destroy();
});

it('allows custom shortNames', function() {
return runify.createAgent(type, { hostPath: host, shortName: '$testing' }).then(agent => {
return agent.evalScript('$testing.evalScript("print(1)")').then(result => {
assert(result.error === null, 'no error');
assert.equal(result.stdout.indexOf('1'), 0);
});
});
});

it('runs SyntaxErrors', function () {
return agent.evalScript('foo x++').then(function (result) {
assert(result.error, 'error is present');
Expand Down

0 comments on commit b2b8aff

Please sign in to comment.