env-editor
Advanced tools
Comparing version 0.3.1 to 0.4.0
210
index.js
'use strict'; | ||
const path = require('path'); | ||
const editors = () => [{ | ||
id: 'sublime', | ||
name: 'Sublime Text', | ||
bin: 'subl', | ||
isTerminalEditor: false, | ||
paths: [ | ||
'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl', | ||
'/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl' | ||
], | ||
keywords: [] | ||
}, { | ||
id: 'atom', | ||
name: 'Atom', | ||
bin: 'atom', | ||
isTerminalEditor: false, | ||
paths: [ | ||
'/Applications/Atom.app/Contents/Resources/app/atom.sh' | ||
], | ||
keywords: [] | ||
}, { | ||
id: 'vscode', | ||
name: 'Visual Studio Code', | ||
bin: 'code', | ||
isTerminalEditor: false, | ||
paths: [ | ||
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code' | ||
], | ||
keywords: [ | ||
'vs code' | ||
] | ||
}, { | ||
id: 'webstorm', | ||
name: 'WebStorm', | ||
bin: 'wstorm', | ||
isTerminalEditor: false, | ||
paths: [], | ||
keywords: [] | ||
}, { | ||
id: 'textmate', | ||
name: 'TextMate', | ||
bin: 'mate', | ||
isTerminalEditor: false, | ||
paths: [], | ||
keywords: [] | ||
}, { | ||
id: 'vim', | ||
name: 'Vim', | ||
bin: 'vim', | ||
isTerminalEditor: true, | ||
paths: [], | ||
keywords: [] | ||
}, { | ||
id: 'neovim', | ||
name: 'NeoVim', | ||
bin: 'nvim', | ||
isTerminalEditor: true, | ||
paths: [], | ||
keywords: [ | ||
'vim' | ||
] | ||
}, { | ||
id: 'intellij', | ||
name: 'IntelliJ IDEA', | ||
bin: 'idea', | ||
isTerminalEditor: false, | ||
paths: [], | ||
keywords: [ | ||
'idea', | ||
'java', | ||
'jetbrains', | ||
'ide' | ||
] | ||
}]; | ||
const allEditors = () => [ | ||
{ | ||
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: [] | ||
}, | ||
{ | ||
id: 'atom', | ||
name: 'Atom', | ||
binary: 'atom', | ||
isTerminalEditor: false, | ||
paths: [ | ||
'/Applications/Atom.app/Contents/Resources/app/atom.sh' | ||
], | ||
keywords: [] | ||
}, | ||
{ | ||
id: 'vscode', | ||
name: 'Visual Studio Code', | ||
binary: 'code', | ||
isTerminalEditor: false, | ||
paths: [ | ||
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code' | ||
], | ||
keywords: [ | ||
'vs code' | ||
] | ||
}, | ||
{ | ||
id: 'webstorm', | ||
name: 'WebStorm', | ||
binary: 'wstorm', | ||
isTerminalEditor: false, | ||
paths: [], | ||
keywords: [] | ||
}, | ||
{ | ||
id: 'textmate', | ||
name: 'TextMate', | ||
binary: 'mate', | ||
isTerminalEditor: false, | ||
paths: [], | ||
keywords: [] | ||
}, | ||
{ | ||
id: 'vim', | ||
name: 'Vim', | ||
binary: 'vim', | ||
isTerminalEditor: true, | ||
paths: [], | ||
keywords: [] | ||
}, | ||
{ | ||
id: 'neovim', | ||
name: 'NeoVim', | ||
binary: 'nvim', | ||
isTerminalEditor: true, | ||
paths: [], | ||
keywords: [ | ||
'vim' | ||
] | ||
}, | ||
{ | ||
id: 'intellij', | ||
name: 'IntelliJ IDEA', | ||
binary: 'idea', | ||
isTerminalEditor: false, | ||
paths: [], | ||
keywords: [ | ||
'idea', | ||
'java', | ||
'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 | ||
@@ -89,3 +124,3 @@ if ( | ||
needle === editor.name.toLowerCase() || | ||
bin === editor.bin | ||
binary === editor.binary | ||
) { | ||
@@ -95,4 +130,4 @@ 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; | ||
@@ -111,4 +146,4 @@ } | ||
id, | ||
name: input, | ||
bin, | ||
name: editor, | ||
binary, | ||
isTerminalEditor: false, | ||
@@ -120,3 +155,3 @@ paths: [], | ||
module.exports.default = () => { | ||
module.exports.defaultEditor = () => { | ||
const editor = process.env.EDITOR || process.env.VISUAL; | ||
@@ -126,2 +161,3 @@ | ||
throw new Error( | ||
// eslint-disable-next-line indent | ||
` | ||
@@ -136,6 +172,6 @@ Your $EDITOR environment variable is not set. | ||
return get(editor); | ||
return getEditor(editor); | ||
}; | ||
module.exports.get = get; | ||
module.exports.all = editors; | ||
module.exports.getEditor = getEditor; | ||
module.exports.allEditors = allEditors; |
{ | ||
"name": "env-editor", | ||
"version": "0.3.1", | ||
"description": "Get info about the default editor or a specific editor", | ||
"license": "MIT", | ||
"repository": "sindresorhus/env-editor", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"env", | ||
"editor", | ||
"environment", | ||
"variable", | ||
"default", | ||
"editors", | ||
"main", | ||
"user", | ||
"meta", | ||
"metadata", | ||
"info", | ||
"name", | ||
"binary", | ||
"path", | ||
"sublime", | ||
"atom", | ||
"vscode", | ||
"webstorm", | ||
"textmate", | ||
"vim", | ||
"neovim", | ||
"intellij" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "env-editor", | ||
"version": "0.4.0", | ||
"description": "Get metadata on the default editor or a specific editor", | ||
"license": "MIT", | ||
"repository": "sindresorhus/env-editor", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"env", | ||
"editor", | ||
"environment", | ||
"variable", | ||
"default", | ||
"editors", | ||
"main", | ||
"user", | ||
"meta", | ||
"metadata", | ||
"info", | ||
"name", | ||
"binary", | ||
"path", | ||
"sublime", | ||
"atom", | ||
"vscode", | ||
"webstorm", | ||
"textmate", | ||
"vim", | ||
"neovim", | ||
"intellij", | ||
"nano", | ||
"emacs" | ||
], | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
# env-editor [![Build Status](https://travis-ci.org/sindresorhus/env-editor.svg?branch=master)](https://travis-ci.org/sindresorhus/env-editor) | ||
> Get info about the default editor or a specific editor | ||
> Get metadata on the default editor or a specific editor | ||
@@ -18,2 +18,4 @@ This module is used by [`open-editor`](https://github.com/sindresorhus/open-editor). | ||
- IntelliJ | ||
- GNU nano | ||
- GNU Emacs | ||
@@ -24,3 +26,3 @@ | ||
``` | ||
$ npm install --save env-editor | ||
$ npm install env-editor | ||
``` | ||
@@ -32,10 +34,24 @@ | ||
```js | ||
const envEditor = require('env-editor'); | ||
const {defaultEditor, getEditor, allEditors} = require('env-editor'); | ||
envEditor.get('sublime'); | ||
defaultEditor(); | ||
/* | ||
{ | ||
id: 'atom', | ||
name: 'Atom', | ||
binary: 'atom', | ||
isTerminalEditor: false, | ||
paths: [ | ||
'/Applications/Atom.app/Contents/Resources/app/atom.sh' | ||
], | ||
keywords: [] | ||
} | ||
*/ | ||
getEditor('sublime'); | ||
/* | ||
{ | ||
id: 'sublime', | ||
name: 'Sublime Text', | ||
bin: 'subl', | ||
binary: 'subl', | ||
isTerminalEditor: false, | ||
@@ -49,2 +65,17 @@ paths: [ | ||
*/ | ||
allEditors(); | ||
/* | ||
[ | ||
{ | ||
id: 'atom', | ||
… | ||
}, | ||
{ | ||
id: 'sublime, | ||
… | ||
}, | ||
… | ||
] | ||
*/ | ||
``` | ||
@@ -55,11 +86,11 @@ | ||
### .default() | ||
### defaultEditor() | ||
Returns info about the default editor. | ||
Returns metadata on the default editor. | ||
The user is expected to have the `$EDITOR` environment variable set, and if not, a user-friendly error is thrown. | ||
### .get(editor) | ||
### getEditor(editor) | ||
Returns info about the specified editor. | ||
Returns metadata on the specified editor. | ||
@@ -74,5 +105,5 @@ #### editor | ||
### .all() | ||
### allEditors() | ||
Returns an array with info on all the editors. | ||
Returns an array with metadata on all the editors. | ||
@@ -79,0 +110,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8210
5
231
108
3