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

Rename args to arguments and bin to binary, add TypeScript definition #6

Merged
merged 1 commit into from
Apr 23, 2019
Merged
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
69 changes: 69 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {PathLike} from 'line-column-path';

declare namespace openEditor {
interface Options {
/**
Name, command, or binary path of the editor. Default: [Auto-detected](https://github.com/sindresorhus/env-editor).
BendingBender marked this conversation as resolved.
Show resolved Hide resolved

__Only use this option if you really have to.__ Can be useful if you want to force a specific editor or implement your own auto-detection.
*/
readonly editor?: string;
}

interface EditorRunConfig {
/**
Editor binary name.
*/
binary: string;

/**
Arguments provided to the editor binary.
*/
arguments: string[];

/**
A flag indicating whether the editor runs in the terminal.
*/
isTerminalEditor: boolean;
}
}

declare const openEditor: {
/**
Open the given files in the user's editor at specific line and column if supported by the editor. It does not wait for the editor to start or quit.

@param files - Items should be in the format `foo.js:1:5` or `{file: 'foo.js', line: 1: column: 5}`.

@example
```
import openEditor = require('open-editor');

openEditor([
'unicorn.js:5:3',
{
file: 'readme.md',
line: 10,
column: 2
}
]);
```
*/
(files: readonly PathLike[], options?: openEditor.Options): void;

/**
Same as `openEditor()`, but returns an object with the binary name, arguments, and a flag indicating whether the editor runs in the terminal.

Can be useful if you want to handle opening the files yourself.

@example
```
{binary: 'subl', arguments: ['foo.js:1:5'], isTerminalEditor: false}
```
*/
make(
files: readonly PathLike[],
options?: openEditor.Options
): openEditor.EditorRunConfig;
};

export = openEditor;
24 changes: 12 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@
const childProcess = require('child_process');
const envEditor = require('env-editor');
const lineColumnPath = require('line-column-path');
const opn = require('opn');
const open = require('open');

const make = (files, options = {}) => {
if (!Array.isArray(files)) {
throw new TypeError(`Expected an \`Array\`, got ${typeof files}`);
}

const editor = options.editor ? envEditor.get(options.editor) : envEditor.default();
const args = [];
const editor = options.editor ? envEditor.getEditor(options.editor) : envEditor.defaultEditor();
const editorArguments = [];

if (editor.id === 'vscode') {
args.push('--goto');
editorArguments.push('--goto');
}

for (const file of files) {
const parsed = lineColumnPath.parse(file);

if (['sublime', 'atom', 'vscode'].indexOf(editor.id) !== -1) {
args.push(lineColumnPath.stringify(parsed));
editorArguments.push(lineColumnPath.stringify(parsed));
continue;
}

if (['webstorm', 'intellij'].indexOf(editor.id) !== -1) {
args.push(lineColumnPath.stringify(parsed, {column: false}));
editorArguments.push(lineColumnPath.stringify(parsed, {column: false}));
continue;
}

if (editor.id === 'textmate') {
args.push('--line', lineColumnPath.stringify(parsed, {
editorArguments.push('--line', lineColumnPath.stringify(parsed, {
file: false
}), parsed.file);
continue;
}

if (['vim', 'neovim'].indexOf(editor.id) !== -1) {
args.push(`+call cursor(${parsed.line}, ${parsed.column})`, parsed.file);
editorArguments.push(`+call cursor(${parsed.line}, ${parsed.column})`, parsed.file);
continue;
}

args.push(parsed.file);
editorArguments.push(parsed.file);
}

return {
bin: editor.bin,
args,
binary: editor.binary,
arguments: editorArguments,
isTerminalEditor: editor.isTerminalEditor
};
};
Expand All @@ -65,7 +65,7 @@ module.exports = (files, options) => {
const result = make(files, {...options, editor: ''});

for (const file of result.args) {
opn(file);
open(file);
}
});

Expand Down
48 changes: 48 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {expectType} from 'tsd';
import openEditor = require('.');

const options: openEditor.Options = {};

openEditor([
'unicorn.js:5:3',
{
file: 'readme.md',
line: 10,
column: 2
}
]);
openEditor(
[
'unicorn.js:5:3',
{
file: 'readme.md',
line: 10,
column: 2
}
],
{editor: 'vi'}
);

expectType<openEditor.EditorRunConfig>(
openEditor.make([
'unicorn.js:5:3',
{
file: 'readme.md',
line: 10,
column: 2
}
])
);
expectType<openEditor.EditorRunConfig>(
openEditor.make(
[
'unicorn.js:5:3',
{
file: 'readme.md',
line: 10,
column: 2
}
],
{editor: 'vi'}
)
);
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"open",
Expand All @@ -42,12 +43,13 @@
"intellij"
],
"dependencies": {
"env-editor": "^0.3.1",
"line-column-path": "^1.0.0",
"opn": "^6.0.0"
"env-editor": "^0.4.0",
"line-column-path": "^2.0.0",
"open": "^6.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Open the given files in the user's editor at specific line and column if support

#### files

Type: `Array<string | Object>`
Type: `Array<string | object>`

Items should be in the format `foo.js:1:5` or `{file: 'foo.js', line: 1: column: 5}`.

Expand All @@ -68,7 +68,7 @@ Name, command, or binary path of the editor.

Same as `openEditor()`, but returns an object with the binary name, arguments, and a flag indicating whether the editor runs in the terminal.

Example: `{bin: 'subl', args: ['foo.js:1:5'], isTerminalEditor: false}`
Example: `{binary: 'subl', arguments: ['foo.js:1:5'], isTerminalEditor: false}`

Can be useful if you want to handle opening the files yourself.

Expand Down
40 changes: 20 additions & 20 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ test('object input', t => {
}
),
{
bin: 'subl',
args: fixtureFiles,
binary: 'subl',
arguments: fixtureFiles,
isTerminalEditor: false
}
);
});

