feat: use launch-editor for open-in-editor functionality

This commit is contained in:
Pooya Parsa 2018-01-06 23:27:20 +03:30
parent b941b5e4d1
commit 4e24ec7eba
9 changed files with 75 additions and 63 deletions

View File

@ -285,9 +285,7 @@ Options.defaults = {
},
chokidar: {}
},
editor: {
editor: 'code'
},
editor: undefined,
hooks: null,
messages: {
error_404: 'This page could not be found',

View File

@ -46,7 +46,7 @@ module.exports = function errorMiddleware(err, req, res, next) {
}
// Show stack trace
const youch = new Youch(err, req, readSource.bind(this))
const youch = new Youch(err, req, readSource.bind(this), this.options.router.base)
if (isJson) {
youch.toJSON().then(json => { sendResponse(JSON.stringify(json, undefined, 2), 'text/json') })
} else {

View File

@ -0,0 +1,21 @@
const url = require('url')
const launch = require('launch-editor')
const lineNumberRE = /:(\d+)$/
// Temporary middleware until https://github.com/yyx990803/launch-editor/pull/1 releases
module.exports = (specifiedEditor, onErrorCallback) => {
return function launchEditorMiddleware(req, res, next) {
const { file } = url.parse(req.url, true).query || {}
if (!file) {
res.statusCode = 500
res.end(`launch-editor-middleware: required query param "file" is missing.`)
} else {
const fileName = file.replace(lineNumberRE, '')
const lineNumberMatch = file.match(lineNumberRE)
const lineNumber = lineNumberMatch && lineNumberMatch[1]
launch(fileName, lineNumber, specifiedEditor, onErrorCallback)
res.end()
}
}
}

View File

@ -1,23 +0,0 @@
const openInEditor = require('open-in-editor')
module.exports = function openInEditorMiddleware(req, res) {
// Lazy load open-in-editor
const editor = openInEditor.configure(this.options.editor)
// Parse Query
const query = req.url.split('?')[1].split('&').reduce((q, part) => {
const s = part.split('=')
q[s[0]] = decodeURIComponent(s[1])
return q
}, {})
// eslint-disable-next-line no-console
console.log('[open in editor]', query.file)
editor.open(query.file).then(() => {
res.end('opened in editor!')
}).catch(err => {
res.end(err)
})
}

View File

@ -1,22 +1,21 @@
const ansiHTML = require('ansi-html')
const serialize = require('serialize-javascript')
const serveStatic = require('serve-static')
const compression = require('compression')
const _ = require('lodash')
const { join, resolve } = require('path')
const fs = require('fs-extra')
const { createBundleRenderer } = require('vue-server-renderer')
const { setAnsiColors, isUrl, waitFor } = require('../common/utils')
const Debug = require('debug')
const connect = require('connect')
const { Options } = require('../common')
const MetaRenderer = require('./meta')
const launchMiddleware = require('./middleware/launch-editor-middleware')
const { setAnsiColors, isUrl, waitFor } = require('../common/utils')
const { Options } = require('../common')
const MetaRenderer = require('./meta')
const errorMiddleware = require('./middleware/error')
const nuxtMiddleware = require('./middleware/nuxt')
const openInEditorMiddleware = require('./middleware/open-in-editor')
const debug = Debug('nuxt:render')
debug.color = 4 // Force blue color
@ -228,7 +227,7 @@ module.exports = class Renderer {
if (this.options.debug && this.options.dev) {
this.useMiddleware({
path: '__open-in-editor',
handler: openInEditorMiddleware.bind(this)
handler: launchMiddleware(this.options.editor)
})
}

View File

@ -56,7 +56,7 @@
"npm": ">=5.0.0"
},
"dependencies": {
"@nuxtjs/youch": "^4.0.1",
"@nuxtjs/youch": "^4.2.2",
"ansi-html": "^0.0.7",
"autoprefixer": "^7.2.4",
"babel-core": "^6.26.0",
@ -82,11 +82,12 @@
"hash-sum": "^1.0.2",
"html-minifier": "3.5.8",
"html-webpack-plugin": "^2.30.1",
"launch-editor": "^1.0.0",
"launch-editor-middleware": "^1.0.0",
"lodash": "^4.17.4",
"lru-cache": "^4.1.1",
"memory-fs": "^0.4.1",
"minimist": "^1.2.0",
"open-in-editor": "^2.2.0",
"opencollective": "^1.0.3",
"postcss": "^6.0.15",
"postcss-cssnext": "^3.0.2",

View File

@ -26,21 +26,14 @@ test.serial('Init Nuxt.js', async t => {
})
test.serial('/test/__open-in-editor (open-in-editor)', async t => {
const logSpy = await interceptLog()
const { body } = await rp(url('/test/__open-in-editor?file=pages/index.vue'), { resolveWithFullResponse: true })
t.is(body, 'opened in editor!')
release()
t.is(logSpy.getCall(0).args[0], '[open in editor]')
t.true(logSpy.calledOnce)
t.is(body, '')
})
test.serial('/test/__open-in-editor should return error (open-in-editor)', async t => {
const logSpy = await interceptLog()
const { body } = await rp(url('/test/__open-in-editor?file='), { resolveWithFullResponse: true })
t.is(body, 'File is not specified')
release()
t.is(logSpy.getCall(0).args[0], '[open in editor]')
t.true(logSpy.calledOnce)
const { error, statusCode } = await t.throws(rp(url('/test/__open-in-editor?file='), { resolveWithFullResponse: true }))
t.is(statusCode, 500)
t.is(error, 'launch-editor-middleware: required query param "file" is missing.')
})
test.serial('/test/error should return error stack trace (Youch)', async t => {

View File

@ -4,10 +4,6 @@ module.exports = {
},
dev: true, // Needed for __open-in-editor middleware
debug: true,
editor: {
cmd: 'echo',
pattern: ''
},
build: {
stats: false
}

View File

@ -97,9 +97,9 @@
dependencies:
arrify "^1.0.1"
"@nuxtjs/youch@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@nuxtjs/youch/-/youch-4.0.1.tgz#73e7e1c8646e1e9c931c6d3216c317c90ff455b8"
"@nuxtjs/youch@^4.2.1":
version "4.2.1"
resolved "https://registry.yarnpkg.com/@nuxtjs/youch/-/youch-4.2.1.tgz#5ac7bc56ab554a7bfc2f173ee764db16f33b01ca"
dependencies:
cookie "^0.3.1"
mustache "^2.3.0"
@ -315,6 +315,10 @@ array-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93"
array-filter@~0.0.0:
version "0.0.1"
resolved "https://registry.yarnpkg.com/array-filter/-/array-filter-0.0.1.tgz#7da8cf2e26628ed732803581fd21f67cacd2eeec"
array-find-index@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/array-find-index/-/array-find-index-1.0.2.tgz#df010aa1287e164bbda6f9723b0a96a1ec4187a1"
@ -330,6 +334,14 @@ array-includes@^3.0.3:
define-properties "^1.1.2"
es-abstract "^1.7.0"
array-map@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/array-map/-/array-map-0.0.0.tgz#88a2bab73d1cf7bcd5c1b118a003f66f665fa662"
array-reduce@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/array-reduce/-/array-reduce-0.0.0.tgz#173899d3ffd1c7d9383e4479525dbe278cab5f2b"
array-union@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/array-union/-/array-union-1.0.2.tgz#9a34410e4f4e3da23dea375be5be70f24778ec39"
@ -1584,7 +1596,7 @@ circular-json@^0.3.1:
version "0.3.3"
resolved "https://registry.yarnpkg.com/circular-json/-/circular-json-0.3.3.tgz#815c99ea84f6809529d2f45791bdf82711352d66"
clap@^1.0.9, clap@^1.1.3:
clap@^1.0.9:
version "1.2.3"
resolved "https://registry.yarnpkg.com/clap/-/clap-1.2.3.tgz#4f36745b32008492557f46412d66d50cb99bce51"
dependencies:
@ -4399,6 +4411,19 @@ latest-version@^3.0.0:
dependencies:
package-json "^4.0.0"
launch-editor-middleware@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/launch-editor-middleware/-/launch-editor-middleware-1.0.0.tgz#1336164fdfdffa5cdaa83612fdbd962af32a726e"
dependencies:
launch-editor "^1.0.0"
launch-editor@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/launch-editor/-/launch-editor-1.0.0.tgz#236b5d6891ca946c3544dcf2f82d3d7d7320ba76"
dependencies:
chalk "^2.3.0"
shell-quote "^1.6.1"
lazy-cache@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"
@ -5190,13 +5215,6 @@ onetime@^2.0.0:
dependencies:
mimic-fn "^1.0.0"
open-in-editor@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/open-in-editor/-/open-in-editor-2.2.0.tgz#c5b21aa76f6acd4cbbd3c3b2e77dccb4b75a2020"
dependencies:
clap "^1.1.3"
os-homedir "~1.0.2"
opencollective@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/opencollective/-/opencollective-1.0.3.tgz#aee6372bc28144583690c3ca8daecfc120dd0ef1"
@ -5245,7 +5263,7 @@ os-browserify@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/os-browserify/-/os-browserify-0.3.0.tgz#854373c7f5c2315914fc9bfc6bd8238fdda1ec27"
os-homedir@^1.0.0, os-homedir@^1.0.1, os-homedir@~1.0.2:
os-homedir@^1.0.0, os-homedir@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3"
@ -6800,6 +6818,15 @@ shebang-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
shell-quote@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.6.1.tgz#f4781949cce402697127430ea3b3c5476f481767"
dependencies:
array-filter "~0.0.0"
array-map "~0.0.0"
array-reduce "~0.0.0"
jsonify "~0.0.0"
signal-exit@^3.0.0, signal-exit@^3.0.1, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"