Skip to content

Commit

Permalink
Require Node.js 8, add support for nano and Emacs, add TypeScript def…
Browse files Browse the repository at this point in the history
…inition (#6)
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 18, 2019
1 parent f9ae547 commit 9d1bd79
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '6'
- '4'
- '10'
- '8'
82 changes: 82 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export interface Editor {
id: string;
name: string;
binary: string;
isTerminalEditor: boolean;
paths: string[];
keywords: string[];
}

/**
Get info about the default editor.
The user is expected to have the `$EDITOR` environment variable set, and if not, a user-friendly error is thrown.
@returns Metadata on the default editor.
@example
```
import {defaultEditor} from 'env-editor';
defaultEditor();
// {
// id: 'atom',
// name: 'Atom',
// binary: 'atom',
// isTerminalEditor: false,
// paths: [
// '/Applications/Atom.app/Contents/Resources/app/atom.sh'
// ],
// keywords: []
// }
```
*/
export function defaultEditor(): Editor;

/**
Get info about a specific editor.
@param editor - This can be pretty flexible. It matches against all the data it has. For example, to get Sublime Text, you could write either of the following: `sublime`, `Sublime Text`, `subl`.
@returns Metadata on the specified editor.
@example
```
import {getEditor} from 'env-editor';
getEditor('sublime');
// {
// id: 'sublime',
// name: 'Sublime Text',
// binary: 'subl',
// isTerminalEditor: false,
// paths: [
// '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
// '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl'
// ],
// keywords: []
// }
```
*/
export function getEditor(editor: string): Editor;

/**
@returns Metadata on all the editors.
@example
```
import {allEditors} from 'env-editor';
allEditors();
// [
// {
// id: 'atom',
// ...
// }, {
// id: 'sublime,
// ...
// },
// ...
// ]
```
*/
export function allEditors(): Editor[];
70 changes: 47 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
const path = require('path');

const editors = () => [{
const allEditors = () => [{
id: 'sublime',
name: 'Sublime Text',
bin: 'subl',
binary: 'subl',
isTerminalEditor: false,
paths: [
'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
Expand All @@ -14,7 +14,7 @@ const editors = () => [{
}, {
id: 'atom',
name: 'Atom',
bin: 'atom',
binary: 'atom',
isTerminalEditor: false,
paths: [
'/Applications/Atom.app/Contents/Resources/app/atom.sh'
Expand All @@ -23,7 +23,7 @@ const editors = () => [{
}, {
id: 'vscode',
name: 'Visual Studio Code',
bin: 'code',
binary: 'code',
isTerminalEditor: false,
paths: [
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code'
Expand All @@ -34,28 +34,28 @@ const editors = () => [{
}, {
id: 'webstorm',
name: 'WebStorm',
bin: 'wstorm',
binary: 'wstorm',
isTerminalEditor: false,
paths: [],
keywords: []
}, {
id: 'textmate',
name: 'TextMate',
bin: 'mate',
binary: 'mate',
isTerminalEditor: false,
paths: [],
keywords: []
}, {
id: 'vim',
name: 'Vim',
bin: 'vim',
binary: 'vim',
isTerminalEditor: true,
paths: [],
keywords: []
}, {
id: 'neovim',
name: 'NeoVim',
bin: 'nvim',
binary: 'nvim',
isTerminalEditor: true,
paths: [],
keywords: [
Expand All @@ -64,7 +64,7 @@ const editors = () => [{
}, {
id: 'intellij',
name: 'IntelliJ IDEA',
bin: 'idea',
binary: 'idea',
isTerminalEditor: false,
paths: [],
keywords: [
Expand All @@ -73,26 +73,49 @@ const editors = () => [{
'jetbrains',
'ide'
]
}, {
id: 'nano',
name: 'GNU nano',
binary: 'nano',
isTerminalEditor: true,
paths: [],
keywords: []
}, {
id: 'emacs',
name: 'GNU Emacs',
binary: 'emacs',
isTerminalEditor: true,
paths: [],
keywords: []
}, {
id: 'emacsforosx',
name: 'GNU Emacs for Mac OS X',
binary: 'Emacs',
isTerminalEditor: false,
paths: [
'/Applications/Emacs.app/Contents/MacOS/Emacs'
],
keywords: []
}];

const get = input => {
input = input.trim();
const needle = input.toLowerCase();
const getEditor = editor => {
editor = editor.trim();
const needle = editor.toLowerCase();
const id = needle.split(/[/\\]/).pop().replace(/\s/g, '-');
const bin = id.split('-')[0];
const binary = id.split('-')[0];

for (const editor of editors()) {
for (const editor of allEditors()) {
// TODO: Maybe use `leven` module for more flexible matching
if (
needle === editor.id ||
needle === editor.name.toLowerCase() ||
bin === editor.bin
binary === editor.binary
) {
return editor;
}

for (const p of editor.paths) {
if (path.normalize(needle) === path.normalize(p)) {
for (const editorPath of editor.paths) {
if (path.normalize(needle) === path.normalize(editorPath)) {
return editor;
}
}
Expand All @@ -106,19 +129,20 @@ const get = input => {

return {
id,
name: input,
bin,
name: editor,
binary,
isTerminalEditor: false,
paths: [],
keywords: []
};
};

module.exports.default = () => {
module.exports.defaultEditor = () => {
const editor = process.env.EDITOR || process.env.VISUAL;

if (!editor) {
throw new Error(
// eslint-disable-next-line indent
`
Your $EDITOR environment variable is not set.
Set it to the command/path of your editor in ~/.zshenv or ~/.bashrc:
Expand All @@ -128,8 +152,8 @@ Set it to the command/path of your editor in ~/.zshenv or ~/.bashrc:
);
}

return get(editor);
return getEditor(editor);
};

module.exports.get = get;
module.exports.all = editors;
module.exports.getEditor = getEditor;
module.exports.allEditors = allEditors;
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {expectType} from 'tsd';
import {Editor, getEditor, defaultEditor, allEditors} from '.';

expectType<Editor>(getEditor('sublime'));
expectType<Editor>(defaultEditor());
expectType<Editor[]>(allEditors());
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 9d1bd79

Please sign in to comment.