Skip to content

Commit fc84ded

Browse files
committed
Fix JavaScript runs
Always run in the context of this package, to utilise the preconfigured Babel setup, but without altering the CWD. Builds on #1427 and fixes #1436.
1 parent 3c5ccca commit fc84ded

File tree

1 file changed

+44
-57
lines changed

1 file changed

+44
-57
lines changed

lib/grammars/javascript.js

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,57 @@
11
'use babel';
22

3+
import path from 'path';
34
import GrammarUtils from '../grammar-utils';
45

5-
export default {
6-
'Babel ES6 JavaScript': {
7-
'Selection Based': {
8-
command: 'babel-node',
9-
args: context => ['-e', context.getCode()],
10-
},
11-
'File Based': {
12-
command: 'babel-node',
13-
args: context => [context.filepath],
6+
const babel = path.join(__dirname, '../..', 'node_modules', '.bin', 'babel');
7+
8+
const args = ({ filepath }) => ['-c', `${babel} --filename '${babel}' < '${filepath}'| node`];
9+
10+
exports.Dart = {
11+
'Selection Based': {
12+
command: 'dart',
13+
args: (context) => {
14+
const code = context.getCode();
15+
const tmpFile = GrammarUtils.createTempFileWithCode(code, '.dart');
16+
return [tmpFile];
1417
},
1518
},
16-
Dart: {
17-
'Selection Based': {
18-
command: 'dart',
19-
args: (context) => {
20-
const code = context.getCode();
21-
const tmpFile = GrammarUtils.createTempFileWithCode(code, '.dart');
22-
return [tmpFile];
23-
},
24-
},
25-
'File Based': {
26-
command: 'dart',
27-
args: ({ filepath }) => [filepath],
28-
},
19+
'File Based': {
20+
command: 'dart',
21+
args: ({ filepath }) => [filepath],
2922
},
30-
JavaScript: {
31-
'Selection Based': {
32-
command: 'babel-node',
33-
args: context => ['-e', context.getCode()],
34-
},
35-
'File Based': {
36-
command: 'babel-node',
37-
args: ({ filepath }) => [filepath],
23+
};
24+
exports.JavaScript = {
25+
'Selection Based': {
26+
command: 'bash',
27+
args: (context) => {
28+
const code = context.getCode();
29+
const filepath = GrammarUtils.createTempFileWithCode(code, '.js');
30+
return args({ filepath });
3831
},
3932
},
40-
'JavaScript for Automation (JXA)': {
41-
'Selection Based': {
42-
command: 'osascript',
43-
args: context => ['-l', 'JavaScript', '-e', context.getCode()],
44-
},
45-
'File Based': {
46-
command: 'osascript',
47-
args: ({ filepath }) => ['-l', 'JavaScript', filepath],
48-
},
33+
'File Based': { command: 'bash', args },
34+
};
35+
exports['Babel ES6 JavaScript'] = exports.JavaScript;
36+
exports['JavaScript with JSX'] = exports.JavaScript;
37+
38+
exports['JavaScript for Automation (JXA)'] = {
39+
'Selection Based': {
40+
command: 'osascript',
41+
args: context => ['-l', 'JavaScript', '-e', context.getCode()],
4942
},
50-
'JavaScript with JSX': {
51-
'Selection Based': {
52-
command: 'babel-node',
53-
args: context => ['-e', context.getCode()],
54-
},
55-
'File Based': {
56-
command: 'babel-node',
57-
args: ({ filepath }) => [filepath],
58-
},
43+
'File Based': {
44+
command: 'osascript',
45+
args: ({ filepath }) => ['-l', 'JavaScript', filepath],
5946
},
60-
TypeScript: {
61-
'Selection Based': {
62-
command: 'ts-node',
63-
args: context => ['-e', context.getCode()],
64-
},
65-
'File Based': {
66-
command: 'ts-node',
67-
args: ({ filepath }) => [filepath],
68-
},
47+
};
48+
exports.TypeScript = {
49+
'Selection Based': {
50+
command: 'ts-node',
51+
args: context => ['-e', context.getCode()],
52+
},
53+
'File Based': {
54+
command: 'ts-node',
55+
args: ({ filepath }) => [filepath],
6956
},
7057
};

0 commit comments

Comments
 (0)