test('editor - generic', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'noop'}), {
bin: 'noop',
args: [
binary: 'noop',
arguments: [
'unicorn.js',
'rainbow.js'
],
Expand All @@ -46,32 +46,32 @@ test('editor - generic', t => {

test('editor - Sublime', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'sublime'}), {
bin: 'subl',
args: fixtureFiles,
binary: 'subl',
arguments: fixtureFiles,
isTerminalEditor: false
});
});

test('editor - Atom', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'atom'}), {
bin: 'atom',
args: fixtureFiles,
binary: 'atom',
arguments: fixtureFiles,
isTerminalEditor: false
});
});

test('editor - VS Code', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'vscode'}), {
bin: 'code',
args: ['--goto'].concat(fixtureFiles),
binary: 'code',
arguments: ['--goto'].concat(fixtureFiles),
isTerminalEditor: false
});
});

test('editor - WebStorm', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'webstorm'}), {
bin: 'wstorm',
args: [
binary: 'wstorm',
arguments: [
'unicorn.js:10',
'rainbow.js:43'
],
Expand All @@ -81,8 +81,8 @@ test('editor - WebStorm', t => {

test('editor - TextMate', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'textmate'}), {
bin: 'mate',
args: [
binary: 'mate',
arguments: [
'--line',
'10:20',
'unicorn.js',
Expand All @@ -96,8 +96,8 @@ test('editor - TextMate', t => {

test('editor - Vim', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'vim'}), {
bin: 'vim',
args: [
binary: 'vim',
arguments: [
'+call cursor(10, 20)',
'unicorn.js',
'+call cursor(43, 4)',
Expand All @@ -109,8 +109,8 @@ test('editor - Vim', t => {

test('editor - NeoVim', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'neovim'}), {
bin: 'nvim',
args: [
binary: 'nvim',
arguments: [
'+call cursor(10, 20)',
'unicorn.js',
'+call cursor(43, 4)',
Expand All @@ -122,8 +122,8 @@ test('editor - NeoVim', t => {

test('editor - IntelliJ IDEA', t => {
t.deepEqual(openEditor.make(fixtureFiles, {editor: 'intellij'}), {
bin: 'idea',
args: [
binary: 'idea',
arguments: [
'unicorn.js:10',
'rainbow.js:43'
],
Expand Down