Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor open handling and add basic tests
- Loading branch information
1 parent
a1ae203
commit 80bcbf5
Showing
4 changed files
with
145 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const spawn = require('child_process').spawn; | ||
|
||
const lineColumn = message => `${message.line}:${message.column}`; | ||
|
||
const openReport = (report, opts) => { | ||
opts = Object.assign({ | ||
editor: process.env.EDITOR, | ||
spawn | ||
}, opts); | ||
|
||
if (report.errorCount === 0) { | ||
return; | ||
} | ||
|
||
if (!opts.editor) { | ||
console.log(` | ||
\`open\` option was used, but your $EDITOR environment variable is empty. | ||
Fix it by setting path to your editor of choice in ~/.bashrc or ~/.zshrc: | ||
export EDITOR=atom | ||
`); | ||
return; | ||
} | ||
|
||
const executableName = opts.editor.split(path.sep).pop(); | ||
const args = []; | ||
|
||
report.results | ||
.filter(result => result.errorCount > 0) | ||
.forEach(result => { | ||
// VS Code requires --goto option for path:line:column | ||
if (executableName === 'code') { | ||
args.push('--goto'); | ||
} | ||
|
||
// Sublime Text, Atom, and VS Code support opening file at exact position | ||
if (['subl', 'atom', 'code'].indexOf(executableName) >= 0) { | ||
args.push(result.filePath + ':' + lineColumn(result.messages[0])); | ||
return; | ||
} | ||
|
||
// WebStorm supports opening file on a specific line (no column support) | ||
if (executableName === 'wstorm') { | ||
args.push(result.filePath + ':' + result.messages[0].line); | ||
return; | ||
} | ||
|
||
// TextMate requires a `--line` option | ||
if (executableName === 'mate') { | ||
args.push('--line', lineColumn(result.messages[0]), result.filePath); | ||
return; | ||
} | ||
|
||
args.push(result.filePath); | ||
}); | ||
|
||
opts.spawn(opts.editor, args, { | ||
detached: true, | ||
stdio: 'ignore' | ||
}).unref(); | ||
}; | ||
|
||
module.exports = openReport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
"gitignore-filter.js", | ||
"index.js", | ||
"cli.js", | ||
"open-report.js", | ||
"options-manager.js", | ||
"config" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import fn from '../open-report'; | ||
|
||
const singleFileReportFixture = { | ||
errorCount: 1, | ||
results: [ | ||
{ | ||
errorCount: 1, | ||
filePath: 'foo.js', | ||
messages:[ | ||
{ | ||
line: 42, | ||
column: 43 | ||
} | ||
] | ||
} | ||
] | ||
}; | ||
|
||
const singleSpawnMacro = (t, input, expected) => { | ||
t.plan(3); | ||
|
||
fn(input.report, { | ||
editor: input.editor, | ||
spawn: (cmd, args, opts) => { | ||
t.is(cmd, expected.cmd); | ||
t.deepEqual(args, expected.args); | ||
|
||
return { | ||
unref: () => { | ||
t.pass(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
test('VS Code - single file report', singleSpawnMacro, { | ||
editor: 'code', | ||
report: singleFileReportFixture | ||
}, { | ||
cmd: 'code', | ||
args: ['--goto', 'foo.js:42:43'] | ||
}); | ||
|
||
test('Sublime Text - single file report', singleSpawnMacro, { | ||
editor: 'subl', | ||
report: singleFileReportFixture | ||
}, { | ||
cmd: 'subl', | ||
args: ['foo.js:42:43'] | ||
}); | ||
|
||
test('Atom - single file report', singleSpawnMacro, { | ||
editor: 'atom', | ||
report: singleFileReportFixture | ||
}, { | ||
cmd: 'atom', | ||
args: ['foo.js:42:43'] | ||
}); | ||
|
||
test('WebStorm - single file report', singleSpawnMacro, { | ||
editor: 'wstorm', | ||
report: singleFileReportFixture | ||
}, { | ||
cmd: 'wstorm', | ||
args: ['foo.js:42'] | ||
}); | ||
|
||
test('TextMate - single file report', singleSpawnMacro, { | ||
editor: 'mate', | ||
report: singleFileReportFixture | ||
}, { | ||
cmd: 'mate', | ||
args: ['--line', '42:43', 'foo.js'] | ||
}); |