index.js 783 B

12345678910111213141516171819202122232425262728
  1. const url = require('url')
  2. const path = require('path')
  3. const launch = require('launch-editor')
  4. module.exports = (specifiedEditor, srcRoot, onErrorCallback) => {
  5. if (typeof specifiedEditor === 'function') {
  6. onErrorCallback = specifiedEditor
  7. specifiedEditor = undefined
  8. }
  9. if (typeof srcRoot === 'function') {
  10. onErrorCallback = srcRoot
  11. srcRoot = undefined
  12. }
  13. srcRoot = srcRoot || process.cwd()
  14. return function launchEditorMiddleware (req, res, next) {
  15. const { file } = url.parse(req.url, true).query || {}
  16. if (!file) {
  17. res.statusCode = 500
  18. res.end(`launch-editor-middleware: required query param "file" is missing.`)
  19. } else {
  20. launch(path.resolve(srcRoot, file), specifiedEditor, onErrorCallback)
  21. res.end()
  22. }
  23. }
  24. }