express-open-in-editor
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -0,1 +1,5 @@ | ||
## 3.0.0 (September 21, 2017) | ||
- Reworked to follow `express` best practices (#1) | ||
## 2.0.0 (March 22, 2017) | ||
@@ -2,0 +6,0 @@ |
44
index.js
@@ -5,42 +5,26 @@ var parseUrl = require('parseurl'); | ||
var configure = require('open-in-editor').configure; | ||
var MESSAGE_PREFIX = '[' + require('./package.json').name + '] '; | ||
function fail(code, message) { | ||
res.statusCode = code; | ||
res.end(MESSAGE_PREFIX + message); | ||
} | ||
module.exports = function(options) { | ||
options = options || {}; | ||
var url = options.url || '/open-in-editor'; | ||
var opener = configure(options, function(err) { | ||
console.warn('[open-in-editor] configure error: ', err); | ||
var opener = configure(options || {}, function(err) { | ||
console.warn(NAME + ' configure error: ', err); | ||
}); | ||
return function openInEditor(req, res, next) { | ||
function fail(code, message) { | ||
res.statusCode = code; | ||
res.end('[open-in-editor] ' + message); | ||
if (!opener) { | ||
var msg = MESSAGE_PREFIX + 'Request to open file failed, editor is not set up'; | ||
console.warn(msg); | ||
return fail(res, 400, msg); | ||
} | ||
var parsedUrl = parseUrl(req); | ||
if (parsedUrl.pathname !== url) { | ||
next(); | ||
return; | ||
} | ||
if (req.method !== 'GET' && req.method !== 'HEAD') { | ||
res.statusCode = req.method === 'OPTIONS' ? 200 : 405; | ||
res.setHeader('Allow', 'GET, HEAD, OPTIONS'); | ||
res.setHeader('Content-Length', '0'); | ||
res.end(); | ||
return; | ||
} | ||
if (!opener) { | ||
var msg = 'Request to open file failed, editor is not set up'; | ||
console.warn('[open-in-editor] ', msg); | ||
return fail(400, msg); | ||
} | ||
var filename = querystring.parse(parsedUrl.query).file; | ||
if (!filename) { | ||
return fail(400, 'Parameter missed: file'); | ||
return fail(res, 400, 'Parameter missed: file'); | ||
} | ||
@@ -60,3 +44,3 @@ | ||
function(e) { | ||
fail(500, 'ERROR: ' + e); | ||
fail(res, 500, 'ERROR: ' + e); | ||
} | ||
@@ -63,0 +47,0 @@ ); |
{ | ||
"name": "express-open-in-editor", | ||
"version": "2.0.0", | ||
"description": "Express extension to open file in editor", | ||
"version": "3.0.0", | ||
"description": "Express middleware to open file in editor", | ||
"author": "Roman Dvornov <rdvornov@gmail.com>", | ||
@@ -10,2 +10,3 @@ "license": "MIT", | ||
"express", | ||
"middleware", | ||
"extension", | ||
@@ -12,0 +13,0 @@ "open", |
[![NPM version](https://img.shields.io/npm/v/express-open-in-editor.svg)](https://www.npmjs.com/package/express-open-in-editor) | ||
[![Dependency Status](https://img.shields.io/david/lahmatiy/express-open-in-editor.svg)](https://david-dm.org/lahmatiy/express-open-in-editor) | ||
Express extension to open any file in an editor. Based on [open-in-editor](https://github.com/lahmatiy/open-in-editor). | ||
Express middleware to open any file in an editor by request to defined route. Based on [open-in-editor](https://github.com/lahmatiy/open-in-editor). | ||
@@ -20,5 +20,12 @@ ## Install | ||
// There are few ways to setup: | ||
// - to trigger middleware on *GET* request to `/open-in-editor` | ||
app.get('/open-in-editor', openInEditor()); | ||
// - to trigger middleware on *any* request method to `/open-in-editor` | ||
app.use('/open-in-editor', openInEditor()); | ||
// - to trigger middleware on *any* request method to *any* path | ||
// (not recomended unless server's single purpose is to open files in editor) | ||
app.use(openInEditor()); | ||
// ... | ||
``` | ||
@@ -28,3 +35,3 @@ | ||
By default extension uses `process.env.VISUAL` or `process.env.EDITOR` (with this priority) to get the command to open file in an editor. It could be set globally or with main script: | ||
By default `express-open-in-editor` uses `process.env.VISUAL` or `process.env.EDITOR` (with this priority) to define the command to open a file in an editor. It could be set globally or on script execution: | ||
@@ -37,7 +44,7 @@ ``` | ||
For more details about editor setup see [open-in-editor](https://github.com/lahmatiy/open-in-editor). | ||
For more details about setup see [open-in-editor](https://github.com/lahmatiy/open-in-editor) description. | ||
### Using with webpack-dev-server | ||
Although `webpack-dev-server` uses `express` to create server, you can't use `app.use()` to apply extension. Instead you should define it in `setup` method (see [issue](https://github.com/webpack/webpack-dev-server/issues/285) for details). | ||
Although `webpack-dev-server` uses `express` to create a server, you have the same options to apply the middleware to it. The only difference is that you should define it in `setup` method (see [issue](https://github.com/webpack/webpack-dev-server/issues/285) for details). | ||
@@ -48,3 +55,3 @@ ```js | ||
setup: function(app) { | ||
app.use(openInEditor()); | ||
app.use('/open-in-editor', openInEditor()); | ||
} | ||
@@ -60,52 +67,4 @@ }); | ||
### options | ||
Options are optional and passes to [open-in-editor](https://github.com/lahmatiy/open-in-editor) as is. | ||
#### url | ||
Type: `String` | ||
Default: `/open-in-editor` | ||
Request to this path triggers middleware. | ||
#### editor | ||
Type: `String` | ||
Values: `sublime`, `atom`, `code` | ||
Default: *not set* | ||
Editor to open files. Option accepts one of preset values. When value is set, extension tries to detect command to launch an editor. | ||
Supported editors: | ||
- `sublime` – Sublime Text | ||
- `atom` – Atom Editor | ||
- `code` – Visual Studio Code | ||
#### cmd | ||
Type: `String` | ||
Default: *not set* | ||
Command to launch an editor. This option overrides whatever is set in `editor` option. | ||
Command could contain placeholders that will be replaced with actual values. Supported placeholders: `filename`, `line` and `column`. | ||
```js | ||
app.use(openInEditor({ | ||
cmd: 'code -r -g {filename}:{line}:{column}' | ||
})); | ||
``` | ||
If no `{filename}` placeholder is present, then `{filename}:{line}:{column}` is appended to the value of this option. That way previous example could be simplified: | ||
```js | ||
app.use(openInEditor({ | ||
cmd: 'code -r -g' | ||
})); | ||
``` | ||
## Related projects | ||
@@ -112,0 +71,0 @@ |
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
10363
40
74