Skip to content

Commit 650778f

Browse files
Open editor on clicking compilation error
1 parent 3371e52 commit 650778f

5 files changed

Lines changed: 578 additions & 444 deletions

File tree

lib/templates/error.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,14 +1085,16 @@
10851085
</header>
10861086
<main class="container">
10871087
<article class="content">
1088+
<div onclick="fetch('/ember_open_editor_for_template_compilation_error')">
10881089
<h1 class="error-header">Dang! Looks like a {{errorType}}.</h1>
1089-
{{#if location.file}}
1090-
<h2 class="error-message">{{location.file}}{{#if location.line}}:{{location.line}}{{/if}}</h2>
1091-
{{/if}}
1090+
{{#if location.file}}
1091+
<h2 class="error-message">{{location.file}}{{#if location.line}}:{{location.line}}{{/if}}</h2>
1092+
{{/if}}
10921093

1093-
{{#if codeFrame}}
1094-
<pre><code class="javascript">{{codeFrame}}</code></pre>
1095-
{{/if}}
1094+
{{#if codeFrame}}
1095+
<pre><code class="javascript">{{codeFrame}}</code></pre>
1096+
{{/if}}
1097+
</div>
10961098

10971099
<div class="collapsible">
10981100
<input id="toggle" type="checkbox">

lib/utils/filename-normaliser.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
const pkg = require(`${process.cwd()}/package.json`);
6+
7+
module.exports = function normaliseFileName(fileName) {
8+
let projectName = pkg.name;
9+
let normalisedFileName = fileName.split(path.sep);
10+
11+
// Classic Structure
12+
// actual location : projectName/app/templates/templateName.hbs
13+
// location as per stack : project-name/templates/templateName.hbs
14+
// should be normalised to: app/templates/templateName.hbs
15+
16+
// Pods
17+
// actual location : projectName/app/routeName/templateName.hbs
18+
// location as per stack : project-name/routeName/templateName.hbs
19+
// should be normalised to: app/routeName/templateName.hbs
20+
21+
// in-repo engine
22+
// actual location : projectName/lib/engine-name/addon/templates/templateName.hbs
23+
// location as per stack : engine-name/templates/cars.hbs
24+
// should be normalised to: lib/engine-name/addon/templates/templateName.hbs
25+
26+
// handle templates inside application with either pod or classic structure
27+
if (normalisedFileName[0] === projectName) {
28+
normalisedFileName[0] = 'app';
29+
} else {
30+
// handle templates inside in-repo engines
31+
normalisedFileName = ['lib', normalisedFileName.shift(), 'addon', ...normalisedFileName];
32+
}
33+
34+
return normalisedFileName.join(path.sep);
35+
}

lib/watcher-middleware.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
const url = require('url');
44
const path = require('path');
5+
const launchEditor = require('launch-editor');
56

67
const errorHandler = require('./utils/error-handler');
8+
const normaliseFileName = require('./utils/filename-normaliser');
79

810
module.exports = function watcherMiddleware(watcher, options) {
911
options = options || {};
@@ -34,6 +36,21 @@ module.exports = function watcherMiddleware(watcher, options) {
3436

3537
next();
3638
}, (buildError) => {
39+
if (request.url === '/ember_open_editor_for_template_compilation_error') {
40+
let errorLocation = buildError.broccoliPayload.error.location || {};
41+
let fileName = errorLocation.file;
42+
let normalisedFileName = normaliseFileName(fileName);
43+
44+
launchEditor(normalisedFileName, () => {
45+
if (!process.env.EDITOR || !process.env.VISUAL) {
46+
console.log(
47+
'To set up the editor integration, set the env variables EDITOR or VISUAL ' +
48+
'to an editor of your choice and restart the server.'
49+
);
50+
}
51+
});
52+
}
53+
3754
errorHandler(response, {
3855
'buildError': buildError,
3956
'liveReloadPath': options.liveReloadPath

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"homepage": "https://github.com/ember-cli/broccoli-middleware",
2121
"dependencies": {
2222
"handlebars": "^4.0.4",
23+
"launch-editor": "^2.2.1",
2324
"mime-types": "^2.1.18"
2425
},
2526
"devDependencies": {

0 commit comments

Comments
 (0)