feat: open in editor

This commit is contained in:
Pooya Parsa 2017-09-10 00:51:51 +04:30
parent 2db304e222
commit b41fcbc789
3 changed files with 36 additions and 4 deletions

View File

@ -275,6 +275,9 @@ Options.defaults = {
}, },
chokidar: {} chokidar: {}
}, },
editor: {
editor: 'code'
},
messages: { messages: {
error_404: 'This page could not be found', error_404: 'This page could not be found',
server_error: 'Server error', server_error: 'Server error',

View File

@ -232,6 +232,32 @@ export default class Renderer extends Tapable {
}) })
} }
// open in editor for debug mode only
const _this = this
if (this.options.debug) {
this.useMiddleware({
path: '_open',
handler (req, res) {
// Lazy load open-in-editor
const openInEditor = require('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)
})
}
})
}
// For serving static/ files to / // For serving static/ files to /
this.useMiddleware(serveStatic(resolve(this.options.srcDir, 'static'), this.options.render.static)) this.useMiddleware(serveStatic(resolve(this.options.srcDir, 'static'), this.options.render.static))
@ -405,7 +431,6 @@ export default class Renderer extends Tapable {
const contents = smc.sourceContentFor(source) const contents = smc.sourceContentFor(source)
if (contents) { if (contents) {
frame.contents = contents frame.contents = contents
return
} }
} }
} }
@ -431,12 +456,15 @@ export default class Renderer extends Tapable {
this.options.buildDir this.options.buildDir
] ]
// Scan filesystem // Scan filesystem for real path
for (let pathDir of searchPath) { for (let pathDir of searchPath) {
let fullPath = resolve(pathDir, frame.fileName) let fullPath = resolve(pathDir, frame.fileName)
let source = await fs.readFile(fullPath, 'utf-8').catch(() => null) let source = await fs.readFile(fullPath, 'utf-8').catch(() => null)
if (source) { if (source) {
frame.contents = source if (!frame.contents) {
frame.contents = source
}
frame.fullPath = fullPath
return return
} }
} }

View File

@ -64,7 +64,7 @@
"npm": ">=3.10.0" "npm": ">=3.10.0"
}, },
"dependencies": { "dependencies": {
"@nuxtjs/youch": "3.0.2", "@nuxtjs/youch": "3.1.0",
"ansi-html": "^0.0.7", "ansi-html": "^0.0.7",
"autoprefixer": "^7.1.3", "autoprefixer": "^7.1.3",
"babel-core": "^6.26.0", "babel-core": "^6.26.0",
@ -93,6 +93,7 @@
"lru-cache": "^4.1.1", "lru-cache": "^4.1.1",
"memory-fs": "^0.4.1", "memory-fs": "^0.4.1",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"open-in-editor": "^2.2.0",
"opencollective": "^1.0.3", "opencollective": "^1.0.3",
"pify": "^3.0.0", "pify": "^3.0.0",
"postcss": "^6.0.10", "postcss": "^6.0.10",